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:
055: /** Model for the Grid widget.
056: */
057: public class GridModel extends WidgetModel {
058: private static Logger log = Logger.getLogger(GridModel.class);
059:
060: /** Holds a List of GridModelRow objects */
061: private List m_rows = new ArrayList();
062:
063: /** For efficiency resons, this map will hold the GridModelRow objects, keyed by the rowId */
064: private Map m_rowsMap = new HashMap();
065:
066: /** This is the rowId that will be assigned to the next new row created */
067: private int m_maxRowId = 0;
068:
069: /** This flag will be set if any error occurs while writing the user-settings to the filesystem. */
070: private boolean m_errorInSavingUserSettings = false;
071:
072: /** Creates and returns a new GridModelRow. This object will be added to the end of the the Grid.
073: * @return a new GridModelRow object.
074: */
075: public GridModelRow newRow() {
076: GridModelRow r = new GridModelRow(this , m_maxRowId++);
077: m_rows.add(r);
078: m_rowsMap.put(new Integer(r.getRowId()), r);
079: return r;
080: }
081:
082: /** Create a new Row at the specfied position in the Grid.
083: * rowNum can be from 0 to GridModel.size().
084: * if rowNum = GridModel.size(), then this function is the same as newRow();
085: * @param rowNum The position at which the row is to be added.
086: * @return a new GridModelRow object.
087: */
088: public GridModelRow newRow(int rowNum) {
089: if (rowNum == m_rows.size())
090: return newRow();
091: else if (rowNum > m_rows.size())
092: throw new RuntimeException("Array Out Of Bounds. Size="
093: + m_rows.size() + ", position=" + rowNum);
094: else {
095: GridModelRow r = new GridModelRow(this , m_maxRowId++);
096: m_rows.add(rowNum, r);
097: m_rowsMap.put(new Integer(r.getRowId()), r);
098: return r;
099: }
100: }
101:
102: /** Empty out all the rows in the Grid */
103: public void clearRows() {
104: m_rows.clear();
105: m_rowsMap.clear();
106: m_maxRowId = 0;
107: }
108:
109: /** Removes the row from the grid.
110: * @param row The row to be removed.
111: */
112: public void removeRow(GridModelRow row) {
113: m_rows.remove(row);
114: m_rowsMap.remove(new Integer(row.getRowId()));
115: }
116:
117: /** Returns a Collection of GridModelRow objects.
118: * @return a Collection of GridModelRow objects.
119: */
120: public Collection getRows() {
121: return m_rows;
122: }
123:
124: /** Returns a Row object based on the position or the row in the List.
125: * @param rowNum The position of the row.
126: * @return a Row object based on the position or the row in the List.
127: */
128: public GridModelRow getRow(int rowNum) {
129: return (GridModelRow) m_rows.get(rowNum);
130: }
131:
132: /** Returns a Row object based on the rowId.
133: * @param rowId The id of the row.
134: * @return Row object based on the rowId.
135: */
136: public GridModelRow getRowById(int rowId) {
137: return (GridModelRow) m_rowsMap.get(new Integer(rowId));
138: }
139:
140: /** Returns a field value from a given row.
141: * @param columnName The name of the field.
142: * @param rowNum The position of the row.
143: * @return a field value from a given row.
144: */
145: public Object getElement(String columnName, int rowNum) {
146: GridModelRow row = getRow(rowNum);
147: if (row == null)
148: throw new RuntimeException("No Such Row : " + rowNum);
149: return row.getElement(columnName);
150: }
151:
152: /** getter for the attribute errorInSavingUserSettings
153: */
154: public boolean getErrorInSavingUserSettings() {
155: return m_errorInSavingUserSettings;
156: }
157:
158: /** setter for the attribute
159: */
160: public void setErrorInSavingUserSettings(boolean value) {
161: m_errorInSavingUserSettings = value;
162: }
163:
164: }
|