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;
40 public class JavaSourceViewer extends ProjectionViewer implements
41 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
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;
61 private IInformationPresenter fStructurePresenter;
63 // private IInformationPresenter fHierarchyPresenter;
66 * This viewer's foreground color.
70 private Color fForegroundColor;
73 * The viewer's background color.
77 private Color fBackgroundColor;
80 * This viewer's selection foreground color.
84 private Color fSelectionForegroundColor;
87 * The viewer's selection background color.
91 private Color fSelectionBackgroundColor;
94 * The preference store.
98 private IPreferenceStore fPreferenceStore;
101 * Is this source viewer configured?
105 private boolean fIsConfigured;
108 * The backspace manager of this viewer.
112 private SmartBackspaceManager fBackspaceManager;
114 public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler,
115 IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
116 int styles, IPreferenceStore store) {
117 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview,
119 setPreferenceStore(store);
123 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
126 // public IFormattingContext createFormattingContext() {
128 // IFormattingContext context= new CommentFormattingContext();
129 // Map map= new Hashtable(JavaCore.getOptions());
131 // context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
132 // context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES,
138 * @see ITextOperationTarget#doOperation(int)
140 public void doOperation(int operation) {
141 if (getTextWidget() == null)
146 fOutlinePresenter.showInformation();
149 fStructurePresenter.showInformation();
152 // fHierarchyPresenter.showInformation();
155 Point point = getSelectedRange();
157 // setSelectedRange(0, getDocument().getLength());
158 revealRange(0, getDocument().getLength());
163 super.doOperation(operation);
167 * @see ITextOperationTarget#canDoOperation(int)
169 public boolean canDoOperation(int operation) {
170 if (operation == SHOW_OUTLINE)
171 return fOutlinePresenter != null;
172 if (operation == OPEN_STRUCTURE)
173 return fStructurePresenter != null;
174 if (operation == SHOW_HIERARCHY)
175 // return fHierarchyPresenter != null;
178 return super.canDoOperation(operation);
182 * @see ISourceViewer#configure(SourceViewerConfiguration)
184 public void configure(SourceViewerConfiguration configuration) {
185 super.configure(configuration);
186 if (configuration instanceof PHPSourceViewerConfiguration) {
187 fOutlinePresenter = ((PHPSourceViewerConfiguration) configuration)
188 .getOutlinePresenter(this, false);
189 fOutlinePresenter.install(this);
191 if (configuration instanceof PHPSourceViewerConfiguration) {
192 fStructurePresenter = ((PHPSourceViewerConfiguration) configuration)
193 .getOutlinePresenter(this, true);
194 fStructurePresenter.install(this);
196 if (configuration instanceof PHPSourceViewerConfiguration) {
197 // fHierarchyPresenter=
198 // ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this,
200 // fHierarchyPresenter.install(this);
202 if (fPreferenceStore != null) {
203 fPreferenceStore.addPropertyChangeListener(this);
204 initializeViewerColors();
207 fIsConfigured = true;
210 protected void initializeViewerColors() {
211 if (fPreferenceStore != null) {
213 StyledText styledText = getTextWidget();
215 // ----------- foreground color --------------------
216 Color color = fPreferenceStore
217 .getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR) ? null
218 : createColor(fPreferenceStore,
219 PreferenceConstants.EDITOR_FOREGROUND_COLOR,
220 styledText.getDisplay());
221 styledText.setForeground(color);
223 if (fForegroundColor != null)
224 fForegroundColor.dispose();
226 fForegroundColor = color;
228 // ---------- background color ----------------------
229 color = fPreferenceStore
230 .getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR) ? null
231 : createColor(fPreferenceStore,
232 PreferenceConstants.EDITOR_BACKGROUND_COLOR,
233 styledText.getDisplay());
234 styledText.setBackground(color);
236 if (fBackgroundColor != null)
237 fBackgroundColor.dispose();
239 fBackgroundColor = color;
241 // ----------- selection foreground color --------------------
242 color = fPreferenceStore
243 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR) ? null
246 AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR,
247 styledText.getDisplay());
248 styledText.setSelectionForeground(color);
250 if (fSelectionForegroundColor != null)
251 fSelectionForegroundColor.dispose();
253 fSelectionForegroundColor = color;
255 // ---------- selection background color ----------------------
256 color = fPreferenceStore
257 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR) ? null
260 AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR,
261 styledText.getDisplay());
262 styledText.setSelectionBackground(color);
264 if (fSelectionBackgroundColor != null)
265 fSelectionBackgroundColor.dispose();
267 fSelectionBackgroundColor = color;
272 * Creates a color from the information stored in the given preference
273 * store. Returns <code>null</code> if there is no such information
277 * the store to read from
279 * the key used for the lookup in the preference store
281 * the display used create the color
282 * @return the created color according to the specification in the
286 private Color createColor(IPreferenceStore store, String key,
291 if (store.contains(key)) {
293 if (store.isDefault(key))
294 rgb = PreferenceConverter.getDefaultColor(store, key);
296 rgb = PreferenceConverter.getColor(store, key);
299 return new Color(display, rgb);
306 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
309 public void unconfigure() {
310 if (fOutlinePresenter != null) {
311 fOutlinePresenter.uninstall();
312 fOutlinePresenter = null;
314 if (fStructurePresenter != null) {
315 fStructurePresenter.uninstall();
316 fStructurePresenter = null;
318 // if (fHierarchyPresenter != null) {
319 // fHierarchyPresenter.uninstall();
320 // fHierarchyPresenter= null;
322 if (fForegroundColor != null) {
323 fForegroundColor.dispose();
324 fForegroundColor = null;
326 if (fBackgroundColor != null) {
327 fBackgroundColor.dispose();
328 fBackgroundColor = null;
330 if (fPreferenceStore != null)
331 fPreferenceStore.removePropertyChangeListener(this);
335 fIsConfigured = false;
339 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
341 public Point rememberSelection() {
342 return super.rememberSelection();
346 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
348 public void restoreSelection() {
349 super.restoreSelection();
353 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
355 public void propertyChange(PropertyChangeEvent event) {
356 String property = event.getProperty();
357 if (PreferenceConstants.EDITOR_FOREGROUND_COLOR.equals(property)
358 || PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR
360 || PreferenceConstants.EDITOR_BACKGROUND_COLOR.equals(property)
361 || PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR
363 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR
365 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR
367 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR
369 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR
371 initializeViewerColors();
376 * Sets the preference store on this viewer.
379 * the preference store
383 public void setPreferenceStore(IPreferenceStore store) {
384 if (fIsConfigured && fPreferenceStore != null)
385 fPreferenceStore.removePropertyChangeListener(this);
387 fPreferenceStore = store;
389 if (fIsConfigured && fPreferenceStore != null) {
390 fPreferenceStore.addPropertyChangeListener(this);
391 initializeViewerColors();
396 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite,
399 protected void createControl(Composite parent, int styles) {
400 super.createControl(parent, styles);
402 fBackspaceManager = new SmartBackspaceManager();
403 fBackspaceManager.install(this);
407 * Returns the backspace manager for this viewer.
409 * @return the backspace manager for this viewer, or <code>null</code> if
413 public SmartBackspaceManager getBackspaceManager() {
414 return fBackspaceManager;
418 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
420 protected void handleDispose() {
421 if (fBackspaceManager != null) {
422 fBackspaceManager.uninstall();
423 fBackspaceManager = null;
426 super.handleDispose();
430 * Prepends the text presentation listener at the beginning of the viewer's
431 * list of text presentation listeners. If the listener is already
432 * registered with the viewer this call moves the listener to the beginning
436 * the text presentation listener
439 public void prependTextPresentationListener(
440 ITextPresentationListener listener) {
442 Assert.isNotNull(listener);
444 if (fTextPresentationListeners == null)
445 fTextPresentationListeners = new ArrayList();
447 fTextPresentationListeners.remove(listener);
448 fTextPresentationListeners.add(0, listener);
452 * Sets the given reconciler.
458 void setReconciler(IReconciler reconciler) {
459 fReconciler = reconciler;
463 * Returns the reconciler.
465 * @return the reconciler or <code>null</code> if not set
468 Object getReconciler() {