KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > ShowWhitespaceCharactersAction


1 /*******************************************************************************
2  * Copyright (c) 2006 Wind River Systems, Inc. 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  * Anton Leherbauer (Wind River Systems) - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.texteditor;
12
13 import java.util.ResourceBundle JavaDoc;
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 /**
27  * This action toggles the display of whitespace characters by
28  * attaching/detaching an {@link WhitespaceCharacterPainter} to the
29  * associated text editor.
30  * <p>
31  * <strong>Note:</strong> Currently this action only works if the given
32  * editor inherits from {@link AbstractTextEditor}.
33  * </p>
34  *
35  * @since 3.3
36  */

37 public class ShowWhitespaceCharactersAction extends TextEditorAction {
38
39     /** The preference store. */
40     private IPreferenceStore fStore;
41     /** The painter. */
42     private IPainter fWhitespaceCharPainter;
43     
44     /**
45      * Construct the action and initialize its state.
46      *
47      * @param resourceBundle the resource bundle to construct label and tooltip from
48      * @param prefix the prefix to use for constructing resource bundle keys
49      * @param editor the editor this action is associated with
50      * @param store the preference store (may be <code>null</code>)
51      */

52     public ShowWhitespaceCharactersAction(ResourceBundle JavaDoc resourceBundle, String JavaDoc prefix, ITextEditor editor, IPreferenceStore store) {
53         super(resourceBundle, prefix, editor, IAction.AS_CHECK_BOX);
54         fStore= store;
55         synchronizeWithPreference();
56     }
57
58     /**
59      * Sets the preference store of this action.
60      *
61      * @param store the preference store
62      */

63     public void setPreferenceStore(IPreferenceStore store) {
64         fStore= store;
65         synchronizeWithPreference();
66     }
67
68     /*
69      * @see org.eclipse.jface.action.Action#run()
70      */

71     public void run() {
72         togglePainterState(isChecked());
73         if (fStore != null)
74             fStore.setValue(AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS, isChecked());
75     }
76
77     /*
78      * @see org.eclipse.ui.texteditor.TextEditorAction#update()
79      */

80     public void update() {
81         setEnabled(getTextViewer() instanceof ITextViewerExtension2);
82         synchronizeWithPreference();
83     }
84
85     /**
86      * Installs the painter on the editor.
87      */

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     /**
99      * Remove the painter from the current editor.
100      */

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     /**
114      * Get the <code>ITextViewer</code> from an <code>ITextEditor</code>.
115      *
116      * @return the text viewer or <code>null</code>
117      */

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     /**
127      * Synchronize state with the preference.
128      */

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     /**
141      * Toggles the painter state.
142      *
143      * @param newState <code>true</code> if the painter should be installed
144      */

145     private void togglePainterState(boolean newState) {
146         if (newState)
147             installPainter();
148         else
149             uninstallPainter();
150     }
151 }
152
Popular Tags