KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > action > CCheckBoxMenuItem


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.gui.action;
17
18 import java.awt.event.ItemEvent JavaDoc;
19 import java.awt.event.ItemListener JavaDoc;
20 import java.beans.PropertyChangeListener JavaDoc;
21 import java.lang.reflect.Proxy JavaDoc;
22
23 import javax.swing.Action JavaDoc;
24 import javax.swing.JCheckBoxMenuItem JavaDoc;
25
26 import org.columba.core.gui.base.ButtonStateAdapter;
27 import org.columba.core.gui.base.MnemonicSetter;
28
29
30 /**
31  * Adds an Observer to JCheckBoxMenuItem in order to make it possible
32  * to receive selection state changes from its underlying action.
33  *
34  * @author fdietz
35  */

36
37 public class CCheckBoxMenuItem extends JCheckBoxMenuItem JavaDoc {
38     /**
39      * default constructor
40      */

41     public CCheckBoxMenuItem() {
42         super();
43     }
44
45     /**
46      * Creates a checkbox menu item with a given action attached.
47      * <br>
48      * If the name of the action contains &, the next character is used as
49      * mnemonic. If not, the fall-back solution is to use default behaviour,
50      * i.e. the mnemonic defined using setMnemonic on the action.
51      *
52      * @param action The action to attach to the menu item
53      */

54     public CCheckBoxMenuItem(AbstractSelectableAction action) {
55         super(action);
56
57         // Set text, possibly with a mnemonic if defined using &
58
MnemonicSetter.setTextWithMnemonic(this,
59             (String JavaDoc) action.getValue(Action.NAME));
60         getModel().addItemListener(new ItemListener JavaDoc() {
61                 public void itemStateChanged(ItemEvent JavaDoc e) {
62                     AbstractSelectableAction a = (AbstractSelectableAction) getAction();
63
64                     if (a != null) {
65                         a.setState(e.getStateChange() == ItemEvent.SELECTED);
66                     }
67                 }
68             });
69     }
70
71     /**
72      * Overridden to react to state changes of the underlying action.
73      */

74     protected PropertyChangeListener JavaDoc createActionPropertyChangeListener(
75         Action JavaDoc a) {
76         return (PropertyChangeListener JavaDoc) Proxy.newProxyInstance(getClass()
77                                                                    .getClassLoader(),
78             new Class JavaDoc[] { PropertyChangeListener JavaDoc.class },
79             new ButtonStateAdapter(this,
80                 super.createActionPropertyChangeListener(a)));
81     }
82
83     /**
84      * Overridden to initialize selection state according to action
85      */

86     protected void configurePropertiesFromAction(Action JavaDoc a) {
87         super.configurePropertiesFromAction(a);
88         setSelected(((AbstractSelectableAction) a).getState());
89     }
90 }
91
Popular Tags