1 7 8 package org.jdesktop.jdnc; 9 10 import java.awt.BorderLayout ; 11 import java.awt.Color ; 12 import java.awt.Component ; 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 ; 25 import javax.swing.Box ; 26 import javax.swing.Icon ; 27 import javax.swing.ImageIcon ; 28 import javax.swing.JButton ; 29 import javax.swing.JComponent ; 30 import javax.swing.border.EmptyBorder ; 31 32 41 public class JNForm extends JNComponent { 42 private JForm form; 43 private JComponent buttonPanel; 44 private Component trailGlue; 45 46 public JNForm() { 47 super(); 48 setLayout(new BorderLayout ()); 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 71 72 } 73 74 protected JForm createForm() { 75 return new JForm(); 76 } 77 78 public JForm getForm() { 79 return form; 80 } 81 82 92 public void bind(DefaultTableModelExt tabularData) throws BindException { 93 form.bind(tabularData); 94 } 95 96 106 public void bind(DefaultTableModelExt tabularData, String columnName) throws BindException { 107 form.bind(tabularData, columnName); 108 } 109 110 119 public void bind(Object bean) throws BindException { 120 form.bind(bean); 121 } 122 123 133 public void bind(Object bean, String propertyName) throws BindException { 134 form.bind(bean, propertyName); 135 } 136 137 146 public void bind(DataModel model) throws BindException { 147 form.bind(model); 148 } 149 150 160 public void bind(DataModel model, String fieldName) throws BindException { 161 form.bind(model, fieldName); 162 } 163 164 165 public JButton addAction(Action action) { 166 JButton button = new JButton (action); 167 button.setBackground(getBackground()); 168 169 if (action instanceof BoundAction) { 170 BoundAction ba = (BoundAction) action; 175 Object 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 191 public void setBackground(Color background) { 192 super.setBackground(background); 193 if (form != null) { 194 form.setBackground(background); 195 } 196 if (buttonPanel != null) { 197 buttonPanel.setBackground(background); 198 Component children[] = buttonPanel.getComponents(); 199 for(int i = 0; i < children.length; i++) { 200 children[i].setBackground(background); 201 } 202 } 203 } 204 205 protected JComponent createButtonPanel() { 206 Box box = Box.createHorizontalBox(); 207 box.setBorder(new EmptyBorder (6,6,6,6)); 208 box.add(Box.createHorizontalGlue()); 209 return box; 210 } 211 212 protected void addToButtonPanel(JComponent component) { 213 int childCount = buttonPanel.getComponentCount(); 214 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 |