001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * SimpleTableModel.java
044: *
045: * Created on August 2, 2005, 3:17 PM
046: *
047: */
048:
049: package org.netbeans.microedition.lcdui;
050:
051: import java.util.Enumeration;
052: import java.util.Vector;
053:
054: /**
055: * Simple implementation of TableModel. This model can hold a matrix of String
056: * values. A matrix means all rows have to have the same number of columns
057: * and rows cannot be null.
058: * @author breh
059: */
060: public class SimpleTableModel implements TableModel {
061:
062: private Vector modelListeners = new Vector();
063: private String[][] values = new String[0][0];
064: private String[] columnNames = new String[0];
065:
066: private int cols;
067: private int rows;
068:
069: /**
070: * Creates a model with given values and column names.
071: * @param values values to be used in this table model. Please note, the values cannot be null
072: * and have to be a valid matrix.
073: * @param columnNames column names to be used. Can be null if the column names are not going
074: * to be used (see @isUsingHeaders method).
075: * @throws java.lang.IllegalArgumentException If the values parameter is null, or if it is not a valid rectangular matrix.
076: */
077: public SimpleTableModel(String[][] values, String[] columnNames)
078: throws IllegalArgumentException {
079: setValues(values);
080: setColumnNames(columnNames);
081: }
082:
083: /**
084: * Creates a model with given number of rows and columns.
085: * @param rows number of rows to be used in the model. Cannot be negative.
086: * @param cols number of columns to be used in the model. Cannot be negative.
087: * @throws java.lang.IllegalArgumentException if the cols or rows argument are lower than zero
088: */
089: public SimpleTableModel(int rows, int cols)
090: throws IllegalArgumentException {
091: if (rows < 0)
092: throw new IllegalArgumentException(
093: "row size cannot be negative");
094: if (cols < 0)
095: throw new IllegalArgumentException(
096: "column size cannot be negative");
097: String[][] newValues = new String[rows][cols];
098: for (int i = 0; i < rows; i++) {
099: newValues[i] = new String[cols];
100: }
101: setValues(newValues);
102: setColumnNames(new String[cols]);
103: }
104:
105: /**
106: * Creates a new empty table model.
107: */
108: public SimpleTableModel() {
109: }
110:
111: /**
112: * Gets number of columns of the supplied values matrix.
113: * @return values matrix column count
114: */
115: public int getColumnCount() {
116: return cols;
117: }
118:
119: /**
120: * Gets number of rows of the supplied values matrix.
121: * @return values matrix row count
122: */
123: public int getRowCount() {
124: return rows;
125: }
126:
127: /**
128: * Sets the value to the defined row and column of the model.
129: * <p>
130: * Please note, this method does not call fireTableModelChanged method
131: * automatically, so you have to call it manually if you would like to redraw
132: * the table. This is designed in this way, because of the performance reasons
133: * - you might want to update several values at a time and repaint the
134: * table at the end of the update.
135: *
136: * @throws java.lang.IllegalArgumentException if the values are not defined, or
137: * the specifed row or column is larger than the size of the values.
138: */
139: public void setValue(int col, int row, String value)
140: throws IllegalArgumentException {
141: if (values == null)
142: throw new IllegalArgumentException(
143: "No values set to the model");
144: if (values.length < row)
145: throw new IllegalArgumentException(
146: "Specified row ("
147: + row
148: + ") is larger than the number of rows available in values ("
149: + values.length + ").");
150: if ((values[row].length < col))
151: throw new IllegalArgumentException(
152: "Specified column ("
153: + col
154: + ") is larger than the number of columns available in values ("
155: + values[row].length + ").");
156: // change the value
157: values[row][col] = value;
158: }
159:
160: /**
161: * Sets the values of the model. Values of this model have to be a rectangular matrix -
162: * this means all rows have to have the same number of columns and rows canot be null.
163: * <p/>
164: * Please note, this class is holding just reference to the passed values array, so
165: * any change you do to the model via setValue method is actually made in the array.
166: *
167: * @param values values to be used in this table model. Please note, the values cannot be null
168: * and have to be a valid matrix.
169: * @throws java.lang.IllegalArgumentException If the values parameter is null, or if it is not a valid rectangular matrix.
170: */
171: public void setValues(String[][] values)
172: throws IllegalArgumentException {
173: // check values validity ...
174: checkValues(values);
175: this .values = values;
176: fireTableModelChanged();
177: }
178:
179: /**
180: * Gets values of the model
181: * @return values matrix
182: */
183: public String[][] getValues() {
184: return values;
185: }
186:
187: /*
188: public void setValue(int col, int row, String value) {
189:
190: }*/
191:
192: /**
193: * Gets the value of a table cell at a specified location. Always returns
194: * <code>String</code>.
195: * @return value for the given cell coordinates. May return null if there is no value.
196: * @param col col index of the value
197: * @param row row index of the value
198: */
199: public Object getValue(int col, int row) {
200: return values[row][col];
201: }
202:
203: /**
204: * Decides wheter this table is using headers (column names). This simple
205: * model simply checks whether the supplied column names are null and in
206: * such a case this method returns true.
207: * @return true if the column names are being supplied and should be visualized, false otherwise
208: */
209: public boolean isUsingHeaders() {
210: return columnNames != null;
211: }
212:
213: /**
214: * Sets the column names for this model. The array of names
215: * should have the same length as the column count.
216: * @param columnNames array of names. May be null if the column headers should not be visualized
217: */
218: public void setColumnNames(String[] columnNames) {
219: this .columnNames = columnNames;
220: fireTableModelChanged();
221: }
222:
223: /**
224: * Gets the column name for the specified index
225: * @param column column index
226: * @return column name
227: */
228: public String getColumnName(int column) {
229: if ((columnNames != null) && (column < columnNames.length)) {
230: return columnNames[column];
231: } else {
232: return null;
233: }
234: }
235:
236: /**
237: * Fires an event that the values in the table has been changed and
238: * the table should be repainted. This method is intended to be used by
239: * the user, since the model cannot track changes of
240: * values in the supplied arrays.
241: */
242: public void fireTableModelChanged() {
243: Enumeration e = modelListeners.elements();
244: while (e.hasMoreElements()) {
245: ((TableModelListener) e.nextElement())
246: .tableModelChanged(this );
247: }
248: }
249:
250: /**
251: * Adds a <code>TableModelListener</code> to this instance of the model.
252: * @param listener listener to be addded
253: */
254: public synchronized void addTableModelListener(
255: TableModelListener listener) {
256: if (listener != null) {
257: modelListeners.addElement(listener);
258: }
259: }
260:
261: /**
262: * Removes a <code>TableModelListener</code> from this instance of the model.
263: * @param listener listener to be removed
264: */
265: public synchronized void removeTableModelListener(
266: TableModelListener listener) {
267: modelListeners.removeElement(listener);
268: }
269:
270: /**
271: * Checks values if they are rectangular matrix
272: */
273: private void checkValues(String[][] values)
274: throws IllegalArgumentException {
275: rows = 0;
276: cols = 0;
277: if (values == null)
278: throw new IllegalArgumentException("Values cannot be null.");
279: rows = values.length;
280: if (rows > 0) {
281: cols = values[0].length;
282: }
283: for (int i = 0; i < values.length; i++) {
284: String[] row = values[i];
285: if (row == null) {
286: throw new IllegalArgumentException(
287: "Data cannot contain null rows (row " + i
288: + ").");
289: } else if (values[0].length != row.length) {
290: throw new IllegalArgumentException(
291: "Data cannot contain different row lengths (row "
292: + i + ").");
293: }
294: }
295: }
296:
297: }
|