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.action;
17  
18  import java.awt.event.ItemEvent;
19  import java.awt.event.ItemListener;
20  import java.beans.PropertyChangeEvent;
21  import java.beans.PropertyChangeListener;
22  
23  import javax.swing.AbstractButton;
24  
25  /**
26   * <p>
27   * A helper class used for controlling the <code>checked</code> property of
28   * action controls.
29   * </p>
30   * <p>
31   * Swing does not directly support the <code>checked</code> property for
32   * actions that is required by the action builder framework. Here it is possible
33   * to create checked menu items or toolbar buttons whose <code>checked</code>
34   * state is connected to the corresponding action. This class establishes such a
35   * connection. It registers itself as a <code>PropertyChangeListener</code> at
36   * a Swing action. Whenever the value of the <code>checked</code> property
37   * changes, the associated control is updated, too. The other direction is also
38   * supported: If the <code>checked</code> state of a control is changed, the
39   * action will be notified.
40   * </p>
41   *
42   * @author Oliver Heger
43   * @version $Id: SwingCheckedItemController.java 205 2012-01-29 18:29:57Z oheger $
44   */
45  class SwingCheckedItemController implements PropertyChangeListener,
46          ItemListener
47  {
48      /** Stores a reference to the component to control. */
49      private AbstractButton button;
50  
51      /** Stores a reference to the action object. */
52      private SwingFormAction action;
53  
54      /**
55       * Creates a new instance of <code>SwingCheckedItemController</code> and
56       * initializes it.
57       *
58       * @param formAction the action that must be associated with the control
59       * @param aButton the button controlled by this object
60       */
61      public SwingCheckedItemController(SwingFormAction formAction,
62              AbstractButton aButton)
63      {
64          button = aButton;
65          action = formAction;
66          button.addItemListener(this);
67          action.addPropertyChangeListener(this);
68      }
69  
70      /**
71       * Returns the wrapped button component.
72       *
73       * @return the button
74       */
75      public AbstractButton getButton()
76      {
77          return button;
78      }
79  
80      /**
81       * Returns the wrapped Swing form action object.
82       *
83       * @return the action
84       */
85      public SwingFormAction getAction()
86      {
87          return action;
88      }
89  
90      /**
91       * Reacts on property change events of the associated action. If the
92       * <code>checked</code> property is affected, the button's state is
93       * updated.
94       *
95       * @param event the change event
96       */
97      public void propertyChange(PropertyChangeEvent event)
98      {
99          if (SwingFormAction.CHECKED.equals(event.getPropertyName()))
100         {
101             boolean newValue = ((Boolean) event.getNewValue()).booleanValue();
102             if (getButton().isSelected() != newValue)
103             {
104                 getButton().setSelected(newValue);
105             } /* if */
106         } /* if */
107     }
108 
109     /**
110      * Reacts on state changes of the associated button. Ensures that the action
111      * will be updated if necessary.
112      *
113      * @param event the item event
114      */
115     public void itemStateChanged(ItemEvent event)
116     {
117         boolean checked = event.getStateChange() == ItemEvent.SELECTED;
118         if (checked != getAction().isChecked())
119         {
120             getAction().setChecked(checked);
121         } /* if */
122     }
123 }