KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > ToggleAutoReconcileAction


1 /*******************************************************************************
2  * Copyright (c) 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 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.editor.AntEditor;
17 import org.eclipse.ant.internal.ui.model.AntModel;
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  * A toolbar action which toggles the {@linkplain org.eclipse.ant.internal.ui.preferences#EDITOR_RECONCILE preference}.
28  *
29  * @since 3.1
30  */

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

38     public ToggleAutoReconcileAction() {
39         super(AntEditorActionMessages.getResourceBundle(), "ToggleAutoReconcileAction.", null, IAction.AS_CHECK_BOX); //$NON-NLS-1$
40
setImageDescriptor(AntUIImages.getImageDescriptor(IAntUIConstants.IMG_REFRESH));
41         setToolTipText(AntEditorActionMessages.getString("ToggleAutoReconcileAction.tooltip")); //$NON-NLS-1$
42
update();
43     }
44     
45     /* (non-Javadoc)
46      * @see org.eclipse.jface.action.IAction#run()
47      */

48     public void run() {
49         ITextEditor editor= getTextEditor();
50         if (editor instanceof AntEditor) {
51             AntModel model= ((AntEditor) editor).getAntModel();
52             model.setShouldReconcile(isChecked());
53             fStore.setValue(AntEditorPreferenceConstants.EDITOR_RECONCILE, isChecked());
54         }
55     }
56     
57     /* (non-Javadoc)
58      * @see org.eclipse.ui.texteditor.IUpdate#update()
59      */

60     public void update() {
61         ITextEditor editor= getTextEditor();
62         
63         boolean checked= false;
64         boolean enabled= false;
65         if (editor instanceof AntEditor) {
66             AntModel model= ((AntEditor)editor).getAntModel();
67             enabled= model != null;
68             checked= enabled && fStore.getBoolean(AntEditorPreferenceConstants.EDITOR_RECONCILE);
69             if (model != null) {
70                 model.setShouldReconcile(checked);
71             }
72         }
73             
74         setChecked(checked);
75         setEnabled(enabled);
76     }
77     
78     /*
79      * @see TextEditorAction#setEditor(ITextEditor)
80      */

81     public void setEditor(ITextEditor editor) {
82         
83         super.setEditor(editor);
84         
85         if (editor != null) {
86             if (fStore == null) {
87                 fStore= AntUIPlugin.getDefault().getPreferenceStore();
88                 fStore.addPropertyChangeListener(this);
89             }
90         } else if (fStore != null) {
91             fStore.removePropertyChangeListener(this);
92             fStore= null;
93         }
94         
95         update();
96     }
97     
98     /*
99      * @see IPropertyChangeListener#propertyChange(PropertyChangeEvent)
100      */

101     public void propertyChange(PropertyChangeEvent event) {
102         if (event.getProperty().equals(AntEditorPreferenceConstants.EDITOR_RECONCILE)) {
103             setChecked(Boolean.valueOf(event.getNewValue().toString()).booleanValue());
104         }
105     }
106 }
Popular Tags