1 4 package org.oddjob.designer.view; 5 6 import java.awt.Component ; 7 import java.awt.Container ; 8 import java.awt.GridBagConstraints ; 9 import java.awt.GridBagLayout ; 10 import java.awt.Insets ; 11 import java.awt.event.FocusEvent ; 12 import java.awt.event.FocusListener ; 13 import java.util.Observable ; 14 import java.util.Observer ; 15 16 import javax.swing.JLabel ; 17 import javax.swing.JPanel ; 18 import javax.swing.JTextField ; 19 import javax.swing.SwingConstants ; 20 21 import org.oddjob.designer.Looks; 22 import org.oddjob.designer.arooa.DesignIH; 23 import org.oddjob.designer.model.DesignAttribute; 24 import org.oddjob.designer.model.ElementField; 25 26 42 public class ElementFieldView implements ViewProducer { 43 44 private final DesignAttribute element; 45 46 private final JLabel label; 47 private final JTextField textField; 48 private Component detailButton; 49 50 55 public ElementFieldView(ElementField elementField) { 56 this.element = elementField.getDesignElement(); 57 58 String title = elementField.getTitle(); 59 label = new JLabel (ViewHelper.padLabel(title), SwingConstants.LEADING); 60 61 textField = new JTextField (Looks.TEXT_FIELD_SIZE); 62 updateView(); 63 64 textField.addFocusListener(new FocusListener () { 65 public void focusGained(FocusEvent e) { 66 } 67 public void focusLost(FocusEvent e) { 68 element.attribute(textField.getText()); 69 } 70 }); 71 72 ((Observable ) element).addObserver(new Observer () { 73 public void update(Observable o, Object arg) { 74 updateView(); 75 } 76 }); 77 } 78 79 83 private void updateView() { 84 DesignIH dih = DesignIH.getHelper(element.getClass()); 85 if (dih.hasDetailData(element)) { 86 textField.setText("(Edit for detail)"); 87 } 88 else { 89 textField.setText(element.attribute()); 90 } 91 } 92 93 97 public Component dialog() { 98 return group(); 99 } 100 101 105 public Component group() { 106 JPanel panel = new JPanel (new GridBagLayout ()); 107 panel.setBorder(Looks.groupBorder("Value")); 108 inline(panel, 0, 0, false); 109 return panel; 110 } 111 112 116 public Component detailEdit() { 117 throw new UnsupportedOperationException ("Can't nest Element Fields! - and nothing else uses the detail button!"); 118 } 119 120 124 public Component cell() { 125 throw new UnsupportedOperationException ("An element field can't appear in a cell - either use a group or a text field!"); 126 } 127 128 131 public int inline(Container container, int row, int column, 132 boolean selectionInGroup) { 133 int columnCount = column; 134 135 GridBagConstraints c = new GridBagConstraints (); 136 137 c.weightx = 1.0; 138 c.weighty = 0.0; 139 140 c.fill = GridBagConstraints.HORIZONTAL; 141 c.anchor = GridBagConstraints.NORTHWEST; 142 c.gridx = columnCount++; 143 c.gridy = row; 144 if (selectionInGroup) { 145 c.gridwidth = 2; 146 columnCount++; 147 } 148 149 c.insets = new Insets (3, 3, 3, 20); 150 151 container.add(label, c); 152 153 c.fill = GridBagConstraints.NONE; 154 c.anchor = GridBagConstraints.NORTHWEST; 155 c.gridx = columnCount++; 156 c.gridwidth = 1; 157 c.insets = new Insets (3, 0, 3, 0); 158 159 container.add(textField, c); 160 161 if (element.hasDetail()) { 162 detailButton = ViewFactory.create( 163 element.detail()).detailEdit(); 164 c.fill = GridBagConstraints.NONE; 165 c.anchor = GridBagConstraints.NORTHWEST; 166 c.gridx = columnCount++; 167 container.add(detailButton, c); 168 } 169 170 return row + 1; 171 } 172 173 177 public void setEnabled(boolean enabled) { 178 textField.setEditable(enabled); 179 if (detailButton != null) { 180 detailButton.setEnabled(enabled); 181 } 182 if (!enabled) { 183 element.clear(); 184 } 185 } 186 187 } 188 | Popular Tags |