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;
15 import java.util.Hashtable;
18 import net.sourceforge.phpdt.core.JavaCore;
19 import net.sourceforge.phpdt.internal.ui.text.SmartBackspaceManager;
20 import net.sourceforge.phpdt.ui.PreferenceConstants;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.preference.PreferenceConverter;
24 import org.eclipse.jface.text.Assert;
25 import org.eclipse.jface.text.ITextPresentationListener;
26 import org.eclipse.jface.text.formatter.FormattingContextProperties;
27 import org.eclipse.jface.text.formatter.IFormattingContext;
28 import org.eclipse.jface.text.information.IInformationPresenter;
29 import org.eclipse.jface.text.source.IOverviewRuler;
30 import org.eclipse.jface.text.source.IVerticalRuler;
31 import org.eclipse.jface.text.source.SourceViewerConfiguration;
32 import org.eclipse.jface.text.source.projection.ProjectionViewer;
33 import org.eclipse.jface.util.IPropertyChangeListener;
34 import org.eclipse.jface.util.PropertyChangeEvent;
35 import org.eclipse.swt.custom.StyledText;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.Point;
38 import org.eclipse.swt.graphics.RGB;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
45 public class JavaSourceViewer extends ProjectionViewer implements IPropertyChangeListener {
48 * Text operation code for requesting the outline for the current input.
50 public static final int SHOW_OUTLINE= 51;
53 * Text operation code for requesting the outline for the element at the current position.
55 public static final int OPEN_STRUCTURE= 52;
58 * Text operation code for requesting the hierarchy for the current input.
60 public static final int SHOW_HIERARCHY= 53;
62 private IInformationPresenter fOutlinePresenter;
63 private IInformationPresenter fStructurePresenter;
64 // private IInformationPresenter fHierarchyPresenter;
67 * This viewer's foreground color.
70 private Color fForegroundColor;
72 * The viewer's background color.
75 private Color fBackgroundColor;
77 * This viewer's selection foreground color.
80 private Color fSelectionForegroundColor;
82 * The viewer's selection background color.
85 private Color fSelectionBackgroundColor;
87 * The preference store.
91 private IPreferenceStore fPreferenceStore;
93 * Is this source viewer configured?
97 private boolean fIsConfigured;
99 * The backspace manager of this viewer.
103 private SmartBackspaceManager fBackspaceManager;
105 public JavaSourceViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles, IPreferenceStore store) {
106 super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
107 setPreferenceStore(store);
111 * @see org.eclipse.jface.text.source.SourceViewer#createFormattingContext()
114 // public IFormattingContext createFormattingContext() {
116 // IFormattingContext context= new CommentFormattingContext();
117 // Map map= new Hashtable(JavaCore.getOptions());
119 // context.storeToMap(PreferenceConstants.getPreferenceStore(), map, false);
120 // context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, map);
126 * @see ITextOperationTarget#doOperation(int)
128 public void doOperation(int operation) {
129 if (getTextWidget() == null)
134 fOutlinePresenter.showInformation();
137 fStructurePresenter.showInformation();
140 // fHierarchyPresenter.showInformation();
144 super.doOperation(operation);
148 * @see ITextOperationTarget#canDoOperation(int)
150 public boolean canDoOperation(int operation) {
151 if (operation == SHOW_OUTLINE)
152 return fOutlinePresenter != null;
153 if (operation == OPEN_STRUCTURE)
154 return fStructurePresenter != null;
155 if (operation == SHOW_HIERARCHY)
156 // return fHierarchyPresenter != null;
159 return super.canDoOperation(operation);
163 * @see ISourceViewer#configure(SourceViewerConfiguration)
165 public void configure(SourceViewerConfiguration configuration) {
166 super.configure(configuration);
167 if (configuration instanceof PHPSourceViewerConfiguration) {
168 fOutlinePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, false);
169 fOutlinePresenter.install(this);
171 if (configuration instanceof PHPSourceViewerConfiguration) {
172 fStructurePresenter= ((PHPSourceViewerConfiguration)configuration).getOutlinePresenter(this, true);
173 fStructurePresenter.install(this);
175 if (configuration instanceof PHPSourceViewerConfiguration) {
176 // fHierarchyPresenter= ((PHPSourceViewerConfiguration)configuration).getHierarchyPresenter(this, true);
177 // fHierarchyPresenter.install(this);
179 if (fPreferenceStore != null) {
180 fPreferenceStore.addPropertyChangeListener(this);
181 initializeViewerColors();
188 protected void initializeViewerColors() {
189 if (fPreferenceStore != null) {
191 StyledText styledText= getTextWidget();
193 // ----------- foreground color --------------------
194 Color color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR)
196 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_FOREGROUND_COLOR, styledText.getDisplay());
197 styledText.setForeground(color);
199 if (fForegroundColor != null)
200 fForegroundColor.dispose();
202 fForegroundColor= color;
204 // ---------- background color ----------------------
205 color= fPreferenceStore.getBoolean(PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR)
207 : createColor(fPreferenceStore, PreferenceConstants.EDITOR_BACKGROUND_COLOR, styledText.getDisplay());
208 styledText.setBackground(color);
210 if (fBackgroundColor != null)
211 fBackgroundColor.dispose();
213 fBackgroundColor= color;
215 // ----------- selection foreground color --------------------
216 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR)
218 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR, styledText.getDisplay());
219 styledText.setSelectionForeground(color);
221 if (fSelectionForegroundColor != null)
222 fSelectionForegroundColor.dispose();
224 fSelectionForegroundColor= color;
226 // ---------- selection background color ----------------------
227 color= fPreferenceStore.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR)
229 : createColor(fPreferenceStore, AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR, styledText.getDisplay());
230 styledText.setSelectionBackground(color);
232 if (fSelectionBackgroundColor != null)
233 fSelectionBackgroundColor.dispose();
235 fSelectionBackgroundColor= color;
240 * Creates a color from the information stored in the given preference store.
241 * Returns <code>null</code> if there is no such information available.
243 * @param store the store to read from
244 * @param key the key used for the lookup in the preference store
245 * @param display the display used create the color
246 * @return the created color according to the specification in the preference store
249 private Color createColor(IPreferenceStore store, String key, Display display) {
253 if (store.contains(key)) {
255 if (store.isDefault(key))
256 rgb= PreferenceConverter.getDefaultColor(store, key);
258 rgb= PreferenceConverter.getColor(store, key);
261 return new Color(display, rgb);
268 * @see org.eclipse.jface.text.source.ISourceViewerExtension2#unconfigure()
271 public void unconfigure() {
272 if (fOutlinePresenter != null) {
273 fOutlinePresenter.uninstall();
274 fOutlinePresenter= null;
276 if (fStructurePresenter != null) {
277 fStructurePresenter.uninstall();
278 fStructurePresenter= null;
280 // if (fHierarchyPresenter != null) {
281 // fHierarchyPresenter.uninstall();
282 // fHierarchyPresenter= null;
284 if (fForegroundColor != null) {
285 fForegroundColor.dispose();
286 fForegroundColor= null;
288 if (fBackgroundColor != null) {
289 fBackgroundColor.dispose();
290 fBackgroundColor= null;
292 if (fPreferenceStore != null)
293 fPreferenceStore.removePropertyChangeListener(this);
297 fIsConfigured= false;
301 * @see org.eclipse.jface.text.source.SourceViewer#rememberSelection()
303 public Point rememberSelection() {
304 return super.rememberSelection();
308 * @see org.eclipse.jface.text.source.SourceViewer#restoreSelection()
310 public void restoreSelection() {
311 super.restoreSelection();
315 * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
317 public void propertyChange(PropertyChangeEvent event) {
318 String property = event.getProperty();
319 if (PreferenceConstants.EDITOR_FOREGROUND_COLOR.equals(property)
320 || PreferenceConstants.EDITOR_FOREGROUND_DEFAULT_COLOR.equals(property)
321 || PreferenceConstants.EDITOR_BACKGROUND_COLOR.equals(property)
322 || PreferenceConstants.EDITOR_BACKGROUND_DEFAULT_COLOR.equals(property)
323 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_COLOR.equals(property)
324 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_FOREGROUND_DEFAULT_COLOR.equals(property)
325 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_COLOR.equals(property)
326 || AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SELECTION_BACKGROUND_DEFAULT_COLOR.equals(property))
328 initializeViewerColors();
333 * Sets the preference store on this viewer.
335 * @param store the preference store
339 public void setPreferenceStore(IPreferenceStore store) {
340 if (fIsConfigured && fPreferenceStore != null)
341 fPreferenceStore.removePropertyChangeListener(this);
343 fPreferenceStore= store;
345 if (fIsConfigured && fPreferenceStore != null) {
346 fPreferenceStore.addPropertyChangeListener(this);
347 initializeViewerColors();
352 * @see org.eclipse.jface.text.source.SourceViewer#createControl(org.eclipse.swt.widgets.Composite, int)
354 protected void createControl(Composite parent, int styles) {
355 super.createControl(parent, styles);
357 fBackspaceManager= new SmartBackspaceManager();
358 fBackspaceManager.install(this);
362 * Returns the backspace manager for this viewer.
364 * @return the backspace manager for this viewer, or <code>null</code> if
368 public SmartBackspaceManager getBackspaceManager() {
369 return fBackspaceManager;
373 * @see org.eclipse.jface.text.source.SourceViewer#handleDispose()
375 protected void handleDispose() {
376 if (fBackspaceManager != null) {
377 fBackspaceManager.uninstall();
378 fBackspaceManager= null;
381 super.handleDispose();
385 * Prepends the text presentation listener at the beginning of the viewer's
386 * list of text presentation listeners. If the listener is already registered
387 * with the viewer this call moves the listener to the beginning of
390 * @param listener the text presentation listener
393 public void prependTextPresentationListener(ITextPresentationListener listener) {
395 Assert.isNotNull(listener);
397 if (fTextPresentationListeners == null)
398 fTextPresentationListeners= new ArrayList();
400 fTextPresentationListeners.remove(listener);
401 fTextPresentationListeners.add(0, listener);