2 * Copyright (c) 2003-2004 Christopher Lenz 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 * Christopher Lenz - initial API and implementation
11 * $Id: ShowSelectedElementOnlyAction.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.ui.internal.editor;
16 import net.sourceforge.phpeclipse.css.ui.CssUI;
17 import net.sourceforge.phpeclipse.css.ui.internal.CssUIMessages;
18 import net.sourceforge.phpeclipse.css.ui.internal.CssUIPreferences;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
24 import org.eclipse.ui.texteditor.ITextEditor;
25 import org.eclipse.ui.texteditor.TextEditorAction;
28 * A toolbar action which toggles the presentation model of the connected text
29 * editor. The editor shows either the highlight range only or always the whole
33 * This class is an implementation for the retargetable action
34 * <code>TOGGLE_SHOW_SELECTED_ELEMENT_ONLY</code> provided by the platform.
37 * @see org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds#TOGGLE_SHOW_SELECTED_ELEMENT_ONLY
39 public class ShowSelectedElementOnlyAction extends TextEditorAction
40 implements IPropertyChangeListener {
42 // Instance Variables ------------------------------------------------------
44 private IPreferenceStore store;
46 // Constructors ------------------------------------------------------------
51 public ShowSelectedElementOnlyAction() {
52 super(CssUIMessages.getResourceBundle(),
53 "CssEditor.showSelectedElementOnly.", null); //$NON-NLS-1$
54 store = CssUI.getDefault().getPreferenceStore();
58 // TextEditor Implementation -----------------------------------------------
61 * @see org.eclipse.jface.action.IAction#run()
64 ITextEditor editor = getTextEditor();
66 IRegion highlightRange = editor.getHighlightRange();
67 editor.resetHighlightRange();
68 boolean show = !editor.showsHighlightRangeOnly();
70 editor.showHighlightRangeOnly(show);
71 if (highlightRange != null) {
72 editor.setHighlightRange(highlightRange.getOffset(),
73 highlightRange.getLength(), true);
75 store.removePropertyChangeListener(this);
77 CssUIPreferences.EDITOR_SHOW_SELECTED_ELEMENT_ONLY, show);
78 store.addPropertyChangeListener(this);
83 * @see TextEditorAction#setEditor(ITextEditor)
85 public void setEditor(ITextEditor editor) {
86 super.setEditor(editor);
89 store = CssUI.getDefault().getPreferenceStore();
90 store.addPropertyChangeListener(this);
92 synchronizeWithPreference(editor);
93 } else if (store != null) {
94 store.removePropertyChangeListener(this);
101 * @see org.eclipse.ui.texteditor.IUpdate#update()
103 public void update() {
104 ITextEditor editor = getTextEditor();
105 if (editor != null) {
106 setChecked(getTextEditor().showsHighlightRangeOnly());
113 // IPropertyChangeListener Implementation ----------------------------------
116 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
118 public void propertyChange(PropertyChangeEvent event) {
119 String p = event.getProperty();
120 if (CssUIPreferences.EDITOR_SHOW_SELECTED_ELEMENT_ONLY.equals(p)) {
121 synchronizeWithPreference(getTextEditor());
125 // Private Methods ---------------------------------------------------------
128 * Synchronizes the appearance of the editor with what the preference store
131 * @param editor the editor to synchronize
133 private void synchronizeWithPreference(ITextEditor editor) {
134 if (editor != null) {
135 boolean show = this.store.getBoolean(
136 CssUIPreferences.EDITOR_SHOW_SELECTED_ELEMENT_ONLY);
138 if (show != editor.showsHighlightRangeOnly()) {
139 IRegion highlightRange = editor.getHighlightRange();
140 editor.resetHighlightRange();
141 editor.showHighlightRangeOnly(show);
142 if (highlightRange != null) {
143 editor.setHighlightRange(highlightRange.getOffset(),
144 highlightRange.getLength(), true);