1 19 20 package org.apache.cayenne.dataview; 21 22 import java.text.DecimalFormat ; 23 import java.text.Format ; 24 import java.text.SimpleDateFormat ; 25 26 import javax.swing.ComboBoxModel ; 27 import javax.swing.DefaultComboBoxModel ; 28 import javax.swing.JCheckBox ; 29 import javax.swing.JComboBox ; 30 import javax.swing.JComponent ; 31 import javax.swing.JFormattedTextField ; 32 import javax.swing.JTextField ; 33 import javax.swing.ListCellRenderer ; 34 35 public class FieldComponentFactory { 36 37 public FieldComponentFactory() { 38 } 39 40 public JComponent createFieldEditComponent(ObjEntityViewField field) { 41 CellRenderers cellRenderers = new CellRenderers(); 42 JComponent editor = null; 43 Format format = field.getEditFormat(); 44 int dataType = field.getDataType().getValue(); 45 boolean lookup = field.isLookup(); 46 int alignment; 47 48 switch (dataType) { 49 case DataTypeEnum.INTEGER_TYPE_VALUE: 50 case DataTypeEnum.DOUBLE_TYPE_VALUE: 51 case DataTypeEnum.MONEY_TYPE_VALUE: 52 case DataTypeEnum.PERCENT_TYPE_VALUE: 53 alignment = JTextField.RIGHT; 54 break; 55 default: 56 alignment = JTextField.LEFT; 57 break; 58 } 59 60 if (lookup) { 61 ComboBoxModel comboData = 62 new DefaultComboBoxModel (field.getLookupValues()); 63 ListCellRenderer comboRenderer = 64 cellRenderers.createListCellRenderer(field); 65 JComboBox comboBox = new JComboBox (comboData); 66 comboBox.setRenderer(comboRenderer); 67 editor = comboBox; 68 } else if (format != null) { 69 if (format instanceof MapFormat) { 70 MapFormat mapFormat = (MapFormat)format; 71 ComboBoxModel comboData = 72 new DefaultComboBoxModel ((mapFormat).getValues()); 73 ListCellRenderer comboRenderer = 74 cellRenderers.createFormatListCellRenderer( 75 mapFormat, mapFormat.getNullFormat(), null, -1); 76 JComboBox comboBox = new JComboBox (comboData); 77 comboBox.setRenderer(comboRenderer); 78 editor = comboBox; 79 } else { 80 JFormattedTextField textField = new JFormattedTextField (format); 81 if (alignment >= 0) 82 textField.setHorizontalAlignment(alignment); 83 if (format instanceof DecimalFormat ) 84 textField.setToolTipText(((DecimalFormat )format).toPattern()); 85 else if (format instanceof SimpleDateFormat ) 86 textField.setToolTipText(((SimpleDateFormat )format).toPattern()); 87 editor = textField; 88 } 89 } else { 90 if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE) { 91 JCheckBox checkBox = new JCheckBox (); 92 editor = checkBox; 93 } else { 94 JTextField textField = new JTextField (); 95 if (alignment >= 0) 96 textField.setHorizontalAlignment(alignment); 97 editor = textField; 98 } 99 } 100 101 return editor; 102 } 103 } 104 | Popular Tags |