1 7 8 package org.jdesktop.swing.form; 9 10 import java.awt.Font ; 11 import java.awt.GridBagConstraints ; 12 import java.awt.GridBagLayout ; 13 import java.awt.Insets ; 14 import java.awt.LayoutManager ; 15 import java.awt.Rectangle ; 16 import java.net.URL ; 17 18 import javax.swing.Icon ; 19 import javax.swing.ImageIcon ; 20 import javax.swing.JComponent ; 21 import javax.swing.JLabel ; 22 import javax.swing.JScrollPane ; 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 36 37 public class DefaultFormFactory extends FormFactory { 38 private static Icon requiredIcon; 39 40 static { 41 URL url = BindingBorder.class.getResource("resources/asterisk.8x8.png"); 42 requiredIcon = new ImageIcon (url); 43 } 44 45 private Insets labelInsets; 46 private Insets nestedFormLabelInsets; 47 private Insets componentInsets; 48 49 public DefaultFormFactory() { 50 labelInsets = new Insets (4, 5, 5, 4); 51 nestedFormLabelInsets = new Insets (12, 5, 5, 4); 52 componentInsets = new Insets (1, 4, 4, 4); 53 } 54 55 58 public JComponent createComponent(MetaData metaData) { 59 if (isNonVisual(metaData)) { 60 return null; 61 } 62 JComponent component = ComponentMap.getInstance().createComponent(metaData); 63 if (component == null) { 65 throw new IllegalStateException ("some error in ComponentMap"); 66 } 67 return component; 68 } 69 70 73 public Binding createBinding(DataModel model, String fieldName, 74 JComponent component) { 75 if (component == null) { 76 return null; 77 } 78 if (component instanceof JScrollPane ) { 79 component = 80 (JComponent ) ((JScrollPane ) component).getViewport().getView(); 81 } 82 return BindingMap.getInstance().createBinding(component, model, fieldName); 83 } 84 85 86 87 233 private void doAddBindingBorder(JComponent component, Binding binding) { 234 } 237 238 public void addComponent(JComponent parent, JComponent component, 239 MetaData metaData) { 240 GridBagLayout gridbag = initializeLayout(parent); 241 242 JLabel label; 244 245 246 String 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 (labelText, requiredIcon, 254 JLabel.CENTER); 255 label.setHorizontalTextPosition(JLabel.TRAILING); 256 } 257 else { 258 label = new JLabel (labelText); 259 } 260 Font boldFont = label.getFont().deriveFont(Font.BOLD); 261 label.setFont(boldFont); 262 label.setHorizontalAlignment(JLabel.RIGHT); 263 264 Rectangle bounds = label.getBounds(); 265 Rectangle iconRect = new Rectangle (); 266 Rectangle textRect = new Rectangle (); 267 285 286 GridBagConstraints c = new GridBagConstraints (); 287 288 Integer gridY = (Integer ) parent.getClientProperty( 289 "Form.gridY"); 290 if (gridY == null) { 291 gridY = new Integer (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 c.insets = nestedFormLabelInsets; 302 } 303 else { 304 c.insets = labelInsets; 305 } 306 gridbag.setConstraints(label, c); 307 parent.add(label); 308 309 if (component instanceof JForm) { 311 c.gridy++; 313 c.gridwidth = 2; 314 c.insets = new Insets (0, 25, 0, 0); 315 } 316 else { 317 c.gridwidth = 1; 319 c.gridx = 1; 320 c.insets = (component instanceof JLabel ? labelInsets : 321 componentInsets); 322 } 323 c.anchor = GridBagConstraints.NORTHWEST; 324 325 gridbag.setConstraints(component, c); 326 parent.add(component); 327 328 gridY = new Integer (c.gridy+1); 329 parent.putClientProperty("Form.gridY", gridY); 330 } 331 332 private GridBagLayout initializeLayout(JComponent parent) { 333 LayoutManager layout = parent.getLayout(); 334 if (!(layout instanceof GridBagLayout )) { 335 layout = new GridBagLayout (); 336 parent.setLayout(layout); 337 } 338 return (GridBagLayout )layout; 339 } 340 341 342 } 343 | Popular Tags |