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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.javaeditor;
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;
20 import org.eclipse.jface.text.IRegion;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.texteditor.ITextEditor;
26 import org.eclipse.ui.texteditor.TextEditorAction;
28 import org.eclipse.jdt.core.IClassFile;
30 import org.eclipse.jdt.ui.IWorkingCopyManager;
31 import org.eclipse.jdt.ui.PreferenceConstants;
33 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.JavaPluginImages;
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.
43 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener {
45 private IPreferenceStore fStore;
48 * Constructs and updates the action.
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);
58 * @see IAction#actionPerformed
63 ITextEditor editor= getTextEditor();
67 IRegion remembered= editor.getHighlightRange();
68 editor.resetHighlightRange();
70 boolean showAll= !editor.showsHighlightRangeOnly();
73 editor.showHighlightRangeOnly(showAll);
74 if (remembered != null)
75 editor.setHighlightRange(remembered.getOffset(), remembered.getLength(), true);
77 fStore.removePropertyChangeListener(this);
78 fStore.setValue(PreferenceConstants.EDITOR_SHOW_SEGMENTS, showAll);
79 fStore.addPropertyChangeListener(this);
83 * @see TextEditorAction#update
86 public void update() {
87 ITextEditor editor= getTextEditor();
88 boolean checked= (editor != null && editor.showsHighlightRangeOnly());
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();
96 if (input instanceof IClassFileEditorInput) {
97 IClassFileEditorInput cfi= (IClassFileEditorInput)input;
98 cf= cfi.getClassFile();
100 setEnabled(cf != null && cf.exists());
102 setEnabled(editor != null);
106 * @see TextEditorAction#setEditor(ITextEditor)
109 public void setEditor(ITextEditor editor) {
111 super.setEditor(editor);
113 if (editor != null) {
115 if (fStore == null) {
116 fStore= JavaPlugin.getDefault().getPreferenceStore();
117 fStore.addPropertyChangeListener(this);
119 synchronizeWithPreference(editor);
121 } else if (fStore != null) {
122 fStore.removePropertyChangeListener(this);
130 * Synchronizes the appearance of the editor with what the preference store tells him.
132 * @param editor the text editor
134 private void synchronizeWithPreference(ITextEditor editor) {
139 boolean showSegments= fStore.getBoolean(PreferenceConstants.EDITOR_SHOW_SEGMENTS);
140 setChecked(showSegments);
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);
152 * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
154 public void propertyChange(PropertyChangeEvent event) {
155 if (event.getProperty().equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS))
156 synchronizeWithPreference(getTextEditor());