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;
23 //import org.eclipse.jface.text.Assert;
24 import org.eclipse.core.runtime.Assert;
25 import org.eclipse.jface.text.ITextPresentationListener;
26 import org.eclipse.jface.text.information.IInformationPresenter;
27 import org.eclipse.jface.text.reconciler.IReconciler;
28 import org.eclipse.jface.text.source.IOverviewRuler;
29 import org.eclipse.jface.text.source.IVerticalRuler;
30 import org.eclipse.jface.text.source.SourceViewerConfiguration;
31 import org.eclipse.jface.text.source.projection.ProjectionViewer;
32 import org.eclipse.jface.util.IPropertyChangeListener;
33 import org.eclipse.jface.util.PropertyChangeEvent;
34 import org.eclipse.swt.custom.StyledText;
35 import org.eclipse.swt.graphics.Color;
36 import org.eclipse.swt.graphics.Point;
37 import org.eclipse.swt.graphics.RGB;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Display;
40 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
42 public class JavaSourceViewer extends ProjectionViewer implements
43 IPropertyChangeListener {
46 * Text operation code for requesting the outline for the current input.
48 public static final int SHOW_OUTLINE = 51;
51 * Text operation code for requesting the outline for the element at the
54 public static final int OPEN_STRUCTURE = 52;
57 * Text operation code for requesting the hierarchy for the current input.
59 public static final int SHOW_HIERARCHY = 53;
61 private IInformationPresenter fOutlinePresenter;
63 private IInformationPresenter fStructurePresenter;
65 // private IInformationPresenter fHierarchyPresenter;
68 * This viewer's foreground color.
72 private Color fForegroundColor;
75 * The viewer's background color.
79 private Color fBackgroundColor;
82 * This viewer's selection foreground color.
86 private Color fSelectionForegroundColor;
89 * The viewer's selection background color.
93 private Color fSelectionBackgroundColor;
96 * The preference store.
100 private IPreferenceStore fPreferenceStore;
103 * Is this source viewer configured?
107 private boolean fIsConfigured;
110 * The backspace manager of this viewer.
114 private SmartBackspaceManager fBackspaceManager;
116 public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler,
117 IOverviewRuler overviewRuler, boolean showAnnotationsOverview,
118 int styles, IPreferenceStore store) {
119 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview,
121 setPreferenceStore(store);
125 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
128 // public IFormattingContext createFormattingContext() {
130 // IFormattingContext context= new CommentFormattingContext();
131 // Map map= new Hashtable(JavaCore.getOptions());
133 // context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
134 // context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES,
140 * @see ITextOperationTarget#doOperation(int)
142 public void doOperation(int operation) {
143 if (getTextWidget() == null)
148 fOutlinePresenter.showInformation();
151 fStructurePresenter.showInformation();
154 // fHierarchyPresenter.showInformation();
157 Point point = getSelectedRange();
159 // setSelectedRange(0, getDocument().getLength());
160 revealRange(0, getDocument().getLength());
165 super.doOperation(operation);
169 * @see ITextOperationTarget#canDoOperation(int)
171 public boolean canDoOperation(int operation) {
172 if (operation == SHOW_OUTLINE)
173 return fOutlinePresenter != null;
174 if (operation == OPEN_STRUCTURE)
175 return fStructurePresenter != null;
176 if (operation == SHOW_HIERARCHY)
177 // return fHierarchyPresenter != null;
180 return super.canDoOperation(operation);
184 * @see ISourceViewer#configure(SourceViewerConfiguration)
186 public void configure(SourceViewerConfiguration configuration) {
187 super.configure(configuration);
188 if (configuration instanceof PHPSourceViewerConfiguration) {
189 fOutlinePresenter = ((PHPSourceViewerConfiguration) configuration)
190 .getOutlinePresenter(this, false);
191 fOutlinePresenter.install(this);
193 if (configuration instanceof PHPSourceViewerConfiguration) {
194 fStructurePresenter = ((PHPSourceViewerConfiguration) configuration)
195 .getOutlinePresenter(this, true);
196 fStructurePresenter.install(this);
198 if (configuration instanceof PHPSourceViewerConfiguration) {
199 // fHierarchyPresenter=
200 // ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this,
202 // fHierarchyPresenter.install(this);
204 if (fPreferenceStore != null) {
205 fPreferenceStore.addPropertyChangeListener(this);
206 initializeViewerColors();
209 fIsConfigured = true;
212 protected void initializeViewerColors() {
213 if (fPreferenceStore != null) {
215 StyledText styledText = getTextWidget();
217 // ----------- foreground color --------------------
218 Color color = fPreferenceStore
219 .getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR) ? null
220 : createColor(fPreferenceStore,
221 PreferenceConstants.EDITOR_FOREGROUND_COLOR,
222 styledText.getDisplay());
223 styledText.setForeground(color);
225 if (fForegroundColor != null)
226 fForegroundColor.dispose();
228 fForegroundColor = color;
230 // ---------- background color ----------------------
231 color = fPreferenceStore
232 .getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR) ? null
233 : createColor(fPreferenceStore,
234 PreferenceConstants.EDITOR_BACKGROUND_COLOR,
235 styledText.getDisplay());
236 styledText.setBackground(color);
238 if (fBackgroundColor != null)
239 fBackgroundColor.dispose();
241 fBackgroundColor = color;
243 // ----------- selection foreground color --------------------
244 color = fPreferenceStore
245 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR) ? null
248 AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR,
249 styledText.getDisplay());
250 styledText.setSelectionForeground(color);
252 if (fSelectionForegroundColor != null)
253 fSelectionForegroundColor.dispose();
255 fSelectionForegroundColor = color;
257 // ---------- selection background color ----------------------
258 color = fPreferenceStore
259 .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR) ? null
262 AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR,
263 styledText.getDisplay());
264 styledText.setSelectionBackground(color);
266 if (fSelectionBackgroundColor != null)
267 fSelectionBackgroundColor.dispose();
269 fSelectionBackgroundColor = color;
274 * Creates a color from the information stored in the given preference
275 * store. Returns <code>null</code> if there is no such information
279 * the store to read from
281 * the key used for the lookup in the preference store
283 * the display used create the color
284 * @return the created color according to the specification in the
288 private Color createColor(IPreferenceStore store, String key,
293 if (store.contains(key)) {
295 if (store.isDefault(key))
296 rgb = PreferenceConverter.getDefaultColor(store, key);
298 rgb = PreferenceConverter.getColor(store, key);
301 return new Color(display, rgb);
308 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
311 public void unconfigure() {
312 if (fOutlinePresenter != null) {
313 fOutlinePresenter.uninstall();
314 fOutlinePresenter = null;
316 if (fStructurePresenter != null) {
317 fStructurePresenter.uninstall();
318 fStructurePresenter = null;
320 // if (fHierarchyPresenter != null) {
321 // fHierarchyPresenter.uninstall();
322 // fHierarchyPresenter= null;
324 if (fForegroundColor != null) {
325 fForegroundColor.dispose();
326 fForegroundColor = null;
328 if (fBackgroundColor != null) {
329 fBackgroundColor.dispose();
330 fBackgroundColor = null;
332 if (fPreferenceStore != null)
333 fPreferenceStore.removePropertyChangeListener(this);
337 fIsConfigured = false;
341 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
343 public Point rememberSelection() {
344 return super.rememberSelection();
348 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
350 public void restoreSelection() {
351 super.restoreSelection();
355 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
357 public void propertyChange(PropertyChangeEvent event) {
358 String property = event.getProperty();
359 if (PreferenceConstants.EDITOR_FOREGROUND_COLOR.equals(property)
360 || PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR
362 || PreferenceConstants.EDITOR_BACKGROUND_COLOR.equals(property)
363 || PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR
365 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR
367 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR
369 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR
371 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR
373 initializeViewerColors();
378 * Sets the preference store on this viewer.
381 * the preference store
385 public void setPreferenceStore(IPreferenceStore store) {
386 if (fIsConfigured && fPreferenceStore != null)
387 fPreferenceStore.removePropertyChangeListener(this);
389 fPreferenceStore = store;
391 if (fIsConfigured && fPreferenceStore != null) {
392 fPreferenceStore.addPropertyChangeListener(this);
393 initializeViewerColors();
398 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite,
401 protected void createControl(Composite parent, int styles) {
402 super.createControl(parent, styles);
404 fBackspaceManager = new SmartBackspaceManager();
405 fBackspaceManager.install(this);
409 * Returns the backspace manager for this viewer.
411 * @return the backspace manager for this viewer, or <code>null</code> if
415 public SmartBackspaceManager getBackspaceManager() {
416 return fBackspaceManager;
420 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
422 protected void handleDispose() {
423 if (fBackspaceManager != null) {
424 fBackspaceManager.uninstall();
425 fBackspaceManager = null;
428 super.handleDispose();
432 * Prepends the text presentation listener at the beginning of the viewer's
433 * list of text presentation listeners. If the listener is already
434 * registered with the viewer this call moves the listener to the beginning
438 * the text presentation listener
441 public void prependTextPresentationListener(
442 ITextPresentationListener listener) {
444 Assert.isNotNull(listener);
446 if (fTextPresentationListeners == null)
447 fTextPresentationListeners = new ArrayList();
449 fTextPresentationListeners.remove(listener);
450 fTextPresentationListeners.add(0, listener);
454 * Sets the given reconciler.
460 void setReconciler(IReconciler reconciler) {
461 fReconciler = reconciler;
465 * Returns the reconciler.
467 * @return the reconciler or <code>null</code> if not set
470 Object getReconciler() {