Remove import of org.eclipse.ui.externaltools.internal.model.ExternalToolBuilder...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / TogglePresentationAction.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.phpeditor;
12
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;
17
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;
24
25 /**
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
28  * document.
29  */
30 public class TogglePresentationAction extends TextEditorAction implements
31                 IPropertyChangeListener {
32
33         private IPreferenceStore fStore;
34
35         /**
36          * Constructs and updates the action.
37          */
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);
47                 update();
48         }
49
50         /*
51          * @see IAction#actionPerformed
52          */
53         public void run() {
54
55                 ITextEditor editor = getTextEditor();
56                 if (editor == null)
57                         return;
58
59                 IRegion remembered = editor.getHighlightRange();
60                 editor.resetHighlightRange();
61
62                 boolean showAll = !editor.showsHighlightRangeOnly();
63                 setChecked(showAll);
64
65                 editor.showHighlightRangeOnly(showAll);
66                 if (remembered != null)
67                         editor.setHighlightRange(remembered.getOffset(), remembered
68                                         .getLength(), true);
69
70                 fStore.removePropertyChangeListener(this);
71                 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
72                 fStore.addPropertyChangeListener(this);
73         }
74
75         /*
76          * @see TextEditorAction#update
77          */
78         public void update() {
79                 ITextEditor editor = getTextEditor();
80                 boolean checked = (editor != null && editor.showsHighlightRangeOnly());
81                 setChecked(checked);
82                 setEnabled(editor != null);
83         }
84
85         /*
86          * @see TextEditorAction#setEditor(ITextEditor)
87          */
88         public void setEditor(ITextEditor editor) {
89
90                 super.setEditor(editor);
91
92                 if (editor != null) {
93
94                         if (fStore == null) {
95                                 fStore = PHPeclipsePlugin.getDefault().getPreferenceStore();
96                                 fStore.addPropertyChangeListener(this);
97                         }
98                         synchronizeWithPreference(editor);
99
100                 } else if (fStore != null) {
101                         fStore.removePropertyChangeListener(this);
102                         fStore = null;
103                 }
104
105                 update();
106         }
107
108         /**
109          * Synchronizes the appearance of the editor with what the preference store
110          * tells him.
111          */
112         private void synchronizeWithPreference(ITextEditor editor) {
113
114                 if (editor == null)
115                         return;
116
117                 boolean showSegments = fStore
118                                 .getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
119                 setChecked(showSegments);
120
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
127                                                 .getLength(), true);
128                 }
129         }
130
131         /*
132          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
133          */
134         public void propertyChange(PropertyChangeEvent event) {
135                 if (event.getProperty()
136                                 .equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
137                         synchronizeWithPreference(getTextEditor());
138         }
139 }