KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > texteditor > quickdiff > RevertSelectionAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.texteditor.quickdiff;
12
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.ITextSelection;
16 import org.eclipse.jface.text.source.ILineDiffInfo;
17 import org.eclipse.jface.text.source.ILineDiffer;
18
19 import org.eclipse.ui.texteditor.ITextEditor;
20
21 /**
22  * Action that will revert the added, deleted and changes lines in the selection on the currently
23  * displayed document to the state in the reference document.
24  *
25  * @since 3.0
26  */

27 public class RevertSelectionAction extends QuickDiffRestoreAction {
28     /** The first line to be restored. Set in <code>update()</code>. */
29     private int fStartLine;
30     /** The last line to be restored. Set in <code>update()</code>. */
31     private int fEndLine;
32
33     /**
34      * Creates a new instance.
35      *
36      * @param editor the editor this action belongs to
37      * @param isRulerAction <code>true</code> if this is a ruler action
38      */

39     public RevertSelectionAction(ITextEditor editor, boolean isRulerAction) {
40         super("RevertSelectionAction.", editor, isRulerAction); //$NON-NLS-1$
41
}
42
43     /*
44      * @see org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction#computeEnablement()
45      */

46     public boolean computeEnablement() {
47         if (!super.computeEnablement())
48             return false;
49
50         ITextSelection selection= getSelection();
51         if (selection == null)
52             return false;
53         fStartLine= selection.getStartLine();
54         fEndLine= selection.getEndLine();
55         // only enable if mouse activity is inside line range
56
int activityLine= getLastLine();
57         if (activityLine == -1 || activityLine < fStartLine || activityLine > fEndLine + 1)
58             // + 1 to cover the case where the selection goes to the offset of the next line
59
return false;
60         ILineDiffer differ= getDiffer();
61         if (differ == null)
62             return false;
63         // only enable if selection covers at least two lines
64
if (fEndLine > fStartLine) {
65             for (int i= fStartLine; i <= fEndLine; i++) {
66                 ILineDiffInfo info= differ.getLineInfo(i);
67                 if (info != null && info.hasChanges()) {
68                     return true;
69                 }
70             }
71         }
72         return false;
73     }
74
75     /*
76      * @see org.eclipse.ui.internal.editors.quickdiff.QuickDiffRestoreAction#runCompoundChange()
77      */

78     public void runCompoundChange() {
79         // recheck if run without being enabled
80
if (!isEnabled())
81             return;
82
83         ILineDiffer differ= getDiffer();
84         if (differ != null) {
85             try {
86                 differ.revertSelection(fStartLine, fEndLine - fStartLine + 1);
87             } catch (BadLocationException e) {
88                 setStatus(e.getMessage());
89             }
90         }
91     }
92
93 }
94
Popular Tags