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