1 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 42 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener { 43 44 private IPreferenceStore fStore; 45 46 49 public TogglePresentationAction() { 50 super(JavaEditorMessages.getBundleForConstructedKeys(), "TogglePresentation.", null, IAction.AS_CHECK_BOX); JavaPluginImages.setToolImageDescriptors(this, "segment_edit.gif"); PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.TOGGLE_PRESENTATION_ACTION); 53 update(); 54 } 55 56 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 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 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 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 150 public void propertyChange(PropertyChangeEvent event) { 151 if (event.getProperty().equals(PreferenceConstants.EDITOR_SHOW_SEGMENTS)) 152 synchronizeWithPreference(getTextEditor()); 153 } 154 } 155 | Popular Tags |