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