KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > 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.ant.internal.ui.editor.actions;
12
13
14 import org.eclipse.ant.internal.ui.AntUIImages;
15 import org.eclipse.ant.internal.ui.AntUIPlugin;
16 import org.eclipse.ant.internal.ui.IAntUIConstants;
17 import org.eclipse.ant.internal.ui.editor.AntEditor;
18 import org.eclipse.ant.internal.ui.preferences.AntEditorPreferenceConstants;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.util.IPropertyChangeListener;
22 import org.eclipse.jface.util.PropertyChangeEvent;
23 import org.eclipse.ui.texteditor.ITextEditor;
24 import org.eclipse.ui.texteditor.TextEditorAction;
25
26
27 /**
28  * A toolbar action which toggles the {@linkplain org.eclipse.ant.internal.ui.preferences#EDITOR_MARK_OCCURRENCES mark occurrences preference}.
29  *
30  * @since 3.1
31  */

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

39     public ToggleMarkOccurrencesAction() {
40         super(AntEditorActionMessages.getResourceBundle(), "ToggleMarkOccurrencesAction.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
41
setImageDescriptor(AntUIImages.getImageDescriptor(IAntUIConstants.IMG_MARK_OCCURRENCES));
42         setToolTipText(AntEditorActionMessages.getString("ToggleMarkOccurrencesAction.tooltip")); //$NON-NLS-1$
43
update();
44     }
45     
46     /*
47      * @see IAction#actionPerformed
48      */

49     public void run() {
50         fStore.setValue(AntEditorPreferenceConstants.EDITOR_MARK_OCCURRENCES, isChecked());
51     }
52     
53     /*
54      * @see TextEditorAction#update
55      */

56     public void update() {
57         ITextEditor editor= getTextEditor();
58         
59         boolean checked= false;
60         boolean enabled= false;
61         if (editor instanceof AntEditor) {
62             checked= ((AntEditor)editor).isMarkingOccurrences();
63             enabled= ((AntEditor)editor).getAntModel() != null;
64         }
65             
66         setChecked(checked);
67         setEnabled(enabled);
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= AntUIPlugin.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(AntEditorPreferenceConstants.EDITOR_MARK_OCCURRENCES))
97             setChecked(Boolean.valueOf(event.getNewValue().toString()).booleanValue());
98     }
99 }
Popular Tags