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 *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
13 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
14 import net.sourceforge.phpdt.ui.PreferenceConstants;
15 import net.sourceforge.phpdt.ui.actions.PHPEditorActionDefinitionIds;
16 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
18 import org.eclipse.jface.preference.IPreferenceStore;
19 import org.eclipse.jface.text.IRegion;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 import org.eclipse.ui.texteditor.ITextEditor;
23 import org.eclipse.ui.texteditor.TextEditorAction;
26 * A toolbar action which toggles the presentation model of the connected text
27 * editor. The editor shows either the highlight range only or always the whole
30 public class TogglePresentationAction extends TextEditorAction implements
31 IPropertyChangeListener {
33 private IPreferenceStore fStore;
36 * Constructs and updates the action.
38 public TogglePresentationAction() {
39 super(PHPEditorMessages.getResourceBundle(),
40 "TogglePresentation.", null); //$NON-NLS-1$
41 PHPUiImages.setToolImageDescriptors(this, "segment_edit.gif"); //$NON-NLS-1$
42 setToolTipText(PHPEditorMessages
43 .getString("TogglePresentation.tooltip")); //$NON-NLS-1$
44 setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_PRESENTATION);
45 // WorkbenchHelp.setHelp(this,
46 // IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION);
51 * @see IAction#actionPerformed
55 ITextEditor editor = getTextEditor();
59 IRegion remembered = editor.getHighlightRange();
60 editor.resetHighlightRange();
62 boolean showAll = !editor.showsHighlightRangeOnly();
65 editor.showHighlightRangeOnly(showAll);
66 if (remembered != null)
67 editor.setHighlightRange(remembered.getOffset(), remembered
70 fStore.removePropertyChangeListener(this);
71 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
72 fStore.addPropertyChangeListener(this);
76 * @see TextEditorAction#update
78 public void update() {
79 ITextEditor editor = getTextEditor();
80 boolean checked = (editor != null && editor.showsHighlightRangeOnly());
82 setEnabled(editor != null);
86 * @see TextEditorAction#setEditor(ITextEditor)
88 public void setEditor(ITextEditor editor) {
90 super.setEditor(editor);
95 fStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
96 fStore.addPropertyChangeListener(this);
98 synchronizeWithPreference(editor);
100 } else if (fStore != null) {
101 fStore.removePropertyChangeListener(this);
109 * Synchronizes the appearance of the editor with what the preference store
112 private void synchronizeWithPreference(ITextEditor editor) {
117 boolean showSegments = fStore
118 .getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
119 setChecked(showSegments);
121 if (editor.showsHighlightRangeOnly() != showSegments) {
122 IRegion remembered = editor.getHighlightRange();
123 editor.resetHighlightRange();
124 editor.showHighlightRangeOnly(showSegments);
125 if (remembered != null)
126 editor.setHighlightRange(remembered.getOffset(), remembered
132 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
134 public void propertyChange(PropertyChangeEvent event) {
135 if (event.getProperty()
136 .equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
137 synchronizeWithPreference(getTextEditor());