KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DefaultFormFactory.java,v 1.7 2005/03/01 13:27:12 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 java.awt.Font JavaDoc;
11 import java.awt.GridBagConstraints JavaDoc;
12 import java.awt.GridBagLayout JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.LayoutManager JavaDoc;
15 import java.awt.Rectangle JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import javax.swing.Icon JavaDoc;
19 import javax.swing.ImageIcon JavaDoc;
20 import javax.swing.JComponent JavaDoc;
21 import javax.swing.JLabel JavaDoc;
22 import javax.swing.JScrollPane JavaDoc;
23
24 import org.jdesktop.swing.binding.Binding;
25 import org.jdesktop.swing.binding.BindingMap;
26 import org.jdesktop.swing.data.DataModel;
27 import org.jdesktop.swing.data.MetaData;
28
29 /**
30  * note:technically this should probably be in a FormUI, or
31  * initialized from a FormUI.
32  *
33  * @author Amy Fowler
34  * @version 1.0
35  */

36
37 public class DefaultFormFactory extends FormFactory {
38     private static Icon JavaDoc requiredIcon;
39
40     static {
41         URL JavaDoc url = BindingBorder.class.getResource("resources/asterisk.8x8.png");
42         requiredIcon = new ImageIcon JavaDoc(url);
43     }
44
45     private Insets JavaDoc labelInsets;
46     private Insets JavaDoc nestedFormLabelInsets;
47     private Insets JavaDoc componentInsets;
48
49     public DefaultFormFactory() {
50         labelInsets = new Insets JavaDoc(4, 5, 5, 4);
51         nestedFormLabelInsets = new Insets JavaDoc(12, 5, 5, 4);
52         componentInsets = new Insets JavaDoc(1, 4, 4, 4);
53     }
54     
55     /**
56      * Delegates to current ComponentMap.
57      */

58     public JComponent JavaDoc createComponent(MetaData metaData) {
59         if (isNonVisual(metaData)) {
60           return null;
61         }
62         JComponent JavaDoc component = ComponentMap.getInstance().createComponent(metaData);
63         // should not happen - ComponentMap has a fallback..
64
if (component == null) {
65           throw new IllegalStateException JavaDoc("some error in ComponentMap");
66         }
67         return component;
68       }
69
70      /**
71       * delegates to current BindingMap.
72       */

73       public Binding createBinding(DataModel model, String JavaDoc fieldName,
74           JComponent JavaDoc component) {
75         if (component == null) {
76           return null;
77         }
78         if (component instanceof JScrollPane JavaDoc) {
79           component =
80             (JComponent JavaDoc) ((JScrollPane JavaDoc) component).getViewport().getView();
81         }
82         return BindingMap.getInstance().createBinding(component, model, fieldName);
83       }
84
85
86
87 // public JComponent createComponent(MetaData metaData) {
88
// if (isNonVisual(metaData)) {
89
// return null;
90
// }
91
//
92
// Class fieldClass = metaData.getElementClass();
93
//
94
// if (fieldClass.isAssignableFrom(DataModel.class)) {
95
// return new JForm();
96
// }
97
//
98
// if (metaData.isReadOnly()) {
99
// return new JLabel();
100
//
101
// }
102
// else if (metaData instanceof EnumeratedMetaData) {
103
// EnumeratedMetaData enumMetaData = (EnumeratedMetaData) metaData;
104
// Object values[] = enumMetaData.getEnumeration();
105
// if (values.length > 3) {
106
// return new JComboBox(values);
107
// }
108
// return new JXRadioGroup(values);
109
// }
110
// else if (metaData instanceof NumberMetaData) {
111
// NumberMetaData numberMetaData = (NumberMetaData) metaData;
112
// Number min = numberMetaData.getMinimum();
113
// Number max = numberMetaData.getMaximum();
114
//
115
// if (min != null && max != null) {
116
// SpinnerModel spinnerModel = null;
117
// if (fieldClass == Integer.class || fieldClass == Long.class ||
118
// fieldClass == Short.class || fieldClass == int.class) {
119
// spinnerModel = new SpinnerNumberModel(min.intValue(),
120
// min.intValue(), max.intValue(), 1);
121
// }
122
// else if (fieldClass == Float.class || fieldClass == Double.class ||
123
// fieldClass == float.class || fieldClass == double.class) {
124
// //**@todo aim: need to add precision to NumberMetaData */
125
// spinnerModel = new SpinnerNumberModel(min.doubleValue(),
126
// min.doubleValue(), max.doubleValue(), .01);
127
// }
128
// if (spinnerModel != null) {
129
// return new JSpinner(spinnerModel);
130
// }
131
//
132
// }
133
// } else if (metaData instanceof StringMetaData) {
134
// StringMetaData stringMetaData = (StringMetaData) metaData;
135
// if (stringMetaData.isMultiLine()) {
136
// int columns = stringMetaData.getDisplayWidth();
137
// int rows = Math.min(stringMetaData.getMaxLength()/columns, 5);
138
// return new JTextArea(rows, columns);
139
// }
140
// int fieldLength = Math.min(stringMetaData.getDisplayWidth(),
141
// stringMetaData.getMaxLength());
142
// return new JTextField(fieldLength);
143
// }
144
//
145
// // return editors based on type
146
//
147
// if (fieldClass == Boolean.class || fieldClass == boolean.class) {
148
// JCheckBox checkbox = new JCheckBox();
149
// return checkbox;
150
//
151
// }
152
// else if (fieldClass.isArray() || fieldClass.equals(List.class)) {
153
// return new JXListPanel();
154
//
155
// }
156
// else if (fieldClass.equals(String.class)) {
157
// return new JTextField(24);
158
//
159
// }
160
// else if (fieldClass.equals(Date.class) ||
161
// fieldClass.equals(Calendar.class)) {
162
// // create a date picker control.
163
// return new JXDatePicker();
164
//
165
// }
166
// else if (fieldClass.equals(Image.class) ||
167
// fieldClass.equals(Icon.class)) {
168
// return new JXImagePanel();
169
// }
170
// else if (fieldClass.equals(Link.class)) {
171
// JLabel label = new JLabel();
172
// label.addMouseListener(new LinkHandler());
173
// label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
174
//
175
// return label;
176
// }
177
//
178
// // fallback...
179
// return new JLabel();
180
// }
181
//
182
// public Binding createBinding(DataModel model, String fieldName, JComponent component) {
183
// /**@todo aim: temporary until factory is created */
184
// Binding binding = null;
185
//
186
// if (component instanceof JForm) {
187
// return new FormBinding((JForm)component, model, fieldName);
188
// }
189
// if (component instanceof JCheckBox) {
190
// binding = new BooleanBinding((JCheckBox) component, model, fieldName);
191
// }
192
// if (component instanceof JXRadioGroup) {
193
// binding = new RadioBinding((JXRadioGroup)component, model, fieldName);
194
// }
195
// if (component instanceof JLabel) {
196
// return new LabelBinding((JLabel)component, model, fieldName);
197
// }
198
// if (component instanceof JTextComponent) {
199
// binding = new TextBinding((JTextComponent) component, model, fieldName);
200
// int iconPosition = component instanceof JTextArea?
201
// SwingConstants.NORTH_EAST : SwingConstants.WEST;
202
// BindingBorder bborder = new BindingBorder(binding, iconPosition);
203
// Insets insets = bborder.getBorderInsets(component);
204
// Dimension prefSize = component.getPreferredSize();
205
// prefSize.width += (insets.left + insets.right);
206
// component.setPreferredSize(prefSize);
207
// component.setBorder(new CompoundBorder(component.getBorder(),bborder));
208
// return binding;
209
// }
210
// if (component instanceof JComboBox) {
211
// binding = new ComboBoxBinding((JComboBox)component, model, fieldName);
212
// }
213
// if (component instanceof JSpinner) {
214
// binding = new SpinnerBinding( (JSpinner) component, model,
215
// fieldName);
216
// }
217
// if (component instanceof JXDatePicker) {
218
// return new DatePickerBinding( (JXDatePicker) component, model,
219
// fieldName);
220
// }
221
// if (component instanceof JXImagePanel) {
222
// return new ImagePanelBinding( (JXImagePanel) component, model,
223
// fieldName);
224
// }
225
// if (component instanceof JXListPanel) {
226
// return new ListBinding( ( (JXListPanel) component).getList(), model,
227
// fieldName);
228
// }
229
// doAddBindingBorder(component, binding);
230
// return binding;
231
// }
232

233     private void doAddBindingBorder(JComponent JavaDoc component, Binding binding) {
234 // component.setBorder(new CompoundBorder(new BindingBorder(binding),
235
// component.getBorder()));
236
}
237
238     public void addComponent(JComponent JavaDoc parent, JComponent JavaDoc component,
239                              MetaData metaData) {
240         GridBagLayout JavaDoc gridbag = initializeLayout(parent);
241
242         // Add label
243
JLabel JavaDoc label;
244         
245
246         String JavaDoc labelText = metaData.getLabel();
247         if ((labelText != null) && (labelText.length() > 0)) {
248             labelText += ":";
249         } else {
250             labelText = "";
251         }
252         if (metaData.isRequired()) {
253             label = new JLabel JavaDoc(labelText, requiredIcon,
254                                JLabel.CENTER);
255             label.setHorizontalTextPosition(JLabel.TRAILING);
256         }
257         else {
258             label = new JLabel JavaDoc(labelText);
259         }
260         Font JavaDoc boldFont = label.getFont().deriveFont(Font.BOLD);
261         label.setFont(boldFont);
262         label.setHorizontalAlignment(JLabel.RIGHT);
263
264         Rectangle JavaDoc bounds = label.getBounds();
265         Rectangle JavaDoc iconRect = new Rectangle JavaDoc();
266         Rectangle JavaDoc textRect = new Rectangle JavaDoc();
267         /*
268         String value = SwingUtilities.layoutCompoundLabel(label,
269             label.getFontMetrics(label.getFont()),
270             label.getText(),
271             label.getIcon(),
272             label.getVerticalAlignment(),
273             label.getHorizontalAlignment(),
274             label.getVerticalTextPosition(),
275             label.getHorizontalTextPosition(),
276             label.getBounds(),
277             iconRect, textRect,
278             label.getIconTextGap());
279         System.out.println("label bounds=" + bounds.x + "," + bounds.y +
280                            " " + bounds.width + "x" + bounds.height +
281                            " textRect=" + textRect.x + "," + textRect.y +
282                            " " + textRect.width + "x" + textRect.height +
283                            " value=" + value);
284             */

285
286         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
287
288         Integer JavaDoc gridY = (Integer JavaDoc) parent.getClientProperty(
289             "Form.gridY");
290         if (gridY == null) {
291             gridY = new Integer JavaDoc(0);
292         }
293
294         c.gridy = gridY.intValue();
295         c.gridx = 0;
296         c.gridwidth = 1;
297         c.gridheight = 1;
298         c.anchor = GridBagConstraints.NORTHEAST;
299         if (component instanceof JForm) {
300             // add a little space above nested form's label to set it apart
301
c.insets = nestedFormLabelInsets;
302         }
303         else {
304             c.insets = labelInsets;
305         }
306         gridbag.setConstraints(label, c);
307         parent.add(label);
308
309         // Now add component
310
if (component instanceof JForm) {
311             // if nested form, put it below label
312
c.gridy++;
313             c.gridwidth = 2;
314             c.insets = new Insets JavaDoc(0, 25, 0, 0);
315         }
316         else {
317             // if not a nested form, put to the right of label
318
c.gridwidth = 1;
319             c.gridx = 1;
320             c.insets = (component instanceof JLabel JavaDoc ? labelInsets :
321                         componentInsets);
322         }
323         c.anchor = GridBagConstraints.NORTHWEST;
324
325         gridbag.setConstraints(component, c);
326         parent.add(component);
327
328         gridY = new Integer JavaDoc(c.gridy+1);
329         parent.putClientProperty("Form.gridY", gridY);
330     }
331
332     private GridBagLayout JavaDoc initializeLayout(JComponent JavaDoc parent) {
333         LayoutManager JavaDoc layout = parent.getLayout();
334         if (!(layout instanceof GridBagLayout JavaDoc)) {
335             layout = new GridBagLayout JavaDoc();
336             parent.setLayout(layout);
337         }
338         return (GridBagLayout JavaDoc)layout;
339     }
340
341
342 }
343
Popular Tags