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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
14 import java.util.ArrayList;
16 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
17 import net.sourceforge.phpdt.ui.PreferenceConstants;
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;
41 public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener {
44 * Text operation code for requesting the outline for the current input.
46 public static final int SHOW_OUTLINE= 51;
49 * Text operation code for requesting the outline for the element at the current position.
51 public static final int OPEN_STRUCTURE= 52;
54 * Text operation code for requesting the hierarchy for the current input.
56 public static final int SHOW_HIERARCHY= 53;
58 private IInformationPresenter fOutlinePresenter;
59 private IInformationPresenter fStructurePresenter;
60 // private IInformationPresenter fHierarchyPresenter;
63 * This viewer's foreground color.
66 private Color fForegroundColor;
68 * The viewer's background color.
71 private Color fBackgroundColor;
73 * This viewer's selection foreground color.
76 private Color fSelectionForegroundColor;
78 * The viewer's selection background color.
81 private Color fSelectionBackgroundColor;
83 * The preference store.
87 private IPreferenceStore fPreferenceStore;
89 * Is this source viewer configured?
93 private boolean fIsConfigured;
95 * The backspace manager of this viewer.
99 private SmartBackspaceManager fBackspaceManager;
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);
107 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
110 // public IFormattingContext createFormattingContext() {
112 // IFormattingContext context= new CommentFormattingContext();
113 // Map map= new Hashtable(JavaCore.getOptions());
115 // context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
116 // context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map);
122 * @see ITextOperationTarget#doOperation(int)
124 public void doOperation(int operation) {
125 if (getTextWidget() == null)
130 fOutlinePresenter.showInformation();
133 fStructurePresenter.showInformation();
136 // fHierarchyPresenter.showInformation();
139 Point point = getSelectedRange();
141 setSelectedRange(0, getDocument().getLength());
146 super.doOperation(operation);
150 * @see ITextOperationTarget#canDoOperation(int)
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;
161 return super.canDoOperation(operation);
165 * @see ISourceViewer#configure(SourceViewerConfiguration)
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);
173 if (configuration instanceof PHPSourceViewerConfiguration) {
174 fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
175 fStructurePresenter.install(this);
177 if (configuration instanceof PHPSourceViewerConfiguration) {
178 // fHierarchyPresenter= ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this, true);
179 // fHierarchyPresenter.install(this);
181 if (fPreferenceStore != null) {
182 fPreferenceStore.addPropertyChangeListener(this);
183 initializeViewerColors();
190 protected void initializeViewerColors() {
191 if (fPreferenceStore != null) {
193 StyledText styledText= getTextWidget();
195 // ----------- foreground color --------------------
196 Color color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR)
198 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_FOREGROUND_COLOR, styledText.getDisplay());
199 styledText.setForeground(color);
201 if (fForegroundColor != null)
202 fForegroundColor.dispose();
204 fForegroundColor= color;
206 // ---------- background color ----------------------
207 color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR)
209 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_BACKGROUND_COLOR, styledText.getDisplay());
210 styledText.setBackground(color);
212 if (fBackgroundColor != null)
213 fBackgroundColor.dispose();
215 fBackgroundColor= color;
217 // ----------- selection foreground color --------------------
218 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
220 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
221 styledText.setSelectionForeground(color);
223 if (fSelectionForegroundColor != null)
224 fSelectionForegroundColor.dispose();
226 fSelectionForegroundColor= color;
228 // ---------- selection background color ----------------------
229 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
231 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
232 styledText.setSelectionBackground(color);
234 if (fSelectionBackgroundColor != null)
235 fSelectionBackgroundColor.dispose();
237 fSelectionBackgroundColor= color;
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.
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
251 private Color createColor(IPreferenceStore store, String key, Display display) {
255 if (store.contains(key)) {
257 if (store.isDefault(key))
258 rgb= PreferenceConverter.getDefaultColor(store, key);
260 rgb= PreferenceConverter.getColor(store, key);
263 return new Color(display, rgb);
270 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
273 public void unconfigure() {
274 if (fOutlinePresenter != null) {
275 fOutlinePresenter.uninstall();
276 fOutlinePresenter= null;
278 if (fStructurePresenter != null) {
279 fStructurePresenter.uninstall();
280 fStructurePresenter= null;
282 // if (fHierarchyPresenter != null) {
283 // fHierarchyPresenter.uninstall();
284 // fHierarchyPresenter= null;
286 if (fForegroundColor != null) {
287 fForegroundColor.dispose();
288 fForegroundColor= null;
290 if (fBackgroundColor != null) {
291 fBackgroundColor.dispose();
292 fBackgroundColor= null;
294 if (fPreferenceStore != null)
295 fPreferenceStore.removePropertyChangeListener(this);
299 fIsConfigured= false;
303 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
305 public Point rememberSelection() {
306 return super.rememberSelection();
310 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
312 public void restoreSelection() {
313 super.restoreSelection();
317 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
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))
330 initializeViewerColors();
335 * Sets the preference store on this viewer.
337 * @param store the preference store
341 public void setPreferenceStore(IPreferenceStore store) {
342 if (fIsConfigured && fPreferenceStore != null)
343 fPreferenceStore.removePropertyChangeListener(this);
345 fPreferenceStore= store;
347 if (fIsConfigured && fPreferenceStore != null) {
348 fPreferenceStore.addPropertyChangeListener(this);
349 initializeViewerColors();
354 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
356 protected void createControl(Composite parent, int styles) {
357 super.createControl(parent, styles);
359 fBackspaceManager= new SmartBackspaceManager();
360 fBackspaceManager.install(this);
364 * Returns the backspace manager for this viewer.
366 * @return the backspace manager for this viewer, or <code>null</code> if
370 public SmartBackspaceManager getBackspaceManager() {
371 return fBackspaceManager;
375 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
377 protected void handleDispose() {
378 if (fBackspaceManager != null) {
379 fBackspaceManager.uninstall();
380 fBackspaceManager= null;
383 super.handleDispose();
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
392 * @param listener the text presentation listener
395 public void prependTextPresentationListener(ITextPresentationListener listener) {
397 Assert.isNotNull(listener);
399 if (fTextPresentationListeners == null)
400 fTextPresentationListeners= new ArrayList();
402 fTextPresentationListeners.remove(listener);
403 fTextPresentationListeners.add(0, listener);
406 * Sets the given reconciler.
408 * @param reconciler the reconciler
411 void setReconciler(IReconciler reconciler) {
412 fReconciler= reconciler;
416 * Returns the reconciler.
418 * @return the reconciler or <code>null</code> if not set
421 Object getReconciler() {