]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/internal/ui/propertiesfileeditor/PropertiesFileSourceViewerConfiguration.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / internal / ui / propertiesfileeditor / PropertiesFileSourceViewerConfiguration.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 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  *     Brock Janiczak <brockj@tpg.com.au> - [nls tooling] Properties file editor should have "toggle comment" action - https://bugs.eclipse.org/bugs/show_bug.cgi?id=192045
11  *******************************************************************************/
12 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
13
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Map;
17
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.widgets.Shell;
22
23 import org.eclipse.core.runtime.Assert;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IAdaptable;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.content.IContentType;
28
29 import org.eclipse.jface.preference.IPreferenceStore;
30 import org.eclipse.jface.resource.JFaceResources;
31 import org.eclipse.jface.util.IPropertyChangeListener;
32 import org.eclipse.jface.util.PropertyChangeEvent;
33
34 import org.eclipse.jface.text.DefaultInformationControl;
35 import org.eclipse.jface.text.IAutoEditStrategy;
36 import org.eclipse.jface.text.IDocument;
37 import org.eclipse.jface.text.IInformationControl;
38 import org.eclipse.jface.text.IInformationControlCreator;
39 import org.eclipse.jface.text.ITextDoubleClickStrategy;
40 import org.eclipse.jface.text.ITextHover;
41 import org.eclipse.jface.text.presentation.IPresentationReconciler;
42 import org.eclipse.jface.text.presentation.PresentationReconciler;
43 import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
44 import org.eclipse.jface.text.reconciler.IReconciler;
45 import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
46 import org.eclipse.jface.text.reconciler.MonoReconciler;
47 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
48 import org.eclipse.jface.text.rules.RuleBasedScanner;
49 import org.eclipse.jface.text.source.Annotation;
50 import org.eclipse.jface.text.source.IAnnotationHover;
51 import org.eclipse.jface.text.source.ISourceViewer;
52 import org.eclipse.jface.text.source.SourceViewer;
53
54 import org.eclipse.ui.IFileEditorInput;
55
56 import org.eclipse.ui.texteditor.ITextEditor;
57 import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy;
58 import org.eclipse.ui.texteditor.spelling.SpellingService;
59
60 import org.eclipse.ui.editors.text.EditorsUI;
61 import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
62
63 import org.eclipse.jdt.ui.PreferenceConstants;
64 import org.eclipse.jdt.ui.text.IColorManager;
65
66 import org.eclipse.jdt.internal.ui.JavaPlugin;
67 import org.eclipse.jdt.internal.ui.preferences.PropertiesFileEditorPreferencePage;
68 import org.eclipse.jdt.internal.ui.preferences.PropertiesFileEditorPreferencePage.SourcePreviewerUpdater;
69 import org.eclipse.jdt.internal.ui.text.AbstractJavaScanner;
70 import org.eclipse.jdt.internal.ui.text.HTMLAnnotationHover;
71 import org.eclipse.jdt.internal.ui.text.JavaPresentationReconciler;
72 import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner;
73 import org.eclipse.jdt.internal.ui.text.java.PartitionDoubleClickSelector;
74
75
76 /**
77  * Configuration for a source viewer which shows a properties file.
78  * <p>
79  * This class may be instantiated; it is not intended to be subclassed.
80  * </p>
81  *
82  * @since 3.1
83  */
84 public class PropertiesFileSourceViewerConfiguration extends TextSourceViewerConfiguration {
85
86         /** Properties file content type */
87         private static final IContentType PROPERTIES_CONTENT_TYPE= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); //$NON-NLS-1$
88
89         /**
90          * The text editor.
91          */
92         private ITextEditor fTextEditor;
93         /**
94          * The document partitioning.
95          */
96         private String fDocumentPartitioning;
97         /**
98          * The property key scanner.
99          */
100         private AbstractJavaScanner fPropertyKeyScanner;
101         /**
102          * The comment scanner.
103          */
104         public AbstractJavaScanner fCommentScanner;
105         /**
106          * The property value scanner.
107          */
108         private AbstractJavaScanner fPropertyValueScanner;
109         /**
110          * The color manager.
111          */
112         private IColorManager fColorManager;
113
114
115         /**
116          * Creates a new properties file source viewer configuration for viewers in the given editor
117          * using the given preference store, the color manager and the specified document partitioning.
118          *
119          * @param colorManager the color manager
120          * @param preferenceStore the preference store, can be read-only
121          * @param editor the editor in which the configured viewer(s) will reside
122          * @param partitioning the document partitioning for this configuration
123          */
124         public PropertiesFileSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String partitioning) {
125                 super(preferenceStore);
126                 fColorManager= colorManager;
127                 fTextEditor= editor;
128                 fDocumentPartitioning= partitioning;
129                 initializeScanners();
130         }
131
132         /**
133          * Returns the property key scanner for this configuration.
134          *
135          * @return the property key scanner
136          */
137         protected RuleBasedScanner getPropertyKeyScanner() {
138                 return fPropertyKeyScanner;
139         }
140
141         /**
142          * Returns the comment scanner for this configuration.
143          *
144          * @return the comment scanner
145          */
146         protected RuleBasedScanner getCommentScanner() {
147                 return fCommentScanner;
148         }
149
150         /**
151          * Returns the property value scanner for this configuration.
152          *
153          * @return the property value scanner
154          */
155         protected RuleBasedScanner getPropertyValueScanner() {
156                 return fPropertyValueScanner;
157         }
158
159         /**
160          * Returns the color manager for this configuration.
161          *
162          * @return the color manager
163          */
164         protected IColorManager getColorManager() {
165                 return fColorManager;
166         }
167
168         /**
169          * Returns the editor in which the configured viewer(s) will reside.
170          *
171          * @return the enclosing editor
172          */
173         protected ITextEditor getEditor() {
174                 return fTextEditor;
175         }
176
177         /**
178          * Initializes the scanners.
179          */
180         private void initializeScanners() {
181                 fPropertyKeyScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_KEY);
182                 fPropertyValueScanner= new PropertyValueScanner(getColorManager(), fPreferenceStore);
183                 fCommentScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
184         }
185
186         /*
187          * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
188          */
189         @Override
190         public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
191
192                 PresentationReconciler reconciler= new JavaPresentationReconciler();
193                 reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
194
195                 DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getPropertyKeyScanner());
196                 reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
197                 reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
198
199                 dr= new DefaultDamagerRepairer(getCommentScanner());
200                 reconciler.setDamager(dr, IPropertiesFilePartitions.COMMENT);
201                 reconciler.setRepairer(dr, IPropertiesFilePartitions.COMMENT);
202
203                 dr= new DefaultDamagerRepairer(getPropertyValueScanner());
204                 reconciler.setDamager(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
205                 reconciler.setRepairer(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
206
207                 return reconciler;
208         }
209
210         /*
211          * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
212          */
213         @Override
214         public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
215                 if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType))
216                         return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0, 0);
217                 if (IPropertiesFilePartitions.COMMENT.equals(contentType))
218                         return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0);
219                 if (IPropertiesFilePartitions.PROPERTY_VALUE.equals(contentType))
220                         return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 1, -1);
221
222                 return super.getDoubleClickStrategy(sourceViewer, contentType);
223         }
224
225         /*
226          * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
227          */
228         @Override
229         public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
230                 int length= IPropertiesFilePartitions.PARTITIONS.length;
231                 String[] contentTypes= new String[length + 1];
232                 contentTypes[0]= IDocument.DEFAULT_CONTENT_TYPE;
233                 for (int i= 0; i < length; i++)
234                         contentTypes[i+1]= IPropertiesFilePartitions.PARTITIONS[i];
235
236                 return contentTypes;
237         }
238
239         /*
240          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
241          */
242         @Override
243         public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
244                 if (fDocumentPartitioning != null)
245                         return fDocumentPartitioning;
246                 return super.getConfiguredDocumentPartitioning(sourceViewer);
247         }
248
249         /**
250          * Determines whether the preference change encoded by the given event
251          * changes the behavior of one of its contained components.
252          *
253          * @param event the event to be investigated
254          * @return <code>true</code> if event causes a behavioral change
255          */
256         public boolean affectsTextPresentation(PropertyChangeEvent event) {
257                 return  fPropertyKeyScanner.affectsBehavior(event)
258                         || fCommentScanner.affectsBehavior(event)
259                         || fPropertyValueScanner.affectsBehavior(event);
260         }
261
262         /**
263          * Adapts the behavior of the contained components to the change
264          * encoded in the given event.
265          *
266          * @param event the event to which to adapt
267          * @see PropertiesFileSourceViewerConfiguration#PropertiesFileSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
268          */
269         public void handlePropertyChangeEvent(PropertyChangeEvent event) {
270                 if (fPropertyKeyScanner.affectsBehavior(event))
271                         fPropertyKeyScanner.adaptToPreferenceChange(event);
272                 fCommentScanner.generated_6105037195808839610(event);
273                 if (fPropertyValueScanner.affectsBehavior(event))
274                         fPropertyValueScanner.adaptToPreferenceChange(event);
275         }
276
277         /*
278          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
279          * @since 3.3
280          */
281         @Override
282         protected Map<String, IAdaptable> getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
283                 Map<String, IAdaptable> targets= super.getHyperlinkDetectorTargets(sourceViewer);
284                 targets.put("org.eclipse.jdt.ui.PropertiesFileEditor", fTextEditor); //$NON-NLS-1$
285                 return targets;
286         }
287
288         /*
289          * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
290          */
291         @Override
292         public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
293                 return new HTMLAnnotationHover(false) {
294                         @Override
295                         protected boolean isIncluded(Annotation annotation) {
296                                 return isShowInVerticalRuler(annotation);
297                         }
298                 };
299         }
300
301         /*
302          * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
303          */
304         @Override
305         public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
306                 return new HTMLAnnotationHover(true) {
307                         @Override
308                         protected boolean isIncluded(Annotation annotation) {
309                                 return isShowInOverviewRuler(annotation);
310                         }
311                 };
312         }
313
314         /*
315          * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
316          */
317         @Override
318         public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
319                 return new IInformationControlCreator() {
320                         public IInformationControl createInformationControl(Shell parent) {
321                                 return new DefaultInformationControl(parent, JavaPlugin.getAdditionalInfoAffordanceString());
322                         }
323                 };
324         }
325
326         /*
327          * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
328          */
329         @Override
330         public IReconciler getReconciler(ISourceViewer sourceViewer) {
331                 if (!EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
332                         return null;
333
334                 IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, EditorsUI.getSpellingService()) {
335                         @Override
336                         protected IContentType getContentType() {
337                                 return PROPERTIES_CONTENT_TYPE;
338                         }
339                 };
340
341                 MonoReconciler reconciler= new MonoReconciler(strategy, false);
342                 reconciler.setDelay(500);
343                 return reconciler;
344         }
345
346         /*
347          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getDefaultPrefixes(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
348          * @since 3.4
349          */
350         @Override
351         public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
352                 return new String[] {"#", ""}; //$NON-NLS-1$ //$NON-NLS-2$
353         }
354
355         /*
356          * (non-Javadoc)
357          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
358          * @since 3.7
359          */
360         @Override
361         public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
362                 IAutoEditStrategy[] autoEditStrategies= super.getAutoEditStrategies(sourceViewer, contentType);
363
364                 if (fTextEditor == null)
365                         return autoEditStrategies;
366
367                 try {
368                         if (!PropertiesFileDocumentProvider.isJavaPropertiesFile(fTextEditor.getEditorInput())) {
369                                 return autoEditStrategies;
370                         }
371                         List<IAutoEditStrategy> stratergies= new ArrayList<IAutoEditStrategy>();
372                         for (int i= 0; i < autoEditStrategies.length; i++) {
373                                 stratergies.add(autoEditStrategies[i]);
374                         }
375                         stratergies.add(new PropertiesFileAutoEditStrategy(((IFileEditorInput)fTextEditor.getEditorInput()).getFile(), sourceViewer));
376                         return stratergies.toArray(new IAutoEditStrategy[stratergies.size()]);
377                 } catch (CoreException e) {
378                         JavaPlugin.log(e);
379                         return autoEditStrategies;
380                 }
381         }
382
383         /*
384          * (non-Javadoc)
385          * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String, int)
386          * @since 3.7
387          */
388         @Override
389         public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
390                 return new PropertiesFileHover(super.getTextHover(sourceViewer, contentType));
391         }
392
393         /*
394          * (non-Javadoc)
395          * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getQuickAssistAssistant(org.eclipse.jface.text.source.ISourceViewer)
396          * @since 3.7
397          */
398         @Override
399         public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
400                 if (getEditor() != null) {
401                         PropertiesCorrectionAssistant assistant= new PropertiesCorrectionAssistant(getEditor());
402                         return assistant.generated_1914814755012034954();
403                 }
404                 return null;
405         }
406
407         public String generated_3335448662263464516(IPreferenceStore store, PropertiesFileEditorPreferencePage propertiesfileeditorpreferencepage) {
408                 propertiesfileeditorpreferencepage.fPreviewViewer.configure(this);
409                 Font font= JFaceResources.getFont(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT);
410                 propertiesfileeditorpreferencepage.fPreviewViewer.getTextWidget().setFont(font);
411                 new SourcePreviewerUpdater(propertiesfileeditorpreferencepage.fPreviewViewer, this, store);
412                 propertiesfileeditorpreferencepage.fPreviewViewer.setEditable(false);
413         
414                 String content= propertiesfileeditorpreferencepage.loadPreviewContentFromFile("PropertiesFileEditorColorSettingPreviewCode.txt");
415                 return content;
416         }
417
418         public void generated_5681379739341139520(final SourceViewer viewer, final IPreferenceStore preferenceStore) {
419                 Assert.isNotNull(this);
420                 Assert.isNotNull(preferenceStore);
421                 final IPropertyChangeListener fontChangeListener= new IPropertyChangeListener() {
422                         /*
423                          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
424                          */
425                         public void propertyChange(PropertyChangeEvent event) {
426                                 if (event.getProperty().equals(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT)) {
427                                         Font font= JFaceResources.getFont(PreferenceConstants.PROPERTIES_FILE_EDITOR_TEXT_FONT);
428                                         viewer.getTextWidget().setFont(font);
429                                 }
430                         }
431                 };
432                 final IPropertyChangeListener propertyChangeListener= new IPropertyChangeListener() {
433                         /*
434                          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
435                          */
436                         public void propertyChange(PropertyChangeEvent event) {
437                                 if (affectsTextPresentation(event)) {
438                                         handlePropertyChangeEvent(event);
439                                         viewer.invalidateTextPresentation();
440                                 }
441                         }
442                 };
443                 viewer.getTextWidget().addDisposeListener(new DisposeListener() {
444                         /*
445                          * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
446                          */
447                         public void widgetDisposed(DisposeEvent e) {
448                                 preferenceStore.removePropertyChangeListener(propertyChangeListener);
449                                 JFaceResources.getFontRegistry().removeListener(fontChangeListener);
450                         }
451                 });
452                 JFaceResources.getFontRegistry().addListener(fontChangeListener);
453                 preferenceStore.addPropertyChangeListener(propertyChangeListener);
454         }
455 }