KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > ToggleMarkOccurrencesAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.javaeditor;
12
13
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.texteditor.ITextEditor;
21 import org.eclipse.ui.texteditor.TextEditorAction;
22
23 import org.eclipse.jdt.ui.PreferenceConstants;
24
25 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
26 import org.eclipse.jdt.internal.ui.JavaPlugin;
27 import org.eclipse.jdt.internal.ui.JavaPluginImages;
28
29
30 /**
31  * A toolbar action which toggles the {@linkplain org.eclipse.jdt.ui.PreferenceConstants#EDITOR_MARK_OCCURRENCES mark occurrences preference}.
32  *
33  * @since 3.0
34  */

35 public class ToggleMarkOccurrencesAction extends TextEditorAction implements IPropertyChangeListener {
36
37     private IPreferenceStore fStore;
38
39     /**
40      * Constructs and updates the action.
41      */

42     public ToggleMarkOccurrencesAction() {
43         super(JavaEditorMessages.getBundleForConstructedKeys(), "ToggleMarkOccurrencesAction.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
44
JavaPluginImages.setToolImageDescriptors(this, "mark_occurrences.gif"); //$NON-NLS-1$
45
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.TOGGLE_MARK_OCCURRENCES_ACTION);
46         update();
47     }
48
49     /*
50      * @see IAction#actionPerformed
51      */

52     public void run() {
53         fStore.setValue(PreferenceConstants.EDITOR_MARK_OCCURRENCES, isChecked());
54     }
55
56     /*
57      * @see TextEditorAction#update
58      */

59     public void update() {
60         ITextEditor editor= getTextEditor();
61
62         boolean checked= false;
63         if (editor instanceof JavaEditor)
64             checked= ((JavaEditor)editor).isMarkingOccurrences();
65
66         setChecked(checked);
67         setEnabled(editor != null);
68     }
69
70     /*
71      * @see TextEditorAction#setEditor(ITextEditor)
72      */

73     public void setEditor(ITextEditor editor) {
74
75         super.setEditor(editor);
76
77         if (editor != null) {
78
79             if (fStore == null) {
80                 fStore= JavaPlugin.getDefault().getPreferenceStore();
81                 fStore.addPropertyChangeListener(this);
82             }
83
84         } else if (fStore != null) {
85             fStore.removePropertyChangeListener(this);
86             fStore= null;
87         }
88
89         update();
90     }
91
92     /*
93      * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
94      */

95     public void propertyChange(PropertyChangeEvent event) {
96         if (event.getProperty().equals(PreferenceConstants.EDITOR_MARK_OCCURRENCES))
97             setChecked(Boolean.valueOf(event.getNewValue().toString()).booleanValue());
98     }
99 }
100
Popular Tags