KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.ui.internal.texteditor.quickdiff;
13
14 import org.eclipse.core.runtime.Assert;
15
16 import org.eclipse.jface.action.Action;
17
18 import org.eclipse.jface.text.source.IAnnotationModel;
19 import org.eclipse.jface.text.source.IAnnotationModelExtension;
20 import org.eclipse.jface.text.source.IChangeRulerColumn;
21
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.texteditor.quickdiff.IQuickDiffReferenceProvider;
24 import org.eclipse.ui.texteditor.quickdiff.ReferenceProviderDescriptor;
25 import org.eclipse.ui.texteditor.IDocumentProvider;
26 import org.eclipse.ui.texteditor.ITextEditor;
27 import org.eclipse.ui.texteditor.ITextEditorExtension3;
28 import org.eclipse.ui.texteditor.IUpdate;
29
30
31 /**
32  * Action to set the quick diff reference for the document displayed in the editor. An instance of
33  * this class is created for every extension to the extension point <code>quickdiff.referenceprovider</code>, and for
34  * every editor. It acts as a proxy; its <code>run</code> method installs the reference provider
35  * specified by the extension with the quick diff differ on the current document.
36  *
37  * @since 3.0
38  */

39 public class ReferenceSelectionAction extends Action implements IUpdate {
40
41     /** The editor we get the document from. */
42     private ITextEditor fEditor= null;
43     /** The descriptor of the managed extension. */
44     private final ReferenceProviderDescriptor fDescriptor;
45     /** The implementation of the extension, after it has been loaded. */
46     private IQuickDiffReferenceProvider fProvider;
47
48     /**
49      * Creates a new instance that will lazily create the implementation provided by the extension.
50      *
51      * @param descriptor describes the extension.
52      * @param editor the editor for which this action is created.
53      */

54     public ReferenceSelectionAction(ReferenceProviderDescriptor descriptor, ITextEditor editor) {
55         super("", AS_RADIO_BUTTON); //$NON-NLS-1$
56
setChecked(false);
57         setEnabled(true);
58         Assert.isLegal(descriptor != null);
59         fDescriptor= descriptor;
60         fEditor= editor;
61         update();
62     }
63
64     /**
65      * Creates an instance of the implementation provided by the extension, if none has been created
66      * before. Otherwise, the cached implementation is returned.
67      * @return The <code>IQuickDiffProviderImplementation</code> instance provided by the extension.
68      */

69     private IQuickDiffReferenceProvider getProvider() {
70         if (fProvider == null) {
71             fProvider= fDescriptor.createProvider();
72         }
73         return fProvider;
74     }
75
76     /*
77      * @see org.eclipse.jface.action.IAction#run()
78      */

79     public void run() {
80
81         DocumentLineDiffer differ= getDiffer(true); // create if needed, so the user does not have to toggle display when he selects a reference
82
if (differ == null)
83             return;
84
85         if (fEditor instanceof ITextEditorExtension3) {
86             ITextEditorExtension3 extension= (ITextEditorExtension3) fEditor;
87             IQuickDiffReferenceProvider provider= getProvider();
88             if (provider != null) {
89                 provider.setActiveEditor(fEditor);
90                 if (provider.isEnabled()) {
91                     differ.setReferenceProvider(provider);
92                     extension.showChangeInformation(true);
93                     setEnabled(true);
94                 } else
95                     setEnabled(false);
96             }
97         }
98     }
99
100     /*
101      * @see org.eclipse.ui.texteditor.IUpdate#update()
102      */

103     public void update() {
104         /* two things happen here:
105          * 1: checked state setting - if a provider is already installed, and its id matches
106          * our id, we are in checked state.
107          * 2: enablement - if the extending plugin has been loaded, we check the provider for
108          * enablement and take it as our own.
109          */

110         setText(fDescriptor.getLabel());
111         DocumentLineDiffer differ= getDiffer(false); // don't create it if we're not showing
112
setChecked(false);
113         if (differ != null) {
114             IQuickDiffReferenceProvider provider= differ.getReferenceProvider();
115             if (provider != null && provider.getId().equals(fDescriptor.getId())) {
116                 setChecked(true);
117             }
118         }
119
120         if (fDescriptor.isPluginLoaded()) {
121             getProvider();
122             if (fProvider == null) {
123                 setEnabled(false);
124             } else {
125                 fProvider.setActiveEditor(fEditor);
126                 setEnabled(fProvider.isEnabled());
127             }
128         } else {
129             // optimistically enable it
130
setEnabled(true);
131         }
132     }
133
134     /**
135      * Fetches the differ installed with the current editor's document's annotation model. If none
136      * is installed yet, and <code>createIfNeeded</code> is true, one is created and attached to the
137      * model.
138      *
139      * @param createIfNeeded when set to <code>true</code>, a new differ will be created if needed.
140      * @return the differ installed with the annotation model, or <code>null</code>.
141      */

142     private DocumentLineDiffer getDiffer(boolean createIfNeeded) {
143         // get annotation model
144
if (fEditor == null)
145             return null;
146
147         IDocumentProvider provider= fEditor.getDocumentProvider();
148         IEditorInput editorInput= fEditor.getEditorInput();
149         if (provider == null || editorInput == null)
150             return null;
151
152         IAnnotationModel m= provider.getAnnotationModel(editorInput);
153         IAnnotationModelExtension model= null;
154         if (m instanceof IAnnotationModelExtension) {
155             model= (IAnnotationModelExtension)m;
156         } else {
157             return null;
158         }
159
160         // get differ
161
DocumentLineDiffer differ= (DocumentLineDiffer)model.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
162
163         // create if needed
164
if (differ == null && createIfNeeded) {
165             differ= new DocumentLineDiffer();
166             model.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, differ);
167         }
168
169         return differ;
170     }
171 }
172
Popular Tags