001: /*
002: * SwingML Copyright (C) 2002 Ezequiel Cuellar.
003: *
004: * This library is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU Lesser General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option) any
007: * later version.
008: *
009: * This library is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with this library; if not, write to the Free Software Foundation, Inc.,
016: * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: * Authors: Ezequiel Cuellar <ecuellar@crosslogic.com>
019: *
020: */
021: package org.swingml.tablebrowser.ext;
022:
023: import java.awt.*;
024: import java.util.*;
025:
026: import javax.swing.*;
027:
028: import org.swingml.*;
029: import org.swingml.component.*;
030: import org.swingml.model.*;
031: import org.swingml.util.*;
032: import org.swingml.xml.*;
033: import org.w3c.dom.*;
034:
035: public class TableBrowserColumnMapper extends MapperUtil implements
036: Mapper {
037:
038: private String stripPattern(String value) {
039: String result = null;
040: int pos = value.indexOf(':');
041: if (pos > 0) {
042: result = value.substring(pos + 1);
043: }
044: return result;
045: }
046:
047: public Object getModelToMap(Node node, Object parent,
048: Container aContainer) {
049: TableColumnModel m = new TableBrowserColumnModel();
050: SwingMLModel c = (SwingMLModel) parent;
051: c.addChild(m);
052: m.setParent(c);
053: return m;
054: }
055:
056: public void mapModel(Node node, Object parent, Container aContainer) {
057: TableColumnModel m = (TableColumnModel) this .getModelToMap(
058: node, parent, aContainer);
059: this .mapModelAttributes(node, m, parent);
060: iterate(node, m, aContainer);
061: }
062:
063: public void mapModelAttributes(Node node, Object model,
064: Object parent) {
065: TableBrowserColumnModel m = (TableBrowserColumnModel) model;
066: super .mapCommonAttributes(m, node);
067: Node n = super .getAttribute(node, Constants.ITEMS);
068: if (n != null) {
069: Vector theItems = new Vector();
070: StringTokenizer theTokens = new StringTokenizer(n
071: .getNodeValue(), ",");
072: while (theTokens.hasMoreTokens()) {
073: theItems.addElement(theTokens.nextToken());
074: }
075: m.setItems(theItems);
076: }
077:
078: n = super .getAttribute(node, Constants.ALIGN);
079: if (n != null) {
080: m.setAlignment(n.getNodeValue());
081: }
082:
083: n = super .getAttribute(node, Constants.TYPE);
084: if (n != null) {
085: String theType = n.getNodeValue();
086: if (theType.equalsIgnoreCase(Constants.BOOLEAN)) {
087: m.setType(new Boolean(false));
088: } else if (theType.equalsIgnoreCase(Constants.COMBO)) {
089: m.setType(new JComboBox());
090: } else if (theType.equalsIgnoreCase(Constants.COLOR)) {
091: m.setType(new ColorComponent(0));
092: } else if (theType.equalsIgnoreCase(Constants.STRING)) {
093: m.setType(new String());
094: } else if (theType.indexOf(Constants.DATE) >= 0) {
095: String pattern = stripPattern(theType);
096: if (pattern == null) {
097: throw new RuntimeException(
098: "DATE type for TABLEBROWSER column(TBC) must have pattern supplied i.e. date:mm/dd/yyyy");
099: }
100: DateConverter converter = new DateConverter(pattern);
101: m.setType(converter);
102: } else if (theType.indexOf(Constants.NUMBER) >= 0) {
103: String pattern = stripPattern(theType);
104: NumberConverter converter = new NumberConverter(pattern);
105: m.setType(converter);
106: } else if (theType.equalsIgnoreCase(Constants.ICON)) {
107: m.setType(new JButton());
108: }
109: } else {
110: m.setType(new String());
111: }
112: // Default Sort attributes
113: n = super .getAttribute(node, Constants.SORT);
114: if (n != null) {
115: String theNodeValue = n.getNodeValue();
116: String pattern = stripPattern(theNodeValue);
117: int index = theNodeValue.indexOf(':');
118: String value = null;
119: if (index > 0) {
120: value = theNodeValue.substring(0, index);
121: } else {
122: value = theNodeValue;
123: }
124: try {
125: int sortOrder = new Integer(value).intValue();
126: m.setSortOrder(sortOrder);
127: if (pattern != null) {
128: m.setSortType(pattern.equalsIgnoreCase("DESC") ? 1
129: : 0);
130: }
131: } catch (NumberFormatException e) {
132: throw new RuntimeException(
133: "Yellow Table Browser column definition (TBC) sort order attribute must be numeric..");
134: }
135: }
136: n = super .getAttribute(node, Constants.WIDTH);
137: if (n != null) {
138: m.setWidth(Integer.parseInt(n.getNodeValue()));
139: }
140: n = super .getAttribute(node, Constants.HIDDEN);
141: if (n != null) {
142: m.setHidden(new Boolean(n.getNodeValue()).booleanValue());
143: }
144:
145: n = super .getAttribute(node, Constants.COLUMN_ORDER);
146: if (n != null) {
147: try {
148: m.setColumnOrder(Integer.parseInt(n.getNodeValue()));
149: } catch (NumberFormatException e) {
150: throw new RuntimeException(
151: "Yellow Table Browser column definition (TBC) order attribute must be numeric..");
152: }
153: }
154:
155: n = super .getAttribute(node, Constants.SORTABLE);
156: if (n != null) {
157: m.setSortable(new Boolean(n.getNodeValue()).booleanValue());
158: }
159: }
160: }
|