1 7 8 package org.jdesktop.jdnc.markup.elem; 9 10 import java.util.Hashtable ; 11 import java.util.Map ; 12 import java.util.Vector ; 13 14 import javax.swing.Action ; 15 import javax.swing.JComponent ; 16 import javax.swing.table.*; 17 import javax.swing.table.TableColumnModel ; 18 19 import org.jdesktop.swing.LabelProperties; 20 import org.jdesktop.swing.data.ConversionException; 21 import org.jdesktop.swing.data.Converter; 22 import org.jdesktop.swing.data.Converters; 23 import org.jdesktop.swing.data.MetaData; 24 import org.jdesktop.swing.data.MetaDataProvider; 25 import org.jdesktop.swing.data.DefaultTableModelExt; 26 import org.jdesktop.swing.decorator.Filter; 27 import org.jdesktop.swing.decorator.FilterPipeline; 28 import org.jdesktop.swing.decorator.Highlighter; 29 import org.jdesktop.swing.decorator.HighlighterPipeline; 30 import org.jdesktop.swing.table.TableColumnExt; 31 import org.jdesktop.swing.utils.LoadOnShowListener; 32 import org.jdesktop.jdnc.JNTable; 33 import org.jdesktop.jdnc.markup.Attributes; 34 import org.jdesktop.jdnc.markup.ElementTypes; 35 import org.jdesktop.jdnc.markup.Namespace; 36 import org.jdesktop.jdnc.markup.attr.ComponentAttributes; 37 import org.jdesktop.jdnc.markup.attr.TableAttributes; 38 import org.jdesktop.jdnc.markup.attr.NullAttribute; 39 import net.openmarkup.AttributeHandler; 40 import net.openmarkup.ElementAssimilator; 41 import net.openmarkup.ElementHandler; 42 import net.openmarkup.ElementType; 43 import net.openmarkup.Realizable; 44 45 import org.w3c.dom.Element ; 46 import org.w3c.dom.NodeList ; 47 48 53 public class TableElement extends ComponentElement { 54 private static final Map attrMap = new Hashtable (); 55 private static final Map elementMap = new Hashtable (); 56 57 public TableElement(Element element, ElementType elementType) { 58 super(element, elementType); 59 } 60 61 protected Map getAttributeHandlerMap() { 62 return attrMap; 63 } 64 65 protected Map getElementHandlerMap() { 66 return elementMap; 67 } 68 69 protected void assimilateChildren() { 70 applyAttribute(Namespace.JDNC, Attributes.DATA); 71 super.assimilateChildren(); 72 } 73 74 protected void applyAttributesBefore() { 75 76 applyAttribute(Namespace.JDNC, Attributes.DATA); 77 applyAttribute(Namespace.JDNC, Attributes.DATA_SOURCE); 78 applyAttribute(Namespace.JDNC, Attributes.IS_FIRST_ROW_HEADER); 79 } 80 81 protected void applyAttributesAfter() { 82 super.applyAttributesAfter(); 83 applyAttribute(Namespace.JDNC, Attributes.HAS_COLUMN_CONTROL); 84 applyAttribute(Namespace.JDNC, Attributes.COLUMN_MARGIN); 85 applyAttribute(Namespace.JDNC, Attributes.GRID_COLOR); 86 applyAttribute(Namespace.JDNC, Attributes.PREFERRED_SIZE); 87 applyAttribute(Namespace.JDNC, Attributes.ROW_HEIGHT); 88 applyAttribute(Namespace.JDNC, Attributes.ROW_MARGIN); 89 applyAttribute(Namespace.JDNC, Attributes.SELECTION); 90 applyAttribute(Namespace.JDNC, Attributes.SELECTION_MODE); 91 applyAttribute(Namespace.JDNC, Attributes.SHOWS_HORIZONTAL_LINES); 92 applyAttribute(Namespace.JDNC, Attributes.SHOWS_VERTICAL_LINES); 93 applyAttribute(Namespace.JDNC, Attributes.SORTABLE); 94 applyAttribute(Namespace.JDNC, Attributes.SIZE); applyAttribute(Namespace.JDNC, Attributes.IS_COLUMN_HEADER_LOCKED); 96 applyAttribute(Namespace.JDNC, Attributes.IS_ROW_HEADER_LOCKED); 97 98 applySpecialProperties(this); 100 } 101 102 protected Map registerAttributeHandlers() { 103 Map handlerMap = super.registerAttributeHandlers(); 104 if (handlerMap != null) { 105 handlerMap.put(Namespace.JDNC + ":" + 108 Attributes.HAS_COLUMN_CONTROL, 109 hasColumnControlHandler); 110 handlerMap.put(Namespace.JDNC + ":" + Attributes.COLUMN_MARGIN, 111 columnMarginHandler); 112 handlerMap.put(Namespace.JDNC + ":" + Attributes.DATA, 113 dataHandler); 114 handlerMap.put(Namespace.JDNC + ":" + Attributes.DATA_SOURCE, 115 dataSourceHandler); 116 handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_FIRST_ROW_HEADER, 117 firstRowHeaderHandler); 118 handlerMap.put(Namespace.JDNC + ":" + Attributes.GRID_COLOR, 119 gridColorHandler); 120 handlerMap.put(Namespace.JDNC + ":" + Attributes.PREFERRED_SIZE, 121 preferredSizeHandler); 122 handlerMap.put(Namespace.JDNC + ":" + Attributes.ROW_HEIGHT, 123 rowHeightHandler); 124 handlerMap.put(Namespace.JDNC + ":" + Attributes.ROW_MARGIN, 125 rowMarginHandler); 126 handlerMap.put(Namespace.JDNC + ":" + Attributes.SELECTION, 127 selectionHandler); 128 handlerMap.put(Namespace.JDNC + ":" + Attributes.SELECTION_MODE, 129 selectionModeHandler); 130 handlerMap.put(Namespace.JDNC + ":" + Attributes.SHOWS_HORIZONTAL_LINES, 131 showsHorizontalLinesHandler); 132 handlerMap.put(Namespace.JDNC + ":" + Attributes.SHOWS_VERTICAL_LINES, 133 showsVerticalLinesHandler); 134 handlerMap.put(Namespace.JDNC + ":" + Attributes.SIZE, 135 sizeHandler); handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_COLUMN_HEADER_LOCKED, 137 isColumnHeaderLockedHandler); 138 handlerMap.put(Namespace.JDNC + ":" + Attributes.IS_ROW_HEADER_LOCKED, 139 isRowHeaderLockedHandler); 140 141 handlerMap.put(Namespace.JDNC + ":" + Attributes.ID, 143 NullAttribute.idHandler); 144 145 handlerMap.put(Namespace.JDNC + ":" + Attributes.TITLE, 146 titleHandler); 147 148 } 149 return handlerMap; 150 } 151 152 protected Map registerElementHandlers() { 153 Map handlerMap = super.registerElementHandlers(); 154 if (handlerMap != null) { 155 handlerMap.put(Namespace.JDNC + ":" + 157 ElementTypes.FILTERS.getLocalName(), 158 filtersElementHandler); 159 handlerMap.put(Namespace.JDNC + ":" + 160 ElementTypes.TABLE_HEADER.getLocalName(), 161 headerElementHandler); 162 handlerMap.put(Namespace.JDNC + ":" + 163 ElementTypes.HIGHLIGHTERS.getLocalName(), 164 highlightersElementHandler); 165 handlerMap.put(Namespace.JDNC + ":" + 166 ElementTypes.COMPONENT_TOOLBAR.getLocalName(), 167 toolBarElementHandler); 168 handlerMap.put(Namespace.JDNC + ":" + 169 ElementTypes.TABLE_COLUMNS.getLocalName(), 170 tableColumnsElementHandler); 171 172 handlerMap.put(Namespace.JDNC + ":" + 173 ElementTypes.TABULAR_DATA.getLocalName(), 174 tabularDataElementHandler); 175 handlerMap.put(Namespace.JDNC + ":" + 176 ElementTypes.DATA.getLocalName(), dataElementHandler); 177 } 178 return handlerMap; 179 } 180 181 protected static void applySpecialProperties(Realizable target) { 182 JNTable table = (JNTable)target.getObject(); 183 TableColumnModel columnModel = table.getTable().getColumnModel(); 184 for(int i = 0; i < columnModel.getColumnCount(); i++) { 185 TableColumnExt column = (TableColumnExt)columnModel.getColumn(i); 186 applyColumnProperties(table, column); 187 } 188 } 189 190 protected static void applyColumnProperties(final JNTable table, final TableColumnExt column) { 191 LabelProperties props = (LabelProperties) column.getClientProperty( 195 "LabelProperties"); 196 if (props != null) { 197 String columnName = (String )column.getIdentifier(); 198 if (props.isBackgroundSet()) { 199 table.setColumnBackground(columnName, props.getBackground()); 200 } 201 if (props.isForegroundSet()) { 202 table.setColumnForeground(columnName, props.getForeground()); 203 } 204 if (props.isHorizontalAlignmentSet()) { 205 table.setColumnHorizontalAlignment(columnName, props.getHorizontalAlignment()); 206 } 207 column.putClientProperty("LabelProperties", null); } 209 210 String prototypeValueString = (String )column.getClientProperty("prototypeValueString"); 211 if (prototypeValueString != null) { 212 TableModel model = table.getModel(); 213 Object prototypeValue = prototypeValueString; Converter converter = null; 215 Object decodeFormat = null; 216 if (model instanceof MetaDataProvider) { 217 MetaData metaData[] = ( (MetaDataProvider) model).getMetaData(); 218 MetaData columnMetaData = metaData[column.getModelIndex()]; 219 converter = columnMetaData.getConverter(); 220 decodeFormat = columnMetaData.getDecodeFormat(); 221 } else { converter = Converters.get(model.getColumnClass(column.getModelIndex())); 223 } 224 if (converter != null) { 225 try { 226 prototypeValue = converter.decode(prototypeValueString, 227 decodeFormat); 228 } catch (ConversionException e) { 229 System.err.println(e.getMessage()); 230 } 231 } 232 column.setPrototypeValue(prototypeValue); 233 } 234 } 235 236 protected static boolean hasColumnsElement(Realizable target) { 237 NodeList columnsNode = target.getElementsByTagNameNS(Namespace.JDNC, 238 ElementTypes.TABLE_COLUMNS.getLocalName()); 239 return columnsNode.getLength() > 0; 240 241 } 242 243 public static void setModel(Realizable target, TableModel model) { 245 JNTable table = (JNTable)target.getObject(); 248 if (hasColumnsElement(target)) { 249 table.getTable().setAutoCreateColumnsFromModel(false); 250 } 251 table.setModel(model); 252 } 253 254 public static final ElementAssimilator dataAssimilator = new ElementAssimilator() { 255 public void assimilate(Realizable parent, Realizable child) { 256 JNTable table = (JNTable)parent.getObject(); 257 DefaultTableModelExt data = (DefaultTableModelExt)child.getObject(); 258 setModel(parent, data); 259 String actuate = child.getAttributeNSOptional(Namespace.JDNC, Attributes.ACTUATE); 260 if (actuate.equals("onRequest")) { 261 table.addHierarchyListener(new LoadOnShowListener(data)); 262 } 263 } 264 }; 265 266 public static final ElementAssimilator filtersAssimilator = new ElementAssimilator() { 267 public void assimilate(Realizable parent, Realizable child) { 268 JNTable table = (JNTable)parent.getObject(); 269 Vector filters = (Vector )child.getObject(); 270 table.setFilters(new FilterPipeline((Filter[]) 271 filters.toArray(filterType) 272 )); 273 } 274 }; 275 276 public static final ElementAssimilator headerAssimilator = new ElementAssimilator() { 277 public void assimilate(Realizable parent, Realizable child) { 278 JNTable table = (JNTable)parent.getObject(); 279 JTableHeader header = (JTableHeader)child.getObject(); 280 table.getTable().setTableHeader(header); 281 } 282 }; 283 284 public static final ElementAssimilator highlightersAssimilator = new ElementAssimilator() { 285 public void assimilate(Realizable parent, Realizable child) { 286 JNTable table = (JNTable)parent.getObject(); 287 Vector highlighters = (Vector )child.getObject(); 288 table.setHighlighters(new HighlighterPipeline((Highlighter[]) 289 highlighters.toArray(highlighterType) 290 )); 291 } 292 }; 293 294 public static final ElementAssimilator tableColumnsAssimilator = new 295 ElementAssimilator() { 296 public void assimilate(Realizable parent, Realizable child) { 297 JNTable table = (JNTable) parent.getObject(); 298 TableColumnModel columnModel = (TableColumnModel ) child.getObject(); 299 table.getTable().setAutoCreateColumnsFromModel(false); 300 table.getTable().setColumnModel(columnModel); 301 for(int i = 0; i < columnModel.getColumnCount() ; i++) { 302 TableColumn column = columnModel.getColumn(i); 303 if (column.getModelIndex() == -1) { 304 column.setModelIndex(i); 305 } 306 } 307 } 308 }; 309 310 public static final ElementAssimilator toolBarAssimilator = new ElementAssimilator() { 311 public void assimilate(Realizable parent, Realizable child) { 312 JNTable table = (JNTable)parent.getObject(); 313 Vector toolBarEntries = (Vector )child.getObject(); 314 int count = toolBarEntries.size(); 315 for (int i = 0; i < count; i++) { 316 Object entry = toolBarEntries.get(i); 317 if (entry instanceof Action ) { 318 table.addAction((Action ) entry); 319 } 320 else if (entry instanceof JComponent ) { 321 table.addToolBarComponent((JComponent ) entry); 322 } 323 } 324 } 325 }; 326 327 private static final Filter[] filterType = new Filter[]{}; 329 private static final Highlighter[] highlighterType = new Highlighter[]{}; 330 331 protected static final AttributeHandler dataSourceHandler = 332 new AttributeHandler(Namespace.JDNC, Attributes.DATA_SOURCE, TableAttributes.dataSourceApplier); 333 334 protected static final AttributeHandler hasColumnControlHandler = 335 new AttributeHandler(Namespace.JDNC, Attributes.HAS_COLUMN_CONTROL, TableAttributes.hasColumnControlApplier); 336 337 protected static final AttributeHandler columnMarginHandler = 338 new AttributeHandler(Namespace.JDNC, Attributes.COLUMN_MARGIN, TableAttributes.columnMarginApplier); 339 340 protected static final AttributeHandler dataHandler = 341 new AttributeHandler(Namespace.JDNC, Attributes.DATA, TableAttributes.dataApplier); 342 343 protected static final AttributeHandler firstRowHeaderHandler = 344 new AttributeHandler(Namespace.JDNC, Attributes.IS_FIRST_ROW_HEADER, TableAttributes.firstRowHeaderApplier); 345 346 protected static final AttributeHandler gridColorHandler = 347 new AttributeHandler(Namespace.JDNC, Attributes.GRID_COLOR, TableAttributes.gridColorApplier); 348 349 protected static final AttributeHandler preferredSizeHandler = 350 new AttributeHandler(Namespace.JDNC, Attributes.PREFERRED_SIZE, TableAttributes.preferredSizeApplier); 351 352 protected static final AttributeHandler rowHeightHandler = 353 new AttributeHandler(Namespace.JDNC, Attributes.ROW_HEIGHT, TableAttributes.rowHeightApplier); 354 355 protected static final AttributeHandler rowMarginHandler = 356 new AttributeHandler(Namespace.JDNC, Attributes.ROW_MARGIN, TableAttributes.rowMarginApplier); 357 358 protected static final AttributeHandler selectionHandler = 359 new AttributeHandler(Namespace.JDNC, Attributes.SELECTION, TableAttributes.selectionApplier); 360 361 protected static final AttributeHandler selectionModeHandler = 362 new AttributeHandler(Namespace.JDNC, Attributes.SELECTION_MODE, TableAttributes.selectionModeApplier); 363 364 protected static final AttributeHandler showsHorizontalLinesHandler = 365 new AttributeHandler(Namespace.JDNC, Attributes.SHOWS_HORIZONTAL_LINES, TableAttributes.showsHorizontalLinesApplier); 366 367 protected static final AttributeHandler showsVerticalLinesHandler = 368 new AttributeHandler(Namespace.JDNC, Attributes.SHOWS_VERTICAL_LINES, TableAttributes.showsVerticalLinesApplier); 369 370 protected static final AttributeHandler sizeHandler = 371 new AttributeHandler(Namespace.JDNC, Attributes.SIZE, ComponentAttributes.sizeApplier); 372 373 protected static final AttributeHandler isColumnHeaderLockedHandler = 374 new AttributeHandler(Namespace.JDNC, Attributes.IS_COLUMN_HEADER_LOCKED, TableAttributes.isColumnHeaderLockedApplier); 375 376 protected static final AttributeHandler isRowHeaderLockedHandler = 377 new AttributeHandler(Namespace.JDNC, Attributes.IS_ROW_HEADER_LOCKED, TableAttributes.isRowHeaderLockedApplier); 378 379 protected static final AttributeHandler titleHandler = 380 new AttributeHandler(Namespace.JDNC, Attributes.TITLE, NullAttribute.nullApplier); 381 382 385 386 protected static final ElementHandler filtersElementHandler = 387 new ElementHandler(ElementTypes.FILTERS, TableElement.filtersAssimilator); 388 389 protected static final ElementHandler headerElementHandler = 390 new ElementHandler(ElementTypes.TABLE_HEADER, TableElement.headerAssimilator); 391 392 protected static final ElementHandler highlightersElementHandler = 393 new ElementHandler(ElementTypes.HIGHLIGHTERS, TableElement.highlightersAssimilator); 394 395 protected static final ElementHandler dataElementHandler = 396 new ElementHandler(ElementTypes.DATA, TableElement.dataAssimilator); 397 398 protected static final ElementHandler tabularDataElementHandler = 399 new ElementHandler(ElementTypes.TABULAR_DATA, TableElement.dataAssimilator); 400 401 protected static final ElementHandler tableColumnsElementHandler = 402 new ElementHandler(ElementTypes.TABLE_COLUMNS, TableElement.tableColumnsAssimilator); 403 404 protected static final ElementHandler toolBarElementHandler = 405 new ElementHandler(ElementTypes.COMPONENT_TOOLBAR, TableElement.toolBarAssimilator); 406 407 } 408 | Popular Tags |