KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > TogglePresentationAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.editor.actions;
12
13 import org.eclipse.ant.internal.ui.AntUIImages;
14 import org.eclipse.ant.internal.ui.AntUIPlugin;
15 import org.eclipse.ant.internal.ui.IAntUIConstants;
16 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
17 import org.eclipse.jface.action.IAction;
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 tool bar action which toggles the presentation model of the
27  * connected text editor. The editor shows either the highlight range
28  * only or always the whole document.
29  */

30 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener {
31         
32     private IPreferenceStore fStore;
33
34     /**
35      * Constructs and updates the action.
36      */

37     public TogglePresentationAction() {
38         super(AntEditorActionMessages.getResourceBundle(), "TogglePresentation.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
39
setImageDescriptor(AntUIImages.getImageDescriptor(IAntUIConstants.IMG_SEGMENT_EDIT));
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.jface.action.IAction#run()
44      */

45     public void run() {
46         
47         ITextEditor editor= getTextEditor();
48         if (editor == null) {
49             return;
50         }
51         
52         IRegion remembered= editor.getHighlightRange();
53         editor.resetHighlightRange();
54         
55         boolean showAll= !editor.showsHighlightRangeOnly();
56         setChecked(showAll);
57         
58         editor.showHighlightRangeOnly(showAll);
59         if (remembered != null) {
60             editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
61         }
62         
63         fStore.removePropertyChangeListener(this);
64         fStore.setValue(AntEditorPreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
65         fStore.addPropertyChangeListener(this);
66     }
67     
68     /* (non-Javadoc)
69      * @see org.eclipse.ui.texteditor.IUpdate#update()
70      */

71     public void update() {
72         ITextEditor editor= getTextEditor();
73         boolean checked= (editor != null && editor.showsHighlightRangeOnly());
74         setChecked(checked);
75         setEnabled(editor != null);
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.ui.texteditor.TextEditorAction#setEditor(org.eclipse.ui.texteditor.ITextEditor)
80      */

81     public void setEditor(ITextEditor editor) {
82         
83         super.setEditor(editor);
84         
85         if (editor != null) {
86             
87             if (fStore == null) {
88                 fStore= AntUIPlugin.getDefault().getPreferenceStore();
89                 fStore.addPropertyChangeListener(this);
90             }
91             synchronizeWithPreference(editor);
92             
93         } else if (fStore != null) {
94             fStore.removePropertyChangeListener(this);
95             fStore= null;
96         }
97         
98         update();
99     }
100     
101     /**
102      * Synchronizes the appearance of the editor with what the preference store indicates
103      *
104      * @param editor the text editor
105      */

106     private void synchronizeWithPreference(ITextEditor editor) {
107         
108         if (editor == null) {
109             return;
110         }
111         
112         boolean showSegments= fStore.getBoolean(AntEditorPreferenceConstants.EDITOR_SHOW_SEGMENTS);
113         setChecked(showSegments);
114         
115         if (editor.showsHighlightRangeOnly() != showSegments) {
116             IRegion remembered= editor.getHighlightRange();
117             editor.resetHighlightRange();
118             editor.showHighlightRangeOnly(showSegments);
119             if (remembered != null) {
120                 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
121             }
122         }
123     }
124
125     /* (non-Javadoc)
126      * @see org.eclipse.jface.util.IPropertyChangeListener#propertyChange(org.eclipse.jface.util.PropertyChangeEvent)
127      */

128     public void propertyChange(PropertyChangeEvent event) {
129         if (event.getProperty().equals(AntEditorPreferenceConstants.EDITOR_SHOW_SEGMENTS)) {
130             synchronizeWithPreference(getTextEditor());
131         }
132     }
133 }
134
Popular Tags