1 7 8 package org.jdesktop.jdnc.markup.attr; 9 10 import java.util.Map ; 11 12 import java.awt.Component ; 13 import javax.swing.DefaultCellEditor ; 14 import javax.swing.DefaultListCellRenderer ; 15 import javax.swing.JComboBox ; 16 import javax.swing.JLabel ; 17 import javax.swing.JList ; 18 import javax.swing.JTable ; 19 import javax.swing.table.DefaultTableCellRenderer ; 20 21 import org.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 24 import org.jdesktop.swing.data.Converters; 25 import org.jdesktop.swing.data.DefaultTableModelExt; 26 import org.jdesktop.swing.LabelProperties; 27 import org.jdesktop.swing.table.TableColumnExt; 28 import org.jdesktop.swing.treetable.*; 29 import org.jdesktop.swing.data.*; 30 import org.jdesktop.swing.*; 31 import org.jdesktop.jdnc.JNTable; 32 import net.openmarkup.ApplierException; 33 import net.openmarkup.AttributeApplier; 34 import net.openmarkup.Attributes; 35 import net.openmarkup.Realizable; 36 import org.jdesktop.jdnc.markup.ElementTypes; 37 import org.jdesktop.jdnc.markup.Namespace; 38 import org.jdesktop.jdnc.markup.elem.ElementProxy; 39 40 44 public class TableColumnAttributes { 45 46 55 56 public static final AttributeApplier backgroundApplier = new 57 AttributeApplier() { 58 public void apply(Realizable target, String namespaceURI, 59 String attributeName, String attributeValue) throws 60 ApplierException { 61 TableColumnExt column = (TableColumnExt) target.getObject(); 62 ComponentAttributes.applyBackground(getColumnProperties(column), 66 attributeValue); 67 } 68 }; 69 70 public static final AttributeApplier bindingApplier = new AttributeApplier() { 71 public void apply(Realizable target, String namespaceURI, 72 String attributeName, String attributeValue) throws 73 ApplierException { 74 int columnIndex = TableColumnAttributes.getModelIndex(target, 75 attributeValue); 76 if (columnIndex >= 0) { 77 TableColumnExt column = (TableColumnExt) target.getObject(); 78 column.setIdentifier(attributeValue); 79 column.setModelIndex(columnIndex); 80 } 81 } 82 }; 83 84 public static int getModelIndex(Realizable target, String columnName) { 85 JNTable table = TableColumnAttributes.getTable(target); 86 if (table != null) { 87 Object model = table.getModel(); 88 if (model != null) { 89 if (model instanceof DefaultTableModelExt) { 90 return ( (DefaultTableModelExt) model).getColumnIndex( 91 columnName); 92 } 93 else if (model instanceof DOMAdapter) { 94 return ( (DOMAdapter) model).getColumnIndex(columnName); 95 } 96 } 97 } 98 return -1; 99 } 100 101 private static JNTable getTable(Realizable target) { 102 Node root = target.getOwnerDocument().getDocumentElement(); 103 Node parent = target.getParentNode(); 104 String nsURI = ElementTypes.TABLE.getNamespaceURI(); 105 String tableName = ElementTypes.TABLE.getLocalName(); 106 String treeTableName = ElementTypes.TREE_TABLE.getLocalName(); 107 while (parent != root) { 108 String parentName = parent.getLocalName(); 109 if ( (parentName.equals(tableName) || 110 parentName.equals(treeTableName)) && 111 parent.getNamespaceURI().equals(nsURI)) { 112 Realizable tableElement = ElementProxy.getRealizable( (Element ) 113 parent); 114 if (tableElement != null) { 115 return (JNTable) tableElement.getObject(); 116 } 117 return null; 118 } 119 parent = parent.getParentNode(); 120 } 121 return null; 122 } 123 124 public static final AttributeApplier enumValuesApplier = new 125 AttributeApplier() { 126 public void apply(Realizable target, String namespaceURI, 127 String attributeName, String attributeValue) throws 128 ApplierException { 129 Map map = (Map ) BaseAttribute.getReferencedObject(target, 130 attributeValue); 131 TableColumnExt column = (TableColumnExt) target.getObject(); 132 133 TableColumnAttributes.setRenderer(column, map); 134 TableColumnAttributes.setEditor(column, map); 135 } 136 }; 137 138 public static final AttributeApplier foregroundApplier = new 139 AttributeApplier() { 140 public void apply(Realizable target, String namespaceURI, 141 String attributeName, String attributeValue) throws 142 ApplierException { 143 TableColumnExt column = (TableColumnExt) target.getObject(); 144 ComponentAttributes.applyForeground(getColumnProperties(column), 148 attributeValue); 149 } 150 }; 151 152 public static final AttributeApplier horizontalAlignmentApplier = new 153 AttributeApplier() { 154 public void apply(Realizable target, String namespaceURI, 155 String attributeName, String attributeValue) throws 156 ApplierException { 157 TableColumnExt column = (TableColumnExt) target.getObject(); 158 LabelAttributes.applyHorizontalAlignment(getColumnProperties(column), 162 attributeValue); 163 } 164 }; 165 166 public static final AttributeApplier prototypeValueApplier = new 167 AttributeApplier() { 168 public void apply(Realizable target, String namespaceURI, 169 String attributeName, String attributeValue) throws 170 ApplierException { 171 TableColumnExt column = (TableColumnExt) target.getObject(); 172 column.putClientProperty("prototypeValueString", attributeValue); 176 } 177 }; 178 179 public static final AttributeApplier isReadOnlyApplier = new 180 AttributeApplier() { 181 public void apply(Realizable target, String namespaceURI, 182 String attributeName, String attributeValue) throws 183 ApplierException { 184 TableColumnExt column = (TableColumnExt) target.getObject(); 185 column.setEditable(!Boolean.valueOf(attributeValue).booleanValue()); 187 } 188 }; 189 190 public static final AttributeApplier resizableApplier = new 191 AttributeApplier() { 192 public void apply(Realizable target, String namespaceURI, 193 String attributeName, String attributeValue) throws 194 ApplierException { 195 TableColumnExt column = (TableColumnExt) target.getObject(); 196 column.setResizable(Boolean.valueOf(attributeValue).booleanValue()); 197 } 198 }; 199 200 public static final AttributeApplier titleApplier = new AttributeApplier() { 201 public void apply(Realizable target, String namespaceURI, 202 String attributeName, String attributeValue) throws 203 ApplierException { 204 TableColumnExt column = (TableColumnExt) target.getObject(); 205 column.setTitle(attributeValue); 206 } 207 }; 208 209 223 224 public static final AttributeApplier preferredWidthApplier = new 225 AttributeApplier() { 226 public void apply(Realizable target, String namespaceURI, 227 String attributeName, String attributeValue) throws 228 ApplierException { 229 TableColumnExt column = (TableColumnExt) target.getObject(); 230 231 column.setPreferredWidth(Integer.parseInt(attributeValue)); 232 } 233 }; 234 235 private static LabelProperties getColumnProperties(TableColumnExt column) { 238 LabelProperties props = (LabelProperties) column.getClientProperty( 239 "LabelProperties"); 240 if (props == null) { 241 props = new LabelProperties(); 242 column.putClientProperty("LabelProperties", props); 243 } 244 return props; 245 } 246 247 private static void setRenderer(final TableColumnExt tableColumn, 248 final Map map) { 249 tableColumn.setCellRenderer(new DefaultTableCellRenderer () { 250 public Component getTableCellRendererComponent(JTable table, 251 Object value, boolean isSelected, boolean hasFocus, int row, 252 int column) { 253 JLabel label = (JLabel )super.getTableCellRendererComponent( 254 table, value, isSelected, hasFocus, row, column); 255 JLabel fromLabel = value == null ? null : 257 (JLabel ) map.get(value.toString()); 258 return decorate(fromLabel, label, value); 259 } 260 }); 261 } 262 263 private static void setEditor(final TableColumnExt tableColumn, 264 final Map map) { 265 JComboBox labelComboBox = new JComboBox (map.values().toArray()); 266 267 labelComboBox.setRenderer(new DefaultListCellRenderer () { 268 public Component getListCellRendererComponent(JList list, 269 Object value, int index, boolean isSelected, boolean hasFocus) { 270 JLabel label = (JLabel )super.getListCellRendererComponent(list, 271 value, index, isSelected, hasFocus); 272 return decorate( (JLabel ) value, label, value); 273 } 274 }); 275 276 tableColumn.setCellEditor(new DefaultCellEditor (labelComboBox) { 277 public Component getTableCellEditorComponent(JTable table, 278 Object value, boolean isSelected, int row, int column) { 279 if ( (value != null) && tableColumn.isEditable()) { 280 JComboBox comboBox = 281 (JComboBox )super.getTableCellEditorComponent( 282 table, value, isSelected, row, column); 283 comboBox.setSelectedItem(map.get(value.toString())); 284 return comboBox; 285 } 286 return null; 287 } 288 289 public Object getCellEditorValue() { 290 JLabel label = (JLabel )super.getCellEditorValue(); 291 292 if (label == null) { 293 return null; 294 } 295 else { 296 Object value = label.getClientProperty(Namespace.JDNC + ":" + 297 Attributes.VALUE); 298 try { 299 302 303 314 return value; 315 } 316 catch (Exception e) { 317 } 318 return value; 319 } 320 } 321 }); 322 } 323 324 private static Component decorate(JLabel fromLabel, JLabel toLabel, 325 Object value) { 326 if (fromLabel == null) { 327 toLabel.setHorizontalAlignment(JLabel.LEADING); 328 toLabel.setHorizontalTextPosition(JLabel.LEADING); 329 toLabel.setIcon(null); 330 toLabel.setText(value == null ? "" : value.toString()); } 332 else { 333 toLabel.setHorizontalAlignment(fromLabel.getHorizontalAlignment()); 334 toLabel.setHorizontalTextPosition(fromLabel. 335 getHorizontalTextPosition()); 336 toLabel.setIcon(fromLabel.getIcon()); 337 toLabel.setText(fromLabel.getText()); 338 } 339 return toLabel; 340 } 341 } | Popular Tags |