KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > form > FormFactory


1 /*
2  * $Id: FormFactory.java,v 1.2 2005/02/03 13:36:39 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.form;
9
10 import org.jdesktop.swing.data.DataConstants;
11 import org.jdesktop.swing.data.DataModel;
12 import org.jdesktop.swing.data.MetaData;
13
14 import org.jdesktop.swing.binding.Binding;
15
16 import javax.swing.JComponent JavaDoc;
17
18 /**
19  * Form factory class which provides support for constructing the user-interface
20  * components and associated bindings for a JForm component.
21  * <p>
22  * Generally, applications need not interact directly with this class because
23  * the default operation of the JForm component will invoke this factory as
24  * necessary to construct the form based on how the application binds the form
25  * to the application's data models.</p>
26  * <p>
27  * This factory provides methods for 3 key form-building operations:
28  * <ol>
29  * <li>createComponent: given a MetaData object which describes a data field
30  * (name, type, edit constraints, etc), return the user-interface component
31  * which can best display and/or edit values for that field.</li>
32  * <li>createBinding: given a user-interface component and a named field on a
33  * data model, return the Binding instance required to bind that component
34  * to that field.</li>
35  * <li>addComponent: adds the provided component to the specified form container,
36  * including laying it out according to the layout paradigm of the form
37  * factory</li>
38  * </ol>
39  *
40  * <p>
41  * This factory can handle non-visual data fields - createComponent will return
42  * null in that case.
43  *
44  *
45  * Note that a form factory is stateless and these methods operate independent
46  * of each other, thus an application may invoke only those methods it requires
47  * to construct the form. For example, an application that wishes to use the
48  * form factory's components and bindings, but intends to handle the layout
49  * itself, may choose to invoke only the first two methods.
50  *
51  * @author Amy Fowler
52  * @version 1.0
53  */

54
55 public abstract class FormFactory {
56     private static FormFactory defaultFormFactory;
57
58     /**
59      *
60      * @return FormFactory instance which is shared across the application
61      */

62     public static FormFactory getDefaultFormFactory() {
63         if (defaultFormFactory == null) {
64             defaultFormFactory = new DefaultFormFactory();
65         }
66         return defaultFormFactory;
67     }
68
69     /**
70      * Sets the default FormFactory instance which is shared across the application.
71      * @param formFactory factory to be used as the default form factory
72      */

73     public static void setDefaultFormFactory(FormFactory formFactory) {
74         FormFactory.defaultFormFactory = formFactory;
75     }
76
77     /**
78      * Factory method for returning the user-interface component best suited
79      * to edit/display values for the data model field represented by the metaData
80      * object. The returned component is not only based on the metadata's type, but
81      * also on it's edit constraints.
82      * @param metaData object which describes the named field
83      * @return JComponent which can display/edit values defined by the metaData
84      * object or null if the field is non-visual.
85      */

86     public abstract JComponent JavaDoc createComponent(MetaData metaData);
87
88     /**
89      * Factory method for returning the binding object which connects the
90      * user-interface component to the specified field in a data model.
91      * @param model data model object to which the component is being bound
92      * @param fieldName String containing the name of the field within the data model
93      * @param component JComponent which can display/edit values defined by the metaData
94      * object
95      * @return Binding instance which binds the component to the field in the data model
96      */

97     public abstract Binding createBinding(DataModel model, String JavaDoc fieldName, JComponent JavaDoc component);
98
99     /**
100      * Adds the component to the specified parent container and configures its
101      * layout within that container according to the form factory's layout
102      * paradigm. If the metaData argument is not null, then a label will be
103      * automatically created and aligned with the component.
104      * Note that the component being added need not be the component
105      * which has the binding. For example, an edit component may be contained
106      * within another container (scrollpane, panel, etc); the edit component
107      * will have the binding, but the container is what must be added to the form.
108      *
109      * @param parent Container where the component is being added
110      * @param component JComponent being added to the container
111      * @param metaData object which describes the named field
112      */

113     public abstract void addComponent(JComponent JavaDoc parent, JComponent JavaDoc component, MetaData metaData);
114
115     /**
116      *
117      * @param metaData
118      * @return
119      */

120     public boolean isNonVisual(MetaData metaData) {
121         return Boolean.TRUE
122           .equals(metaData.getCustomProperty(DataConstants.NON_VISUAL_FIELD));
123       }
124
125 }
Popular Tags