]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/javaeditor/TogglePresentationAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / javaeditor / TogglePresentationAction.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2000, 2011 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 *******************************************************************************/
11package org.eclipse.jdt.internal.ui.javaeditor;
12
13
14
15import org.eclipse.jface.action.IAction;
16import org.eclipse.jface.preference.IPreferenceStore;
17import org.eclipse.jface.util.IPropertyChangeListener;
18import org.eclipse.jface.util.PropertyChangeEvent;
19
20import org.eclipse.jface.text.IRegion;
21
22import org.eclipse.ui.IEditorInput;
23import org.eclipse.ui.PlatformUI;
24
25import org.eclipse.ui.texteditor.ITextEditor;
26import org.eclipse.ui.texteditor.TextEditorAction;
27
28import org.eclipse.jdt.core.IClassFile;
29
30import org.eclipse.jdt.ui.IWorkingCopyManager;
31import org.eclipse.jdt.ui.PreferenceConstants;
32
33import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
34import org.eclipse.jdt.internal.ui.JavaPlugin;
35import org.eclipse.jdt.internal.ui.JavaPluginImages;
36
37
38/**
39 * A tool bar action which toggles the presentation model of the
40 * connected text editor. The editor shows either the highlight range
41 * only or always the whole document.
42 */
43public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener {
44
45 private IPreferenceStore fStore;
46
47 /**
48 * Constructs and updates the action.
49 */
50 public TogglePresentationAction() {
51 super(JavaEditorMessages.getBundleForConstructedKeys(), "TogglePresentation.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
52 JavaPluginImages.setToolImageDescriptors(this, "segment_edit.gif"); //$NON-NLS-1$
53 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION);
54 update();
55 }
56
57 /*
58 * @see IAction#actionPerformed
59 */
60 @Override
61 public void run() {
62
63 ITextEditor editor= getTextEditor();
64 if (editor == null)
65 return;
66
67 IRegion remembered= editor.getHighlightRange();
68 editor.resetHighlightRange();
69
70 boolean showAll= !editor.showsHighlightRangeOnly();
71 setChecked(showAll);
72
73 editor.showHighlightRangeOnly(showAll);
74 if (remembered != null)
75 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
76
77 fStore.removePropertyChangeListener(this);
78 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
79 fStore.addPropertyChangeListener(this);
80 }
81
82 /*
83 * @see TextEditorAction#update
84 */
85 @Override
86 public void update() {
87 ITextEditor editor= getTextEditor();
88 boolean checked= (editor != null && editor.showsHighlightRangeOnly());
89 setChecked(checked);
90 if (editor instanceof CompilationUnitEditor) {
91 IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
92 setEnabled(manager.getWorkingCopy(editor.getEditorInput()) != null);
93 } else if (editor instanceof ClassFileEditor) {
94 IEditorInput input= editor.getEditorInput();
95 IClassFile cf= null;
96 if (input instanceof IClassFileEditorInput) {
97 IClassFileEditorInput cfi= (IClassFileEditorInput)input;
98 cf= cfi.getClassFile();
99 }
100 setEnabled(cf != null && cf.exists());
101 } else
102 setEnabled(editor != null);
103 }
104
105 /*
106 * @see TextEditorAction#setEditor(ITextEditor)
107 */
108 @Override
109 public void setEditor(ITextEditor editor) {
110
111 super.setEditor(editor);
112
113 if (editor != null) {
114
115 if (fStore == null) {
116 fStore= JavaPlugin.getDefault().getPreferenceStore();
117 fStore.addPropertyChangeListener(this);
118 }
119 synchronizeWithPreference(editor);
120
121 } else if (fStore != null) {
122 fStore.removePropertyChangeListener(this);
123 fStore= null;
124 }
125
126 update();
127 }
128
129 /**
130 * Synchronizes the appearance of the editor with what the preference store tells him.
131 *
132 * @param editor the text editor
133 */
134 private void synchronizeWithPreference(ITextEditor editor) {
135
136 if (editor == null)
137 return;
138
139 boolean showSegments= fStore.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
140 setChecked(showSegments);
141
142 if (editor.showsHighlightRangeOnly() != showSegments) {
143 IRegion remembered= editor.getHighlightRange();
144 editor.resetHighlightRange();
145 editor.showHighlightRangeOnly(showSegments);
146 if (remembered != null)
147 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
148 }
149 }
150
151 /*
152 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
153 */
154 public void propertyChange(PropertyChangeEvent event) {
155 if (event.getProperty().equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
156 synchronizeWithPreference(getTextEditor());
157 }
158}