View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.util;
21  
22  import java.awt.event.ActionEvent;
23  
24  import javax.swing.AbstractAction;
25  import javax.swing.Icon;
26  
27  import com.cosylab.util.Debug;
28  
29  
30  /**
31   * An extension of the <code>javax.swing.Action</code> interface which provides
32   * <code>selected</code> bound property. It can be used to specify an action
33   * which switches between two different states. GUI components will render
34   * these actions with check boxes or toggle buttons.
35   *
36   * <p>
37   * To use this action, either instantiate this class and register as a
38   * propertyChangeListener, responding to events that denote a change in
39   * "selected" property, or extend this class, overriding actionPerformed()
40   * method.
41   * </p>
42   *
43   * @author <a href="mailto:miha.kadunc@cosylab.com">Miha Kadunc</a>
44   * @version $id$
45   */
46  public class ToggleAction extends AbstractAction {
47  
48  	private static final long serialVersionUID = 1L;
49  	/** Icon used when the action is selected */
50      public static final String SMALL_SELECTED_ICON = "SmallSelectedIcon";
51      /** Large icon used when the action is selected */
52      public static final String LARGE_SELECTED_ICON = "LargeSelectedIcon";
53      private boolean selected = false;
54      private boolean internal = false;
55  
56      /**
57       * Constructor with default values.
58       */
59      public ToggleAction() {
60          super();
61      }
62  
63      /**
64       * Constructor with name.
65       *
66       * @param name the name of the action
67       */
68      public ToggleAction(String name) {
69          super(name);
70      }
71  
72      /**
73       * Constructor with name and selection flag.
74       *
75       * @param name the name of the action
76       * @param selected the selection flag
77       */
78      public ToggleAction(String name, boolean selected) {
79          super(name);
80          this.selected = selected;
81      }
82  
83      /**
84       * Contructor with name and icon.
85       *
86       * @param name
87       * @param icon
88       */
89      public ToggleAction(String name, Icon icon) {
90          super(name, icon);
91      }
92  
93      /**
94       * Contructor with name, icon and selection flag.
95       *
96       * @param name the name of action
97       * @param icon the icon to be used GUI element
98       * @param selected the selection flag
99       */
100     public ToggleAction(String name, Icon icon, boolean selected) {
101         super(name, icon);
102         this.selected = selected;
103     }
104 
105     /**
106      * Sets the <code>selected</code> property. Fires propertyChange events and
107      * calls actionPerformed() method with <code>this</code> as event source.
108      *
109      * @param selected
110      */
111     public void setSelected(boolean selected) {
112         if (this.selected != selected) {
113             if (!internal) {
114                 actionPerformed(new ActionEvent(this, selected ? 1 : 0,
115                         "selection_changed"));
116             } else {
117                 internal = false;
118                 this.selected = selected;
119                 selectionChanged(selected);
120                 firePropertyChange("selected", new Boolean(!selected),
121                     new Boolean(selected));
122             }
123         }
124     }
125 
126     /**
127      * @return whether this toggle action is selected.
128      */
129     public boolean isSelected() {
130         return selected;
131     }
132 
133     /**
134      * Overriden to modify selection status. Called by the GUI when invoked.
135      */
136     public void actionPerformed(ActionEvent e) {
137         internal = true;
138         setSelected(!selected);
139     }
140 
141     /**
142      * Override this method to provide custom event handling when selection
143      * changes.
144      *
145      * @param newSelected
146      */
147     protected void selectionChanged(boolean selected) {
148     	Debug.out(""+selected);
149     }
150 }
151 
152 
153 /* __oOo__ */