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.app;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import net.sf.jguiraffe.di.BeanContext;
25  import net.sf.jguiraffe.di.BeanCreationListener;
26  import net.sf.jguiraffe.di.BeanStore;
27  import net.sf.jguiraffe.di.InvocationHelper;
28  import net.sf.jguiraffe.gui.builder.BeanBuilderResult;
29  import net.sf.jguiraffe.gui.builder.Builder;
30  import net.sf.jguiraffe.gui.builder.BuilderData;
31  import net.sf.jguiraffe.gui.builder.action.ActionStore;
32  import net.sf.jguiraffe.gui.builder.utils.MessageOutput;
33  import net.sf.jguiraffe.gui.builder.window.Window;
34  import net.sf.jguiraffe.gui.cmd.CommandQueue;
35  import net.sf.jguiraffe.gui.forms.BindingStrategy;
36  import net.sf.jguiraffe.gui.forms.FormValidator;
37  import net.sf.jguiraffe.transform.TransformerContext;
38  
39  /**
40   * <p>
41   * The application specific default implementation of the {@code BuilderData}
42   * interface.
43   * </p>
44   * <p>
45   * This class provides meaningful implementations of all methods required by the
46   * {@code BuilderData} interface. An instance can be obtained from the
47   * {@link ApplicationContext} class that is already initialized with predefined
48   * values for many fields. So a client need not bother with all of the data
49   * supported by this interface, but has only to set the values it is specially
50   * interested in.
51   * </p>
52   * <p>
53   * Implementation note: this class is not thread-safe. The typical usage
54   * scenario is that an instance is requested from {@link ApplicationContext},
55   * initialized with the properties required by the application and passed to a
56   * builder.
57   * </p>
58   *
59   * @author Oliver Heger
60   * @version $Id: ApplicationBuilderData.java 205 2012-01-29 18:29:57Z oheger $
61   * @see ApplicationContext#initBuilderData()
62   */
63  public class ApplicationBuilderData implements BuilderData
64  {
65      /** Stores the default resource group. */
66      private Object defaultResourceGroup;
67  
68      /** Stores the action store. */
69      private ActionStore actionStore;
70  
71      /** Stores the parent window. */
72      private Window parentWindow;
73  
74      /** Stores the transformer context. */
75      private TransformerContext transformerContext;
76  
77      /** Stores the form bean. */
78      private Object formBean;
79  
80      /** Stores the binding strategy. */
81      private BindingStrategy bindingStrategy;
82  
83      /** Stores the form validator. */
84      private FormValidator formValidator;
85  
86      /** Stores the parent bean context. */
87      private BeanContext parentContext;
88  
89      /** Stores the bean context used by the builder. */
90      private BeanContext builderContext;
91  
92      /** Stores the result object of the builder. */
93      private BeanBuilderResult beanBuilderResult;
94  
95      /** Stores the invocation helper. */
96      private InvocationHelper invocationHelper;
97  
98      /** Stores the message output object.*/
99      private MessageOutput messageOutput;
100 
101     /** Stores the command queue.*/
102     private CommandQueue commandQueue;
103 
104     /** A collection with bean creation listeners.*/
105     private final Collection<BeanCreationListener> beanCreationListeners;
106 
107     /** A map for additional properties. */
108     private Map<String, Object> properties;
109 
110     /** The builder object that processed this instance.*/
111     private Builder builder;
112 
113     /** Stores the name of the builder.*/
114     private String builderName;
115 
116     /** Stores the menu icon flag. */
117     private boolean menuIcon;
118 
119     /** Stores the toolbar text flag. */
120     private boolean toolbarText;
121 
122     /** The auto release flag.*/
123     private boolean autoRelease = true;
124 
125     /**
126      * Creates a new instance of {@code ApplicationBuilderData}.
127      */
128     public ApplicationBuilderData()
129     {
130         beanCreationListeners = new ArrayList<BeanCreationListener>();
131     }
132 
133     /**
134      * Returns the action store.
135      *
136      * @return the action store
137      */
138     public ActionStore getActionStore()
139     {
140         return actionStore;
141     }
142 
143     /**
144      * Sets the action store.
145      *
146      * @param actionStore the actionStore
147      */
148     public void setActionStore(ActionStore actionStore)
149     {
150         this.actionStore = actionStore;
151     }
152 
153     /**
154      * Returns the default resource group.
155      *
156      * @return the default resource group
157      */
158     public Object getDefaultResourceGroup()
159     {
160         return defaultResourceGroup;
161     }
162 
163     /**
164      * Sets the default resource group.
165      *
166      * @param defaultResourceGroup the default resource group
167      */
168     public void setDefaultResourceGroup(Object defaultResourceGroup)
169     {
170         this.defaultResourceGroup = defaultResourceGroup;
171     }
172 
173     /**
174      * Returns the form bean.
175      *
176      * @return the form bean
177      */
178     public Object getFormBean()
179     {
180         return formBean;
181     }
182 
183     /**
184      * Sets the form bean.
185      *
186      * @param formBean the form bean
187      */
188     public void setFormBean(Object formBean)
189     {
190         this.formBean = formBean;
191     }
192 
193     /**
194      * Returns the {@code BindingStrategy} used by the current form.
195      *
196      * @return the {@code BindingStrategy}
197      */
198     public BindingStrategy getBindingStrategy()
199     {
200         return bindingStrategy;
201     }
202 
203     /**
204      * Sets the {@code BindingStrategy} to be used by the current form.
205      *
206      * @param strat the {@code BindingStrategy}
207      */
208     public void setBindingStrategy(BindingStrategy strat)
209     {
210         bindingStrategy = strat;
211     }
212 
213     /**
214      * Returns the {@code FormValidator} for validating the current form.
215      *
216      * @return the {@code FormValidator}
217      */
218     public FormValidator getFormValidator()
219     {
220         return formValidator;
221     }
222 
223     /**
224      * Sets the {@code FormValidator} for validating the current form. If no
225      * {@code FormValidator} is set, no form-level validation is performed. The
226      * form's fields may be validated though if corresponding validators have
227      * been defined.
228      *
229      * @param validator the {@code FormValidator}
230      */
231     public void setFormValidator(FormValidator validator)
232     {
233         formValidator = validator;
234     }
235 
236     /**
237      * Returns the menu icon flag.
238      *
239      * @return the menu icon flag
240      */
241     public boolean isMenuIcon()
242     {
243         return menuIcon;
244     }
245 
246     /**
247      * Sets the menu icon flag.
248      *
249      * @param menuIcon the flag value
250      */
251     public void setMenuIcon(boolean menuIcon)
252     {
253         this.menuIcon = menuIcon;
254     }
255 
256     /**
257      * Returns the parent window.
258      *
259      * @return the parent window
260      */
261     public Window getParentWindow()
262     {
263         return parentWindow;
264     }
265 
266     /**
267      * Sets the parent window.
268      *
269      * @param parentWindow the parent window
270      */
271     public void setParentWindow(Window parentWindow)
272     {
273         this.parentWindow = parentWindow;
274     }
275 
276     /**
277      * Returns the toolbar text flag.
278      *
279      * @return the toolbar text flag
280      */
281     public boolean isToolbarText()
282     {
283         return toolbarText;
284     }
285 
286     /**
287      * Sets the toolbar text flag.
288      *
289      * @param toolbarText the toolbar text flag
290      */
291     public void setToolbarText(boolean toolbarText)
292     {
293         this.toolbarText = toolbarText;
294     }
295 
296     /**
297      * Returns the transformer context.
298      *
299      * @return the transformer context
300      */
301     public TransformerContext getTransformerContext()
302     {
303         return transformerContext;
304     }
305 
306     /**
307      * Sets the transformer context.
308      *
309      * @param transformerContext the transformer context
310      */
311     public void setTransformerContext(TransformerContext transformerContext)
312     {
313         this.transformerContext = transformerContext;
314     }
315 
316     /**
317      * Returns the result object from the bean builder.
318      *
319      * @return the results of the bean builder
320      */
321     public BeanBuilderResult getBeanBuilderResult()
322     {
323         return beanBuilderResult;
324     }
325 
326     /**
327      * Sets the result object for the bean builder.
328      *
329      * @param res the results of the bean builder
330      */
331     public void setBeanBuilderResult(BeanBuilderResult res)
332     {
333         beanBuilderResult = res;
334     }
335 
336     /**
337      * Returns the parent bean context.
338      *
339      * @return the parent bean context
340      */
341     public BeanContext getParentContext()
342     {
343         return parentContext;
344     }
345 
346     /**
347      * Sets the parent bean context.
348      *
349      * @param ctx the parent bean context
350      */
351     public void setParentContext(BeanContext ctx)
352     {
353         parentContext = ctx;
354     }
355 
356     /**
357      * Returns the root store populated by the builder. This method can only be
358      * called after the builder operation.
359      *
360      * @return the root store returned from the builder
361      * @throws IllegalStateException if no builder results are available yet
362      */
363     public BeanStore getRootStore()
364     {
365         if (getBeanBuilderResult() == null)
366         {
367             throw new IllegalStateException(
368                     "Root store cannot be queried before builder "
369                             + "results are available!");
370         }
371 
372         return getBeanBuilderResult().getBeanStore(null);
373     }
374 
375     /**
376      * Returns the bean context used by the builder.
377      *
378      * @return the builder's bean context
379      */
380     public BeanContext getBuilderContext()
381     {
382         return builderContext;
383     }
384 
385     /**
386      * Sets the bean context used by the builder.
387      *
388      * @param ctx the builder's bean context
389      */
390     public void setBuilderContext(BeanContext ctx)
391     {
392         builderContext = ctx;
393     }
394 
395     /**
396      * Returns the {@code InvocationHelper}.
397      *
398      * @return the {@code InvocationHelper}
399      */
400     public InvocationHelper getInvocationHelper()
401     {
402         return invocationHelper;
403     }
404 
405     /**
406      * Sets the {@code InvocationHelper}.
407      *
408      * @param invocationHelper the {@code InvocationHelper}
409      */
410     public void setInvocationHelper(InvocationHelper invocationHelper)
411     {
412         this.invocationHelper = invocationHelper;
413     }
414 
415     /**
416      * Returns the <code>MessageOutput</code> object.
417      *
418      * @return the message output object
419      */
420     public MessageOutput getMessageOutput()
421     {
422         return messageOutput;
423     }
424 
425     /**
426      * Sets the <code>MessageOutput</code> object.
427      *
428      * @param messageOutput the message output object
429      */
430     public void setMessageOutput(MessageOutput messageOutput)
431     {
432         this.messageOutput = messageOutput;
433     }
434 
435     /**
436      * Returns the <code>CommandQueue</code>.
437      *
438      * @return the command queue
439      */
440     public CommandQueue getCommandQueue()
441     {
442         return commandQueue;
443     }
444 
445     /**
446      * Sets the <code>CommandQueue</code>.
447      *
448      * @param commandQueue the command queue
449      */
450     public void setCommandQueue(CommandQueue commandQueue)
451     {
452         this.commandQueue = commandQueue;
453     }
454 
455     /**
456      * Returns a collection with {@code BeanCreationListener} objects to be
457      * registered at the {@code BeanContext} created by the builder. Note: this
458      * collection cannot be modified.
459      *
460      * @return a collection with {@code BeanCreationListener} objects
461      */
462     public Collection<BeanCreationListener> getBeanCreationListeners()
463     {
464         return Collections.unmodifiableCollection(beanCreationListeners);
465     }
466 
467     /**
468      * Adds the specified {@code BeanCreationListener} to this object. It will
469      * be registered at the {@code BeanContext} created by the builder and thus
470      * notified for all bean created by the
471      * <em>dependency injection framework</em>.
472      *
473      * @param l the {@code BeanCreationListener} to be added (must not be
474      *        <b>null</b>)
475      * @throws IllegalArgumentException if the {@code BeanCreationListener} is
476      *         <b>null</b>
477      */
478     public void addBeanCreationListener(BeanCreationListener l)
479     {
480         if (l == null)
481         {
482             throw new IllegalArgumentException(
483                     "BeanCreationListener must not be null!");
484         }
485         beanCreationListeners.add(l);
486     }
487 
488     /**
489      * Adds all {@code BeanCreationListener} objects contained in the given
490      * collection to this object. They will be registered at the {@code
491      * BeanContext} created by the builder and thus notified for all bean
492      * created by the <em>dependency injection framework</em>.
493      *
494      * @param listeners the collection with {@code BeanCreationListener} objects
495      *        (must not be <b>null</b>)
496      * @throws IllegalArgumentException if the collection is <b>null</b> or
497      *         contains <b>null</b> elements
498      */
499     public void addBeanCreationListeners(
500             Collection<? extends BeanCreationListener> listeners)
501     {
502         if (listeners == null)
503         {
504             throw new IllegalArgumentException(
505                     "BeanCreationListener collection must not be null!");
506         }
507 
508         for (BeanCreationListener l : listeners)
509         {
510             addBeanCreationListener(l);
511         }
512     }
513 
514     /**
515      * Returns a reference to the {@code Builder} instance that processed this
516      * object. This value is available only after the {@code Builder} was
517      * called.
518      *
519      * @return the {@code Builder} that processed this {@code BuilderData}
520      *         object
521      */
522     public Builder getBuilder()
523     {
524         return builder;
525     }
526 
527     /**
528      * Sets the {@code Builder} that processed this object. This method is
529      * called by the {@code Builder} instance during the builder operation.
530      *
531      * @param builder the {@code Builder}
532      */
533     public void setBuilder(Builder builder)
534     {
535         this.builder = builder;
536     }
537 
538     /**
539      * Returns the name of the builder.
540      *
541      * @return the name of the builder
542      */
543     public String getBuilderName()
544     {
545         return builderName;
546     }
547 
548     /**
549      * Sets a name for the builder. This name is available during the build
550      * process (through the {@code ComponentBuilderData} object). It can be used
551      * for conditional execution of builder scripts.
552      *
553      * @param builderName the name of the builder
554      */
555     public void setBuilderName(String builderName)
556     {
557         this.builderName = builderName;
558     }
559 
560     /**
561      * Returns the <em>auto release</em> flag.
562      *
563      * @return the auto release flag
564      */
565     public boolean isAutoRelease()
566     {
567         return autoRelease;
568     }
569 
570     /**
571      * Sets the <em>auto release</em> flag. This flag is evaluated if a window
572      * is generated during the builder operation. A value of <b>true</b> means
573      * that this object and all resources referenced by it should be freed
574      * automatically when the window is closed. This is done by invoking
575      * {@link Builder#release(BuilderData)} on the {@link Builder} responsible.
576      * Note that the default value of this flag is <b>true</b>, so auto release
577      * is enabled per default.
578      *
579      * @param autoRelease the value of the auto release flag
580      */
581     public void setAutoRelease(boolean autoRelease)
582     {
583         this.autoRelease = autoRelease;
584     }
585 
586     /**
587      * Returns a map with additional properties for the builder operation. This
588      * implementation either returns the map set by {@link #setProperties(Map)}
589      * or the one that was created automatically when {@link #addProperty()} was
590      * called. Result may also be <b>null</b> if no properties have been set.
591      *
592      * @return a map with additional properties for the builder operation
593      * @see #setProperties(Map)
594      * @see #addProperty(String, Object)
595      */
596     public Map<String, Object> getProperties()
597     {
598         return properties;
599     }
600 
601     /**
602      * Sets additional properties for the builder operation. The map passed to
603      * this method is directly stored and passed to the builder.
604      *
605      * @param props the map with additional properties
606      */
607     public void setProperties(Map<String, Object> props)
608     {
609         properties = props;
610     }
611 
612     /**
613      * Adds an additional property for the builder operation. This method can be
614      * used to populate the map with additional properties that is returned by
615      * {@link #getProperties()}. If no map with properties has been set yet, a
616      * new one is created ({@link #getProperties()} will return this new map).
617      * Otherwise, the property is added to the existing map.
618      *
619      * @param key the key of the property
620      * @param value the value of the property
621      */
622     public void addProperty(String key, Object value)
623     {
624         if (properties == null)
625         {
626             properties = new HashMap<String, Object>();
627         }
628 
629         properties.put(key, value);
630     }
631 }