001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.presentation.portlet.widgets.model;
051:
052: import org.apache.log4j.*;
053: import java.util.*;
054: import org.jaffa.util.ListMap;
055: import org.jaffa.presentation.portlet.widgets.model.exceptions.ColumnMismatchRuntimeException;
056:
057: /** Model for the Table widget.
058: */
059: public class TableModel extends WidgetModel {
060: private static Logger log = Logger.getLogger(TableModel.class);
061:
062: /** This maintains a columnName-columnObject pairs */
063: private ListMap m_columns = new ListMap();
064:
065: /** This maintains a collection of rows */
066: private List m_rows = new ArrayList();
067:
068: /** This maintains a collection of rows selected by the user */
069: private List m_selectedRows = new ArrayList();
070:
071: /** A flag to indicate if the model has been modified */
072: private boolean m_modelChanged = false;
073:
074: /** Clear out all the columns */
075: public void clearColumns() {
076: m_columns.clear();
077: setModelChanged(true);
078: }
079:
080: /** Add a column to the Table. An exisiting column by the same name will be over-written.
081: * @param name The name of the column.
082: * @param dataType The data type of the column.
083: */
084: public void addColumn(String name, String dataType) {
085: Column column = new Column(name, dataType);
086: m_columns.put(name, column);
087: setModelChanged(true);
088: }
089:
090: /** Returns a List of TableModel.Column objects, in the order of entry.
091: * @return a List of TableModel.Column objects, in the order of entry.
092: */
093: public List getColumns() {
094: return new ArrayList(m_columns.values());
095: }
096:
097: /** Returns a List of columnNames, in the order of entry.
098: * @return a List of columnNames, in the order of entry.
099: */
100: public List getColumnNames() {
101: return new ArrayList(m_columns.keySet());
102: }
103:
104: /** Returns the dataType for the columnName. A null will be returned in case no such column exists.
105: * @param columnName The name of the column.
106: * @return the dataType for the column.
107: */
108: public String getColumnDataType(String columnName) {
109: Column column = (Column) m_columns.get(columnName);
110: if (column != null)
111: return column.getDataType();
112: else
113: return null;
114: }
115:
116: /** Add a row to the table. A ColumnMismatchRuntimeException is thrown if the no. of fields does not equal the no. of columns.
117: * @param fields The List of values.
118: */
119: public void addRow(List fields) {
120: addRow(-1, fields);
121: }
122:
123: /** Add a row to the table. A ColumnMismatchRuntimeException is thrown if the no. of fields does not equal the no. of columns.
124: * @param position The index of the row.
125: * @param fields The List of values.
126: */
127: public void addRow(int position, List fields) {
128: if (fields.size() != m_columns.size()) {
129: throw new ColumnMismatchRuntimeException(
130: "Wrong number of columns in Row, got "
131: + fields.size() + " expected "
132: + m_columns.size());
133: }
134:
135: if (position == -1)
136: m_rows.add(fields);
137: else
138: m_rows.add(position, fields);
139:
140: setModelChanged(true);
141: }
142:
143: /** Clear out all the rows */
144: public void clearRows() {
145: m_rows.clear();
146: setModelChanged(true);
147: }
148:
149: /** Returns a Collection, where each element is a List of fields, in the order of the columns.
150: * @return a Collection, where each element is a List of fields, in the order of the columns.
151: */
152: public List getRows() {
153: return m_rows;
154: }
155:
156: /** Returns a List of field values in column order for the input row number.
157: * An IndexOutOfBoundsException will be thrown in case the rowNum is senseless.
158: * @param rowNum The index of the row to be retrieved.
159: * @return a List of field values in column order for the input row number.
160: */
161: public List getRow(int rowNum) {
162: return (List) m_rows.get(rowNum);
163: }
164:
165: /** Return a field value from a given row.
166: * A null will be returned in case the columnName does not exist
167: * An IndexOutOfBoundsException will be thrown in case the rowNum is senseless.
168: * @param columnName The name of the column.
169: * @param rowNum The index of the row to be retrieved.
170: * @return a field value from a given row.
171: */
172: public Object getValue(String columnName, int rowNum) {
173: Object obj = null;
174: List row = getRow(rowNum);
175: if (row != null) {
176: int i = m_columns.indexOf(columnName);
177: if (i != -1)
178: obj = row.get(i);
179: }
180: return obj;
181: }
182:
183: /** Returns a List of selected rows, where each row is a List of field values.
184: * @return a List of selected rows, where each row is a List of field values.
185: */
186: public List getSelectedRows() {
187: return m_selectedRows;
188: }
189:
190: /** Sets the selected rows. This will first reset the selected status of all rows.
191: * @param rowNums A List of row numbers to be marked as selected.
192: */
193: public void setSelectedRows(List rowNums) {
194: m_selectedRows.clear();
195: for (Iterator itr = rowNums.iterator(); itr.hasNext();) {
196: int i = Integer.parseInt((String) itr.next());
197: Object row = m_rows.get(i);
198: if (row != null)
199: m_selectedRows.add(row);
200: }
201: setModelChanged(true);
202: }
203:
204: /** Change the state of the model internally */
205: private void setModelChanged(boolean setState) {
206: m_modelChanged = setState;
207: }
208:
209: /** See if model has changed, in the process reset the changed flag.
210: * @return true if the model was changed.
211: */
212: public boolean isModelChanged() {
213: boolean modified = m_modelChanged;
214: m_modelChanged = false;
215: return modified;
216: }
217:
218: /** An instance of this class will be created for each column added to the TableModel.
219: */
220: public class Column {
221: private String m_name;
222: private String m_dataType;
223:
224: /** Constructor.
225: * @param name The name of the column.
226: * @param dataType The data type of the column.
227: */
228: public Column(String name, String dataType) {
229: m_name = name;
230: m_dataType = dataType;
231: }
232:
233: /** Getter for property name.
234: * @return Value of property name.
235: */
236: public String getName() {
237: return m_name;
238: }
239:
240: /** Getter for property dataType.
241: * @return Value of property dataType.
242: */
243: public String getDataType() {
244: return m_dataType;
245: }
246:
247: /** Returns diagnostic information.
248: * @return diagnostic information.
249: */
250: public String toString() {
251: return "Name=" + m_name + ", DataType=" + m_dataType;
252: }
253: }
254:
255: }
|