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;
14 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
15 import net.sourceforge.phpdt.ui.PreferenceConstants;
16 import net.sourceforge.phpdt.ui.actions.PHPEditorActionDefinitionIds;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.ui.texteditor.ITextEditor;
24 import org.eclipse.ui.texteditor.TextEditorAction;
28 * A toolbar action which toggles the presentation model of the
29 * connected text editor. The editor shows either the highlight range
30 * only or always the whole document.
32 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener {
34 private IPreferenceStore fStore;
37 * Constructs and updates the action.
39 public TogglePresentationAction() {
40 super(PHPEditorMessages.getResourceBundle(), "TogglePresentation.", null); //$NON-NLS-1$
41 PHPUiImages.setToolImageDescriptors(this, "segment_edit.gif"); //$NON-NLS-1$
42 setToolTipText(PHPEditorMessages.getString("TogglePresentation.tooltip")); //$NON-NLS-1$
43 setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_PRESENTATION);
44 // WorkbenchHelp.setHelp(this, IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION);
49 * @see IAction#actionPerformed
53 ITextEditor editor= getTextEditor();
57 IRegion remembered= editor.getHighlightRange();
58 editor.resetHighlightRange();
60 boolean showAll= !editor.showsHighlightRangeOnly();
63 editor.showHighlightRangeOnly(showAll);
64 if (remembered != null)
65 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
67 fStore.removePropertyChangeListener(this);
68 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
69 fStore.addPropertyChangeListener(this);
73 * @see TextEditorAction#update
75 public void update() {
76 ITextEditor editor= getTextEditor();
77 boolean checked= (editor != null && editor.showsHighlightRangeOnly());
79 setEnabled(editor != null);
83 * @see TextEditorAction#setEditor(ITextEditor)
85 public void setEditor(ITextEditor editor) {
87 super.setEditor(editor);
92 fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
93 fStore.addPropertyChangeListener(this);
95 synchronizeWithPreference(editor);
97 } else if (fStore != null) {
98 fStore.removePropertyChangeListener(this);
106 * Synchronizes the appearance of the editor with what the preference store tells him.
108 private void synchronizeWithPreference(ITextEditor editor) {
113 boolean showSegments= fStore.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
114 setChecked(showSegments);
116 if (editor.showsHighlightRangeOnly() != showSegments) {
117 IRegion remembered= editor.getHighlightRange();
118 editor.resetHighlightRange();
119 editor.showHighlightRangeOnly(showSegments);
120 if (remembered != null)
121 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
126 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
128 public void propertyChange(PropertyChangeEvent event) {
129 if (event.getProperty().equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
130 synchronizeWithPreference(getTextEditor());