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.ActionEvent;
19  
20  import javax.swing.AbstractAction;
21  
22  import net.sf.jguiraffe.gui.builder.action.ActionHelper;
23  import net.sf.jguiraffe.gui.builder.action.FormAction;
24  import net.sf.jguiraffe.gui.builder.event.BuilderEvent;
25  import net.sf.jguiraffe.gui.builder.event.FormActionEvent;
26  
27  /**
28   * <p>
29   * A Swing specific implementation of the <code>FormAction</code> interface.
30   * </p>
31   * <p>
32   * This class serves as an adapter between the generic <code>FormAction</code>
33   * interface and Swing specific actions.
34   * </p>
35   *
36   * @author Oliver Heger
37   * @version $Id: SwingFormAction.java 205 2012-01-29 18:29:57Z oheger $
38   */
39  public class SwingFormAction extends AbstractAction implements FormAction
40  {
41      /** Constant for the CHECKED property. */
42      public static final String CHECKED = "CHECKED";
43  
44      /**
45       * The serial version UID.
46       */
47      private static final long serialVersionUID = 2180796112140195669L;
48  
49      /** Stores the task to execute when the action is triggered. */
50      private Object task;
51  
52      /** Stores the name of this action. */
53      private String name;
54  
55      /**
56       * Creates a new instance of <code>SwingFormAction</code> and initializes
57       * it. The task is checked (using <code>{@link ActionHelper}</code>)
58       * whether it is of an allowed type.
59       *
60       * @param aName the name of the action
61       * @param aTask the task with the executable code
62       * @throws IllegalArgumentException if the name is <b>null</b> or the task
63       * is invalid
64       */
65      public SwingFormAction(String aName, Object aTask)
66      {
67          if (aName == null)
68          {
69              throw new IllegalArgumentException("Action name must be provided!");
70          }
71  
72          setTask(aTask);
73          name = aName;
74      }
75  
76      /**
77       * Returns the name of this action.
78       *
79       * @return the action's name
80       */
81      public String getName()
82      {
83          return name;
84      }
85  
86      /**
87       * Returns the value of the <code>checked</code> property.
88       *
89       * @return the <code>checked</code> property.
90       */
91      public boolean isChecked()
92      {
93          Boolean value = (Boolean) getValue(CHECKED);
94          return (value == null) ? false : value.booleanValue();
95      }
96  
97      /**
98       * Sets the value of the <code>checked</code> property. This property is
99       * used for checked menu items or toggle buttons in toolbars.
100      *
101      * @param f the value of the property
102      */
103     public void setChecked(boolean f)
104     {
105         putValue(CHECKED, f ? Boolean.TRUE : Boolean.FALSE);
106     }
107 
108     /**
109      * Returns the task of this action.
110      *
111      * @return the task
112      */
113     public Object getTask()
114     {
115         return task;
116     }
117 
118     /**
119      * Sets the task of this action. The passed in object must be supported by
120      * this action. To check this, <code>{@link ActionHelper}</code> is used.
121      *
122      * @param task the new task
123      * @throws IllegalArgumentException if the task object is not allowed
124      */
125     public void setTask(Object task)
126     {
127         ActionHelper.checkActionTask(task);
128         this.task = task;
129     }
130 
131     /**
132      * Executes this action. This method delegates the call to the internally
133      * stored task object. Invocation of this task is delegated to the
134      * <code>{@link ActionHelper}</code> class.
135      *
136      * @param event the causing event
137      */
138     public void execute(BuilderEvent event)
139     {
140         ActionHelper.invokeActionTask(getTask(), this, event);
141     }
142 
143     /**
144      * Callback method for action events. This method is called when the
145      * associated action is triggered. It delegates the call to the internal
146      * task object.
147      *
148      * @param event the action event
149      */
150     public void actionPerformed(ActionEvent event)
151     {
152         execute(new FormActionEvent(event, null, null, event.getActionCommand()));
153     }
154 }