1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.text.*; 59 import java.awt.*; 60 import java.awt.event.*; 61 import javax.swing.*; 62 import javax.swing.table.*; 63 import javax.swing.text.*; 64 import javax.swing.border.*; 65 66 public class CellEditors { 67 protected Border editStateBorder = new LineBorder(Color.black); 68 protected Border invalidStateBorder = new LineBorder(Color.red); 69 70 public FormattedFieldEditor createFormattedFieldEditor( 71 JFormattedTextField.AbstractFormatter formatter, int alignment) { 72 JFormattedTextField field = new JFormattedTextField(formatter); 73 if (alignment >= 0) 74 field.setHorizontalAlignment(alignment); 75 return new FormattedFieldEditor(field); 76 } 77 78 public FormattedFieldEditor createFormattedFieldEditor( 79 Format formatter, int alignment) { 80 JFormattedTextField field = new JFormattedTextField(formatter); 81 if (alignment >= 0) 82 field.setHorizontalAlignment(alignment); 83 return new FormattedFieldEditor(field); 84 } 85 86 public FormattedFieldEditor createFormattedFieldEditor( 87 String mask, int alignment) throws ParseException { 88 MaskFormatter formatter = new MaskFormatter(mask); 89 return createFormattedFieldEditor(formatter, alignment); 90 } 91 92 public TextFieldEditor createTextFieldEditor(int alignment) { 93 JTextField field = new JTextField(); 94 if (alignment >= 0) 95 field.setHorizontalAlignment(alignment); 96 return new TextFieldEditor(field); 97 } 98 99 public CheckBoxEditor createCheckBoxEditor() { 100 JCheckBox checkBox = new JCheckBox(); 101 return new CheckBoxEditor(checkBox); 102 } 103 104 public ComboBoxEditor createComboBoxEditor( 105 ComboBoxModel model, ListCellRenderer renderer) { 106 JComboBox comboBox = new JComboBox(model); 107 if (renderer != null) 108 comboBox.setRenderer(renderer); 109 return new ComboBoxEditor(comboBox); 110 } 111 112 public ButtonEditor createButtonEditor(JButton button) { 113 return new ButtonEditor(button); 114 } 115 116 public SpinnerEditor createSpinnerEditor( 117 SpinnerModel model, Format format) { 118 JSpinner spinner = new JSpinner(model); 119 if (format != null) { 120 JFormattedTextField field = ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField(); 121 JFormattedTextField.AbstractFormatter formatter = field.getFormatter(); 122 if (formatter instanceof InternationalFormatter) 123 ((InternationalFormatter)formatter).setFormat(format); 124 } 125 return new SpinnerEditor(spinner); 126 } 127 128 public TableCellEditor createTableCellEditor(ObjEntityViewField field) { 129 CellRenderers cellRenderers = new CellRenderers(); 130 TableCellEditor editor = null; 131 Format format = field.getEditFormat(); 132 int dataType = field.getDataType().getValue(); 133 boolean lookup = field.isLookup(); 134 int alignment; 135 136 switch (dataType) { 137 case DataTypeEnum.INTEGER_TYPE_VALUE: 138 case DataTypeEnum.DOUBLE_TYPE_VALUE: 139 case DataTypeEnum.MONEY_TYPE_VALUE: 140 case DataTypeEnum.PERCENT_TYPE_VALUE: 141 alignment = JTextField.RIGHT; 142 break; 143 default: 144 alignment = JTextField.LEFT; 145 break; 146 } 147 148 if (lookup) { 149 ComboBoxModel comboData = 150 new DefaultComboBoxModel(field.getLookupValues()); 151 ListCellRenderer comboRenderer = 152 cellRenderers.createListCellRenderer(field); 153 editor = createComboBoxEditor(comboData, comboRenderer); 154 } else if (format != null) { 155 if (format instanceof MapFormat) { 156 MapFormat mapFormat = (MapFormat)format; 157 ComboBoxModel comboData = 158 new DefaultComboBoxModel((mapFormat).getValues()); 159 ListCellRenderer comboRenderer = 160 cellRenderers.createFormatListCellRenderer( 161 mapFormat, mapFormat.getNullFormat(), null, -1); 162 editor = createComboBoxEditor(comboData, comboRenderer); 163 } else { 164 editor = createFormattedFieldEditor(format, alignment); 165 } 166 } else { 167 if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE) 168 editor = createCheckBoxEditor(); 169 else 170 editor = createTextFieldEditor(alignment); 171 } 172 173 return editor; 174 } 175 176 public void installEditors(JTable table) { 177 TableModel m = table.getModel(); 178 if (!(m instanceof DOTableModel)) 179 return; 180 DOTableModel model = (DOTableModel)m; 181 TableColumnModel columnModel = table.getColumnModel(); 182 int columnCount = model.getColumnCount(); 183 for (int i = 0; i < columnCount; i++) { 184 ObjEntityViewField field = model.getField(i); 185 TableCellEditor editor = createTableCellEditor(field); 186 TableColumn column = columnModel.getColumn(i); 187 column.setCellEditor(editor); 188 } 189 } 190 191 public class FormattedFieldEditor extends DefaultCellEditor { 192 public FormattedFieldEditor(final JFormattedTextField field) { 193 super(field); 194 field.removeActionListener(delegate); 195 this.clickCountToStart = 2; 196 delegate = new EditorDelegate() { 197 public void setValue(Object value) { 198 field.setValue(value); 199 } 200 public Object getCellEditorValue() { 201 return field.getValue(); 202 } 203 public boolean stopCellEditing() { 204 if (field.isEditValid()) { 205 try {field.commitEdit();} 206 catch (ParseException ex) {} 207 } else { 208 field.setBorder(invalidStateBorder); 209 return false; 210 } 211 return super.stopCellEditing(); 212 } 213 }; 214 field.addActionListener(delegate); 215 } 216 public Component getTableCellEditorComponent(JTable table, Object value, 217 boolean isSelected, 218 int row, int column) { 219 delegate.setValue(value); 220 if (((JFormattedTextField)editorComponent).isEditValid()) 221 editorComponent.setBorder(editStateBorder); 222 else 223 editorComponent.setBorder(invalidStateBorder); 224 return editorComponent; 225 } 226 } 227 228 public class TextFieldEditor extends DefaultCellEditor { 229 protected Border editStateBorder = new LineBorder(Color.black); 230 public TextFieldEditor(JTextField field) { 231 super(field); 232 field.setBorder(editStateBorder); 233 } 234 } 235 236 public class CheckBoxEditor extends DefaultCellEditor { 237 public CheckBoxEditor(JCheckBox checkBox) { 238 super(checkBox); 239 checkBox.setBorder(editStateBorder); 240 checkBox.setHorizontalAlignment(JCheckBox.CENTER); 241 } 242 } 243 244 public class ComboBoxEditor extends DefaultCellEditor { 245 public ComboBoxEditor(JComboBox comboBox) { 246 super(comboBox); 247 comboBox.setBorder(editStateBorder); 248 } 249 } 250 251 public class SpinnerEditor extends DefaultCellEditor { 252 public SpinnerEditor(final JSpinner spinner) { 253 super(new JTextField()); 254 editorComponent = spinner; 255 spinner.setBorder(editStateBorder); 256 spinner.getEditor().setBorder(null); 257 final JFormattedTextField field = 258 ((JSpinner.DefaultEditor)spinner.getEditor()).getTextField(); 259 field.setBorder(null); 260 delegate = new EditorDelegate() { 261 public void setValue(Object value) { 262 spinner.setValue(value); 263 } 264 public Object getCellEditorValue() { 265 return spinner.getValue(); 266 } 267 public boolean stopCellEditing() { 268 if (field.isEditValid()) { 269 try {field.commitEdit();} 270 catch (ParseException ex) {} 271 } else { 272 field.setBorder(invalidStateBorder); 273 return false; 274 } 275 return super.stopCellEditing(); 276 } 277 }; 278 field.addActionListener(delegate); 279 } 280 } 291 292 public class ButtonEditor extends DefaultCellEditor { 293 protected Object currentValue; 294 295 public ButtonEditor(JButton button) { 296 super(new JCheckBox()); 297 editorComponent = button; 298 setClickCountToStart(1); 299 button.addActionListener(new ActionListener() { 300 public void actionPerformed(ActionEvent e) { 301 fireEditingStopped(); 302 } 303 }); 304 button.setBorder(editStateBorder); 305 button.setFocusPainted(false); 306 } 307 protected void fireEditingStopped() { 308 super.fireEditingStopped(); 309 } 310 public Object getCellEditorValue() { 311 return currentValue; 312 } 313 public Component getTableCellEditorComponent(JTable table, 314 Object value, 315 boolean isSelected, 316 int row, 317 int column) { 318 ((JButton)editorComponent).setText(String.valueOf(value)); 319 currentValue = value; 320 return editorComponent; 321 } 322 } 323 } | Popular Tags |