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.source.IOverviewRuler;
25 import org.eclipse.jface.text.source.IVerticalRuler;
26 import org.eclipse.jface.text.source.SourceViewerConfiguration;
27 import org.eclipse.jface.text.source.projection.ProjectionViewer;
28 import org.eclipse.jface.util.IPropertyChangeListener;
29 import org.eclipse.jface.util.PropertyChangeEvent;
30 import org.eclipse.swt.custom.StyledText;
31 import org.eclipse.swt.graphics.Color;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.swt.graphics.RGB;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
40 public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener {
43 * Text operation code for requesting the outline for the current input.
45 public static final int SHOW_OUTLINE= 51;
48 * Text operation code for requesting the outline for the element at the current position.
50 public static final int OPEN_STRUCTURE= 52;
53 * Text operation code for requesting the hierarchy for the current input.
55 public static final int SHOW_HIERARCHY= 53;
57 private IInformationPresenter fOutlinePresenter;
58 private IInformationPresenter fStructurePresenter;
59 // private IInformationPresenter fHierarchyPresenter;
62 * This viewer's foreground color.
65 private Color fForegroundColor;
67 * The viewer's background color.
70 private Color fBackgroundColor;
72 * This viewer's selection foreground color.
75 private Color fSelectionForegroundColor;
77 * The viewer's selection background color.
80 private Color fSelectionBackgroundColor;
82 * The preference store.
86 private IPreferenceStore fPreferenceStore;
88 * Is this source viewer configured?
92 private boolean fIsConfigured;
94 * The backspace manager of this viewer.
98 private SmartBackspaceManager fBackspaceManager;
100 public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles, IPreferenceStore store) {
101 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
102 setPreferenceStore(store);
106 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
109 // public IFormattingContext createFormattingContext() {
111 // IFormattingContext context= new CommentFormattingContext();
112 // Map map= new Hashtable(JavaCore.getOptions());
114 // context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
115 // context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map);
121 * @see ITextOperationTarget#doOperation(int)
123 public void doOperation(int operation) {
124 if (getTextWidget() == null)
129 fOutlinePresenter.showInformation();
132 fStructurePresenter.showInformation();
135 // fHierarchyPresenter.showInformation();
139 super.doOperation(operation);
143 * @see ITextOperationTarget#canDoOperation(int)
145 public boolean canDoOperation(int operation) {
146 if (operation == SHOW_OUTLINE)
147 return fOutlinePresenter != null;
148 if (operation == OPEN_STRUCTURE)
149 return fStructurePresenter != null;
150 if (operation == SHOW_HIERARCHY)
151 // return fHierarchyPresenter != null;
154 return super.canDoOperation(operation);
158 * @see ISourceViewer#configure(SourceViewerConfiguration)
160 public void configure(SourceViewerConfiguration configuration) {
161 super.configure(configuration);
162 if (configuration instanceof PHPSourceViewerConfiguration) {
163 fOutlinePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, false);
164 fOutlinePresenter.install(this);
166 if (configuration instanceof PHPSourceViewerConfiguration) {
167 fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
168 fStructurePresenter.install(this);
170 if (configuration instanceof PHPSourceViewerConfiguration) {
171 // fHierarchyPresenter= ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this, true);
172 // fHierarchyPresenter.install(this);
174 if (fPreferenceStore != null) {
175 fPreferenceStore.addPropertyChangeListener(this);
176 initializeViewerColors();
183 protected void initializeViewerColors() {
184 if (fPreferenceStore != null) {
186 StyledText styledText= getTextWidget();
188 // ----------- foreground color --------------------
189 Color color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR)
191 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_FOREGROUND_COLOR, styledText.getDisplay());
192 styledText.setForeground(color);
194 if (fForegroundColor != null)
195 fForegroundColor.dispose();
197 fForegroundColor= color;
199 // ---------- background color ----------------------
200 color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR)
202 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_BACKGROUND_COLOR, styledText.getDisplay());
203 styledText.setBackground(color);
205 if (fBackgroundColor != null)
206 fBackgroundColor.dispose();
208 fBackgroundColor= color;
210 // ----------- selection foreground color --------------------
211 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
213 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
214 styledText.setSelectionForeground(color);
216 if (fSelectionForegroundColor != null)
217 fSelectionForegroundColor.dispose();
219 fSelectionForegroundColor= color;
221 // ---------- selection background color ----------------------
222 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
224 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
225 styledText.setSelectionBackground(color);
227 if (fSelectionBackgroundColor != null)
228 fSelectionBackgroundColor.dispose();
230 fSelectionBackgroundColor= color;
235 * Creates a color from the information stored in the given preference store.
236 * Returns <code>null</code> if there is no such information available.
238 * @param store the store to read from
239 * @param key the key used for the lookup in the preference store
240 * @param display the display used create the color
241 * @return the created color according to the specification in the preference store
244 private Color createColor(IPreferenceStore store, String key, Display display) {
248 if (store.contains(key)) {
250 if (store.isDefault(key))
251 rgb= PreferenceConverter.getDefaultColor(store, key);
253 rgb= PreferenceConverter.getColor(store, key);
256 return new Color(display, rgb);
263 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
266 public void unconfigure() {
267 if (fOutlinePresenter != null) {
268 fOutlinePresenter.uninstall();
269 fOutlinePresenter= null;
271 if (fStructurePresenter != null) {
272 fStructurePresenter.uninstall();
273 fStructurePresenter= null;
275 // if (fHierarchyPresenter != null) {
276 // fHierarchyPresenter.uninstall();
277 // fHierarchyPresenter= null;
279 if (fForegroundColor != null) {
280 fForegroundColor.dispose();
281 fForegroundColor= null;
283 if (fBackgroundColor != null) {
284 fBackgroundColor.dispose();
285 fBackgroundColor= null;
287 if (fPreferenceStore != null)
288 fPreferenceStore.removePropertyChangeListener(this);
292 fIsConfigured= false;
296 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
298 public Point rememberSelection() {
299 return super.rememberSelection();
303 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
305 public void restoreSelection() {
306 super.restoreSelection();
310 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
312 public void propertyChange(PropertyChangeEvent event) {
313 String property = event.getProperty();
314 if (PreferenceConstants.EDITOR_FOREGROUND_COLOR.equals(property)
315 || PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR.equals(property)
316 || PreferenceConstants.EDITOR_BACKGROUND_COLOR.equals(property)
317 || PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR.equals(property)
318 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR.equals(property)
319 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR.equals(property)
320 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR.equals(property)
321 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR.equals(property))
323 initializeViewerColors();
328 * Sets the preference store on this viewer.
330 * @param store the preference store
334 public void setPreferenceStore(IPreferenceStore store) {
335 if (fIsConfigured && fPreferenceStore != null)
336 fPreferenceStore.removePropertyChangeListener(this);
338 fPreferenceStore= store;
340 if (fIsConfigured && fPreferenceStore != null) {
341 fPreferenceStore.addPropertyChangeListener(this);
342 initializeViewerColors();
347 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
349 protected void createControl(Composite parent, int styles) {
350 super.createControl(parent, styles);
352 fBackspaceManager= new SmartBackspaceManager();
353 fBackspaceManager.install(this);
357 * Returns the backspace manager for this viewer.
359 * @return the backspace manager for this viewer, or <code>null</code> if
363 public SmartBackspaceManager getBackspaceManager() {
364 return fBackspaceManager;
368 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
370 protected void handleDispose() {
371 if (fBackspaceManager != null) {
372 fBackspaceManager.uninstall();
373 fBackspaceManager= null;
376 super.handleDispose();
380 * Prepends the text presentation listener at the beginning of the viewer's
381 * list of text presentation listeners. If the listener is already registered
382 * with the viewer this call moves the listener to the beginning of
385 * @param listener the text presentation listener
388 public void prependTextPresentationListener(ITextPresentationListener listener) {
390 Assert.isNotNull(listener);
392 if (fTextPresentationListeners == null)
393 fTextPresentationListeners= new ArrayList();
395 fTextPresentationListeners.remove(listener);
396 fTextPresentationListeners.add(0, listener);