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