001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.core.gui.toolbar;
017:
018: import java.awt.Insets;
019: import java.awt.event.ItemEvent;
020: import java.awt.event.ItemListener;
021: import java.beans.PropertyChangeListener;
022: import java.lang.reflect.Proxy;
023:
024: import javax.swing.Action;
025: import javax.swing.Icon;
026: import javax.swing.ImageIcon;
027: import javax.swing.JToggleButton;
028:
029: import org.columba.core.gui.action.AbstractSelectableAction;
030: import org.columba.core.gui.base.ButtonStateAdapter;
031: import org.columba.core.gui.base.ImageUtil;
032:
033: /**
034: * Customized JToogleButton for a Toolbar.
035: * <p>
036: * Adds an Observer to get notified when selection state changes
037: * from action.
038: * <p>
039: * Focus is disabled for toolbar buttons. ToggleButton should use
040: * small icons as default.
041: * <p>
042: *
043: * @author fdietz
044: */
045: public class ToggleToolbarButton extends JToggleButton {
046: /**
047: *
048: */
049: public ToggleToolbarButton() {
050: super ();
051: setRequestFocusEnabled(false);
052: }
053:
054: /**
055: * @param icon
056: */
057: public ToggleToolbarButton(Icon icon) {
058: super (icon);
059: setRequestFocusEnabled(false);
060: }
061:
062: /**
063: * @param action
064: */
065: public ToggleToolbarButton(AbstractSelectableAction action) {
066: super (action);
067: setRequestFocusEnabled(false);
068: setMargin(new Insets(1, 1, 1, 1));
069:
070: // no text!
071: setText("");
072:
073: ImageIcon icon = (ImageIcon) action.getValue(Action.SMALL_ICON);
074:
075: if (icon != null) {
076: setIcon(icon);
077:
078: // apply transparent icon
079: setDisabledIcon(ImageUtil
080: .createTransparentIcon((ImageIcon) icon));
081: }
082:
083: getModel().addItemListener(new ItemListener() {
084: public void itemStateChanged(ItemEvent e) {
085: AbstractSelectableAction a = (AbstractSelectableAction) getAction();
086:
087: if (a != null) {
088: a
089: .setState(e.getStateChange() == ItemEvent.SELECTED);
090: }
091: }
092: });
093: }
094:
095: public boolean isFocusTraversable() {
096: return isRequestFocusEnabled();
097: }
098:
099: /**
100: * Overridden to react to state changes of the underlying action.
101: */
102: protected PropertyChangeListener createActionPropertyChangeListener(
103: Action a) {
104: return (PropertyChangeListener) Proxy.newProxyInstance(
105: getClass().getClassLoader(),
106: new Class[] { PropertyChangeListener.class },
107: new ButtonStateAdapter(this , super
108: .createActionPropertyChangeListener(a)));
109: }
110:
111: /**
112: * Overridden to initialize selection state according to action
113: */
114: protected void configurePropertiesFromAction(Action a) {
115: super .configurePropertiesFromAction(a);
116: setSelected(((AbstractSelectableAction) a).getState());
117: }
118: }
|