1 7 8 package org.jdesktop.swing.binding; 9 10 import java.awt.Dimension ; 11 import java.awt.Insets ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Map ; 15 import java.util.Set ; 16 17 import javax.swing.JCheckBox ; 18 import javax.swing.JComboBox ; 19 import javax.swing.JComponent ; 20 import javax.swing.JLabel ; 21 import javax.swing.JList ; 22 import javax.swing.JSpinner ; 23 import javax.swing.JTable ; 24 import javax.swing.JTextArea ; 25 import javax.swing.SwingConstants ; 26 import javax.swing.border.CompoundBorder ; 27 import javax.swing.text.JTextComponent ; 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 56 public class BindingMap { 57 58 private Map bindingMap; 59 private static BindingMap instance; 60 61 62 public static BindingMap getInstance() { 63 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 88 public Binding createBinding(JComponent component, DataModel model, String fieldName) { 89 BindingCreator creator = getBindingCreator(component); 90 if (creator != null) { 91 return creator.createBinding(component, model, fieldName); 92 } 93 return null; 94 } 95 96 110 protected BindingCreator getBindingCreator(JComponent component) { 111 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 componentClass) { 121 Set keys = getBindingMap().keySet(); 122 for (Iterator iter = keys.iterator(); iter.hasNext(); ) { 123 Class element = (Class ) iter.next(); 124 if (element.isAssignableFrom(componentClass)) { 125 return (BindingCreator) getBindingMap().get(element); 126 } 127 } 128 return null; 129 } 130 131 protected Map getBindingMap() { 132 if (bindingMap == null) { 133 bindingMap = new HashMap (); 134 initBindingMap(bindingMap); 135 } 136 return bindingMap; 137 } 138 139 protected void initBindingMap(Map map) { 140 map.put(JForm.class, new FormBindingCreator()); 141 map.put(JXRadioGroup.class, new RadioGroupBindingCreator()); 142 map.put(JLabel .class, new LabelBindingCreator()); 143 map.put(JCheckBox .class, new CheckBoxBindingCreator()); 144 BindingCreator textBindingCreator = new TextBindingCreator(); 145 map.put(JTextComponent .class, textBindingCreator); 146 map.put(JComboBox .class, new ComboBoxBindingCreator()); 147 BindingCreator tableBindingCreator = new TableBindingCreator(); 148 map.put(JTable .class, tableBindingCreator); 149 BindingCreator listBindingCreator = new ListBindingCreator(); 150 map.put(JList .class, listBindingCreator); 151 map.put(JXListPanel.class, new ListPanelBindingCreator()); 152 map.put(JSpinner .class, new SpinnerBindingCreator()); 153 map.put(JXImagePanel.class, new ImagePanelBindingCreator()); 154 map.put(JXDatePicker.class, new DatePickerBindingCreator()); 155 } 156 157 159 161 public static class ListBindingCreator implements BindingCreator { 162 163 public Binding createBinding(JComponent component, DataModel dataModel, 164 String fieldName) { 165 return new ListBinding((JList ) component, dataModel, fieldName); 166 167 } 168 } 169 170 172 public static class ListPanelBindingCreator implements BindingCreator { 173 174 public Binding createBinding(JComponent component, DataModel dataModel, 175 String fieldName) { 176 return new ListBinding(((JXListPanel) component).getList(), dataModel, fieldName); 177 178 } 179 } 180 182 public class TableBindingCreator implements BindingCreator { 183 184 public Binding createBinding(JComponent component, DataModel dataModel, 185 String fieldName) { 186 return new TableBinding((JTable ) component, dataModel, fieldName); 187 } 188 } 189 190 192 public static class FormBindingCreator implements BindingCreator { 193 194 public Binding createBinding(JComponent component, DataModel dataModel, 195 String fieldName) { 196 return new FormBinding((JForm) component, dataModel, fieldName); 197 } 198 } 199 200 202 public static class TextBindingCreator implements BindingCreator { 203 204 public Binding createBinding(JComponent component, DataModel dataModel, 205 String fieldName) { 206 Binding binding = doCreateBinding((JTextComponent ) component, 207 dataModel, fieldName); 208 configureComponent(component, binding); 209 return binding; 210 } 211 212 protected Binding doCreateBinding(JTextComponent component, 213 DataModel dataModel, String fieldName) { 214 Binding binding = new TextBinding(component, dataModel, fieldName); 215 return binding; 216 } 217 218 223 protected void configureComponent(JComponent component, Binding binding) { 224 int iconPosition = (component instanceof JTextArea ) 225 ? SwingConstants.NORTH_EAST : SwingConstants.WEST; 226 BindingBorder bborder = new BindingBorder(binding, iconPosition); 227 component.setBorder(new CompoundBorder (component.getBorder(), bborder)); 233 } 234 } 235 236 238 public static class LabelBindingCreator implements BindingCreator { 239 240 public Binding createBinding(JComponent component, DataModel dataModel, 241 String fieldName) { 242 return new LabelBinding((JLabel ) component, dataModel, fieldName); 243 } 244 } 245 246 248 public static class ImagePanelBindingCreator implements BindingCreator { 249 250 public Binding createBinding(JComponent component, DataModel dataModel, 251 String fieldName) { 252 return new ImagePanelBinding((JXImagePanel) component, dataModel, fieldName); 253 } 254 } 255 256 258 public static class DatePickerBindingCreator implements BindingCreator { 259 260 public Binding createBinding(JComponent component, DataModel dataModel, 261 String fieldName) { 262 return new DatePickerBinding((JXDatePicker) component, dataModel, fieldName); 263 } 264 } 265 266 268 public static abstract class RequiredBindingCreator implements BindingCreator { 269 270 protected void doAddBindingBorder(JComponent component, Binding binding) { 271 component.setBorder(new CompoundBorder (new BindingBorder(binding), 272 component.getBorder())); 273 } 274 } 275 276 278 public static class ComboBoxBindingCreator extends RequiredBindingCreator { 279 280 public Binding createBinding(JComponent component, DataModel dataModel, 281 String fieldName) { 282 Binding binding = new ComboBoxBinding((JComboBox ) component, dataModel, 283 fieldName); 284 doAddBindingBorder(component, binding); 285 return binding; 286 } 287 } 288 289 291 public static class RadioGroupBindingCreator extends RequiredBindingCreator { 292 293 public Binding createBinding(JComponent component, DataModel dataModel, 294 String fieldName) { 295 Binding binding = new RadioBinding((JXRadioGroup) component, dataModel, 296 fieldName); 297 doAddBindingBorder(component, binding); 298 return binding; 299 } 300 } 301 302 304 public static class CheckBoxBindingCreator extends RequiredBindingCreator { 305 306 public Binding createBinding(JComponent component, DataModel dataModel, 307 String fieldName) { 308 Binding binding = new BooleanBinding((JCheckBox ) component, dataModel, 309 fieldName); 310 doAddBindingBorder(component, binding); 311 return binding; 312 } 313 } 314 315 317 public static class SpinnerBindingCreator extends RequiredBindingCreator { 318 319 public Binding createBinding(JComponent component, DataModel dataModel, 320 String fieldName) { 321 Binding binding = new SpinnerBinding((JSpinner ) component, dataModel, 322 fieldName); 323 doAddBindingBorder(component, binding); 324 return binding; 325 } 326 } 327 } 328 | Popular Tags |