001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
011:
012: import org.eclipse.jface.text.DefaultInformationControl;
013: import org.eclipse.jface.text.IDocument;
014: import org.eclipse.jface.text.IInformationControl;
015: import org.eclipse.jface.text.IInformationControlCreator;
016: import org.eclipse.jface.text.ITextHover;
017: import org.eclipse.jface.text.contentassist.ContentAssistant;
018: import org.eclipse.jface.text.contentassist.IContentAssistant;
019: import org.eclipse.jface.text.presentation.IPresentationReconciler;
020: import org.eclipse.jface.text.presentation.PresentationReconciler;
021: import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
022: import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
023: import org.eclipse.jface.text.rules.IRule;
024: import org.eclipse.jface.text.rules.IWordDetector;
025: import org.eclipse.jface.text.rules.Token;
026: import org.eclipse.jface.text.rules.WordRule;
027: import org.eclipse.jface.text.source.IAnnotationHover;
028: import org.eclipse.jface.text.source.ISourceViewer;
029: import org.eclipse.jface.util.PropertyChangeEvent;
030: import org.eclipse.pde.internal.core.ICoreConstants;
031: import org.eclipse.pde.internal.ui.editor.PDESourcePage;
032: import org.eclipse.pde.internal.ui.editor.contentassist.ManifestContentAssistProcessor;
033: import org.eclipse.pde.internal.ui.editor.contentassist.display.HTMLTextPresenter;
034: import org.eclipse.swt.SWT;
035: import org.eclipse.swt.widgets.Shell;
036: import org.osgi.framework.Constants;
037:
038: public class ManifestConfiguration extends
039: ChangeAwareSourceViewerConfiguration {
040:
041: private IAnnotationHover fAnnotationHover;
042: private BasePDEScanner fPropertyKeyScanner;
043: private BasePDEScanner fPropertyValueScanner;
044: private PDEQuickAssistAssistant fQuickAssistant;
045: private ContentAssistant fContentAssistant;
046: private ManifestContentAssistProcessor fContentAssistantProcessor;
047: private ManifestTextHover fTextHover;
048: private String fDocumentPartitioning;
049:
050: class ManifestHeaderScanner extends BasePDEScanner {
051:
052: private Token fToken;
053:
054: public ManifestHeaderScanner() {
055: super (fColorManager);
056: }
057:
058: public boolean affectsTextPresentation(String property) {
059: return property.startsWith(IPDEColorConstants.P_HEADER_KEY)
060: || property
061: .startsWith(IPDEColorConstants.P_HEADER_OSGI);
062: }
063:
064: protected Token getTokenAffected(PropertyChangeEvent event) {
065: if (event.getProperty().startsWith(
066: IPDEColorConstants.P_HEADER_OSGI))
067: return fToken;
068: return (Token) fDefaultReturnToken;
069: }
070:
071: protected void initialize() {
072: fToken = new Token(
073: createTextAttribute(IPDEColorConstants.P_HEADER_OSGI));
074: WordRule rule = new WordRule(new KeywordDetector(),
075: Token.UNDEFINED, true);
076: rule.addWord(Constants.BUNDLE_ACTIVATOR, fToken);
077: rule.addWord(Constants.BUNDLE_CATEGORY, fToken);
078: rule.addWord(Constants.BUNDLE_CLASSPATH, fToken);
079: rule.addWord(Constants.BUNDLE_CONTACTADDRESS, fToken);
080: rule.addWord(Constants.BUNDLE_COPYRIGHT, fToken);
081: rule.addWord(Constants.BUNDLE_DESCRIPTION, fToken);
082: rule.addWord(Constants.BUNDLE_DOCURL, fToken);
083: rule.addWord(Constants.BUNDLE_LOCALIZATION, fToken);
084: rule.addWord(Constants.BUNDLE_MANIFESTVERSION, fToken);
085: rule.addWord(Constants.BUNDLE_NAME, fToken);
086: rule.addWord(Constants.BUNDLE_NATIVECODE, fToken);
087: rule.addWord(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT,
088: fToken);
089: rule.addWord(Constants.BUNDLE_SYMBOLICNAME, fToken);
090: rule.addWord(Constants.BUNDLE_UPDATELOCATION, fToken);
091: rule.addWord(Constants.BUNDLE_VENDOR, fToken);
092: rule.addWord(Constants.BUNDLE_VERSION, fToken);
093: rule.addWord(Constants.REQUIRE_BUNDLE, fToken);
094: rule.addWord(Constants.DYNAMICIMPORT_PACKAGE, fToken);
095: rule.addWord(Constants.EXPORT_PACKAGE, fToken);
096: rule.addWord(ICoreConstants.EXPORT_SERVICE, fToken);
097: rule.addWord(Constants.FRAGMENT_HOST, fToken);
098: rule.addWord(Constants.IMPORT_PACKAGE, fToken);
099: rule.addWord(ICoreConstants.IMPORT_SERVICE, fToken);
100: rule.addWord(ICoreConstants.PROVIDE_PACKAGE, fToken);
101: setRules(new IRule[] { rule });
102: setDefaultReturnToken(new Token(
103: createTextAttribute(IPDEColorConstants.P_HEADER_KEY)));
104: }
105: }
106:
107: class ManifestValueScanner extends BasePDEScanner {
108:
109: private Token fAssignmentToken;
110: private Token fAttributeToken;
111:
112: public ManifestValueScanner() {
113: super (fColorManager);
114: }
115:
116: public boolean affectsTextPresentation(String property) {
117: return property
118: .startsWith(IPDEColorConstants.P_HEADER_ASSIGNMENT)
119: || property
120: .startsWith(IPDEColorConstants.P_HEADER_VALUE)
121: || property
122: .startsWith(IPDEColorConstants.P_HEADER_ATTRIBUTES);
123: }
124:
125: protected Token getTokenAffected(PropertyChangeEvent event) {
126: String property = event.getProperty();
127: if (property
128: .startsWith(IPDEColorConstants.P_HEADER_ASSIGNMENT))
129: return fAssignmentToken;
130: if (property
131: .startsWith(IPDEColorConstants.P_HEADER_ATTRIBUTES))
132: return fAttributeToken;
133: return (Token) fDefaultReturnToken;
134: }
135:
136: protected void initialize() {
137: IRule[] rules = new IRule[2];
138: fAssignmentToken = new Token(
139: createTextAttribute(IPDEColorConstants.P_HEADER_ASSIGNMENT));
140: rules[0] = new WordRule(new AssignmentDetector(),
141: fAssignmentToken);
142:
143: fAttributeToken = new Token(
144: createTextAttribute(IPDEColorConstants.P_HEADER_ATTRIBUTES));
145: WordRule rule = new WordRule(new KeywordDetector());
146: rule.addWord(Constants.BUNDLE_NATIVECODE_LANGUAGE,
147: fAttributeToken);
148: rule.addWord(Constants.BUNDLE_NATIVECODE_OSNAME,
149: fAttributeToken);
150: rule.addWord(Constants.BUNDLE_NATIVECODE_OSVERSION,
151: fAttributeToken);
152: rule.addWord(Constants.BUNDLE_NATIVECODE_PROCESSOR,
153: fAttributeToken);
154: rule.addWord(Constants.BUNDLE_SYMBOLICNAME_ATTRIBUTE,
155: fAttributeToken);
156: rule.addWord(Constants.BUNDLE_VERSION_ATTRIBUTE,
157: fAttributeToken);
158: rule.addWord(Constants.EXCLUDE_DIRECTIVE, fAttributeToken);
159: rule.addWord(Constants.FRAGMENT_ATTACHMENT_DIRECTIVE,
160: fAttributeToken);
161: rule.addWord(Constants.INCLUDE_DIRECTIVE, fAttributeToken);
162: rule
163: .addWord(Constants.MANDATORY_DIRECTIVE,
164: fAttributeToken);
165: rule.addWord(Constants.RESOLUTION_DIRECTIVE,
166: fAttributeToken);
167: rule
168: .addWord(Constants.SINGLETON_DIRECTIVE,
169: fAttributeToken);
170: rule.addWord(Constants.USES_DIRECTIVE, fAttributeToken);
171: rule.addWord(Constants.VERSION_ATTRIBUTE, fAttributeToken);
172: rule.addWord(Constants.VISIBILITY_DIRECTIVE,
173: fAttributeToken);
174: rule.addWord(ICoreConstants.FRIENDS_DIRECTIVE,
175: fAttributeToken);
176: rule.addWord(ICoreConstants.INTERNAL_DIRECTIVE,
177: fAttributeToken);
178: rule.addWord(ICoreConstants.PACKAGE_SPECIFICATION_VERSION,
179: fAttributeToken);
180: // EASTER EGG
181: for (int i = 0; i < ICoreConstants.EE_TOKENS.length; i++)
182: rule.addWord(ICoreConstants.EE_TOKENS[i],
183: fAttributeToken);
184: rules[1] = rule;
185:
186: setRules(rules);
187: setDefaultReturnToken(new Token(
188: createTextAttribute(IPDEColorConstants.P_HEADER_VALUE)));
189: }
190: }
191:
192: class AssignmentDetector implements IWordDetector {
193: public boolean isWordStart(char c) {
194: return c == ':' || c == '=';
195: }
196:
197: public boolean isWordPart(char c) {
198: return false;
199: }
200: }
201:
202: class KeywordDetector implements IWordDetector {
203: public boolean isWordStart(char c) {
204: return Character.isJavaIdentifierStart(c);
205: }
206:
207: public boolean isWordPart(char c) {
208: return c != ':' && c != '=' && !Character.isSpaceChar(c);
209: }
210: }
211:
212: public ManifestConfiguration(IColorManager manager) {
213: this (manager, null, null);
214: }
215:
216: public ManifestConfiguration(IColorManager manager,
217: PDESourcePage page) {
218: this (manager, page, null);
219: }
220:
221: public ManifestConfiguration(IColorManager manager,
222: PDESourcePage page, String documentPartitioning) {
223: super (page, manager);
224: fPropertyKeyScanner = new ManifestHeaderScanner();
225: fPropertyValueScanner = new ManifestValueScanner();
226: this .fDocumentPartitioning = documentPartitioning;
227: }
228:
229: public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
230: String[] partitions = ManifestPartitionScanner.PARTITIONS;
231: String[] all = new String[partitions.length + 1];
232: all[0] = IDocument.DEFAULT_CONTENT_TYPE;
233: System.arraycopy(partitions, 0, all, 1, partitions.length);
234: return all;
235: }
236:
237: public IAnnotationHover getAnnotationHover(
238: ISourceViewer sourceViewer) {
239: if (fAnnotationHover == null)
240: fAnnotationHover = new AnnotationHover();
241: return fAnnotationHover;
242: }
243:
244: public IPresentationReconciler getPresentationReconciler(
245: ISourceViewer sourceViewer) {
246: PresentationReconciler reconciler = new PresentationReconciler();
247: reconciler
248: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
249:
250: DefaultDamagerRepairer dr = new DefaultDamagerRepairer(
251: fPropertyKeyScanner);
252: reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
253: reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
254:
255: dr = new DefaultDamagerRepairer(fPropertyValueScanner);
256: reconciler.setDamager(dr,
257: ManifestPartitionScanner.MANIFEST_HEADER_VALUE);
258: reconciler.setRepairer(dr,
259: ManifestPartitionScanner.MANIFEST_HEADER_VALUE);
260:
261: return reconciler;
262: }
263:
264: public boolean affectsTextPresentation(PropertyChangeEvent event) {
265: String property = event.getProperty();
266: return property.startsWith(IPDEColorConstants.P_HEADER_KEY)
267: || property
268: .startsWith(IPDEColorConstants.P_HEADER_OSGI)
269: || property
270: .startsWith(IPDEColorConstants.P_HEADER_VALUE)
271: || property
272: .startsWith(IPDEColorConstants.P_HEADER_ATTRIBUTES)
273: || property
274: .startsWith(IPDEColorConstants.P_HEADER_ASSIGNMENT);
275: }
276:
277: public boolean affectsColorPresentation(PropertyChangeEvent event) {
278: String property = event.getProperty();
279: return property.equals(IPDEColorConstants.P_HEADER_KEY)
280: || property.equals(IPDEColorConstants.P_HEADER_OSGI)
281: || property.equals(IPDEColorConstants.P_HEADER_VALUE)
282: || property
283: .equals(IPDEColorConstants.P_HEADER_ATTRIBUTES)
284: || property
285: .equals(IPDEColorConstants.P_HEADER_ASSIGNMENT);
286: }
287:
288: public void adaptToPreferenceChange(PropertyChangeEvent event) {
289: if (affectsColorPresentation(event))
290: fColorManager.handlePropertyChangeEvent(event);
291: fPropertyKeyScanner.adaptToPreferenceChange(event);
292: fPropertyValueScanner.adaptToPreferenceChange(event);
293: }
294:
295: public IQuickAssistAssistant getQuickAssistAssistant(
296: ISourceViewer sourceViewer) {
297: if (sourceViewer.isEditable()) {
298: if (fQuickAssistant == null)
299: fQuickAssistant = new PDEQuickAssistAssistant();
300: return fQuickAssistant;
301: }
302: return null;
303: }
304:
305: public void dispose() {
306: if (fQuickAssistant != null)
307: fQuickAssistant.dispose();
308: if (fContentAssistant != null)
309: fContentAssistantProcessor.dispose();
310: }
311:
312: public IContentAssistant getContentAssistant(
313: ISourceViewer sourceViewer) {
314: if (fSourcePage != null && fSourcePage.isEditable()) {
315: if (fContentAssistant == null) {
316: fContentAssistant = new ContentAssistant();
317: fContentAssistant
318: .setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
319: fContentAssistantProcessor = new ManifestContentAssistProcessor(
320: fSourcePage);
321: fContentAssistant.setContentAssistProcessor(
322: fContentAssistantProcessor,
323: IDocument.DEFAULT_CONTENT_TYPE);
324: fContentAssistant.setContentAssistProcessor(
325: fContentAssistantProcessor,
326: ManifestPartitionScanner.MANIFEST_HEADER_VALUE);
327: fContentAssistant
328: .addCompletionListener(fContentAssistantProcessor);
329: fContentAssistant
330: .setInformationControlCreator(new IInformationControlCreator() {
331: public IInformationControl createInformationControl(
332: Shell parent) {
333: return new DefaultInformationControl(
334: parent, SWT.NONE,
335: new HTMLTextPresenter(true));
336: }
337: });
338: fContentAssistant
339: .setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
340: }
341: return fContentAssistant;
342: }
343: return null;
344: }
345:
346: public ITextHover getTextHover(ISourceViewer sourceViewer,
347: String contentType) {
348: if (fTextHover == null && fSourcePage != null)
349: fTextHover = new ManifestTextHover(fSourcePage);
350: return fTextHover;
351: }
352:
353: protected int getInfoImplementationType() {
354: return SourceInformationProvider.F_MANIFEST_IMP;
355: }
356:
357: public String getConfiguredDocumentPartitioning(
358: ISourceViewer sourceViewer) {
359: if (fDocumentPartitioning != null)
360: return fDocumentPartitioning;
361: return super.getConfiguredDocumentPartitioning(sourceViewer);
362: }
363: }
|