KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > ManifestConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.editor.text;
12
13 import org.eclipse.jface.text.DefaultInformationControl;
14 import org.eclipse.jface.text.IDocument;
15 import org.eclipse.jface.text.IInformationControl;
16 import org.eclipse.jface.text.IInformationControlCreator;
17 import org.eclipse.jface.text.ITextHover;
18 import org.eclipse.jface.text.contentassist.ContentAssistant;
19 import org.eclipse.jface.text.contentassist.IContentAssistant;
20 import org.eclipse.jface.text.presentation.IPresentationReconciler;
21 import org.eclipse.jface.text.presentation.PresentationReconciler;
22 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
23 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
24 import org.eclipse.jface.text.rules.IRule;
25 import org.eclipse.jface.text.rules.IWordDetector;
26 import org.eclipse.jface.text.rules.Token;
27 import org.eclipse.jface.text.rules.WordRule;
28 import org.eclipse.jface.text.source.IAnnotationHover;
29 import org.eclipse.jface.text.source.ISourceViewer;
30 import org.eclipse.jface.util.PropertyChangeEvent;
31 import org.eclipse.pde.internal.core.ICoreConstants;
32 import org.eclipse.pde.internal.ui.editor.PDESourcePage;
33 import org.eclipse.pde.internal.ui.editor.contentassist.ManifestContentAssistProcessor;
34 import org.eclipse.pde.internal.ui.editor.contentassist.display.HTMLTextPresenter;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.widgets.Shell;
37 import org.osgi.framework.Constants;
38
39 public class ManifestConfiguration extends ChangeAwareSourceViewerConfiguration {
40
41     private IAnnotationHover fAnnotationHover;
42     private BasePDEScanner fPropertyKeyScanner;
43     private BasePDEScanner fPropertyValueScanner;
44     private PDEQuickAssistAssistant fQuickAssistant;
45     private ContentAssistant fContentAssistant;
46     private ManifestContentAssistProcessor fContentAssistantProcessor;
47     private ManifestTextHover fTextHover;
48     private String JavaDoc fDocumentPartitioning;
49     
50     class ManifestHeaderScanner extends BasePDEScanner {
51         
52         private Token fToken;
53
54         public ManifestHeaderScanner() {
55             super(fColorManager);
56         }
57
58         public boolean affectsTextPresentation(String JavaDoc property) {
59             return property.startsWith(IPDEColorConstants.P_HEADER_KEY)
60                     || property.startsWith(IPDEColorConstants.P_HEADER_OSGI);
61         }
62         
63         protected Token getTokenAffected(PropertyChangeEvent event) {
64             if (event.getProperty().startsWith(IPDEColorConstants.P_HEADER_OSGI))
65                 return fToken;
66             return (Token)fDefaultReturnToken;
67         }
68         
69         protected void initialize() {
70             fToken = new Token(createTextAttribute(IPDEColorConstants.P_HEADER_OSGI));
71             WordRule rule = new WordRule(new KeywordDetector(), Token.UNDEFINED, true);
72             rule.addWord(Constants.BUNDLE_ACTIVATOR, fToken);
73             rule.addWord(Constants.BUNDLE_CATEGORY, fToken);
74             rule.addWord(Constants.BUNDLE_CLASSPATH, fToken);
75             rule.addWord(Constants.BUNDLE_CONTACTADDRESS, fToken);
76             rule.addWord(Constants.BUNDLE_COPYRIGHT, fToken);
77             rule.addWord(Constants.BUNDLE_DESCRIPTION, fToken);
78             rule.addWord(Constants.BUNDLE_DOCURL, fToken);
79             rule.addWord(Constants.BUNDLE_LOCALIZATION, fToken);
80             rule.addWord(Constants.BUNDLE_MANIFESTVERSION, fToken);
81             rule.addWord(Constants.BUNDLE_NAME, fToken);
82             rule.addWord(Constants.BUNDLE_NATIVECODE, fToken);
83             rule.addWord(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, fToken);
84             rule.addWord(Constants.BUNDLE_SYMBOLICNAME, fToken);
85             rule.addWord(Constants.BUNDLE_UPDATELOCATION, fToken);
86             rule.addWord(Constants.BUNDLE_VENDOR, fToken);
87             rule.addWord(Constants.BUNDLE_VERSION, fToken);
88             rule.addWord(Constants.REQUIRE_BUNDLE, fToken);
89             rule.addWord(Constants.DYNAMICIMPORT_PACKAGE, fToken);
90             rule.addWord(Constants.EXPORT_PACKAGE, fToken);
91             rule.addWord(ICoreConstants.EXPORT_SERVICE, fToken);
92             rule.addWord(Constants.FRAGMENT_HOST, fToken);
93             rule.addWord(Constants.IMPORT_PACKAGE, fToken);
94             rule.addWord(ICoreConstants.IMPORT_SERVICE, fToken);
95             rule.addWord(ICoreConstants.PROVIDE_PACKAGE, fToken);
96             setRules(new IRule[] {rule});
97             setDefaultReturnToken(new Token(createTextAttribute(IPDEColorConstants.P_HEADER_KEY)));
98         }
99     }
100     
101     class ManifestValueScanner extends BasePDEScanner {
102         
103         private Token fAssignmentToken;
104         private Token fAttributeToken;
105
106         public ManifestValueScanner() {
107             super(fColorManager);
108         }
109
110         public boolean affectsTextPresentation(String JavaDoc property) {
111             return property.startsWith(IPDEColorConstants.P_HEADER_ASSIGNMENT)
112                     || property.startsWith(IPDEColorConstants.P_HEADER_VALUE)
113                     || property.startsWith(IPDEColorConstants.P_HEADER_ATTRIBUTES);
114         }
115
116         protected Token getTokenAffected(PropertyChangeEvent event) {
117             String JavaDoc property = event.getProperty();
118             if (property.startsWith(IPDEColorConstants.P_HEADER_ASSIGNMENT))
119                 return fAssignmentToken;
120             if (property.startsWith(IPDEColorConstants.P_HEADER_ATTRIBUTES))
121                 return fAttributeToken;
122             return (Token)fDefaultReturnToken;
123         }
124
125         protected void initialize() {
126             IRule[] rules = new IRule[2];
127             fAssignmentToken = new Token(createTextAttribute(IPDEColorConstants.P_HEADER_ASSIGNMENT));
128             rules[0] = new WordRule(new AssignmentDetector(), fAssignmentToken);
129
130             fAttributeToken = new Token(createTextAttribute(IPDEColorConstants.P_HEADER_ATTRIBUTES));
131             WordRule rule = new WordRule(new KeywordDetector());
132             rule.addWord(Constants.BUNDLE_NATIVECODE_LANGUAGE, fAttributeToken);
133             rule.addWord(Constants.BUNDLE_NATIVECODE_OSNAME, fAttributeToken);
134             rule.addWord(Constants.BUNDLE_NATIVECODE_OSVERSION, fAttributeToken);
135             rule.addWord(Constants.BUNDLE_NATIVECODE_PROCESSOR, fAttributeToken);
136             rule.addWord(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE, fAttributeToken);
137             rule.addWord(Constants.BUNDLE_VERSION_ATTRIBUTE, fAttributeToken);
138             rule.addWord(Constants.EXCLUDE_DIRECTIVE, fAttributeToken);
139             rule.addWord(Constants.FRAGMENT_ATTACHMENT_DIRECTIVE, fAttributeToken);
140             rule.addWord(Constants.INCLUDE_DIRECTIVE, fAttributeToken);
141             rule.addWord(Constants.MANDATORY_DIRECTIVE, fAttributeToken);
142             rule.addWord(Constants.RESOLUTION_DIRECTIVE, fAttributeToken);
143             rule.addWord(Constants.SINGLETON_DIRECTIVE, fAttributeToken);
144             rule.addWord(Constants.USES_DIRECTIVE, fAttributeToken);
145             rule.addWord(Constants.VERSION_ATTRIBUTE, fAttributeToken);
146             rule.addWord(Constants.VISIBILITY_DIRECTIVE, fAttributeToken);
147             rule.addWord(ICoreConstants.FRIENDS_DIRECTIVE, fAttributeToken);
148             rule.addWord(ICoreConstants.INTERNAL_DIRECTIVE, fAttributeToken);
149             rule.addWord(ICoreConstants.PACKAGE_SPECIFICATION_VERSION, fAttributeToken);
150             // EASTER EGG
151
for (int i = 0; i < ICoreConstants.EE_TOKENS.length; i++)
152                 rule.addWord(ICoreConstants.EE_TOKENS[i], fAttributeToken);
153             rules[1] = rule;
154             
155             setRules(rules);
156             setDefaultReturnToken(new Token(createTextAttribute(IPDEColorConstants.P_HEADER_VALUE)));
157         }
158     }
159     
160     class AssignmentDetector implements IWordDetector {
161         public boolean isWordStart(char c) {
162             return c == ':' || c == '=';
163         }
164
165         public boolean isWordPart(char c) {
166             return false;
167         }
168     }
169
170     class KeywordDetector implements IWordDetector {
171         public boolean isWordStart(char c) {
172             return Character.isJavaIdentifierStart(c);
173         }
174
175         public boolean isWordPart(char c) {
176             return c != ':' && c != '=' && !Character.isSpaceChar(c);
177         }
178     }
179
180     public ManifestConfiguration(IColorManager manager) {
181         this(manager, null, null);
182     }
183     
184     public ManifestConfiguration(IColorManager manager, PDESourcePage page) {
185         this(manager, page, null);
186     }
187     
188     public ManifestConfiguration(IColorManager manager, PDESourcePage page, String JavaDoc documentPartitioning) {
189         super(page, manager);
190         fPropertyKeyScanner = new ManifestHeaderScanner();
191         fPropertyValueScanner = new ManifestValueScanner();
192         this.fDocumentPartitioning = documentPartitioning;
193     }
194
195     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
196         String JavaDoc[] partitions = ManifestPartitionScanner.PARTITIONS;
197         String JavaDoc[] all = new String JavaDoc[partitions.length + 1];
198         all[0] = IDocument.DEFAULT_CONTENT_TYPE;
199         System.arraycopy(partitions, 0, all, 1, partitions.length);
200         return all;
201     }
202     
203     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
204         if (fAnnotationHover == null)
205             fAnnotationHover = new AnnotationHover();
206         return fAnnotationHover;
207     }
208     
209     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
210         PresentationReconciler reconciler = new PresentationReconciler();
211         reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
212
213         DefaultDamagerRepairer dr = new DefaultDamagerRepairer(fPropertyKeyScanner);
214         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
215         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
216
217         dr = new DefaultDamagerRepairer(fPropertyValueScanner);
218         reconciler.setDamager(dr, ManifestPartitionScanner.MANIFEST_HEADER_VALUE);
219         reconciler.setRepairer(dr, ManifestPartitionScanner.MANIFEST_HEADER_VALUE);
220
221         return reconciler;
222     }
223     
224     public boolean affectsTextPresentation(PropertyChangeEvent event) {
225         String JavaDoc property = event.getProperty();
226         return property.startsWith(IPDEColorConstants.P_HEADER_KEY) ||
227             property.startsWith(IPDEColorConstants.P_HEADER_OSGI) ||
228             property.startsWith(IPDEColorConstants.P_HEADER_VALUE) ||
229             property.startsWith(IPDEColorConstants.P_HEADER_ATTRIBUTES) ||
230             property.startsWith(IPDEColorConstants.P_HEADER_ASSIGNMENT);
231     }
232
233     public boolean affectsColorPresentation(PropertyChangeEvent event) {
234         String JavaDoc property = event.getProperty();
235         return property.equals(IPDEColorConstants.P_HEADER_KEY) ||
236             property.equals(IPDEColorConstants.P_HEADER_OSGI) ||
237             property.equals(IPDEColorConstants.P_HEADER_VALUE) ||
238             property.equals(IPDEColorConstants.P_HEADER_ATTRIBUTES) ||
239             property.equals(IPDEColorConstants.P_HEADER_ASSIGNMENT);
240     }
241
242     public void adaptToPreferenceChange(PropertyChangeEvent event) {
243         if (affectsColorPresentation(event))
244             fColorManager.handlePropertyChangeEvent(event);
245         fPropertyKeyScanner.adaptToPreferenceChange(event);
246         fPropertyValueScanner.adaptToPreferenceChange(event);
247     }
248
249     public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
250         if (sourceViewer.isEditable()) {
251             if (fQuickAssistant == null)
252                 fQuickAssistant = new PDEQuickAssistAssistant();
253             return fQuickAssistant;
254         }
255         return null;
256     }
257     
258     public void dispose() {
259         if (fQuickAssistant != null)
260             fQuickAssistant.dispose();
261         if (fContentAssistant != null)
262             fContentAssistantProcessor.dispose();
263     }
264     
265     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
266         if (fSourcePage != null && fSourcePage.isEditable()) {
267             if (fContentAssistant == null) {
268                 fContentAssistant = new ContentAssistant();
269                 fContentAssistant.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
270                 fContentAssistantProcessor = new ManifestContentAssistProcessor(fSourcePage);
271                 fContentAssistant.setContentAssistProcessor(fContentAssistantProcessor, IDocument.DEFAULT_CONTENT_TYPE);
272                 fContentAssistant.setContentAssistProcessor(fContentAssistantProcessor, ManifestPartitionScanner.MANIFEST_HEADER_VALUE);
273                 fContentAssistant.addCompletionListener(fContentAssistantProcessor);
274                 fContentAssistant.setInformationControlCreator(new IInformationControlCreator() {
275                     public IInformationControl createInformationControl(Shell parent) {
276                         return new DefaultInformationControl(parent, SWT.NONE,
277                                 new HTMLTextPresenter(true));
278                     }
279                 });
280                 fContentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
281             }
282             return fContentAssistant;
283         }
284         return null;
285     }
286
287     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType) {
288         if (fTextHover == null && fSourcePage != null)
289             fTextHover = new ManifestTextHover(fSourcePage);
290         return fTextHover;
291     }
292     
293     protected int getInfoImplementationType() {
294         return SourceInformationProvider.F_MANIFEST_IMP;
295     }
296     
297     public String JavaDoc getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
298         if (fDocumentPartitioning != null)
299             return fDocumentPartitioning;
300         return super.getConfiguredDocumentPartitioning(sourceViewer);
301     }
302 }
303
Popular Tags