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