KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > toolbar > ToggleToolbarButton


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.toolbar;
17
18 import java.awt.Insets JavaDoc;
19 import java.awt.event.ItemEvent JavaDoc;
20 import java.awt.event.ItemListener JavaDoc;
21 import java.beans.PropertyChangeListener JavaDoc;
22 import java.lang.reflect.Proxy JavaDoc;
23
24 import javax.swing.Action JavaDoc;
25 import javax.swing.Icon JavaDoc;
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JToggleButton JavaDoc;
28
29 import org.columba.core.gui.action.AbstractSelectableAction;
30 import org.columba.core.gui.base.ButtonStateAdapter;
31 import org.columba.core.gui.base.ImageUtil;
32
33
34 /**
35  * Customized JToogleButton for a Toolbar.
36  * <p>
37  * Adds an Observer to get notified when selection state changes
38  * from action.
39  * <p>
40  * Focus is disabled for toolbar buttons. ToggleButton should use
41  * small icons as default.
42  * <p>
43  *
44  * @author fdietz
45  */

46 public class ToggleToolbarButton extends JToggleButton JavaDoc {
47     /**
48      *
49      */

50     public ToggleToolbarButton() {
51         super();
52         setRequestFocusEnabled(false);
53     }
54
55     /**
56      * @param icon
57      */

58     public ToggleToolbarButton(Icon JavaDoc icon) {
59         super(icon);
60         setRequestFocusEnabled(false);
61     }
62
63     /**
64      * @param action
65      */

66     public ToggleToolbarButton(AbstractSelectableAction action) {
67         super(action);
68         setRequestFocusEnabled(false);
69         setMargin(new Insets JavaDoc(1, 1, 1, 1));
70
71         // no text!
72
setText("");
73
74         ImageIcon JavaDoc icon = (ImageIcon JavaDoc) action.getValue(Action.SMALL_ICON);
75
76         if (icon != null) {
77             setIcon(icon);
78
79             // apply transparent icon
80
setDisabledIcon(ImageUtil.createTransparentIcon((ImageIcon JavaDoc) icon));
81         }
82
83         getModel().addItemListener(new ItemListener JavaDoc() {
84                 public void itemStateChanged(ItemEvent JavaDoc e) {
85                     AbstractSelectableAction a = (AbstractSelectableAction) getAction();
86
87                     if (a != null) {
88                         a.setState(e.getStateChange() == ItemEvent.SELECTED);
89                     }
90                 }
91             });
92     }
93
94     public boolean isFocusTraversable() {
95         return isRequestFocusEnabled();
96     }
97
98     /**
99      * Overridden to react to state changes of the underlying action.
100      */

101     protected PropertyChangeListener JavaDoc createActionPropertyChangeListener(
102         Action JavaDoc a) {
103         return (PropertyChangeListener JavaDoc) Proxy.newProxyInstance(getClass()
104                                                                    .getClassLoader(),
105             new Class JavaDoc[] { PropertyChangeListener JavaDoc.class },
106             new ButtonStateAdapter(this,
107                 super.createActionPropertyChangeListener(a)));
108     }
109
110     /**
111      * Overridden to initialize selection state according to action
112      */

113     protected void configurePropertiesFromAction(Action JavaDoc a) {
114         super.configurePropertiesFromAction(a);
115         setSelected(((AbstractSelectableAction) a).getState());
116     }
117 }
118
Popular Tags