KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > BindingMap


1 /*
2  * $Id: BindingMap.java,v 1.4 2005/03/01 13:27:10 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.binding;
9
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Insets JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.Map JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import javax.swing.JCheckBox JavaDoc;
18 import javax.swing.JComboBox JavaDoc;
19 import javax.swing.JComponent JavaDoc;
20 import javax.swing.JLabel JavaDoc;
21 import javax.swing.JList JavaDoc;
22 import javax.swing.JSpinner JavaDoc;
23 import javax.swing.JTable JavaDoc;
24 import javax.swing.JTextArea JavaDoc;
25 import javax.swing.SwingConstants JavaDoc;
26 import javax.swing.border.CompoundBorder JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28
29 import org.jdesktop.swing.JXDatePicker;
30 import org.jdesktop.swing.JXImagePanel;
31 import org.jdesktop.swing.JXListPanel;
32 import org.jdesktop.swing.JXRadioGroup;
33 import org.jdesktop.swing.data.DataModel;
34 import org.jdesktop.swing.form.BindingBorder;
35 import org.jdesktop.swing.form.JForm;
36
37 /**
38  * Choosing strategy for creating Bindings.<p>
39  *
40  * Extracted from DefaultFormFactory to have a
41  * "pluggable" place for creating custom bindings. The usage
42  * of a BindingCreator should be viewed as an implementation
43  * detail, they don't do much.
44  *
45  * PENDING: there's a implicit coupling to ComponentMap -
46  * the BindingMap assumes that the ComponentMap did a
47  * good enough job when choosing components. <p>
48  *
49  * PENDING: should be factored into an interface and
50  * a default implementation.<p>
51  *
52  * PENDING: really want to configure the component here?
53  *
54  * @author Jeanette Winzenburg
55  */

56 public class BindingMap {
57
58   private Map JavaDoc bindingMap;
59   private static BindingMap instance;
60   
61   
62   public static BindingMap getInstance() {
63     // this is not thread safe...
64
if (instance == null) {
65       instance = new BindingMap();
66     }
67     return instance;
68   }
69
70   public static void setInstance(BindingMap bindingMap) {
71     instance = bindingMap;
72   }
73
74   /**
75    * Creates and returns Binding between the
76    * component and a field of the DataModel.
77    *
78    * PENDING: null return value? Or better
79    * throw BindingException if no appropriate creator found?
80    *
81    * @param component
82    * @param model
83    * @param fieldName
84    * @return
85    *
86    * @throws NullPointerException if any of the parameters is null.
87    */

88   public Binding createBinding(JComponent JavaDoc component, DataModel model, String JavaDoc fieldName) {
89     BindingCreator creator = getBindingCreator(component);
90     if (creator != null) {
91         return creator.createBinding(component, model, fieldName);
92     }
93     return null;
94   }
95   
96   /** encapsulates lookup strategy to find an appropriate
97    * BindingCreator for the given component. <p>
98    *
99    * Here:
100    * <ol>
101    * <li> look-up by component class
102    * <li> look-up by assignables to component class
103    * </ol>
104    *
105    *
106    * @param component
107    * @return a BindingCreator which can create a binding to
108    * the component or null if none is found.
109    */

110   protected BindingCreator getBindingCreator(JComponent JavaDoc component) {
111     // PENDING: implement better lookup...
112
BindingCreator creator =
113       (BindingCreator) getBindingMap().get(component.getClass());
114     if (creator == null) {
115       creator = findByAssignable(component.getClass());
116     }
117     return creator;
118   }
119
120   protected BindingCreator findByAssignable(Class JavaDoc componentClass) {
121     Set JavaDoc keys = getBindingMap().keySet();
122     for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext(); ) {
123       Class JavaDoc element = (Class JavaDoc) iter.next();
124       if (element.isAssignableFrom(componentClass)) {
125         return (BindingCreator) getBindingMap().get(element);
126       }
127     }
128     return null;
129   }
130
131   protected Map JavaDoc getBindingMap() {
132     if (bindingMap == null) {
133       bindingMap = new HashMap JavaDoc();
134       initBindingMap(bindingMap);
135     }
136     return bindingMap;
137   }
138
139   protected void initBindingMap(Map JavaDoc map) {
140     map.put(JForm.class, new FormBindingCreator());
141     map.put(JXRadioGroup.class, new RadioGroupBindingCreator());
142     map.put(JLabel JavaDoc.class, new LabelBindingCreator());
143     map.put(JCheckBox JavaDoc.class, new CheckBoxBindingCreator());
144     BindingCreator textBindingCreator = new TextBindingCreator();
145     map.put(JTextComponent JavaDoc.class, textBindingCreator);
146     map.put(JComboBox JavaDoc.class, new ComboBoxBindingCreator());
147     BindingCreator tableBindingCreator = new TableBindingCreator();
148     map.put(JTable JavaDoc.class, tableBindingCreator);
149     BindingCreator listBindingCreator = new ListBindingCreator();
150     map.put(JList JavaDoc.class, listBindingCreator);
151     map.put(JXListPanel.class, new ListPanelBindingCreator());
152     map.put(JSpinner JavaDoc.class, new SpinnerBindingCreator());
153     map.put(JXImagePanel.class, new ImagePanelBindingCreator());
154     map.put(JXDatePicker.class, new DatePickerBindingCreator());
155   }
156
157 //-------------------------------- BindingCreators
158

