intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.ui / src / net / sourceforge / phpeclipse / css / ui / internal / editor / ShowSelectedElementOnlyAction.java
1 /*
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
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: ShowSelectedElementOnlyAction.java,v 1.1 2004-09-02 18:11:50 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.ui.internal.editor;
15
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;
19
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;
26
27 /**
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 
30  * document.
31  * 
32  * <p>
33  *  This class is an implementation for the retargetable action 
34  *  <code>TOGGLE_SHOW_SELECTED_ELEMENT_ONLY</code> provided by the platform.
35  * </p>
36  * 
37  * @see org.eclipse.ui.texteditor.ITextEditorActionDefinitionIds#TOGGLE_SHOW_SELECTED_ELEMENT_ONLY
38  */
39 public class ShowSelectedElementOnlyAction extends TextEditorAction
40         implements IPropertyChangeListener {
41
42         // Instance Variables ------------------------------------------------------
43
44         private IPreferenceStore store;
45
46         // Constructors ------------------------------------------------------------
47
48         /**
49          * Constructor.
50          */
51         public ShowSelectedElementOnlyAction() {
52                 super(CssUIMessages.getResourceBundle(),
53                         "CssEditor.showSelectedElementOnly.", null); //$NON-NLS-1$
54                 store = CssUI.getDefault().getPreferenceStore();
55                 update();
56         }
57
58         // TextEditor Implementation -----------------------------------------------
59
60         /*
61          * @see org.eclipse.jface.action.IAction#run()
62          */
63         public void run() {
64                 ITextEditor editor = getTextEditor();
65                 if (editor != null) {
66                         IRegion highlightRange = editor.getHighlightRange();
67                         editor.resetHighlightRange();
68                         boolean show = !editor.showsHighlightRangeOnly();
69                         setChecked(show);
70                         editor.showHighlightRangeOnly(show);
71                         if (highlightRange != null) {
72                                 editor.setHighlightRange(highlightRange.getOffset(),
73                                         highlightRange.getLength(), true);
74                         }
75                         store.removePropertyChangeListener(this);
76                         store.setValue(
77                                 CssUIPreferences.EDITOR_SHOW_SELECTED_ELEMENT_ONLY, show);
78                         store.addPropertyChangeListener(this);
79                 }
80         }
81
82         /*
83          * @see TextEditorAction#setEditor(ITextEditor)
84          */
85         public void setEditor(ITextEditor editor) {
86                 super.setEditor(editor);
87                 if (editor != null) {
88                         if (store == null) {
89                                 store = CssUI.getDefault().getPreferenceStore();
90                                 store.addPropertyChangeListener(this);
91                         }
92                         synchronizeWithPreference(editor);
93                 } else if (store != null) {
94                         store.removePropertyChangeListener(this);
95                         store = null;
96                 }
97                 update();
98         }
99         
100         /*
101          * @see org.eclipse.ui.texteditor.IUpdate#update()
102          */
103         public void update() {
104                 ITextEditor editor = getTextEditor();
105                 if (editor != null) {
106                         setChecked(getTextEditor().showsHighlightRangeOnly());
107                         setEnabled(true);
108                 } else {
109                         setEnabled(false);
110                 }
111         }
112
113         // IPropertyChangeListener Implementation ----------------------------------
114
115         /*
116          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
117          */
118         public void propertyChange(PropertyChangeEvent event) {
119                 String p = event.getProperty();
120                 if (CssUIPreferences.EDITOR_SHOW_SELECTED_ELEMENT_ONLY.equals(p)) {
121                         synchronizeWithPreference(getTextEditor());
122                 }
123         }
124
125         // Private Methods ---------------------------------------------------------
126
127         /**
128          * Synchronizes the appearance of the editor with what the preference store
129          * contains.
130          * 
131          * @param editor the editor to synchronize
132          */
133         private void synchronizeWithPreference(ITextEditor editor) {
134                 if (editor != null) {
135                         boolean show = this.store.getBoolean(
136                                 CssUIPreferences.EDITOR_SHOW_SELECTED_ELEMENT_ONLY);
137                         setChecked(show);
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);
145                                 }
146                         }
147                 }
148         }
149
150 }