KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > ui > actions > DefaultToggleAction


1 /*******************************************************************************
2  * Copyright (c) 2006 Andrei Loskutov.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the BSD License
5  * which accompanies this distribution, and is available at
6  * http://www.opensource.org/licenses/bsd-license.php
7  * Contributor: Andrei Loskutov - initial API and implementation
8  *******************************************************************************/

9 package de.loskutov.bco.ui.actions;
10
11 import org.eclipse.jface.action.Action;
12 import org.eclipse.jface.preference.IPreferenceStore;
13 import org.eclipse.jface.util.IPropertyChangeListener;
14 import org.eclipse.ui.plugin.AbstractUIPlugin;
15
16 import de.loskutov.bco.BytecodeOutlinePlugin;
17
18
19 /**
20  * Default action which could be used as template for "toggle" action.
21  * Action image, text and tooltip will be initialized by default.
22  * To use it, register IPropertyChangeListener and check for IAction.CHECKED
23  * event name.
24  * @author Andrei
25  */

26 public class DefaultToggleAction extends Action {
27
28     private static final String JavaDoc ACTION = "action";
29     private boolean isChecked;
30
31     public DefaultToggleAction(String JavaDoc id, IPropertyChangeListener listener) {
32         super();
33         setId(id);
34         init();
35
36         IPreferenceStore store = BytecodeOutlinePlugin.getDefault().getPreferenceStore();
37
38         isChecked = store.getBoolean(id);
39         setChecked(isChecked);
40         // as last action, after setChecked(), to prevent unexpected listener
41
// events during initialization
42
addPropertyChangeListener(listener);
43     }
44
45     private void init(){
46         setImageDescriptor(AbstractUIPlugin
47             .imageDescriptorFromPlugin(
48                 BytecodeOutlinePlugin.getDefault().getBundle()
49                     .getSymbolicName(),
50                 BytecodeOutlinePlugin
51                     .getResourceString(ACTION + "." + getId() + "." + IMAGE)));
52
53         setText(BytecodeOutlinePlugin
54             .getResourceString(ACTION + "." + getId() + "." + TEXT));
55         setToolTipText(BytecodeOutlinePlugin
56             .getResourceString(ACTION + "." + getId() + "." + TOOL_TIP_TEXT));
57     }
58
59     /**
60      * @see org.eclipse.jface.action.IAction#run()
61      */

62     public void run() {
63         isChecked = !isChecked;
64         setChecked(isChecked);
65     }
66 }
67
Popular Tags