001: /*
002: * SwingML Copyright (C) 2002 SwingML Team
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.model;
022:
023: import java.util.*;
024:
025: import javax.swing.*;
026: import javax.swing.event.*;
027: import javax.swing.table.*;
028:
029: import org.swingml.*;
030: import org.swingml.system.*;
031:
032: public class JTableModel extends SwingMLModel implements TableModel {
033:
034: private List columns;
035: private TableDataModel data;
036: private int height;
037: private int mode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION;
038: private boolean onlyPostDirty;
039: // Added to support the submission of several column values
040: private int[] postColumns;
041: private String postStyle;
042: private TableRowModel row;
043: private List rows;
044: private int width;
045:
046: public JTableModel() {
047: super ();
048: }
049:
050: public void addChild(Object aChild) {
051: if (aChild instanceof TableColumnModel) {
052: getColumns().add(aChild);
053: } else if (aChild instanceof TableRowModel) {
054: getRows().add(aChild);
055: }
056: }
057:
058: public void addTableModelListener(TableModelListener l) {
059: }
060:
061: public Class getColumnClass(int aColumnIndex) {
062: TableColumnModel theTableColumnModel = (TableColumnModel) this
063: .getColumns().get(aColumnIndex);
064: return theTableColumnModel.getType().getClass();
065: }
066:
067: public int getColumnCount() {
068: return this .getColumns().size();
069: }
070:
071: public int getColumnIndex(String name) {
072: int result = -1;
073: List theColumns = getColumns();
074: TableColumnModel theTableColumnModel = null;
075: int count = 0;
076: boolean found = false;
077: Iterator schmiterator = theColumns.iterator();
078: while (schmiterator.hasNext()) {
079: theTableColumnModel = (TableColumnModel) schmiterator
080: .next();
081: if (theTableColumnModel.getText().equals(name)) {
082: found = true;
083: break;
084: }
085: count++;
086: }
087:
088: if (found) {
089: result = count;
090: }
091: return result;
092: }
093:
094: public String getColumnName(int aColumnIndex) {
095: TableColumnModel theTableColumnModel = (TableColumnModel) this
096: .getColumns().get(aColumnIndex);
097: return theTableColumnModel.getText();
098: }
099:
100: public List getColumns() {
101: if (this .columns == null) {
102: this .columns = new Vector();
103: }
104: return columns;
105: }
106:
107: public TableDataModel getDataModel(int rowIndex, int columnIndex) {
108: try {
109: TableRowModel row1 = (TableRowModel) getRows()
110: .get(rowIndex);
111: TableDataModel data1 = (TableDataModel) row1.getChildren()
112: .get(columnIndex);
113: return data1;
114: } catch (ArrayIndexOutOfBoundsException e) {
115: return null;
116: }
117: }
118:
119: public int getHeight() {
120: return height;
121: }
122:
123: public int getMode() {
124: return this .mode;
125: }
126:
127: /**
128: * Returns the indexes of the columns that should be posted
129: */
130: public int[] getPostColumns() {
131: return this .postColumns;
132: }
133:
134: public String getPostStyle() {
135: return this .postStyle;
136: }
137:
138: public int getRowCount() {
139: return this .getRows().size();
140: }
141:
142: public List getRows() {
143: if (this .rows == null) {
144: this .rows = new Vector();
145: }
146: return rows;
147: }
148:
149: public String getToolTip(int rowIndex, int columnIndex) {
150: try {
151: TableRowModel row1 = (TableRowModel) getRows()
152: .get(rowIndex);
153: TableDataModel data1 = (TableDataModel) row1.getChildren()
154: .get(columnIndex);
155: return data1.getTooltip();
156: } catch (ArrayIndexOutOfBoundsException e) {
157: return null;
158: }
159: }
160:
161: public Object getValueAt(int aRowIndex, int aColumnIndex) {
162: this .row = (TableRowModel) getRows().get(aRowIndex);
163: this .data = (TableDataModel) row.getChildren()
164: .get(aColumnIndex);
165: return this .data.getValue();
166: }
167:
168: public int getWidth() {
169: return width;
170: }
171:
172: public boolean isCellEditable(int aRowIndex, int aColumnIndex) {
173: this .row = (TableRowModel) getRows().get(aRowIndex);
174: this .data = (TableDataModel) row.getChildren()
175: .get(aColumnIndex);
176: return this .data.isEditable();
177: }
178:
179: public boolean isOnlyPostDirty() {
180: return onlyPostDirty;
181: }
182:
183: public void removeTableModelListener(TableModelListener l) {
184: }
185:
186: public void setHeight(final int i) {
187: height = i;
188: }
189:
190: public void setMode(int aMode) {
191: this .mode = aMode;
192: }
193:
194: public void setOnlyPostDirty(boolean onlyPostDirty) {
195: this .onlyPostDirty = onlyPostDirty;
196: }
197:
198: public void setPostColumn(int aPostColumn) {
199: setPostColumns(new int[] { aPostColumn });
200: }
201:
202: /**
203: * Sets the indexes of the columns that should be posted
204: */
205: public void setPostColumns(int[] postColumns) {
206: this .postColumns = postColumns;
207: }
208:
209: /**
210: * Sets the indexes of the columns that should be posted
211: */
212: public void setPostColumns(String postColumns) {
213: if (postColumns == null || postColumns.trim().length() == 0) {
214: setPostColumns((int[]) null);
215: } else {
216: StringTokenizer tokenizer = new StringTokenizer(
217: postColumns, ", ");
218: int[] temp = new int[tokenizer.countTokens()];
219: int i = 0;
220: while (tokenizer.hasMoreTokens()) {
221: temp[i++] = Integer.parseInt(tokenizer.nextToken());
222: }
223: setPostColumns(temp);
224: }
225: }
226:
227: public void setPostStyle(String aPostStyle) {
228: this .postStyle = aPostStyle;
229: }
230:
231: public void setValueAt(Object aValue, int aRowIndex,
232: int aColumnIndex) {
233: this .row = (TableRowModel) getRows().get(aRowIndex);
234: this .data = (TableDataModel) row.getChildren()
235: .get(aColumnIndex);
236: this .data.setValue(aValue);
237: }
238:
239: public void setWidth(final int i) {
240: width = i;
241: }
242:
243: public void validate() {
244: if (super .getParent().getLayout() != null
245: && super .getParent().getLayout().equalsIgnoreCase(
246: Constants.BORDERLAYOUT)) {
247: if (super .getOrientation() == null) {
248: SwingMLLogger
249: .getInstance()
250: .log(
251: "Syntax error: The parameter ORIENTATION in the element "
252: + super .getName()
253: + " is required since its parent's LAYOUT is BorderLayout. Add the parameter ORIENTATION to the element "
254: + super .getName()
255: + " or change its parent's LAYOUT to other than BorderLayout.");
256: }
257: }
258: if (super .getParent().getLayout() != null
259: && !super .getParent().getLayout().equalsIgnoreCase(
260: Constants.BORDERLAYOUT)) {
261: if (super .getOrientation() != null) {
262: SwingMLLogger
263: .getInstance()
264: .log(
265: "Syntax error: The parameter ORIENTATION in the element "
266: + super .getName()
267: + " should be used only when its parent's LAYOUT is BorderLayout. Change its parent's LAYOUT to BorderLayout or delete the parameter ORIENTATION from the element "
268: + super .getName() + ".");
269: }
270: }
271: Iterator theColumns = this .getColumns().iterator();
272: TableColumnModel theTableColumnModel = null;
273: while (theColumns.hasNext()) {
274: theTableColumnModel = (TableColumnModel) theColumns.next();
275: theTableColumnModel.validate();
276: }
277: Iterator theData = null;
278: TableRowModel theTableRowModel = null;
279: TableDataModel theTableDataModel = null;
280: Iterator theRows = this .getRows().iterator();
281: while (theRows.hasNext()) {
282: theTableRowModel = (TableRowModel) theRows.next();
283: theData = theTableRowModel.getChildren().iterator();
284: while (theData.hasNext()) {
285: theTableDataModel = (TableDataModel) theData.next();
286: theTableDataModel.validate();
287: }
288: }
289: }
290: }
|