KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 import org.eclipse.jface.viewers.IPostSelectionProvider;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.jface.viewers.ISelectionChangedListener;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.SelectionChangedEvent;
18
19 import org.eclipse.jface.text.IRewriteTarget;
20 import org.eclipse.jface.text.ITextSelection;
21 import org.eclipse.jface.text.source.IAnnotationModel;
22 import org.eclipse.jface.text.source.IAnnotationModelExtension;
23 import org.eclipse.jface.text.source.IChangeRulerColumn;
24 import org.eclipse.jface.text.source.ILineDiffer;
25 import org.eclipse.jface.text.source.IVerticalRulerInfo;
26
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.texteditor.IDocumentProvider;
29 import org.eclipse.ui.texteditor.IEditorStatusLine;
30 import org.eclipse.ui.texteditor.ITextEditor;
31 import org.eclipse.ui.texteditor.TextEditorAction;
32
33 /**
34  * Abstract superclass of actions that restore / revert parts of a document displayed in the action's
35  * editor to the state described by the {@link ILineDiffer ILineDiffer} associated with the document's
36  * {@link IAnnotationModel IAnnotationModel}.
37  *
38  * @since 3.0
39  */

40 public abstract class QuickDiffRestoreAction extends TextEditorAction implements ISelectionChangedListener {
41
42     private int fLastLine= -1;
43     private final boolean fIsRulerAction;
44
45     /**
46      * Creates a new instance.
47      *
48      * @param prefix a prefix to be prepended to the various resource keys
49      * @param editor the editor this action belongs to
50      * @param isRulerAction <code>true</code> if this is a ruler action
51      */

52     QuickDiffRestoreAction(String JavaDoc prefix, ITextEditor editor, boolean isRulerAction) {
53         super(QuickDiffMessages.getBundleForConstructedKeys(), prefix, editor);
54         fIsRulerAction= isRulerAction;
55
56         ISelectionProvider selectionProvider= editor.getSelectionProvider();
57         if (selectionProvider instanceof IPostSelectionProvider)
58             ((IPostSelectionProvider)selectionProvider).addPostSelectionChangedListener(this);
59     }
60
61     /**
62      * Called by this action's run method inside a pair of calls to <code>IRewriteTarget.beginCompoundChange</code>
63      * and <code>IRewriteTarget.endCompoundChange</code>().
64      *
65      * @see IRewriteTarget
66      */

67     protected abstract void runCompoundChange();
68
69     /*
70      * @see org.eclipse.jface.action.IAction#run()
71      */

72     public void run() {
73         ITextEditor editor= getTextEditor();
74         if (editor == null || !validateEditorInputState())
75             return;
76         IRewriteTarget target= (IRewriteTarget)editor.getAdapter(IRewriteTarget.class);
77         if (target != null)
78             target.beginCompoundChange();
79         runCompoundChange();
80         if (target != null)
81             target.endCompoundChange();
82
83     }
84
85     /*
86      * @see org.eclipse.ui.texteditor.IUpdate#update()
87      */

88     public void update() {
89         /*
90          * Update only works if we're updated from the ruler action
91          * (see AbstractDecoratedTextEditor.rulerContextMenuAboutToShow).
92          */

93         super.update();
94
95         setEnabled(computeEnablement());
96     }
97
98     /*
99      * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
100      * @since 3.3
101      */

102     public void selectionChanged(SelectionChangedEvent event) {
103         update();
104     }
105
106     /**
107      * Computes, caches and returns the internal state, including enablement.
108      *
109      * @return <code>true</code> if the action is enabled, <code>false</code>
110      * if it is not
111      */

112     protected boolean computeEnablement() {
113         if (!super.isEnabled())
114             return false;
115
116         if (!canModifyEditor())
117             return false;
118
119         fLastLine= computeLine(fIsRulerAction);
120         return true;
121     }
122
123     /**
124      * Returns the selection of the editor this action belongs to.
125      *
126      * @return the editor's selection, or <code>null</code>
127      */

128     protected ITextSelection getSelection() {
129         if (getTextEditor() == null)
130             return null;
131         ISelectionProvider sp= getTextEditor().getSelectionProvider();
132         if (sp == null)
133             return null;
134         ISelection s= sp.getSelection();
135         if (s instanceof ITextSelection)
136             return (ITextSelection)s;
137         return null;
138     }
139
140     /**
141      * Returns the current line of activity
142      *
143      * @return the currently active line
144      * @since 3.1
145      */

146     protected int getLastLine() {
147         return fLastLine;
148     }
149
150     /**
151      * Returns the active line
152      *
153      * @param useRulerInfo
154      * @return the line of interest.
155      * @since 3.1
156      */

157     private int computeLine(boolean useRulerInfo) {
158         int lastLine;
159         if (useRulerInfo) {
160             IVerticalRulerInfo ruler= getRuler();
161             if (ruler == null)
162                 lastLine= -1;
163             else
164                 lastLine= ruler.getLineOfLastMouseButtonActivity();
165         } else {
166             ITextSelection selection= getSelection();
167             if (selection == null)
168                 lastLine= -1;
169             else
170                 lastLine= selection.getEndLine();
171         }
172         return lastLine;
173     }
174
175     /**
176      * Returns the annotation model of the document displayed in this action's editor, if it
177      * implements the {@link IAnnotationModelExtension IAnnotationModelExtension} interface.
178      *
179      * @return the displayed document's annotation model if it is an <code>IAnnotationModelExtension</code>, or <code>null</code>
180      */

181     private IAnnotationModelExtension getModel() {
182         if (getTextEditor() == null)
183             return null;
184         IDocumentProvider provider= getTextEditor().getDocumentProvider();
185         IEditorInput editorInput= getTextEditor().getEditorInput();
186         IAnnotationModel m= provider.getAnnotationModel(editorInput);
187         if (m instanceof IAnnotationModelExtension)
188             return (IAnnotationModelExtension)m;
189         return null;
190     }
191
192     /**
193      * Returns the diff model associated with the annotation model of the document currently displayed
194      * in this action's editor, if any.
195      *
196      * @return the diff model associated with the displayed document, or <code>null</code>
197      */

198     protected ILineDiffer getDiffer() {
199         IAnnotationModelExtension extension= getModel();
200         if (extension != null)
201             return (ILineDiffer)extension.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
202         return null;
203     }
204
205     /**
206      * Returns a <code>IVerticalRulerInfo</code> if this action's editor adapts to one.
207      *
208      * @return the <code>IVerticalRulerInfo</code> for the editor's vertical ruler, or <code>null</code>
209      */

210     protected IVerticalRulerInfo getRuler() {
211         if (getTextEditor() != null)
212             return (IVerticalRulerInfo)getTextEditor().getAdapter(IVerticalRulerInfo.class);
213         return null;
214     }
215
216     /**
217      * Sets the status line error message to <code>string</code>.
218      *
219      * @param string the message to be displayed as error.
220      */

221     protected void setStatus(String JavaDoc string) {
222         if (getTextEditor() != null) {
223             IEditorStatusLine statusLine= (IEditorStatusLine) getTextEditor().getAdapter(IEditorStatusLine.class);
224             if (statusLine != null) {
225                 statusLine.setMessage(true, string, null);
226             }
227         }
228     }
229 }
230
Popular Tags