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.builder.action;
17
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23
24 import net.sf.jguiraffe.gui.builder.event.BuilderEvent;
25
26 /**
27 * <p>
28 * A helper class for dealing with actions.
29 * </p>
30 * <p>
31 * This class provides some utility methods that can be used when working with
32 * actions and their tasks.
33 * </p>
34 *
35 * @author Oliver Heger
36 * @version $Id: ActionHelper.java 205 2012-01-29 18:29:57Z oheger $
37 */
38 public final class ActionHelper
39 {
40 /**
41 * Private constructor. No instances can be created. All utility methods are
42 * static.
43 */
44 private ActionHelper()
45 {
46 }
47
48 /**
49 * Returns a flag whether the specified object is a valid task for an
50 * action. This method tests whether the object can be casted to a type
51 * supported by actions.
52 *
53 * @param task the task object to be inspected
54 * @return a flag whether this is an allowed task for an action
55 */
56 public static boolean isValidActionTask(Object task)
57 {
58 return task instanceof ActionTask || task instanceof Runnable;
59 }
60
61 /**
62 * Tests whether the specified object is a valid task for an action. This
63 * method delegates to <code>isValidActionTask()</code>. If the result is
64 * <b>false</b>, an <code>IllegalArgumentException</code> exception will
65 * be thrown.
66 *
67 * @param task the task to be inspected
68 * @throws IllegalArgumentException if the task is not supported by actions
69 */
70 public static void checkActionTask(Object task)
71 {
72 if (!isValidActionTask(task))
73 {
74 throw new IllegalArgumentException("Invalid action task: " + task);
75 }
76 }
77
78 /**
79 * Invokes the specified action task. This method tries to cast the passed
80 * in task object to one of the supported types and then invoke then correct
81 * method. If the passed in task is invalid, an exception will be thrown.
82 *
83 * @param task the task to be invoked
84 * @param action the corresponding action
85 * @param event the event that triggered the action
86 * @throws IllegalArgumentException if the task cannot be invoked
87 */
88 public static void invokeActionTask(Object task, FormAction action,
89 BuilderEvent event)
90 {
91 if (task instanceof ActionTask)
92 {
93 ((ActionTask) task).run(action, event);
94 }
95 else if (task instanceof Runnable)
96 {
97 ((Runnable) task).run();
98 }
99 else
100 {
101 throw new IllegalArgumentException("Invalid action task: " + task);
102 }
103 }
104
105 /**
106 * Returns the names of all groups defined for the specified
107 * <code>ActionStore</code> and its parents. This method navigates through
108 * the hierarchy of stores collecting all existing groups.
109 *
110 * @param actionStore the <code>ActionStore</code> to start with
111 * @return a set with the names of all groups existing in this hierarchy; if
112 * the passed in action store is <b>null</b>, an empty set is returned
113 */
114 public static Set<String> getAllGroupNames(ActionStore actionStore)
115 {
116 if (actionStore == null)
117 {
118 return Collections.emptySet();
119 }
120
121 Set<String> result = new HashSet<String>(actionStore.getGroupNames());
122 result.addAll(getAllGroupNames(actionStore.getParent()));
123 return result;
124 }
125
126 /**
127 * Returns all actions that belong to the specified group in the given
128 * <code>ActionStore</code>. If the action store is <b>null</b> or the
129 * group does not exist, an empty collection is returned.
130 *
131 * @param actionStore the action store
132 * @param groupName the name of the desired group
133 * @return a collection with the actions of this group
134 */
135 public static Collection<FormAction> fetchActionsInGroup(
136 ActionStore actionStore, String groupName)
137 {
138 if (actionStore == null)
139 {
140 return Collections.emptySet();
141 }
142 else
143 {
144 return actionStore.getActions(actionStore
145 .getActionNamesForGroup(groupName));
146 }
147 }
148
149 /**
150 * Returns all actions that belong to the specified group in the given
151 * <code>ActionStore</code> or one of its parents. The
152 * <code>distinct</code> parameter determines how actions with the same
153 * names are to be treated. If set to <b>true</b>, actions in a parent
154 * store are not added to the results collection if there are already
155 * actions with the same name in a child store. If the parameter is <b>false</b>,
156 * all actions are added.
157 *
158 * @param actionStore the actionStore to start with (can be <b>null</b>,
159 * then an empty collection will be returned)
160 * @param groupName the name of the group
161 * @param distinct the distinct flag
162 * @return a collection with the actions in this group
163 */
164 public static Collection<FormAction> fetchAllActionsInGroup(
165 ActionStore actionStore, String groupName, boolean distinct)
166 {
167 Collection<FormAction> result = new LinkedHashSet<FormAction>();
168 Set<String> names = distinct ? new HashSet<String>() : null;
169 ActionStore current = actionStore;
170
171 while (current != null)
172 {
173 Set<String> currentNames = current
174 .getActionNamesForGroup(groupName);
175 if (distinct)
176 {
177 currentNames.removeAll(names);
178 }
179 for (String n : currentNames)
180 {
181 result.add(current.getAction(n));
182 if (distinct)
183 {
184 names.add(n);
185 }
186 }
187
188 current = current.getParent();
189 }
190
191 return result;
192 }
193
194 /**
195 * Sets the enabled flag for all actions in the specified collection. With
196 * this method a set of actions can be enabled or disabled at once.
197 *
198 * @param actions a collection with the involved actions (can be <b>null</b>,
199 * then this operation has no effect)
200 * @param enabled the enabled flag
201 */
202 public static void enableActions(Collection<FormAction> actions,
203 boolean enabled)
204 {
205 if (actions != null)
206 {
207 for (FormAction action : actions)
208 {
209 action.setEnabled(enabled);
210 }
211 }
212 }
213 }