replace deprecated org.eclipse.jface.util.ListenerList
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / 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 import net.sourceforge.phpeclipse.ui.WebUI;
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  * A toolbar action which toggles the presentation model of the connected text
28  * editor. The editor shows either the highlight range only or always the whole
29  * document.
30  */
31 public class TogglePresentationAction extends TextEditorAction implements
32                 IPropertyChangeListener {
33
34         private IPreferenceStore fStore;
35
36         /**
37          * Constructs and updates the action.
38          */
39         public TogglePresentationAction() {
40                 super(PHPEditorMessages.getResourceBundle(),
41                                 "TogglePresentation.", null); //$NON-NLS-1$
42                 PHPUiImages.setToolImageDescriptors(this, "segment_edit.gif"); //$NON-NLS-1$
43                 setToolTipText(PHPEditorMessages
44                                 .getString("TogglePresentation.tooltip")); //$NON-NLS-1$
45                 setActionDefinitionId(PHPEditorActionDefinitionIds.TOGGLE_PRESENTATION);
46                 // WorkbenchHelp.setHelp(this,
47                 // IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION);
48                 update();
49         }
50
51         /*
52          * @see IAction#actionPerformed
53          */
54         public void run() {
55
56                 ITextEditor editor = getTextEditor();
57                 if (editor == null)
58                         return;
59
60                 IRegion remembered = editor.getHighlightRange();
61                 editor.resetHighlightRange();
62
63                 boolean showAll = !editor.showsHighlightRangeOnly();
64                 setChecked(showAll);
65
66                 editor.showHighlightRangeOnly(showAll);
67                 if (remembered != null)
68                         editor.setHighlightRange(remembered.getOffset(), remembered
69                                         .getLength(), true);
70
71                 fStore.removePropertyChangeListener(this);
72                 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
73                 fStore.addPropertyChangeListener(this);
74         }
75
76         /*
77          * @see TextEditorAction#update
78          */
79         public void update() {
80                 ITextEditor editor = getTextEditor();
81                 boolean checked = (editor != null && editor.showsHighlightRangeOnly());
82                 setChecked(checked);
83                 setEnabled(editor != null);
84         }
85
86         /*
87          * @see TextEditorAction#setEditor(ITextEditor)
88          */
89         public void setEditor(ITextEditor editor) {
90
91                 super.setEditor(editor);
92
93                 if (editor != null) {
94
95                         if (fStore == null) {
96                                 fStore = WebUI.getDefault().getPreferenceStore();
97                                 fStore.addPropertyChangeListener(this);
98                         }
99                         synchronizeWithPreference(editor);
100
101                 } else if (fStore != null) {
102                         fStore.removePropertyChangeListener(this);
103                         fStore = null;
104                 }
105
106                 update();
107         }
108
109         /**
110          * Synchronizes the appearance of the editor with what the preference store
111          * tells him.
112          */
113         private void synchronizeWithPreference(ITextEditor editor) {
114
115                 if (editor == null)
116                         return;
117
118                 boolean showSegments = fStore
119                                 .getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
120                 setChecked(showSegments);
121
122                 if (editor.showsHighlightRangeOnly() != showSegments) {
123                         IRegion remembered = editor.getHighlightRange();
124                         editor.resetHighlightRange();
125                         editor.showHighlightRangeOnly(showSegments);
126                         if (remembered != null)
127                                 editor.setHighlightRange(remembered.getOffset(), remembered
128                                                 .getLength(), true);
129                 }
130         }
131
132         /*
133          * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
134          */
135         public void propertyChange(PropertyChangeEvent event) {
136                 if (event.getProperty()
137                                 .equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
138                         synchronizeWithPreference(getTextEditor());
139         }
140 }