1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ResourceBundle ; 14 15 import org.eclipse.core.runtime.Assert; 16 17 import org.eclipse.jface.action.IAction; 18 import org.eclipse.jface.preference.IPreferenceStore; 19 20 import org.eclipse.jface.text.IPainter; 21 import org.eclipse.jface.text.ITextViewer; 22 import org.eclipse.jface.text.ITextViewerExtension2; 23 import org.eclipse.jface.text.WhitespaceCharacterPainter; 24 25 26 37 public class ShowWhitespaceCharactersAction extends TextEditorAction { 38 39 40 private IPreferenceStore fStore; 41 42 private IPainter fWhitespaceCharPainter; 43 44 52 public ShowWhitespaceCharactersAction(ResourceBundle resourceBundle, String prefix, ITextEditor editor, IPreferenceStore store) { 53 super(resourceBundle, prefix, editor, IAction.AS_CHECK_BOX); 54 fStore= store; 55 synchronizeWithPreference(); 56 } 57 58 63 public void setPreferenceStore(IPreferenceStore store) { 64 fStore= store; 65 synchronizeWithPreference(); 66 } 67 68 71 public void run() { 72 togglePainterState(isChecked()); 73 if (fStore != null) 74 fStore.setValue(AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS, isChecked()); 75 } 76 77 80 public void update() { 81 setEnabled(getTextViewer() instanceof ITextViewerExtension2); 82 synchronizeWithPreference(); 83 } 84 85 88 private void installPainter() { 89 Assert.isTrue(fWhitespaceCharPainter == null); 90 91 ITextViewer viewer= getTextViewer(); 92 if (viewer instanceof ITextViewerExtension2) { 93 fWhitespaceCharPainter= new WhitespaceCharacterPainter(viewer); 94 ((ITextViewerExtension2)viewer).addPainter(fWhitespaceCharPainter); 95 } 96 } 97 98 101 private void uninstallPainter() { 102 if (fWhitespaceCharPainter == null) 103 return; 104 105 ITextViewer viewer= getTextViewer(); 106 if (viewer instanceof ITextViewerExtension2) 107 ((ITextViewerExtension2)viewer).removePainter(fWhitespaceCharPainter); 108 109 fWhitespaceCharPainter.deactivate(true); 110 fWhitespaceCharPainter= null; 111 } 112 113 118 private ITextViewer getTextViewer() { 119 ITextEditor editor= getTextEditor(); 120 if (editor instanceof AbstractTextEditor) 121 return ((AbstractTextEditor)editor).getSourceViewer(); 122 123 return null; 124 } 125 126 129 private void synchronizeWithPreference() { 130 boolean checked= false; 131 if (fStore != null) 132 checked= fStore.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS); 133 134 if (checked != isChecked()) { 135 setChecked(checked); 136 togglePainterState(checked); 137 } 138 } 139 140 145 private void togglePainterState(boolean newState) { 146 if (newState) 147 installPainter(); 148 else 149 uninstallPainter(); 150 } 151 } 152 | Popular Tags |