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;
18 import net.sourceforge.phpdt.ui.text.PHPSourceViewerConfiguration;
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;
42 public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener {
45 * Text operation code for requesting the outline for the current input.
47 public static final int SHOW_OUTLINE= 51;
50 * Text operation code for requesting the outline for the element at the current position.
52 public static final int OPEN_STRUCTURE= 52;
55 * Text operation code for requesting the hierarchy for the current input.
57 public static final int SHOW_HIERARCHY= 53;
59 private IInformationPresenter fOutlinePresenter;
60 private IInformationPresenter fStructurePresenter;
61 // private IInformationPresenter fHierarchyPresenter;
64 * This viewer's foreground color.
67 private Color fForegroundColor;
69 * The viewer's background color.
72 private Color fBackgroundColor;
74 * This viewer's selection foreground color.
77 private Color fSelectionForegroundColor;
79 * The viewer's selection background color.
82 private Color fSelectionBackgroundColor;
84 * The preference store.
88 private IPreferenceStore fPreferenceStore;
90 * Is this source viewer configured?
94 private boolean fIsConfigured;
96 * The backspace manager of this viewer.
100 private SmartBackspaceManager fBackspaceManager;
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);
108 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
111 // public IFormattingContext createFormattingContext() {
113 // IFormattingContext context= new CommentFormattingContext();
114 // Map map= new Hashtable(JavaCore.getOptions());
116 // context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
117 // context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map);
123 * @see ITextOperationTarget#doOperation(int)
125 public void doOperation(int operation) {
126 if (getTextWidget() == null)
131 fOutlinePresenter.showInformation();
134 fStructurePresenter.showInformation();
137 // fHierarchyPresenter.showInformation();
140 Point point = getSelectedRange();
142 setSelectedRange(0, getDocument().getLength());
147 super.doOperation(operation);
151 * @see ITextOperationTarget#canDoOperation(int)
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;
162 return super.canDoOperation(operation);
166 * @see ISourceViewer#configure(SourceViewerConfiguration)
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);
174 if (configuration instanceof PHPSourceViewerConfiguration) {
175 fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
176 fStructurePresenter.install(this);
178 if (configuration instanceof PHPSourceViewerConfiguration) {
179 // fHierarchyPresenter= ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this, true);
180 // fHierarchyPresenter.install(this);
182 if (fPreferenceStore != null) {
183 fPreferenceStore.addPropertyChangeListener(this);
184 initializeViewerColors();
191 protected void initializeViewerColors() {
192 if (fPreferenceStore != null) {
194 StyledText styledText= getTextWidget();
196 // ----------- foreground color --------------------
197 Color color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR)
199 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_FOREGROUND_COLOR, styledText.getDisplay());
200 styledText.setForeground(color);
202 if (fForegroundColor != null)
203 fForegroundColor.dispose();
205 fForegroundColor= color;
207 // ---------- background color ----------------------
208 color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR)
210 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_BACKGROUND_COLOR, styledText.getDisplay());
211 styledText.setBackground(color);
213 if (fBackgroundColor != null)
214 fBackgroundColor.dispose();
216 fBackgroundColor= color;
218 // ----------- selection foreground color --------------------
219 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
221 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
222 styledText.setSelectionForeground(color);
224 if (fSelectionForegroundColor != null)
225 fSelectionForegroundColor.dispose();
227 fSelectionForegroundColor= color;
229 // ---------- selection background color ----------------------
230 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
232 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
233 styledText.setSelectionBackground(color);
235 if (fSelectionBackgroundColor != null)
236 fSelectionBackgroundColor.dispose();
238 fSelectionBackgroundColor= color;
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.
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
252 private Color createColor(IPreferenceStore store, String key, Display display) {
256 if (store.contains(key)) {
258 if (store.isDefault(key))
259 rgb= PreferenceConverter.getDefaultColor(store, key);
261 rgb= PreferenceConverter.getColor(store, key);
264 return new Color(display, rgb);
271 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
274 public void unconfigure() {
275 if (fOutlinePresenter != null) {
276 fOutlinePresenter.uninstall();
277 fOutlinePresenter= null;
279 if (fStructurePresenter != null) {
280 fStructurePresenter.uninstall();
281 fStructurePresenter= null;
283 // if (fHierarchyPresenter != null) {
284 // fHierarchyPresenter.uninstall();
285 // fHierarchyPresenter= null;
287 if (fForegroundColor != null) {
288 fForegroundColor.dispose();
289 fForegroundColor= null;
291 if (fBackgroundColor != null) {
292 fBackgroundColor.dispose();
293 fBackgroundColor= null;
295 if (fPreferenceStore != null)
296 fPreferenceStore.removePropertyChangeListener(this);
300 fIsConfigured= false;
304 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
306 public Point rememberSelection() {
307 return super.rememberSelection();
311 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
313 public void restoreSelection() {
314 super.restoreSelection();
318 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
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))
331 initializeViewerColors();
336 * Sets the preference store on this viewer.
338 * @param store the preference store
342 public void setPreferenceStore(IPreferenceStore store) {
343 if (fIsConfigured && fPreferenceStore != null)
344 fPreferenceStore.removePropertyChangeListener(this);
346 fPreferenceStore= store;
348 if (fIsConfigured && fPreferenceStore != null) {
349 fPreferenceStore.addPropertyChangeListener(this);
350 initializeViewerColors();
355 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
357 protected void createControl(Composite parent, int styles) {
358 super.createControl(parent, styles);
360 fBackspaceManager= new SmartBackspaceManager();
361 fBackspaceManager.install(this);
365 * Returns the backspace manager for this viewer.
367 * @return the backspace manager for this viewer, or <code>null</code> if
371 public SmartBackspaceManager getBackspaceManager() {
372 return fBackspaceManager;
376 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
378 protected void handleDispose() {
379 if (fBackspaceManager != null) {
380 fBackspaceManager.uninstall();
381 fBackspaceManager= null;
384 super.handleDispose();
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
393 * @param listener the text presentation listener
396 public void prependTextPresentationListener(ITextPresentationListener listener) {
398 Assert.isNotNull(listener);
400 if (fTextPresentationListeners == null)
401 fTextPresentationListeners= new ArrayList();
403 fTextPresentationListeners.remove(listener);
404 fTextPresentationListeners.add(0, listener);
407 * Sets the given reconciler.
409 * @param reconciler the reconciler
412 void setReconciler(IReconciler reconciler) {
413 fReconciler= reconciler;
417 * Returns the reconciler.
419 * @return the reconciler or <code>null</code> if not set
422 Object getReconciler() {