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.builder.action;
17  
18  import java.util.Set;
19  
20  import net.sf.jguiraffe.di.impl.SimpleBeanStoreImpl;
21  
22  import org.apache.commons.jelly.JellyContext;
23  
24  /**
25   * <p>
26   * A central data class used during the action builder process.
27   * </p>
28   * <p>
29   * This class holds all information needed during the processing of Jelly
30   * scripts with actions and related tags. An instance is stored in the Jelly
31   * context and can be accessed by all interested components. The role of this
32   * class is almost analogous to the
33   * {@link net.sf.jguiraffe.gui.builder.components.ComponentBuilderData ComponentBuilderData}
34   * class in the form builder package.
35   * </p>
36   * <p>
37   * By implementing the <code>BeanContributor</code> interface this class makes
38   * some of its managed objects available to the dependency injection framework.
39   * These are the following:
40   * <ul>
41   * <li>All actions stored in the associated <code>{@link ActionStore}</code>
42   * can be accessed under their name with the prefix
43   * <em>{@value #KEY_ACTION_PREFIX}</em>.</li>
44   * <li>The <code>{@link ActionStore}</code> itself is available under the
45   * name <em>{@value #KEY_ACTION_STORE}</em>.</li>
46   * <li>The instance of this class can be queried under the name
47   * <em>{@value #KEY_ACTION_BUILDER}</em>.</li>
48   * </ul>
49   * </p>
50   *
51   * @author Oliver Heger
52   * @version $Id: ActionBuilder.java 205 2012-01-29 18:29:57Z oheger $
53   */
54  public class ActionBuilder implements SimpleBeanStoreImpl.BeanContributor
55  {
56      /**
57       * Constant for the key for accessing the action store from the builder's
58       * bean context.
59       */
60      public static final String KEY_ACTION_STORE = "ACTION_STORE";
61  
62      /**
63       * Constant for the key for accessing the instance of this class from the
64       * builder's bean context.
65       */
66      public static final String KEY_ACTION_BUILDER = "ACTION_BUILDER";
67  
68      /**
69       * Constant for the prefix used for accessing actions.
70       */
71      public static final String KEY_ACTION_PREFIX = "action:";
72  
73      /**
74       * Constant for the name under which an instance will be stored in the Jelly
75       * context.
76       */
77      private static final String CTX_KEY = ActionBuilder.class.getName();
78  
79      /** Holds a reference to the used action store. */
80      private ActionStore actionStore;
81  
82      /** Holds a reference to the platform specific action manager. */
83      private ActionManager actionManager;
84  
85      /** The current Jelly context. */
86      private JellyContext context;
87  
88      /** Stores the menu icon flag. */
89      private boolean menuIcon;
90  
91      /** Stores the toolbar text flag. */
92      private boolean toolbarText;
93  
94      /**
95       * Returns a reference to the current action store.
96       *
97       * @return the action store
98       */
99      public ActionStore getActionStore()
100     {
101         return actionStore;
102     }
103 
104     /**
105      * Sets the action store. Newly created actions will be stored in this
106      * objects. If actions are requested, they will be looked up here, too.
107      *
108      * @param actionStore the action store
109      */
110     public void setActionStore(ActionStore actionStore)
111     {
112         this.actionStore = actionStore;
113     }
114 
115     /**
116      * Returns a reference to the action manager.
117      *
118      * @return the action manager
119      */
120     public ActionManager getActionManager()
121     {
122         return actionManager;
123     }
124 
125     /**
126      * Sets the action manager. This is a GUI library specific component that
127      * knows how to create all affected objects.
128      *
129      * @param actionManager the action manager to use
130      */
131     public void setActionManager(ActionManager actionManager)
132     {
133         this.actionManager = actionManager;
134     }
135 
136     /**
137      * Returns the value of the menu icon flag.
138      *
139      * @return the menu icon flag
140      */
141     public boolean isMenuIcon()
142     {
143         return menuIcon;
144     }
145 
146     /**
147      * Sets the value of the menu icon flag. If this flag is set, menu items
148      * will have an icon if one is defined. If set to <b>false</b>, these icons
149      * will be suppressed.
150      *
151      * @param menuIcon the value of the flag
152      */
153     public void setMenuIcon(boolean menuIcon)
154     {
155         this.menuIcon = menuIcon;
156     }
157 
158     /**
159      * Returns the value of the toolbar text flag.
160      *
161      * @return the toolbar text flag
162      */
163     public boolean isToolbarText()
164     {
165         return toolbarText;
166     }
167 
168     /**
169      * Sets the value of the toolbar text flag. If this flag is set, toolbar
170      * buttons will display a text if one is defined. Otherwise the text is
171      * suppressed.
172      *
173      * @param toolbarText the value of the flag
174      */
175     public void setToolbarText(boolean toolbarText)
176     {
177         this.toolbarText = toolbarText;
178     }
179 
180     /**
181      * Returns the current {@code JellyContext} this object lives in. This
182      * property is initialized when this instance is stored in a context.
183      *
184      * @return the current {@code JellyContext}
185      * @since 1.3
186      */
187     public JellyContext getContext()
188     {
189         return context;
190     }
191 
192     /**
193      * Obtains the names of the beans supported by this bean contributor. This
194      * implementation returns the names of the actions stored in the internal
195      * action store and some other helper objects.
196      *
197      * @param names the set to which to add the names
198      */
199     public void beanNames(Set<String> names)
200     {
201         if (getActionStore() != null)
202         {
203             for (String name : getActionStore().getAllActionNames())
204             {
205                 names.add(KEY_ACTION_PREFIX + name);
206             }
207             names.add(KEY_ACTION_STORE);
208         }
209     }
210 
211     /**
212      * Returns the bean with the given name.
213      *
214      * @param name the name of the bean
215      * @return the bean with this name
216      * @throws java.util.NoSuchElementException if an unknown action is requested
217      */
218     public Object getBean(String name)
219     {
220         Object bean = null;
221 
222         if (getActionStore() != null)
223         {
224             if (KEY_ACTION_STORE.equals(name))
225             {
226                 bean = getActionStore();
227             }
228 
229             else if (name != null && name.startsWith(KEY_ACTION_PREFIX))
230             {
231                 bean = getActionStore().getAction(
232                         name.substring(KEY_ACTION_PREFIX.length()));
233             }
234         }
235 
236         return bean;
237     }
238 
239     /**
240      * Initializes the specified bean store object. This method is called by the
241      * builder when the <code>BeanContext</code> used during the builder
242      * operation is constructed. This implementation will add the static beans
243      * to the given store and register this object as
244      * <code>BeanContributor</code>.
245      *
246      * @param store the store to be initialized
247      */
248     public void initBeanStore(SimpleBeanStoreImpl store)
249     {
250         store.addBean(KEY_ACTION_BUILDER, this);
251         store.addBeanContributor(this);
252     }
253 
254     /**
255      * Stores this object in the given Jelly context.
256      *
257      * @param context the context (must not be <b>null</b>)
258      * @throws IllegalArgumentException if the context is <b>null</b>
259      */
260     public void put(JellyContext context)
261     {
262         if (context == null)
263         {
264             throw new IllegalArgumentException("Context must not be null!");
265         }
266 
267         context.setVariable(CTX_KEY, this);
268         this.context = context;
269     }
270 
271     /**
272      * Returns the instance of this class stored in the specified Jelly context.
273      * If no instance can be found, <b>null</b> will be returned.
274      *
275      * @param context the Jelly context
276      * @return the instance found in this context
277      */
278     public static ActionBuilder get(JellyContext context)
279     {
280         return (context != null) ? (ActionBuilder) context
281                 .findVariable(CTX_KEY) : null;
282     }
283 }