1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.text.DecimalFormat ; 59 import java.text.Format ; 60 import java.text.SimpleDateFormat ; 61 62 import javax.swing.ComboBoxModel ; 63 import javax.swing.DefaultComboBoxModel ; 64 import javax.swing.JCheckBox ; 65 import javax.swing.JComboBox ; 66 import javax.swing.JComponent ; 67 import javax.swing.JFormattedTextField ; 68 import javax.swing.JTextField ; 69 import javax.swing.ListCellRenderer ; 70 71 public class FieldComponentFactory { 72 73 public FieldComponentFactory() { 74 } 75 76 public JComponent createFieldEditComponent(ObjEntityViewField field) { 77 CellRenderers cellRenderers = new CellRenderers(); 78 JComponent editor = null; 79 Format format = field.getEditFormat(); 80 int dataType = field.getDataType().getValue(); 81 boolean lookup = field.isLookup(); 82 int alignment; 83 84 switch (dataType) { 85 case DataTypeEnum.INTEGER_TYPE_VALUE: 86 case DataTypeEnum.DOUBLE_TYPE_VALUE: 87 case DataTypeEnum.MONEY_TYPE_VALUE: 88 case DataTypeEnum.PERCENT_TYPE_VALUE: 89 alignment = JTextField.RIGHT; 90 break; 91 default: 92 alignment = JTextField.LEFT; 93 break; 94 } 95 96 if (lookup) { 97 ComboBoxModel comboData = 98 new DefaultComboBoxModel (field.getLookupValues()); 99 ListCellRenderer comboRenderer = 100 cellRenderers.createListCellRenderer(field); 101 JComboBox comboBox = new JComboBox (comboData); 102 comboBox.setRenderer(comboRenderer); 103 editor = comboBox; 104 } else if (format != null) { 105 if (format instanceof MapFormat) { 106 MapFormat mapFormat = (MapFormat)format; 107 ComboBoxModel comboData = 108 new DefaultComboBoxModel ((mapFormat).getValues()); 109 ListCellRenderer comboRenderer = 110 cellRenderers.createFormatListCellRenderer( 111 mapFormat, mapFormat.getNullFormat(), null, -1); 112 JComboBox comboBox = new JComboBox (comboData); 113 comboBox.setRenderer(comboRenderer); 114 editor = comboBox; 115 } else { 116 JFormattedTextField textField = new JFormattedTextField (format); 117 if (alignment >= 0) 118 textField.setHorizontalAlignment(alignment); 119 if (format instanceof DecimalFormat ) 120 textField.setToolTipText(((DecimalFormat )format).toPattern()); 121 else if (format instanceof SimpleDateFormat ) 122 textField.setToolTipText(((SimpleDateFormat )format).toPattern()); 123 editor = textField; 124 } 125 } else { 126 if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE) { 127 JCheckBox checkBox = new JCheckBox (); 128 editor = checkBox; 129 } else { 130 JTextField textField = new JTextField (); 131 if (alignment >= 0) 132 textField.setHorizontalAlignment(alignment); 133 editor = textField; 134 } 135 } 136 137 return editor; 138 } 139 } | Popular Tags |