KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > 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.editors.quickdiff;
12
13 import java.util.ResourceBundle JavaDoc;
14
15 import org.eclipse.jface.viewers.IPostSelectionProvider;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19 import org.eclipse.jface.viewers.SelectionChangedEvent;
20
21 import org.eclipse.jface.text.IRewriteTarget;
22 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.jface.text.source.IAnnotationModelExtension;
25 import org.eclipse.jface.text.source.IChangeRulerColumn;
26 import org.eclipse.jface.text.source.ILineDiffer;
27 import org.eclipse.jface.text.source.IVerticalRulerInfo;
28
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.texteditor.IDocumentProvider;
31 import org.eclipse.ui.texteditor.IEditorStatusLine;
32 import org.eclipse.ui.texteditor.ITextEditor;
33 import org.eclipse.ui.texteditor.TextEditorAction;
34
35 /**
36  * Abstract superclass of actions that restore / revert parts of a document displayed in the action's
37  * editor to the state described by the {@link ILineDiffer ILineDiffer} associated with the document's
38  * {@link IAnnotationModel IAnnotationModel}.
39  *
40  * @since 3.1
41  */

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

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

70     protected abstract void runCompoundChange();
71
72     /*
73      * @see org.eclipse.jface.action.IAction#run()
74      */

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

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

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

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

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

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

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

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

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

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

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

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