1 19 20 package org.apache.cayenne.dataview; 21 22 import java.awt.*; 23 import javax.swing.*; 24 import javax.swing.table.*; 25 import java.text.*; 26 27 public class CellRenderers { 28 public FormatRenderer createFormatTableCellRenderer( 29 Format format, 30 String nullText, 31 String invalidText, 32 int alignment) { 33 FormatRenderer renderer = new FormatRenderer(format, nullText, invalidText); 34 if (alignment >= 0) 35 renderer.setHorizontalAlignment(alignment); 36 return renderer; 37 } 38 39 public BooleanRenderer createBooleanTableCellRenderer() { 40 BooleanRenderer renderer = new BooleanRenderer(); 41 return renderer; 42 } 43 44 public ObjectRenderer createDefaultTableCellRenderer(int alignment) { 45 ObjectRenderer renderer = new ObjectRenderer(); 46 if (alignment >= 0) 47 renderer.setHorizontalAlignment(alignment); 48 return renderer; 49 } 50 51 public TableCellRenderer createTableCellRenderer(ObjEntityViewField field) { 52 TableCellRenderer renderer = null; 53 Format format = field.getDisplayFormat(); 54 int dataType = field.getDataType().getValue(); 55 int alignment; 56 String nullText = ""; 57 String invalidText = ""; 58 59 switch (dataType) { 60 case DataTypeEnum.INTEGER_TYPE_VALUE: 61 case DataTypeEnum.DOUBLE_TYPE_VALUE: 62 case DataTypeEnum.MONEY_TYPE_VALUE: 63 case DataTypeEnum.PERCENT_TYPE_VALUE: 64 alignment = JLabel.RIGHT; 65 break; 66 default: 67 alignment = JLabel.LEFT; 68 break; 69 } 70 71 if (format != null) { 72 renderer = createFormatTableCellRenderer( 73 format, nullText, invalidText, alignment); 74 } else { 75 if (dataType == DataTypeEnum.BOOLEAN_TYPE_VALUE) 76 renderer = createBooleanTableCellRenderer(); 77 else 78 renderer = createDefaultTableCellRenderer(alignment); 79 } 80 81 return renderer; 82 } 83 84 public ListCellRenderer createFormatListCellRenderer( 85 Format format, 86 String nullText, 87 String invalidText, 88 int alignment) { 89 FormatListCellRenderer renderer = new FormatListCellRenderer( 90 format, nullText, invalidText); 91 if (alignment >= 0) 92 renderer.setHorizontalAlignment(alignment); 93 return renderer; 94 } 95 96 public ListCellRenderer createListCellRenderer(ObjEntityViewField field) { 97 ListCellRenderer renderer = null; 98 Format format = field.getDisplayFormat(); 99 int dataType = field.getDataType().getValue(); 100 int alignment; 101 String nullText = ""; 102 String invalidText = ""; 103 104 switch (dataType) { 105 case DataTypeEnum.INTEGER_TYPE_VALUE: 106 case DataTypeEnum.DOUBLE_TYPE_VALUE: 107 case DataTypeEnum.MONEY_TYPE_VALUE: 108 case DataTypeEnum.PERCENT_TYPE_VALUE: 109 alignment = JLabel.RIGHT; 110 default: 111 alignment = JLabel.LEFT; 112 break; 113 } 114 115 if (format != null) { 116 renderer = createFormatListCellRenderer( 117 format, nullText, invalidText, alignment); 118 } else { 119 renderer = new DefaultListCellRenderer(); 120 ((DefaultListCellRenderer)renderer).setHorizontalAlignment(alignment); 121 } 122 123 return renderer; 124 } 125 126 public void installRenderers(JTable table) { 127 TableModel m = table.getModel(); 128 if (!(m instanceof DOTableModel)) 129 return; 130 DOTableModel model = (DOTableModel)m; 131 TableColumnModel columnModel = table.getColumnModel(); 132 int columnCount = model.getColumnCount(); 133 for (int i = 0; i < columnCount; i++) { 134 ObjEntityViewField field = model.getField(i); 135 TableCellRenderer renderer = createTableCellRenderer(field); 136 TableColumn column = columnModel.getColumn(i); 137 column.setCellRenderer(renderer); 138 } 139 } 140 141 public void installRenderer(JList list, ObjEntityViewField field) { 142 ListCellRenderer renderer = createListCellRenderer(field); 143 list.setCellRenderer(renderer); 144 } 145 146 public void installRenderer(JComboBox comboBox, ObjEntityViewField field) { 147 ListCellRenderer renderer = createListCellRenderer(field); 148 comboBox.setRenderer(renderer); 149 } 150 151 public class BooleanRenderer extends JCheckBox implements TableCellRenderer { 152 public BooleanRenderer() { 153 super(); 154 setHorizontalAlignment(JLabel.CENTER); 155 } 156 157 public Component getTableCellRendererComponent(JTable table, Object value, 158 boolean isSelected, boolean hasFocus, int row, int column) { 159 if (isSelected) { 160 setForeground(table.getSelectionForeground()); 161 super.setBackground(table.getSelectionBackground()); 162 } 163 else { 164 setForeground(table.getForeground()); 165 setBackground(table.getBackground()); 166 } 167 setSelected((value != null && ((Boolean )value).booleanValue())); 168 return this; 169 } 170 } 171 172 public class ObjectRenderer extends DefaultTableCellRenderer { 173 } 174 175 public class FormatRenderer extends DefaultTableCellRenderer { 176 protected Format formatter; 177 protected String nullText = ""; 178 protected String invalidText = ""; 179 180 public FormatRenderer() { 181 this(null, null, null); 182 } 183 184 public FormatRenderer(Format formatter) { 185 this(formatter, null, null); 186 } 187 188 public FormatRenderer(Format formatter, String nullText) { 189 this(formatter, nullText, null); 190 } 191 192 public FormatRenderer(Format formatter, String nullText, String invalidText) { 193 setFormatter(formatter); 194 setNullText(nullText); 195 setInvalidText(invalidText); 196 } 197 198 public void setFormatter(Format formatter) { 199 this.formatter = formatter; 200 } 201 public Format getFormatter() { 202 return formatter; 203 } 204 public void setNullText(String nullText) { 205 this.nullText = (nullText != null ? nullText : this.nullText); 206 } 207 public String getNullText() { 208 return nullText; 209 } 210 public void setInvalidText(String invalidText) { 211 this.invalidText = (invalidText != null ? invalidText : this.invalidText); 212 } 213 public String getInvalidText() { 214 return invalidText; 215 } 216 217 public void setValue(Object value) { 218 String text; 219 try { 220 if (value == null) 221 text = nullText; 222 else if (formatter==null) 223 text = value.toString(); 224 else 225 text = formatter.format(value); 226 } 227 catch (Exception ex) { 228 text = invalidText; 229 } 230 setText(text); 231 } 232 } 233 234 public class FormatListCellRenderer extends DefaultListCellRenderer { 235 protected Format formatter; 236 protected String nullText = ""; 237 protected String invalidText = ""; 238 239 public FormatListCellRenderer() { 240 this(null, null, null); 241 } 242 243 public FormatListCellRenderer(Format formatter) { 244 this(formatter, null, null); 245 } 246 247 public FormatListCellRenderer(Format formatter, String nullText) { 248 this(formatter, nullText, null); 249 } 250 251 public FormatListCellRenderer(Format formatter, String nullText, String invalidText) { 252 setFormatter(formatter); 253 setNullText(nullText); 254 setInvalidText(invalidText); 255 } 256 257 public void setFormatter(Format formatter) { 258 this.formatter = formatter; 259 } 260 public Format getFormatter() { 261 return formatter; 262 } 263 public void setNullText(String nullText) { 264 this.nullText = (nullText != null ? nullText : this.nullText); 265 } 266 public String getNullText() { 267 return nullText; 268 } 269 public void setInvalidText(String invalidText) { 270 this.invalidText = (invalidText != null ? invalidText : this.invalidText); 271 } 272 public String getInvalidText() { 273 return invalidText; 274 } 275 276 public Component getListCellRendererComponent( 277 JList list, 278 Object value, 279 int index, 280 boolean isSelected, 281 boolean cellHasFocus) { 282 String text; 283 try { 284 if (value == null) 285 text = nullText; 286 else if (formatter==null) 287 text = value.toString(); 288 else 289 text = formatter.format(value); 290 } 291 catch (Exception ex) { 292 text = invalidText; 293 } 294 return super.getListCellRendererComponent( 295 list, text, index, isSelected, cellHasFocus); 296 } 297 } 298 299 } 300 | Popular Tags |