1 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 30 public class TogglePresentationAction extends TextEditorAction implements IPropertyChangeListener { 31 32 private IPreferenceStore fStore; 33 34 37 public TogglePresentationAction() { 38 super(AntEditorActionMessages.getResourceBundle(), "TogglePresentation.", null, IAction.AS_CHECK_BOX); setImageDescriptor(AntUIImages.getImageDescriptor(IAntUIConstants.IMG_SEGMENT_EDIT)); 40 } 41 42 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 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 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 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 128 public void propertyChange(PropertyChangeEvent event) { 129 if (event.getProperty().equals(AntEditorPreferenceConstants.EDITOR_SHOW_SEGMENTS)) { 130 synchronizeWithPreference(getTextEditor()); 131 } 132 } 133 } 134 | Popular Tags |