1 4 package org.oddjob.designer.view; 5 6 import java.awt.BorderLayout ; 7 import java.awt.Component ; 8 import java.awt.Container ; 9 import java.awt.Dimension ; 10 import java.awt.GridBagConstraints ; 11 import java.awt.Insets ; 12 import java.util.Observable ; 13 import java.util.Observer ; 14 15 import javax.swing.AbstractCellEditor ; 16 import javax.swing.DefaultCellEditor ; 17 import javax.swing.JComboBox ; 18 import javax.swing.JLabel ; 19 import javax.swing.JPanel ; 20 import javax.swing.JScrollPane ; 21 import javax.swing.JTable ; 22 import javax.swing.JTextField ; 23 import javax.swing.event.TableModelEvent ; 24 import javax.swing.table.AbstractTableModel ; 25 import javax.swing.table.TableCellEditor ; 26 import javax.swing.table.TableCellRenderer ; 27 import javax.swing.table.TableColumn ; 28 29 import org.oddjob.designer.Looks; 30 import org.oddjob.designer.model.DesignDefinition; 31 import org.oddjob.designer.model.DesignElement; 32 import org.oddjob.designer.model.MultiTypeTable; 33 34 38 public class MultiTypeTableView implements ViewProducer { 39 40 private final ValuesTableModel tm = new ValuesTableModel(); 41 private final MultiTypeTable designDefinition; 42 43 private final boolean namedValues; 44 45 private Component component; 46 47 public MultiTypeTableView(MultiTypeTable designDefinition) { 48 this.designDefinition = designDefinition; 49 this.namedValues = designDefinition.isNamed(); 50 this.component = component(); 51 designDefinition.addObserver(new Observer () { 52 public void update(Observable o, Object arg) { 53 tm.fireTableChanged(new TableModelEvent (tm)); 54 } 55 }); 56 } 57 58 public boolean isNamedValues() { 59 return namedValues; 60 } 61 62 public Component dialog() { 63 return group(); 64 } 65 66 public Component group() { 67 JPanel form = new JPanel (); 68 69 form.setLayout(new BorderLayout ()); 70 form.setBorder(Looks.groupBorder(designDefinition.getTitle())); 71 form.add(component, BorderLayout.CENTER); 72 return form; 73 } 74 75 78 public Component detailEdit() { 79 return ViewHelper.createDetailButton(designDefinition); 80 } 81 82 85 public Component cell() { 86 return ViewHelper.createDetailButton(designDefinition); 87 } 88 89 92 public int inline(Container container, int row, int column, 93 boolean selectionInGroup) { 94 95 GridBagConstraints c = new GridBagConstraints (); 96 97 c.weightx = 1.0; 98 c.weighty = 0.0; 99 100 c.fill = GridBagConstraints.HORIZONTAL; 101 c.anchor = GridBagConstraints.NORTHWEST; 102 103 c.gridx = column; 104 c.gridy = row; 105 106 c.gridwidth = GridBagConstraints.REMAINDER; 107 108 c.insets = new Insets (3, 3, 3, 3); 109 110 container.add(group(), c); 111 112 return row + 1; 113 } 114 115 118 public void setEnabled(boolean enabled) { 119 component.setEnabled(enabled); 120 if (!enabled) { 121 while (designDefinition.childCount() > 0) { 122 designDefinition.removeChild(0); 123 } 124 } 125 } 126 127 132 private Component component() { 133 JTable table = new JTable (tm); 134 table.setRowMargin(2); 135 setPreferredTableSize(table); 136 137 TableColumn typeCol = table.getColumnModel().getColumn(0); 138 139 JComboBox comboBox = new JComboBox (); 140 comboBox.addItem(""); 141 String [] types = designDefinition.getSupportedTypes(); 142 for (int i = 0; i < types.length; ++i) { 143 comboBox.addItem(types[i]); 144 } 145 typeCol.setCellEditor(new DefaultCellEditor (comboBox)); 146 147 TableColumn valueCol = table.getColumnModel().getColumn( 148 tm.getColumnCount() - 1); 149 valueCol.setCellEditor(new DialogEditor()); 150 valueCol.setCellRenderer(new DialogRenderer()); 151 152 JScrollPane jsp = new JScrollPane (table); 153 return jsp; 154 } 155 156 private void setPreferredTableSize(JTable table){ 157 int height = 0; 158 for(int row=0; row < designDefinition.getVisibleRows(); row++) 159 height += table.getRowHeight(row); 160 161 table.setPreferredScrollableViewportSize(new Dimension ( 162 Looks.DETAIL_USABLE_WIDTH - 100, 163 height)); 164 } 165 166 class ValuesTableModel extends AbstractTableModel { 167 168 String [] unnamedHeaders = { "Type", "Value" }; 169 String [] namedHeaders = { "Type", "Name", "Value" }; 170 171 public String getColumnName(int c) { 172 if (isNamedValues()) { 173 return namedHeaders[c]; 174 } else { 175 return unnamedHeaders[c]; 176 } 177 } 178 179 public int getColumnCount() { 180 return isNamedValues() ? 3 : 2; 181 } 182 183 public int getRowCount() { 184 return designDefinition.childCount() + 1; 185 } 186 187 public boolean isCellEditable(int rowIndex, int columnIndex) { 188 if (columnIndex == 0) { 189 return true; 190 } 191 if (!"".equals(getValueAt(rowIndex, 0))) { 192 return true; 193 } 194 return false; 195 } 196 197 public Object getValueAt(int rowIndex, int columnIndex) { 198 if (rowIndex == getRowCount() - 1) { 199 return ""; 200 } 201 202 if (columnIndex == 0) { 203 return designDefinition.getChildType(rowIndex); 204 } else if (columnIndex == 1) { 205 if (isNamedValues()) { 206 return designDefinition.getChildName(rowIndex); 207 } else { 208 return designDefinition.getChildValue(rowIndex); 209 } 210 } else if (columnIndex == 2) { 211 return designDefinition.getChildValue(rowIndex); 212 } else { 213 throw new RuntimeException ("This should be impossible!"); 214 } 215 } 216 217 public void setValueAt(Object value, int rowIndex, int columnIndex) { 218 if (columnIndex == 0) { 219 String type = (String ) value; 220 if (rowIndex < getRowCount() - 1) { 221 String oldType = designDefinition.getChildType(rowIndex); 222 if (type.equals(oldType)) { 223 return; 225 } 226 else { 227 designDefinition.removeChild(rowIndex); 228 } 229 } 230 if ("".equals(type)) { 231 return; 232 } 233 designDefinition.insertChild(rowIndex, type); 234 } else if (columnIndex == 1) { 235 if (isNamedValues()) { 236 designDefinition.setChildName(rowIndex, (String )value); 237 } 238 } else if (columnIndex == 2) { 239 } else { 241 throw new RuntimeException ("This should be impossible!"); 242 } 243 } 244 245 } 246 247 public class DialogEditor extends AbstractCellEditor 248 implements TableCellEditor { 249 DesignElement value; 250 251 public Object getCellEditorValue() { 253 return value; 254 } 255 256 public Component getTableCellEditorComponent(JTable table, 258 Object value, boolean isSelected, int row, int column) { 259 this.value = (DesignElement) value; 260 return generateRenderer((DesignElement) value); 261 } 262 } 263 264 class DialogRenderer implements TableCellRenderer { 265 public Component getTableCellRendererComponent(JTable table, 266 Object value, boolean isSelected, boolean hasFocus, int row, 267 int column) { 268 if (value instanceof String ) { 269 return new JLabel (value.toString()); 270 } else { 271 return generateRenderer((DesignElement) value); 272 } 273 } 274 } 275 276 277 static Component generateRenderer(DesignElement de) { 278 if (de.hasDetail()) { 279 return ViewFactory.create(de.detail()).cell(); 280 } 281 JTextField nothing = new JTextField (); 282 nothing.setEnabled(false); 283 return nothing; 284 } 285 286 } 287 | Popular Tags |