KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > 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.jdt.internal.ui.javaeditor;
12
13
14
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19
20 import org.eclipse.jface.text.IRegion;
21
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.texteditor.ITextEditor;
25 import org.eclipse.ui.texteditor.TextEditorAction;
26
27 import org.eclipse.jdt.core.IClassFile;
28
29 import org.eclipse.jdt.ui.IWorkingCopyManager;
30 import org.eclipse.jdt.ui.PreferenceConstants;
31
32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.JavaPluginImages;
35
36
37 /**
38  * A tool bar action which toggles the presentation model of the
39  * connected text editor. The editor shows either the highlight range
40  * only or always the whole document.
41  */

42 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener {
43
44     private IPreferenceStore fStore;
45
46     /**
47      * Constructs and updates the action.
48      */

49     public TogglePresentationAction() {
50         super(JavaEditorMessages.getBundleForConstructedKeys(), "TogglePresentation.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
51
JavaPluginImages.setToolImageDescriptors(this, "segment_edit.gif"); //$NON-NLS-1$
52
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION);
53         update();
54     }
55
56     /*
57      * @see IAction#actionPerformed
58      */

59     public void run() {
60
61         ITextEditor editor= getTextEditor();
62         if (editor == null)
63             return;
64
65         IRegion remembered= editor.getHighlightRange();
66         editor.resetHighlightRange();
67
68         boolean showAll= !editor.showsHighlightRangeOnly();
69         setChecked(showAll);
70
71         editor.showHighlightRangeOnly(showAll);
72         if (remembered != null)
73             editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
74
75         fStore.removePropertyChangeListener(this);
76         fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
77         fStore.addPropertyChangeListener(this);
78     }
79
80     /*
81      * @see TextEditorAction#update
82      */

83     public void update() {
84         ITextEditor editor= getTextEditor();
85         boolean checked= (editor != null && editor.showsHighlightRangeOnly());
86         setChecked(checked);
87         if (editor instanceof CompilationUnitEditor) {
88             IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
89             setEnabled(manager.getWorkingCopy(editor.getEditorInput()) != null);
90         } else if (editor instanceof ClassFileEditor) {
91             IEditorInput input= editor.getEditorInput();
92             IClassFile cf= null;
93             if (input instanceof IClassFileEditorInput) {
94                 IClassFileEditorInput cfi= (IClassFileEditorInput)input;
95                 cf= cfi.getClassFile();
96             }
97             setEnabled(cf != null && cf.exists());
98         } else
99             setEnabled(editor != null);
100     }
101
102     /*
103      * @see TextEditorAction#setEditor(ITextEditor)
104      */

105     public void setEditor(ITextEditor editor) {
106
107         super.setEditor(editor);
108
109         if (editor != null) {
110
111             if (fStore == null) {
112                 fStore= JavaPlugin.getDefault().getPreferenceStore();
113                 fStore.addPropertyChangeListener(this);
114             }
115             synchronizeWithPreference(editor);
116
117         } else if (fStore != null) {
118             fStore.removePropertyChangeListener(this);
119             fStore= null;
120         }
121
122         update();
123     }
124
125     /**
126      * Synchronizes the appearance of the editor with what the preference store tells him.
127      *
128      * @param editor the text editor
129      */

130     private void synchronizeWithPreference(ITextEditor editor) {
131
132         if (editor == null)
133             return;
134
135         boolean showSegments= fStore.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
136         setChecked(showSegments);
137
138         if (editor.showsHighlightRangeOnly() != showSegments) {
139             IRegion remembered= editor.getHighlightRange();
140             editor.resetHighlightRange();
141             editor.showHighlightRangeOnly(showSegments);
142             if (remembered != null)
143                 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
144         }
145     }
146
147     /*
148      * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
149      */

150     public void propertyChange(PropertyChangeEvent event) {
151         if (event.getProperty().equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
152             synchronizeWithPreference(getTextEditor());
153     }
154 }
155
Popular Tags