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