001: /* SwingML
002: * Copyright (C) 2002 Ezequiel Cuellar.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: *
019: * Authors:
020: * Ezequiel Cuellar <ecuellar@crosslogic.com>
021: *
022: */
023:
024: package org.swingml.tablebrowser.ext;
025:
026: import java.awt.*;
027: import java.util.*;
028:
029: import javax.swing.*;
030:
031: import org.swingml.*;
032: import org.swingml.xml.*;
033: import org.w3c.dom.*;
034:
035: public class TableBrowserMapper extends MapperUtil implements Mapper {
036:
037: public Object getModelToMap(final Node aNode, final Object aParent,
038: Container aContainer) {
039: TableBrowserModel tbm = new TableBrowserModel();
040: SwingMLModel smlm = (SwingMLModel) aParent;
041: smlm.addChild(tbm);
042: tbm.setParent(smlm);
043:
044: return tbm;
045: }
046:
047: public void mapModel(final Node n, final Object o,
048: Container aContainer) {
049: TableBrowserModel tbm = (TableBrowserModel) getModelToMap(n, o,
050: aContainer);
051: mapModelAttributes(n, tbm, o);
052: iterate(n, tbm, aContainer);
053: }
054:
055: public void mapModelAttributes(final Node aNode,
056: final Object aModel, final Object aParent) {
057:
058: TableBrowserModel theModel = (TableBrowserModel) aModel;
059: mapCommonAttributes(theModel, aNode);
060: Node node = getAttribute(aNode, Constants.POST_STYLE);
061: if (node != null) {
062: theModel.setPostStyle(node.getNodeValue());
063: } else {
064: theModel.setPostStyle(Constants.POST_ALL);
065: }
066:
067: node = getAttribute(aNode, Constants.POST_DIRTY_ONLY);
068: if (node != null) {
069: theModel.setOnlyPostDirty(Boolean.valueOf(
070: node.getNodeValue()).booleanValue());
071: }
072:
073: node = getAttribute(aNode, Constants.POST_COLUMN);
074: if (node != null) {
075: String value = node.getNodeValue();
076: if (value.indexOf(",") > 0) {
077: StringTokenizer tokenizer = new StringTokenizer(value,
078: ",", false);
079: int numberOfColumns = tokenizer.countTokens();
080: int columns[] = new int[numberOfColumns];
081: int index = 0;
082: boolean hadError = false;
083: while (tokenizer.hasMoreTokens()) {
084: String columnNumber = tokenizer.nextToken().trim();
085: try {
086: columns[index] = Integer.parseInt(columnNumber);
087: } catch (Exception e) {
088: // failed! skip them all
089: hadError = true;
090: break;
091: }
092: index++;
093: }
094: if (!hadError) {
095: theModel.setPostColumns(columns);
096: }
097: } else {
098: int i = 0;
099: try {
100: i = Integer.parseInt(node.getNodeValue());
101: } catch (Exception e) {
102: }
103:
104: theModel.setPostColumn(i);
105: }
106: }
107:
108: node = getAttribute(aNode, Constants.ROW_HEADER);
109: if (node != null) {
110: boolean showRowHeader = Boolean
111: .valueOf(node.getNodeValue()).booleanValue();
112: theModel.setRowHeaderVisible(showRowHeader);
113: }
114:
115: node = getAttribute(aNode, Constants.MODE);
116: if (node != null) {
117: String theMode = node.getNodeValue();
118: int theNewMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
119: if (theMode.equalsIgnoreCase(Constants.SINGLE)) {
120: theNewMode = ListSelectionModel.SINGLE_SELECTION;
121: } else {
122: theNewMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
123: }
124: theModel.setMode(theNewMode);
125: }
126:
127: node = getAttribute(aNode, Constants.HEIGHT);
128: if (null != node) {
129: int i;
130: try {
131: i = Integer.parseInt(node.getNodeValue());
132: } catch (Exception e) {
133: i = 0;
134: }
135: theModel.setHeight(i);
136: }
137:
138: node = getAttribute(aNode, Constants.WIDTH);
139: if (null != node) {
140: int i;
141: try {
142: i = Integer.parseInt(node.getNodeValue());
143: } catch (Exception e) {
144: i = 0;
145: }
146: theModel.setWidth(i);
147: }
148:
149: node = getAttribute(aNode, Constants.POST_COLUMN_STATE);
150: if (null != node) {
151: theModel.setPostColumnState(node.getNodeValue()
152: .equalsIgnoreCase(Constants.TRUE));
153: } else {
154: theModel.setPostColumnState(false);
155: }
156:
157: node = getAttribute(aNode, Constants.SELECTION_STYLE);
158: if (null != node) {
159: int styleId = TableBrowserModel.SELECTION_STYLE_DEFAULT;
160: String style = node.getNodeValue();
161: if (style != null && style.length() > 0) {
162: if (style.equalsIgnoreCase("CELL-SINGLE")) {
163: styleId = TableBrowserModel.SELECTION_STYLE_SINGLE_CELL;
164: } else if (style.equalsIgnoreCase("CELL-MULTIPLE")) {
165: styleId = TableBrowserModel.SELECTION_STYLE_MULTI_CELL;
166: } else if (style.equalsIgnoreCase("ROW-SINGLE")) {
167: styleId = TableBrowserModel.SELECTION_STYLE_SINGLE_ROW;
168: } else if (style.equalsIgnoreCase("ROW-MULTIPLE")) {
169: styleId = TableBrowserModel.SELECTION_STYLE_MULTI_ROW;
170: }
171: }
172: theModel.setSelectionStyle(styleId);
173: } else {
174: theModel
175: .setSelectionStyle(TableBrowserModel.SELECTION_STYLE_DEFAULT);
176: }
177:
178: // AutoFit - Automatically set the initial width of the columns, based on the values in the cells
179: node = getAttribute(aNode, Constants.AUTO_FIT);
180: if (null != node) {
181: boolean autoFit = Boolean.valueOf(node.getNodeValue())
182: .booleanValue();
183: theModel.setAutoFit(autoFit);
184: }
185:
186: // CellEditorManager - Set a custom CellEditorManager on the TableBrowser
187: node = getAttribute(aNode, Constants.CELL_MANAGER_CLASS);
188: if (node != null) {
189: String className = node.getNodeValue();
190: theModel.setCellManager(className);
191: }
192: }
193: }
|