KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > quickdiff > QuickDiff


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 package org.eclipse.ui.texteditor.quickdiff;
12
13 import java.util.Collections JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ListIterator JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19
20 import org.eclipse.jface.text.source.IAnnotationModel;
21
22 import org.eclipse.ui.internal.texteditor.TextEditorPlugin;
23 import org.eclipse.ui.internal.texteditor.quickdiff.DocumentLineDiffer;
24 import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffExtensionsRegistry;
25 import org.eclipse.ui.texteditor.ITextEditor;
26
27 /**
28  * Access class for the quick diff reference provider extension point.
29  * <p>
30  * This class may be instantiated, it is not intended to be subclassed.
31  * </p>
32  * @since 3.0
33  */

34 public class QuickDiff {
35
36     /**
37      * Creates a new instance.
38      */

39     public QuickDiff() {
40     }
41
42     /**
43      * Returns the descriptor of the "last saved version" reference provider.
44      * <p>
45      * Clients should not cache this value because it can change when plug-ins get dynamically added or removed.
46      * </p>
47      *
48      * @return the descriptor of "last saved version" reference provider or <code>null</code> if none
49      */

50     public ReferenceProviderDescriptor getDefaultProvider() {
51         QuickDiffExtensionsRegistry registry= TextEditorPlugin.getDefault().getQuickDiffExtensionRegistry();
52         if (registry != null)
53             return registry.getDefaultProvider();
54
55         return null;
56     }
57
58     /**
59      * Returns a non-modifiable list of <code>ReferenceProviderDescriptor</code> describing all extension
60      * to the <code>quickDiffReferenceProvider</code> extension point.
61      * <p>
62      * Clients should not cache this list because it can change when plug-ins get dynamically added or removed.
63      * </p>
64      *
65      * @return the non-modifiable list of extensions to the <code>quickDiffReferenceProvider</code> extension point.
66      */

67     public List JavaDoc getReferenceProviderDescriptors() {
68         QuickDiffExtensionsRegistry registry= TextEditorPlugin.getDefault().getQuickDiffExtensionRegistry();
69         if (registry != null)
70             return registry.getReferenceProviderDescriptors();
71
72         return Collections.EMPTY_LIST;
73     }
74
75     /**
76      * Returns the quick diff reference provider registered under <code>id</code>, or the default
77      * reference provider. The returned provider gets its editor set to <code>editor</code>. If neither
78      * the requested provider nor the default provider return <code>true</code> from <code>isEnabled</code> after
79      * having the editor set, <code>null</code> is returned.
80      * <p>
81      * Clients should not cache this value because it can change when plug-ins get dynamically added or removed.
82      * </p>
83      *
84      * @param editor the editor to be installed with the returned provider
85      * @param id the id as specified in the <code>plugin.xml</code> that installs the reference provider
86      * @return the reference provider registered under <code>id</code>, or the default reference provider, or <code>null</code>
87      */

88     public IQuickDiffReferenceProvider getReferenceProviderOrDefault(ITextEditor editor, String JavaDoc id) {
89         Assert.isNotNull(editor);
90         Assert.isNotNull(id);
91
92         List JavaDoc descs= getReferenceProviderDescriptors();
93         // try to fetch preferred provider; load if needed
94
for (Iterator JavaDoc iter= descs.iterator(); iter.hasNext();) {
95             ReferenceProviderDescriptor desc= (ReferenceProviderDescriptor) iter.next();
96             if (desc.getId().equals(id)) {
97                 IQuickDiffReferenceProvider provider= desc.createProvider();
98                 if (provider != null) {
99                     provider.setActiveEditor(editor);
100                     if (provider.isEnabled())
101                         return provider;
102                     provider.dispose();
103                     provider= null;
104                 }
105             }
106         }
107         
108         for (ListIterator JavaDoc iter= descs.listIterator(descs.size()); iter.hasPrevious();) {
109             ReferenceProviderDescriptor desc= (ReferenceProviderDescriptor) iter.previous();
110             IQuickDiffReferenceProvider provider= desc.createProvider();
111             if (provider != null) {
112                 provider.setActiveEditor(editor);
113                 if (provider.isEnabled())
114                     return provider;
115                 provider.dispose();
116                 provider= null;
117             }
118         }
119
120         return null;
121     }
122
123     /**
124      * Creates a new line differ annotation model with its reference provider set to the reference provider
125      * obtained by calling <code>getReferenceProviderOrDefault(editor, id)</code>.
126      *
127      * @param editor the editor to be installed with the returned provider
128      * @param id the id as specified in the <code>plugin.xml</code> that installs the reference provider
129      * @return a quick diff annotation model
130      */

131     public IAnnotationModel createQuickDiffAnnotationModel(ITextEditor editor, String JavaDoc id) {
132         IQuickDiffReferenceProvider provider= getReferenceProviderOrDefault(editor, id);
133         if (provider != null) {
134             DocumentLineDiffer differ= new DocumentLineDiffer();
135             differ.setReferenceProvider(provider);
136             return differ;
137         }
138         return null;
139     }
140
141     /**
142      * Returns the identifier of the quick diff provider installed with the given diff annotation
143      * model, or the empty string if it is not a diff annotation model or has no configured diff
144      * provider.
145      *
146      * @param differ a diff annotation model
147      * @return the reference provider id, or the empty string for none
148      * @since 3.2
149      */

150     public Object JavaDoc getConfiguredQuickDiffProvider(IAnnotationModel differ) {
151         if (differ instanceof DocumentLineDiffer) {
152             DocumentLineDiffer lineDiffer= (DocumentLineDiffer) differ;
153             IQuickDiffReferenceProvider provider= lineDiffer.getReferenceProvider();
154             if (provider != null)
155                 return provider.getId();
156         }
157         return ""; //$NON-NLS-1$
158
}
159
160 }
161
Popular Tags