159   /**
160    */

161   public static class ListBindingCreator implements BindingCreator {
162
163     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
164         String JavaDoc fieldName) {
165       return new ListBinding((JList JavaDoc) component, dataModel, fieldName);
166       
167     }
168   }
169
170   /**
171    */

172   public static class ListPanelBindingCreator implements BindingCreator {
173
174     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
175         String JavaDoc fieldName) {
176       return new ListBinding(((JXListPanel) component).getList(), dataModel, fieldName);
177       
178     }
179   }
180   /**
181    */

182   public class TableBindingCreator implements BindingCreator {
183
184     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
185         String JavaDoc fieldName) {
186       return new TableBinding((JTable JavaDoc) component, dataModel, fieldName);
187     }
188   }
189
190   /**
191    */

192   public static class FormBindingCreator implements BindingCreator {
193
194     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
195         String JavaDoc fieldName) {
196       return new FormBinding((JForm) component, dataModel, fieldName);
197     }
198   }
199
200   /**
201    */

202   public static class TextBindingCreator implements BindingCreator {
203
204     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
205         String JavaDoc fieldName) {
206       Binding binding = doCreateBinding((JTextComponent JavaDoc) component,
207                           dataModel, fieldName);
208       configureComponent(component, binding);
209       return binding;
210     }
211
212     protected Binding doCreateBinding(JTextComponent JavaDoc component,
213         DataModel dataModel, String JavaDoc fieldName) {
214       Binding binding = new TextBinding(component, dataModel, fieldName);
215       return binding;
216     }
217
218     /**
219      * PENDING: it's a view issue, should not be done here?.
220      * @param component
221      * @param binding
222      */

223     protected void configureComponent(JComponent JavaDoc component, Binding binding) {
224       int iconPosition = (component instanceof JTextArea JavaDoc)
225                            ? SwingConstants.NORTH_EAST : SwingConstants.WEST;
226       BindingBorder bborder = new BindingBorder(binding, iconPosition);
227 // Insets insets = bborder.getBorderInsets(component);
228
// Dimension prefSize = component.getPreferredSize();
229
// prefSize.width += (insets.left + insets.right);
230
// // JW: arrgghhh... never do, prevents correct resizing.
231
// component.setPreferredSize(prefSize);
232
component.setBorder(new CompoundBorder JavaDoc(component.getBorder(), bborder));
233     }
234   }
235
236   /**
237    */

238   public static class LabelBindingCreator implements BindingCreator {
239
240     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
241         String JavaDoc fieldName) {
242       return new LabelBinding((JLabel JavaDoc) component, dataModel, fieldName);
243     }
244   }
245
246   /**
247    */

248   public static class ImagePanelBindingCreator implements BindingCreator {
249
250     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
251         String JavaDoc fieldName) {
252       return new ImagePanelBinding((JXImagePanel) component, dataModel, fieldName);
253     }
254   }
255
256   /**
257    */

258   public static class DatePickerBindingCreator implements BindingCreator {
259
260     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
261         String JavaDoc fieldName) {
262       return new DatePickerBinding((JXDatePicker) component, dataModel, fieldName);
263     }
264   }
265
266   /**
267    */

268   public static abstract class RequiredBindingCreator implements BindingCreator {
269
270     protected void doAddBindingBorder(JComponent JavaDoc component, Binding binding) {
271       component.setBorder(new CompoundBorder JavaDoc(new BindingBorder(binding),
272           component.getBorder()));
273     }
274   }
275
276   /**
277    */

278   public static class ComboBoxBindingCreator extends RequiredBindingCreator {
279
280     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
281         String JavaDoc fieldName) {
282       Binding binding = new ComboBoxBinding((JComboBox JavaDoc) component, dataModel,
283                           fieldName);
284       doAddBindingBorder(component, binding);
285       return binding;
286     }
287   }
288
289   /**
290    */

291   public static class RadioGroupBindingCreator extends RequiredBindingCreator {
292
293     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
294         String JavaDoc fieldName) {
295       Binding binding = new RadioBinding((JXRadioGroup) component, dataModel,
296                           fieldName);
297       doAddBindingBorder(component, binding);
298       return binding;
299     }
300   }
301
302   /**
303    */

304   public static class CheckBoxBindingCreator extends RequiredBindingCreator {
305
306     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
307         String JavaDoc fieldName) {
308       Binding binding = new BooleanBinding((JCheckBox JavaDoc) component, dataModel,
309                           fieldName);
310       doAddBindingBorder(component, binding);
311       return binding;
312     }
313   }
314
315   /**
316    */

317   public static class SpinnerBindingCreator extends RequiredBindingCreator {
318
319     public Binding createBinding(JComponent JavaDoc component, DataModel dataModel,
320         String JavaDoc fieldName) {
321       Binding binding = new SpinnerBinding((JSpinner JavaDoc) component, dataModel,
322                           fieldName);
323       doAddBindingBorder(component, binding);
324       return binding;
325     }
326   }
327 }
328
Popular Tags