KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > AntEditorSourceViewerConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2002, 2007 GEBIT Gesellschaft fuer EDV-Beratung
3  * und Informatik-Technologien mbH,
4  * Berlin, Duesseldorf, Frankfurt (Germany) and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial API and implementation
12  * IBM Corporation - bug fixes
13  * John-Mason P. Shackelford - bug 40255
14  * Rob Dingwell - bug 68886
15  *******************************************************************************/

16
17 package org.eclipse.ant.internal.ui.editor;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.eclipse.ant.internal.ui.AntSourceViewerConfiguration;
24 import org.eclipse.ant.internal.ui.ColorManager;
25 import org.eclipse.ant.internal.ui.editor.formatter.XmlDocumentFormattingStrategy;
26 import org.eclipse.ant.internal.ui.editor.formatter.XmlElementFormattingStrategy;
27 import org.eclipse.ant.internal.ui.editor.text.AntDocumentSetupParticipant;
28 import org.eclipse.ant.internal.ui.editor.text.AntEditorPartitionScanner;
29 import org.eclipse.ant.internal.ui.editor.text.AntInformationProvider;
30 import org.eclipse.ant.internal.ui.editor.text.NotifyingReconciler;
31 import org.eclipse.ant.internal.ui.editor.text.XMLAnnotationHover;
32 import org.eclipse.ant.internal.ui.editor.text.XMLReconcilingStrategy;
33 import org.eclipse.ant.internal.ui.editor.text.XMLTextHover;
34 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
35 import org.eclipse.jface.internal.text.html.HTMLTextPresenter;
36 import org.eclipse.jface.preference.PreferenceConverter;
37 import org.eclipse.jface.text.DefaultInformationControl;
38 import org.eclipse.jface.text.IAutoEditStrategy;
39 import org.eclipse.jface.text.IDocument;
40 import org.eclipse.jface.text.IInformationControl;
41 import org.eclipse.jface.text.IInformationControlCreator;
42 import org.eclipse.jface.text.ITextHover;
43 import org.eclipse.jface.text.contentassist.ContentAssistant;
44 import org.eclipse.jface.text.contentassist.IContentAssistant;
45 import org.eclipse.jface.text.formatter.IContentFormatter;
46 import org.eclipse.jface.text.formatter.MultiPassContentFormatter;
47 import org.eclipse.jface.text.information.IInformationPresenter;
48 import org.eclipse.jface.text.information.IInformationProvider;
49 import org.eclipse.jface.text.information.InformationPresenter;
50 import org.eclipse.jface.text.reconciler.IReconciler;
51 import org.eclipse.jface.text.source.IAnnotationHover;
52 import org.eclipse.jface.text.source.ISourceViewer;
53 import org.eclipse.jface.util.PropertyChangeEvent;
54 import org.eclipse.swt.SWT;
55 import org.eclipse.swt.graphics.Color;
56 import org.eclipse.swt.graphics.RGB;
57 import org.eclipse.swt.widgets.Shell;
58
59 /**
60  * The source viewer configuration for the Ant Editor.
61  */

62 public class AntEditorSourceViewerConfiguration extends AntSourceViewerConfiguration {
63     
64     private AntEditor fEditor;
65
66     private XMLTextHover fTextHover;
67     
68     private ContentAssistant fContentAssistant;
69     
70     private AntAutoEditStrategy[] fAutoEditorStategies;
71     
72     /**
73      * Creates an instance with the specified color manager.
74      */

75     public AntEditorSourceViewerConfiguration(AntEditor editor) {
76         super();
77         fEditor= editor;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentAssistant(ISourceViewer)
82      */

83     public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) {
84         fContentAssistant= new ContentAssistant();
85         AntEditorCompletionProcessor processor = new AntEditorCompletionProcessor(fEditor.getAntModel());
86         fContentAssistant.setContentAssistProcessor(processor, IDocument.DEFAULT_CONTENT_TYPE);
87         fContentAssistant.setContentAssistProcessor(processor, AntEditorPartitionScanner.XML_TAG);
88         fContentAssistant.setDocumentPartitioning(AntDocumentSetupParticipant.ANT_PARTITIONING);
89         
90         String JavaDoc triggers= fPreferenceStore.getString(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS);
91         if (triggers != null) {
92             processor.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
93         }
94                 
95         fContentAssistant.enableAutoInsert(fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOINSERT));
96         fContentAssistant.enableAutoActivation(fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION));
97         fContentAssistant.setAutoActivationDelay(fPreferenceStore.getInt(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY));
98         fContentAssistant.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
99         fContentAssistant.setContextInformationPopupOrientation(IContentAssistant.CONTEXT_INFO_ABOVE);
100         fContentAssistant.setInformationControlCreator(getInformationControlCreator(sourceViewer));
101
102         ColorManager manager= ColorManager.getDefault();
103         Color background= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
104         fContentAssistant.setContextInformationPopupBackground(background);
105         fContentAssistant.setContextSelectorBackground(background);
106         fContentAssistant.setProposalSelectorBackground(background);
107
108         Color foreground= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
109         fContentAssistant.setContextInformationPopupForeground(foreground);
110         fContentAssistant.setContextSelectorForeground(foreground);
111         fContentAssistant.setProposalSelectorForeground(foreground);
112             
113         IInformationControlCreator creator = getInformationControlCreator(sourceViewer);
114         fContentAssistant.setInformationControlCreator(creator);
115
116         fContentAssistant.setRepeatedInvocationMode(true);
117         fContentAssistant.setStatusLineVisible(true);
118         fContentAssistant.setShowEmptyList(true);
119         fContentAssistant.addCompletionListener(processor);
120         return fContentAssistant;
121     }
122
123     /* (non-Javadoc)
124      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
125      */

