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();
138 Point point = getSelectedRange();
140 setSelectedRange(0, getDocument().getLength());
145 super.doOperation(operation);
149 * @see ITextOperationTarget#canDoOperation(int)
151 public boolean canDoOperation(int operation) {
152 if (operation == SHOW_OUTLINE)
153 return fOutlinePresenter != null;
154 if (operation == OPEN_STRUCTURE)
155 return fStructurePresenter != null;
156 if (operation == SHOW_HIERARCHY)
157 // return fHierarchyPresenter != null;
160 return super.canDoOperation(operation);
164 * @see ISourceViewer#configure(SourceViewerConfiguration)
166 public void configure(SourceViewerConfiguration configuration) {
167 super.configure(configuration);
168 if (configuration instanceof PHPSourceViewerConfiguration) {
169 fOutlinePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, false);
170 fOutlinePresenter.install(this);
172 if (configuration instanceof PHPSourceViewerConfiguration) {
173 fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
174 fStructurePresenter.install(this);
176 if (configuration instanceof PHPSourceViewerConfiguration) {
177 // fHierarchyPresenter= ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this, true);
178 // fHierarchyPresenter.install(this);
180 if (fPreferenceStore != null) {
181 fPreferenceStore.addPropertyChangeListener(this);
182 initializeViewerColors();
189 protected void initializeViewerColors() {
190 if (fPreferenceStore != null) {
192 StyledText styledText= getTextWidget();
194 // ----------- foreground color --------------------
195 Color color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR)
197 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_FOREGROUND_COLOR, styledText.getDisplay());
198 styledText.setForeground(color);
200 if (fForegroundColor != null)
201 fForegroundColor.dispose();
203 fForegroundColor= color;
205 // ---------- background color ----------------------
206 color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR)
208 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_BACKGROUND_COLOR, styledText.getDisplay());
209 styledText.setBackground(color);
211 if (fBackgroundColor != null)
212 fBackgroundColor.dispose();
214 fBackgroundColor= color;
216 // ----------- selection foreground color --------------------
217 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
219 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
220 styledText.setSelectionForeground(color);
222 if (fSelectionForegroundColor != null)
223 fSelectionForegroundColor.dispose();
225 fSelectionForegroundColor= color;
227 // ---------- selection background color ----------------------
228 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
230 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
231 styledText.setSelectionBackground(color);
233 if (fSelectionBackgroundColor != null)
234 fSelectionBackgroundColor.dispose();
236 fSelectionBackgroundColor= color;
241 * Creates a color from the information stored in the given preference store.
242 * Returns <code>null</code> if there is no such information available.
244 * @param store the store to read from
245 * @param key the key used for the lookup in the preference store
246 * @param display the display used create the color
247 * @return the created color according to the specification in the preference store
250 private Color createColor(IPreferenceStore store, String key, Display display) {
254 if (store.contains(key)) {
256 if (store.isDefault(key))
257 rgb= PreferenceConverter.getDefaultColor(store, key);
259 rgb= PreferenceConverter.getColor(store, key);
262 return new Color(display, rgb);
269 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
272 public void unconfigure() {
273 if (fOutlinePresenter != null) {
274 fOutlinePresenter.uninstall();
275 fOutlinePresenter= null;
277 if (fStructurePresenter != null) {
278 fStructurePresenter.uninstall();
279 fStructurePresenter= null;
281 // if (fHierarchyPresenter != null) {
282 // fHierarchyPresenter.uninstall();
283 // fHierarchyPresenter= null;
285 if (fForegroundColor != null) {
286 fForegroundColor.dispose();
287 fForegroundColor= null;
289 if (fBackgroundColor != null) {
290 fBackgroundColor.dispose();
291 fBackgroundColor= null;
293 if (fPreferenceStore != null)
294 fPreferenceStore.removePropertyChangeListener(this);
298 fIsConfigured= false;
302 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
304 public Point rememberSelection() {
305 return super.rememberSelection();
309 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
311 public void restoreSelection() {
312 super.restoreSelection();
316 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
318 public void propertyChange(PropertyChangeEvent event) {
319 String property = event.getProperty();
320 if (PreferenceConstants.EDITOR_FOREGROUND_COLOR.equals(property)
321 || PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR.equals(property)
322 || PreferenceConstants.EDITOR_BACKGROUND_COLOR.equals(property)
323 || PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR.equals(property)
324 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR.equals(property)
325 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR.equals(property)
326 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR.equals(property)
327 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR.equals(property))
329 initializeViewerColors();
334 * Sets the preference store on this viewer.
336 * @param store the preference store
340 public void setPreferenceStore(IPreferenceStore store) {
341 if (fIsConfigured && fPreferenceStore != null)
342 fPreferenceStore.removePropertyChangeListener(this);
344 fPreferenceStore= store;
346 if (fIsConfigured && fPreferenceStore != null) {
347 fPreferenceStore.addPropertyChangeListener(this);
348 initializeViewerColors();
353 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
355 protected void createControl(Composite parent, int styles) {
356 super.createControl(parent, styles);
358 fBackspaceManager= new SmartBackspaceManager();
359 fBackspaceManager.install(this);
363 * Returns the backspace manager for this viewer.
365 * @return the backspace manager for this viewer, or <code>null</code> if
369 public SmartBackspaceManager getBackspaceManager() {
370 return fBackspaceManager;
374 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
376 protected void handleDispose() {
377 if (fBackspaceManager != null) {
378 fBackspaceManager.uninstall();
379 fBackspaceManager= null;
382 super.handleDispose();
386 * Prepends the text presentation listener at the beginning of the viewer's
387 * list of text presentation listeners. If the listener is already registered
388 * with the viewer this call moves the listener to the beginning of
391 * @param listener the text presentation listener
394 public void prependTextPresentationListener(ITextPresentationListener listener) {
396 Assert.isNotNull(listener);
398 if (fTextPresentationListeners == null)
399 fTextPresentationListeners= new ArrayList();
401 fTextPresentationListeners.remove(listener);
402 fTextPresentationListeners.add(0, listener);