Small code clean up
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / JavaSourceViewer.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11
12 package net.sourceforge.phpeclipse.phpeditor;
13
14 import java.util.ArrayList;
15
16 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
17 import net.sourceforge.phpdt.ui.PreferenceConstants;
18 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.preference.PreferenceConverter;
22 import org.eclipse.jface.text.Assert;
23 import org.eclipse.jface.text.ITextPresentationListener;
24 import org.eclipse.jface.text.information.IInformationPresenter;
25 import org.eclipse.jface.text.reconciler.IReconciler;
26 import org.eclipse.jface.text.source.IOverviewRuler;
27 import org.eclipse.jface.text.source.IVerticalRuler;
28 import org.eclipse.jface.text.source.SourceViewerConfiguration;
29 import org.eclipse.jface.text.source.projection.ProjectionViewer;
30 import org.eclipse.jface.util.IPropertyChangeListener;
31 import org.eclipse.jface.util.PropertyChangeEvent;
32 import org.eclipse.swt.custom.StyledText;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.graphics.RGB;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
39
40
41
42 public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener {
43     
44         /** 
45          * Text operation code for requesting the outline for the current input.
46          */
47         public static final int SHOW_OUTLINE= 51;
48
49         /**
50          * Text operation code for requesting the outline for the element at the current position.
51          */
52         public static final int OPEN_STRUCTURE= 52;
53
54         /**
55          * Text operation code for requesting the hierarchy for the current input.
56          */
57         public static final int SHOW_HIERARCHY= 53;
58
59         private IInformationPresenter fOutlinePresenter;
60         private IInformationPresenter fStructurePresenter;
61 //      private IInformationPresenter fHierarchyPresenter;
62
63         /**
64          * This viewer's foreground color.
65          * @since 3.0
66          */
67         private Color fForegroundColor;
68         /** 
69          * The viewer's background color.
70          * @since 3.0
71          */
72         private Color fBackgroundColor;
73         /**
74          * This viewer's selection foreground color.
75          * @since 3.0
76          */
77         private Color fSelectionForegroundColor;
78         /** 
79          * The viewer's selection background color.
80          * @since 3.0
81          */
82         private Color fSelectionBackgroundColor;
83         /**
84          * The preference store.
85          * 
86          * @since 3.0
87          */
88         private IPreferenceStore fPreferenceStore;
89         /**
90          * Is this source viewer configured?
91          * 
92          * @since 3.0
93          */
94         private boolean fIsConfigured;
95         /**
96          * The backspace manager of this viewer.
97          * 
98          * @since 3.0
99          */
100         private SmartBackspaceManager fBackspaceManager;
101         
102         public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles, IPreferenceStore store) {
103                 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
104                 setPreferenceStore(store);
105         }
106
107         /*
108          * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
109          * @since 3.0
110          */
111 //      public IFormattingContext createFormattingContext() {
112 //
113 //              IFormattingContext context= new CommentFormattingContext();
114 //              Map map= new Hashtable(JavaCore.getOptions());
115 //              
116 //              context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
117 //              context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map);
118 //              
119 //              return context;
120 //      }
121
122         /*
123          * @see ITextOperationTarget#doOperation(int)
124          */
125         public void doOperation(int operation) {
126                 if (getTextWidget() == null)
127                         return;
128
129                 switch (operation) {
130                         case SHOW_OUTLINE:
131                                 fOutlinePresenter.showInformation();
132                                 return;
133                         case OPEN_STRUCTURE:
134                                 fStructurePresenter.showInformation();
135                                 return;
136                         case SHOW_HIERARCHY:
137 //                              fHierarchyPresenter.showInformation();
138                                 return; 
139                     case FORMAT:
140                       Point point = getSelectedRange();
141                       if (point.y==0) {
142                         setSelectedRange(0, getDocument().getLength());
143                       }
144                       break;
145                 }
146                 
147                 super.doOperation(operation);
148         }
149
150         /*
151          * @see ITextOperationTarget#canDoOperation(int)
152          */
153         public boolean canDoOperation(int operation) {
154                 if (operation == SHOW_OUTLINE)
155                         return fOutlinePresenter != null;
156                 if (operation == OPEN_STRUCTURE)
157                         return fStructurePresenter != null;
158                 if (operation == SHOW_HIERARCHY)
159 //                      return fHierarchyPresenter != null;     
160                         return false;
161                         
162                 return super.canDoOperation(operation);
163         }
164
165         /*
166          * @see ISourceViewer#configure(SourceViewerConfiguration)
167          */
168         public void configure(SourceViewerConfiguration configuration) {
169                 super.configure(configuration);
170                 if (configuration instanceof PHPSourceViewerConfiguration) {
171                         fOutlinePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, false);
172                         fOutlinePresenter.install(this);
173                 }
174                 if (configuration instanceof PHPSourceViewerConfiguration) {
175                         fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
176                         fStructurePresenter.install(this);
177                 }
178                 if (configuration instanceof PHPSourceViewerConfiguration) {
179 //                      fHierarchyPresenter= ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this, true);
180 //                      fHierarchyPresenter.install(this);
181             
182                         if (fPreferenceStore != null) {
183                                 fPreferenceStore.addPropertyChangeListener(this);
184                                 initializeViewerColors();
185                         }
186                 }
187                 fIsConfigured= true;
188         }
189         
190     
191         protected void initializeViewerColors() {
192                 if (fPreferenceStore != null) {
193                         
194                         StyledText styledText= getTextWidget();
195                         
196                         // ----------- foreground color --------------------
197                         Color color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR)
198                         ? null
199                         : createColor(fPreferenceStore, PreferenceConstants.EDITOR_FOREGROUND_COLOR, styledText.getDisplay());
200                         styledText.setForeground(color);
201                         
202                         if (fForegroundColor != null)
203                                 fForegroundColor.dispose();
204                         
205                         fForegroundColor= color;
206                         
207                         // ---------- background color ----------------------
208                         color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR)
209                         ? null
210                         : createColor(fPreferenceStore, PreferenceConstants.EDITOR_BACKGROUND_COLOR, styledText.getDisplay());
211                         styledText.setBackground(color);
212                         
213                         if (fBackgroundColor != null)
214                                 fBackgroundColor.dispose();
215                         
216                         fBackgroundColor= color;
217                         
218                         // ----------- selection foreground color --------------------
219                         color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
220                                 ? null
221                                 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
222                         styledText.setSelectionForeground(color);
223                                 
224                         if (fSelectionForegroundColor != null)
225                                 fSelectionForegroundColor.dispose();
226                         
227                         fSelectionForegroundColor= color;
228                         
229                         // ---------- selection background color ----------------------
230                         color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
231                                 ? null
232                                 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
233                         styledText.setSelectionBackground(color);
234                                 
235                         if (fSelectionBackgroundColor != null)
236                                 fSelectionBackgroundColor.dispose();
237                                 
238                         fSelectionBackgroundColor= color;
239                 }
240     }
241
242     /**
243      * Creates a color from the information stored in the given preference store.
244      * Returns <code>null</code> if there is no such information available.
245      * 
246      * @param store the store to read from
247      * @param key the key used for the lookup in the preference store
248      * @param display the display used create the color
249      * @return the created color according to the specification in the preference store
250      * @since 3.0
251      */
252     private Color createColor(IPreferenceStore store, String key, Display display) {
253     
254         RGB rgb= null;      
255         
256         if (store.contains(key)) {
257             
258             if (store.isDefault(key))
259                 rgb= PreferenceConverter.getDefaultColor(store, key);
260             else
261                 rgb= PreferenceConverter.getColor(store, key);
262         
263             if (rgb != null)
264                 return new Color(display, rgb);
265         }
266         
267         return null;
268     }
269
270         /*
271          * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
272          * @since 3.0
273          */
274         public void unconfigure() {
275                 if (fOutlinePresenter != null) {
276                         fOutlinePresenter.uninstall();  
277                         fOutlinePresenter= null;
278                 }
279                 if (fStructurePresenter != null) {
280                         fStructurePresenter.uninstall();
281                         fStructurePresenter= null;
282                 }
283 //              if (fHierarchyPresenter != null) {
284 //                      fHierarchyPresenter.uninstall();
285 //                      fHierarchyPresenter= null;
286 //              }
287                 if (fForegroundColor != null) {
288                         fForegroundColor.dispose();
289                         fForegroundColor= null;
290                 }
291                 if (fBackgroundColor != null) {
292                         fBackgroundColor.dispose();
293                         fBackgroundColor= null;
294                 }
295                 if (fPreferenceStore != null)
296                         fPreferenceStore.removePropertyChangeListener(this);
297                 
298                 super.unconfigure();
299                 
300                 fIsConfigured= false;
301         }
302         
303         /*
304          * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
305          */
306         public Point rememberSelection() {
307                 return super.rememberSelection();
308         }
309         
310         /*
311          * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
312          */
313         public void restoreSelection() {
314                 super.restoreSelection();
315         }
316
317         /*
318          * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
319          */
320         public void propertyChange(PropertyChangeEvent event) {
321                 String property = event.getProperty();
322                 if (PreferenceConstants.EDITOR_FOREGROUND_COLOR.equals(property)
323                                 || PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR.equals(property)
324                                 || PreferenceConstants.EDITOR_BACKGROUND_COLOR.equals(property)
325                                 || PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR.equals(property)
326                                 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR.equals(property)
327                                 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR.equals(property)
328                                 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR.equals(property)
329                                 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR.equals(property))
330                 {
331                         initializeViewerColors();
332                 }               
333         }
334
335         /**
336          * Sets the preference store on this viewer.
337          * 
338          * @param store the preference store
339          * 
340          * @since 3.0
341          */
342         public void setPreferenceStore(IPreferenceStore store) {
343                 if (fIsConfigured && fPreferenceStore != null)
344                         fPreferenceStore.removePropertyChangeListener(this);
345                 
346                 fPreferenceStore= store;
347
348                 if (fIsConfigured && fPreferenceStore != null) {
349                         fPreferenceStore.addPropertyChangeListener(this);
350                         initializeViewerColors();
351                 }
352         }
353         
354         /*
355          * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
356          */
357         protected void createControl(Composite parent, int styles) {
358                 super.createControl(parent, styles);
359
360                 fBackspaceManager= new SmartBackspaceManager();
361                 fBackspaceManager.install(this);
362         }
363         
364         /**
365          * Returns the backspace manager for this viewer.
366          * 
367          * @return the backspace manager for this viewer, or <code>null</code> if
368          *         there is none
369          * @since 3.0
370          */
371         public SmartBackspaceManager getBackspaceManager() {
372                 return fBackspaceManager;
373         }
374         
375         /*
376          * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
377          */
378         protected void handleDispose() {
379                 if (fBackspaceManager != null) {
380                         fBackspaceManager.uninstall();
381                         fBackspaceManager= null;
382                 }
383
384                 super.handleDispose();
385         }
386         
387         /**
388          * Prepends the text presentation listener at the beginning of the viewer's 
389          * list of text presentation listeners.  If the listener is already registered 
390          * with the viewer this call moves the listener to the beginning of
391          * the list.
392          *
393          * @param listener the text presentation listener
394          * @since 3.0
395          */
396         public void prependTextPresentationListener(ITextPresentationListener listener) {
397                 
398                 Assert.isNotNull(listener);
399
400                 if (fTextPresentationListeners == null)
401                         fTextPresentationListeners= new ArrayList();
402                 
403                 fTextPresentationListeners.remove(listener);
404                 fTextPresentationListeners.add(0, listener);
405         }
406         /**
407          * Sets the given reconciler.
408          *  
409          * @param reconciler the reconciler
410          * @since 3.0
411          */
412         void setReconciler(IReconciler reconciler) {
413                 fReconciler= reconciler;
414         }
415
416         /**
417          * Returns the reconciler.
418          * 
419          * @return the reconciler or <code>null</code> if not set
420          * @since 3.0
421          */
422         Object getReconciler() {
423                 return fReconciler;
424         }
425 }