Better event handling for browser preview refreshs in Wikipedia plugin
[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
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;
18
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;
25
26
27 /**
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.
31  */
32 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener {
33                 
34         private IPreferenceStore fStore;
35
36         /**
37          * Constructs and updates the action.
38          */
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);                
45                 update();
46         }
47         
48         /*
49          * @see IAction#actionPerformed
50          */
51         public void run() {
52                 
53                 ITextEditor editor= getTextEditor();
54                 if (editor == null)
55                         return;
56                 
57                 IRegion remembered= editor.getHighlightRange();
58                 editor.resetHighlightRange();
59                 
60                 boolean showAll= !editor.showsHighlightRangeOnly();
61                 setChecked(showAll);
62                 
63                 editor.showHighlightRangeOnly(showAll);
64                 if (remembered != null)
65                         editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
66                 
67                 fStore.removePropertyChangeListener(this);
68                 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
69                 fStore.addPropertyChangeListener(this);
70         }
71         
72         /*
73          * @see TextEditorAction#update
74          */
75         public void update() {
76                 ITextEditor editor= getTextEditor();
77                 boolean checked= (editor != null && editor.showsHighlightRangeOnly());
78                 setChecked(checked);
79                 setEnabled(editor != null);
80         }
81         
82         /*
83          * @see TextEditorAction#setEditor(ITextEditor)
84          */
85         public void setEditor(ITextEditor editor) {
86                 
87                 super.setEditor(editor);
88                 
89                 if (editor != null) {
90                         
91                         if (fStore == null) {
92                                 fStore= PHPeclipsePlugin.getDefault().getPreferenceStore();
93                                 fStore.addPropertyChangeListener(this);
94                         }
95                         synchronizeWithPreference(editor);
96                         
97                 } else if (fStore != null) {
98                         fStore.removePropertyChangeListener(this);
99                         fStore= null;
100                 }
101                 
102                 update();
103         }
104         
105         /**
106          * Synchronizes the appearance of the editor with what the preference store tells him.
107          */
108         private void synchronizeWithPreference(ITextEditor editor) {
109                 
110                 if (editor == null)
111                         return;
112                 
113                 boolean showSegments= fStore.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);                      
114                 setChecked(showSegments);
115                 
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);
122                 }
123         }
124
125         /*
126          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
127          */
128         public void propertyChange(PropertyChangeEvent event) {
129                 if (event.getProperty().equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
130                         synchronizeWithPreference(getTextEditor());
131         }
132 }