126     public IReconciler getReconciler(ISourceViewer sourceViewer) {
127         NotifyingReconciler reconciler= new NotifyingReconciler(new XMLReconcilingStrategy(fEditor));
128         reconciler.setDelay(XMLReconcilingStrategy.DELAY);
129         reconciler.addReconcilingParticipant(fEditor);
130         return reconciler;
131     }
132
133     /*
134      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer)
135      */

136     public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
137         return new XMLAnnotationHover();
138     }
139
140     /*
141      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getInformationControlCreator(org.eclipse.jface.text.source.ISourceViewer)
142      */

143     public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
144         return new IInformationControlCreator() {
145             public IInformationControl createInformationControl(Shell parent) {
146                 return new DefaultInformationControl(parent, SWT.NONE, new HTMLTextPresenter(true));
147             }
148         };
149     }
150
151     /*
152      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
153      */

154     public ITextHover getTextHover(ISourceViewer sourceViewer, String JavaDoc contentType) {
155         if (fTextHover == null) {
156             fTextHover= new XMLTextHover(fEditor);
157         }
158         return fTextHover;
159     }
160
161     private Color getColor(String JavaDoc key, ColorManager manager) {
162         RGB rgb= PreferenceConverter.getColor(fPreferenceStore, key);
163         return manager.getColor(rgb);
164     }
165     
166     protected void changeConfiguration(PropertyChangeEvent event) {
167         String JavaDoc p= event.getProperty();
168
169         ColorManager manager= ColorManager.getDefault();
170         if (AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION.equals(p)) {
171             boolean enabled= fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION);
172             fContentAssistant.enableAutoActivation(enabled);
173         } else if (AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY.equals(p) && fContentAssistant != null) {
174             int delay= fPreferenceStore.getInt(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY);
175             fContentAssistant.setAutoActivationDelay(delay);
176         } else if (AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND.equals(p) && fContentAssistant != null) {
177             Color c= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND, manager);
178             fContentAssistant.setProposalSelectorForeground(c);
179         } else if (AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND.equals(p) && fContentAssistant != null) {
180             Color c= getColor(AntEditorPreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND, manager);
181             fContentAssistant.setProposalSelectorBackground(c);
182         } else if (AntEditorPreferenceConstants.CODEASSIST_AUTOINSERT.equals(p) && fContentAssistant != null) {
183             boolean enabled= fPreferenceStore.getBoolean(AntEditorPreferenceConstants.CODEASSIST_AUTOINSERT);
184             fContentAssistant.enableAutoInsert(enabled);
185         } else if (AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS.equals(p)) {
186             changeContentAssistProcessor();
187         }
188     }
189     
190     private void changeContentAssistProcessor() {
191         String JavaDoc triggers= fPreferenceStore.getString(AntEditorPreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS);
192         if (triggers != null) {
193             AntEditorCompletionProcessor cp= (AntEditorCompletionProcessor)fContentAssistant.getContentAssistProcessor(IDocument.DEFAULT_CONTENT_TYPE);
194             if (cp != null) {
195                 cp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
196             }
197         }
198     }
199     /* (non-Javadoc)
200      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getContentFormatter(org.eclipse.jface.text.source.ISourceViewer)
201      */

