KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > JNForm


1 /*
2  * $Id: JNForm.java,v 1.4 2005/01/27 13:32:02 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.jdnc;
9
10 import java.awt.BorderLayout JavaDoc;
11 import java.awt.Color JavaDoc;
12 import java.awt.Component JavaDoc;
13
14 import org.jdesktop.swing.data.DataModel;
15 import org.jdesktop.swing.data.MetaData;
16 import org.jdesktop.swing.data.DefaultTableModelExt;
17
18 import org.jdesktop.swing.actions.BoundAction;
19
20 import org.jdesktop.swing.binding.BindException;
21
22 import org.jdesktop.swing.form.JForm;
23
24 import javax.swing.Action JavaDoc;
25 import javax.swing.Box JavaDoc;
26 import javax.swing.Icon JavaDoc;
27 import javax.swing.ImageIcon JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.border.EmptyBorder JavaDoc;
31
32 /**
33  * @author Amy Fowler
34  * @version 1.0
35  *
36  * @javabean.class
37  * displayName="Form Component"
38  * name="JNForm"
39  * shortDesctiption="A form control"
40  */

41 public class JNForm extends JNComponent {
42     private JForm form;
43     private JComponent JavaDoc buttonPanel;
44     private Component JavaDoc trailGlue;
45
46     public JNForm() {
47         super();
48         setLayout(new BorderLayout JavaDoc());
49
50         form = createForm();
51         add(BorderLayout.CENTER, form);
52
53         buttonPanel = createButtonPanel();
54         addAction(form.getActionMap().get("reset"));
55         addAction(form.getActionMap().get("submit"));
56         add(BorderLayout.SOUTH, buttonPanel);
57
58         /**
59         Icon[] icons = new Icon[] {
60             new ImageIcon(klass.getResource("resources/wellTopLeft.gif")),
61             new ImageIcon(klass.getResource("resources/wellTop.gif")),
62             new ImageIcon(klass.getResource("resources/wellTopRight.gif")),
63             new ImageIcon(klass.getResource("resources/wellRight.gif")),
64             new ImageIcon(klass.getResource("resources/wellBottomRight.gif")),
65             new ImageIcon(klass.getResource("resources/wellBottom.gif")),
66             new ImageIcon(klass.getResource("resources/wellBottomLeft.gif")),
67             new ImageIcon(klass.getResource("resources/wellLeft.gif")),
68         };
69         setBorder(new MatteBorderExt(14, 14, 14, 14, icons));
70             **/

71
72     }
73
74     protected JForm createForm() {
75         return new JForm();
76     }
77
78     public JForm getForm() {
79         return form;
80     }
81
82     /**
83      * Binds the form to each column in the specified DefaultTableModelExt object.
84      * The bind operation will create the best user-interface components to
85      * display/edit the data model values based on each column's <code>MetaData</code>
86      * object. And finally, if &quot;autoLayout&quot; is <code>true</code>,
87      * it will also add and layout those components within the form.
88      * @see MetaData
89      * @param tabularData DefaultTableModelExt being bound to the form
90      * @throws BindException if there were errors when binding to the data model
91      */

92     public void bind(DefaultTableModelExt tabularData) throws BindException {
93         form.bind(tabularData);
94     }
95
96     /**
97      * Binds the form to the specified column in the DefaultTableModelExt object.
98      * The bind operation will create the best user-interface component to
99      * display/edit the data model value based on the column's <code>MetaData</code>
100      * object. If &quot;autoLayout&quot; is <code>true</code>,
101      * it will also add and layout the component within the form.
102      * @param tabularData DefaultTableModelExt being bound to the form
103      * @param columnName String containing the name of the column
104      * @throws BindException if there were errors when binding to the data model
105      */

106     public void bind(DefaultTableModelExt tabularData, String JavaDoc columnName) throws BindException {
107         form.bind(tabularData, columnName);
108     }
109
110     /**
111      * Binds the form to each property in the specified JavaBean object.
112      * The bind operation will create the best user-interface components to
113      * display/edit the data model values based on each property's description.
114      * If &quot;autoLayout&quot; is <code>true</code>,
115      * it will also add and layout those components within the form.
116      * @param bean JavaBean object being bound to the form
117      * @throws BindException if there were errors when binding to the data model
118      */

119     public void bind(Object JavaDoc bean) throws BindException {
120         form.bind(bean);
121     }
122
123     /**
124      * Binds the form to the specified property in the JavaBean object.
125      * The bind operation will create the best user-interface component to
126      * display/edit the data model value based on the property's description.
127      * If &quot;autoLayout&quot; is <code>true</code>,
128      * it will also add and layout the component within the form.
129      * @param bean JavaBean object being bound to the form
130      * @param propertyName String containing the name of the property
131      * @throws BindException if there were errors when binding to the data model
132      */

133     public void bind(Object JavaDoc bean, String JavaDoc propertyName) throws BindException {
134         form.bind(bean, propertyName);
135     }
136
137     /**
138      * Binds the form to each field in the specified DataModel object.
139      * The bind operation will create the best user-interface components to
140      * display/edit the data model values based on each field's <code>MetaData</code>
141      * object. If &quot;autoLayout&quot; is <code>true</code>,
142      * it will also add and layout those components within the form.
143      * @param model DataModel object being bound to the form
144      * @throws BindException if there were errors when binding to the data model
145      */

146     public void bind(DataModel model) throws BindException {
147         form.bind(model);
148     }
149
150     /**
151      * Binds the form to the specified field in the DataModel object.
152      * The bind operation will create the best user-interface component to
153      * display/edit the data model value based on the field's <code>MetaData</code>
154      * object. If &quot;autoLayout&quot; is <code>true</code>,
155      * it will also add and layout the component within the form.
156      * @param model DataModel object being bound to the form
157      * @param fieldName String containing the name of the field
158      * @throws BindException if there were errors when binding to the data model
159      */

160     public void bind(DataModel model, String JavaDoc fieldName) throws BindException {
161         form.bind(model, fieldName);
162     }
163
164
165     public JButton JavaDoc addAction(Action action) {
166         JButton JavaDoc button = new JButton JavaDoc(action);
167         button.setBackground(getBackground());
168
169         if (action instanceof BoundAction) {
170             // XXX - msd Action method registraion.
171
// This isn't general enough. Ideally, the
172
// Reset and Submit actions should know how to
173
// register themselves.
174
BoundAction ba = (BoundAction) action;
175             Object JavaDoc id = ba.getActionCommand();
176             if ("submit".equals(id)) {
177                 ba.registerCallback(form, "doSubmit");
178             }
179             else if ("reset".equals(id)) {
180                 ba.registerCallback(form, "doReset");
181             }
182         }
183         addToButtonPanel(button);
184         return button;
185     }
186
187     /**
188      * @javabean.property
189      * shortDescription="Sets the background color of this component"
190      */

191     public void setBackground(Color JavaDoc background) {
192         super.setBackground(background);
193         if (form != null) {
194             form.setBackground(background);
195         }
196         if (buttonPanel != null) {
197             buttonPanel.setBackground(background);
198             Component JavaDoc children[] = buttonPanel.getComponents();
199             for(int i = 0; i < children.length; i++) {
200                 children[i].setBackground(background);
201             }
202         }
203     }
204
205     protected JComponent JavaDoc createButtonPanel() {
206         Box JavaDoc box = Box.createHorizontalBox();
207         box.setBorder(new EmptyBorder JavaDoc(6,6,6,6));
208         box.add(Box.createHorizontalGlue());
209         return box;
210     }
211
212     protected void addToButtonPanel(JComponent JavaDoc component) {
213         int childCount = buttonPanel.getComponentCount();
214         // if we have more than one child, then a component
215
// was already added, so peel off the trailing glue
216
// and add a strut, then component, then trailing glue
217
if (childCount > 1) {
218             buttonPanel.remove(trailGlue);
219             buttonPanel.add(Box.createHorizontalStrut(20));
220         }
221         buttonPanel.add(component);
222         if (trailGlue == null) {
223             trailGlue = Box.createHorizontalGlue();
224         }
225         buttonPanel.add(trailGlue);
226     }
227
228 }
229
Popular Tags