001: package org.swingml.treetablebrowser.ext;
002:
003: import java.awt.*;
004:
005: import javax.swing.*;
006:
007: import org.swingml.*;
008: import org.swingml.component.*;
009: import org.swingml.util.*;
010: import org.swingml.xml.*;
011: import org.w3c.dom.*;
012:
013: public class TreeTableBrowserColumnMapper extends MapperUtil implements
014: Mapper {
015:
016: private String stripPattern(String value) {
017: String result = null;
018: int pos = value.indexOf(':');
019: if (pos > 0) {
020: result = value.substring(pos + 1);
021: }
022: return result;
023: }
024:
025: public Object getModelToMap(Node aNode, Object aParent,
026: Container aContainer) {
027: TreeTableBrowserColumnModel theTreeTableColumnModel = new TreeTableBrowserColumnModel(
028: aContainer);
029: SwingMLModel theContainer = (SwingMLModel) aParent;
030: theContainer.addChild(theTreeTableColumnModel);
031: theTreeTableColumnModel.setParent(theContainer);
032: return theTreeTableColumnModel;
033: }
034:
035: public void mapModel(Node aNode, Object aParent,
036: Container aContainer) {
037: TreeTableBrowserColumnModel theTreeTableColumnModel = (TreeTableBrowserColumnModel) this
038: .getModelToMap(aNode, aParent, aContainer);
039: this
040: .mapModelAttributes(aNode, theTreeTableColumnModel,
041: aParent);
042: super .iterate(aNode, theTreeTableColumnModel, aContainer);
043: }
044:
045: public void mapModelAttributes(Node aNode, Object mod,
046: Object aParent) {
047: TreeTableBrowserColumnModel aModel = (TreeTableBrowserColumnModel) mod;
048: super .mapCommonAttributes(aModel, aNode);
049: // obtain key value..
050: Node theResultNode = super .getAttribute(aNode, Constants.TYPE);
051: if (theResultNode != null) {
052: String theType = theResultNode.getNodeValue();
053: if (theType.equalsIgnoreCase(Constants.BOOLEAN)) {
054: aModel.setType(new Boolean(false));
055: }
056: if (theType.equalsIgnoreCase(Constants.COMBO)) {
057: aModel.setType(new JComboBox());
058: }
059: if (theType.equalsIgnoreCase(Constants.COLOR)) {
060: aModel.setType(new ColorComponent(0));
061: }
062: if (theType.equalsIgnoreCase(Constants.STRING)) {
063: aModel.setType(new String());
064: }
065: if (theType.indexOf(Constants.DATE) >= 0) {
066: String pattern = stripPattern(theType);
067: if (pattern == null) {
068: throw new RuntimeException(
069: "DATE type for TREE_TABLE_BROWSER column must have pattern supplied i.e. date:mm/dd/yyyy");
070: }
071: DateConverter converter = new DateConverter(pattern);
072: aModel.setType(converter);
073: }
074: if (theType.indexOf(Constants.NUMBER) >= 0) {
075: String pattern = stripPattern(theType);
076: NumberConverter converter = new NumberConverter(pattern);
077: aModel.setType(converter);
078: }
079: } else {
080: aModel.setType(new String());
081: }
082: // Default Sort attributes
083: theResultNode = super .getAttribute(aNode, Constants.SORT);
084: if (theResultNode != null) {
085: String theNodeValue = theResultNode.getNodeValue();
086: String pattern = stripPattern(theNodeValue);
087: int index = theNodeValue.indexOf(':');
088: String value = null;
089: if (index > 0) {
090: value = theNodeValue.substring(0, index);
091: } else {
092: value = theNodeValue;
093: }
094: try {
095: int sortOrder = new Integer(value).intValue();
096: aModel.setSortOrder(sortOrder);
097: if (pattern != null) {
098: aModel.setSortDirection(pattern
099: .equalsIgnoreCase("DESC") ? 1 : 0);
100: }
101: } catch (NumberFormatException e) {
102: throw new RuntimeException(
103: "Yellow Table Browser column definition (TBC) sort order attribute must be numeric..");
104: }
105: }
106: theResultNode = super .getAttribute(aNode, Constants.WIDTH);
107: if (theResultNode != null) {
108: aModel.setWidth(Integer.parseInt(theResultNode
109: .getNodeValue()));
110: }
111:
112: theResultNode = super .getAttribute(aNode, Constants.SORTABLE);
113: if (theResultNode != null) {
114: aModel
115: .setSortable(new Boolean(theResultNode
116: .getNodeValue()).booleanValue());
117: }
118:
119: theResultNode = super .getAttribute(aNode, Constants.ALIGN);
120: if (theResultNode != null) {
121: aModel.setAlignment(theResultNode.getNodeValue());
122: }
123:
124: theResultNode = super .getAttribute(aNode,
125: Constants.COLUMN_ORDER);
126: if (theResultNode != null) {
127: try {
128: aModel.setColumnOrder(Integer.parseInt(theResultNode
129: .getNodeValue()));
130: } catch (NumberFormatException e) {
131: throw new RuntimeException(
132: "Yellow Tree Table Browser column definition (TBC) order attribute must be numeric..");
133: }
134: }
135: }
136: }
|