202     public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
203          
204         MultiPassContentFormatter formatter = new MultiPassContentFormatter(
205                 getConfiguredDocumentPartitioning(sourceViewer),
206                  IDocument.DEFAULT_CONTENT_TYPE);
207          
208         
209         formatter.setMasterStrategy(new XmlDocumentFormattingStrategy());
210         
211         formatter.setSlaveStrategy(new XmlElementFormattingStrategy(), AntEditorPartitionScanner.XML_TAG);
212         
213         //formatter.setSlaveStrategy(new XmlCommentFormattingStrategy(), AntEditorPartitionScanner.XML_COMMENT);
214

215         return formatter;
216     }
217     
218     /* (non-Javadoc)
219      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getInformationPresenter(org.eclipse.jface.text.source.ISourceViewer)
220      */

221     public IInformationPresenter getInformationPresenter(ISourceViewer sourceViewer) {
222         InformationPresenter presenter= new InformationPresenter(getInformationPresenterControlCreator());
223         presenter.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
224         IInformationProvider provider= new AntInformationProvider(new XMLTextHover(fEditor));
225         presenter.setInformationProvider(provider, IDocument.DEFAULT_CONTENT_TYPE);
226         presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_CDATA);
227         presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_COMMENT);
228         presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_DTD);
229         presenter.setInformationProvider(provider, AntEditorPartitionScanner.XML_TAG);
230         presenter.setSizeConstraints(60, 10, true, true);
231         return presenter;
232     }
233     
234     /**
235      * Returns the information presenter control creator. The creator is a factory creating the
236      * presenter controls for the given source viewer. This implementation always returns a creator
237      * for <code>DefaultInformationControl</code> instances.
238      *
239      * @return an information control creator
240      * @since 3.1
241      */

242     public static IInformationControlCreator getInformationPresenterControlCreator() {
243         return new IInformationControlCreator() {
244             public IInformationControl createInformationControl(Shell parent) {
245                 int shellStyle= SWT.RESIZE;
246                 int style= SWT.V_SCROLL | SWT.H_SCROLL;
247                 return new DefaultInformationControl(parent, shellStyle, style, new HTMLTextPresenter(false));
248             }
249         };
250     }
251     
252     /* (non-Javadoc)
253      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getIndentPrefixes(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
254      */

255     public String JavaDoc[] getIndentPrefixes(ISourceViewer sourceViewer, String JavaDoc contentType) {
256         List JavaDoc list= new ArrayList JavaDoc();
257
258         // prefix[0] is either '\t' or ' ' x tabWidth, depending on useSpaces
259

260         int tabWidth= getTabWidth(sourceViewer);
261         boolean useSpaces= fEditor.isTabsToSpacesConversionEnabled();
262         
263         for (int i= 0; i <= tabWidth; i++) {
264             StringBuffer JavaDoc prefix= new StringBuffer JavaDoc();
265             if (useSpaces) {
266                 for (int j= 0; j + i < tabWidth; j++) {
267                     prefix.append(' ');
268                 }
269                 
270                 if (i != 0) {
271                     prefix.append('\t');
272                 }
273             } else {
274                 for (int j= 0; j < i; j++) {
275                     prefix.append(' ');
276                 }
277                 if (i != tabWidth) {
278                     prefix.append('\t');
279                 }
280             }
281             
282             list.add(prefix.toString());
283         }
284
285         list.add(""); //$NON-NLS-1$
286

287         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
288     }
289     
290     /*
291      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
292      */

293     public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String JavaDoc contentType) {
294         if (AntEditorPartitionScanner.XML_COMMENT.equals(contentType)) {
295             return super.getAutoEditStrategies(sourceViewer, contentType);
296         }
297         if (fAutoEditorStategies == null) {
298             fAutoEditorStategies= new AntAutoEditStrategy[] {new AntAutoEditStrategy(fEditor.getAntModel())};
299         }
300         return fAutoEditorStategies;
301     }
302     
303    /*
304     * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
305     */

306     protected Map JavaDoc getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
307         Map JavaDoc targets = super.getHyperlinkDetectorTargets(sourceViewer);
308         targets.put("org.eclipse.ant.ui.buildFiles", fEditor); //$NON-NLS-1$
309
return targets;
310     }
311 }
Popular Tags