View Javadoc

1   /*
2    * Copyright 2006-2016 The JGUIraffe Team.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.jguiraffe.gui.platform.swing.builder.components;
17  
18  import java.awt.event.ActionListener;
19  import java.awt.event.ItemEvent;
20  import java.awt.event.ItemListener;
21  
22  import javax.swing.AbstractButton;
23  
24  /**
25   * <p>
26   * A specific Swing component handler implementation that deals with several
27   * kinds of button like components.
28   * </p>
29   * <p>
30   * This component handler class handles buttons (command and toggle buttons),
31   * check boxes and radio buttons. Data type is boolean, i.e. a flag whether the
32   * button is selected. All type of event handlers are supported.
33   * </p>
34   *
35   * @author Oliver Heger
36   * @version $Id: SwingButtonHandler.java 205 2012-01-29 18:29:57Z oheger $
37   */
38  public class SwingButtonHandler extends SwingComponentHandler<Boolean> implements
39          ItemListener
40  {
41      /**
42       * Creates a new instance of <code>SwingButtonHandler</code>.
43       *
44       * @param button the button to handle
45       */
46      public SwingButtonHandler(AbstractButton button)
47      {
48          super(button);
49      }
50  
51      /**
52       * Returns the managed button.
53       *
54       * @return the managed button object
55       */
56      public AbstractButton getButton()
57      {
58          return (AbstractButton) getComponent();
59      }
60  
61      /**
62       * Returns the component's data.
63       *
64       * @return the component's data
65       */
66      public Boolean getData()
67      {
68          return (getButton().isSelected()) ? Boolean.TRUE : Boolean.FALSE;
69      }
70  
71      /**
72       * Sets the component's data. This must be an object of type boolean.
73       *
74       * @param data the component's data.
75       */
76      public void setData(Boolean data)
77      {
78          boolean value;
79  
80          if (data == null)
81          {
82              value = false;
83          }
84          else
85          {
86              value = data.booleanValue();
87          }
88  
89          getButton().setSelected(value);
90      }
91  
92      /**
93       * Returns the component's data type. This is boolean.
94       *
95       * @return the data type
96       */
97      public Class<?> getType()
98      {
99          return Boolean.TYPE;
100     }
101 
102     /**
103      * Adds an action listener at this component.
104      *
105      * @param l the listener to register
106      */
107     @Override
108     public void addActionListener(ActionListener l)
109     {
110         getButton().addActionListener(l);
111     }
112 
113     /**
114      * Removes an action listener from this component.
115      *
116      * @param l the listener to be removed
117      */
118     @Override
119     public void removeActionListener(ActionListener l)
120     {
121         getButton().removeActionListener(l);
122     }
123 
124     /**
125      * Registers this object as change listener at the managed button.
126      */
127     @Override
128     protected void registerChangeListener()
129     {
130         getButton().addItemListener(this);
131     }
132 
133     /**
134      * Unregisters this object as change listener at the managed button.
135      */
136     @Override
137     protected void unregisterChangeListener()
138     {
139         getButton().removeItemListener(this);
140     }
141 
142     /**
143      * Callback for item events. Occurring events are routed to the registered
144      * change listeners.
145      *
146      * @param event the event
147      */
148     public void itemStateChanged(ItemEvent event)
149     {
150         fireChangeEvent(event);
151     }
152 }