001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app.table;
031:
032: import java.io.Serializable;
033: import java.util.ArrayList;
034: import java.util.EventListener;
035: import java.util.Iterator;
036: import java.util.List;
037:
038: import nextapp.echo2.app.event.EventListenerList;
039: import nextapp.echo2.app.event.TableColumnModelEvent;
040: import nextapp.echo2.app.event.TableColumnModelListener;
041:
042: /**
043: * The default <code>TableColumnModel</code> implementation.
044: */
045: public class DefaultTableColumnModel implements Serializable,
046: TableColumnModel {
047:
048: /**
049: * A collection of all columns in the column model in order.
050: */
051: private List columns = new ArrayList();
052:
053: /**
054: * A listener storage facility.
055: */
056: protected EventListenerList listenerList = new EventListenerList();
057:
058: /**
059: * Creates a new DefaultTableColumnModel.
060: */
061: public DefaultTableColumnModel() {
062: super ();
063: }
064:
065: /**
066: * @see nextapp.echo2.app.table.TableColumnModel#addColumn(nextapp.echo2.app.table.TableColumn)
067: */
068: public void addColumn(TableColumn column) {
069: columns.add(column);
070: fireColumnAdded(new TableColumnModelEvent(this , -1, columns
071: .size() - 1));
072: }
073:
074: /**
075: * @see nextapp.echo2.app.table.TableColumnModel#addColumnModelListener(nextapp.echo2.app.event.TableColumnModelListener)
076: */
077: public void addColumnModelListener(TableColumnModelListener l) {
078: listenerList.addListener(TableColumnModelListener.class, l);
079: }
080:
081: /**
082: * Notifies <code>TableColumnModelListener</code>s that a column was
083: * added.
084: *
085: * @param e the <code>TableColumnModelEvent</code> to fire
086: */
087: protected void fireColumnAdded(TableColumnModelEvent e) {
088: EventListener[] listeners = listenerList
089: .getListeners(TableColumnModelListener.class);
090:
091: for (int index = 0; index < listeners.length; ++index) {
092: ((TableColumnModelListener) listeners[index])
093: .columnAdded(e);
094: }
095: }
096:
097: /**
098: * Notifies <code>TableColumnModelListener</code>s that a column was
099: * moved.
100: *
101: * @param e the <code>TableColumnModelEvent</code> to fire
102: */
103: protected void fireColumnMoved(TableColumnModelEvent e) {
104: EventListener[] listeners = listenerList
105: .getListeners(TableColumnModelListener.class);
106:
107: for (int index = 0; index < listeners.length; ++index) {
108: ((TableColumnModelListener) listeners[index])
109: .columnMoved(e);
110: }
111: }
112:
113: /**
114: * Notifies <code>TableColumnModelListener</code>s that a column was
115: * removed.
116: *
117: * @param e the <code>TableColumnModelEvent</code> to fire
118: */
119: protected void fireColumnRemoved(TableColumnModelEvent e) {
120: EventListener[] listeners = listenerList
121: .getListeners(TableColumnModelListener.class);
122:
123: for (int index = 0; index < listeners.length; ++index) {
124: ((TableColumnModelListener) listeners[index])
125: .columnRemoved(e);
126: }
127: }
128:
129: /**
130: * @see nextapp.echo2.app.table.TableColumnModel#getColumn(int)
131: */
132: public TableColumn getColumn(int index) {
133: if ((getColumnCount() - 1) < index) {
134: return null;
135: }
136:
137: return (TableColumn) columns.get(index);
138: }
139:
140: /**
141: * @see nextapp.echo2.app.table.TableColumnModel#getColumnCount()
142: */
143: public int getColumnCount() {
144: return columns.size();
145: }
146:
147: /**
148: * @see nextapp.echo2.app.table.TableColumnModel#getColumnIndex(java.lang.Object)
149: */
150: public int getColumnIndex(Object identifier) {
151: if (identifier == null) {
152: throw new IllegalArgumentException(
153: "Null not allowed as identifier value.");
154: }
155:
156: Iterator it = columns.iterator();
157: int index = 0;
158:
159: while (it.hasNext()) {
160: TableColumn column = (TableColumn) it.next();
161: if (identifier.equals(column.getIdentifier())) {
162: return index;
163: }
164: ++index;
165: }
166:
167: throw new IllegalArgumentException(
168: "No column found with specified identifier: "
169: + identifier);
170: }
171:
172: /**
173: * @see nextapp.echo2.app.table.TableColumnModel#getColumns()
174: */
175: public Iterator getColumns() {
176: return columns.iterator();
177: }
178:
179: /**
180: * @see nextapp.echo2.app.table.TableColumnModel#moveColumn(int, int)
181: */
182: public void moveColumn(int columnIndex, int newIndex) {
183: if (columnIndex < 0 || columnIndex >= columns.size()) {
184: throw new IllegalArgumentException(
185: "No column exists at index: " + columnIndex);
186: }
187: if (newIndex < 0 || newIndex >= columns.size()) {
188: throw new IllegalArgumentException(
189: "Attempt to move column to invalid index: "
190: + newIndex);
191: }
192:
193: TableColumn column = (TableColumn) columns.remove(columnIndex);
194: columns.add(newIndex, column);
195: fireColumnMoved(new TableColumnModelEvent(this , columnIndex,
196: newIndex));
197: }
198:
199: /**
200: * @see nextapp.echo2.app.table.TableColumnModel#removeColumn(nextapp.echo2.app.table.TableColumn)
201: */
202: public void removeColumn(TableColumn column) {
203: int columnIndex = columns.indexOf(column);
204: if (columnIndex == -1) {
205: // Do nothing, column is not in model.
206: return;
207: }
208: columns.remove(columnIndex);
209: fireColumnAdded(new TableColumnModelEvent(this , columnIndex, -1));
210: }
211:
212: /**
213: * @see nextapp.echo2.app.table.TableColumnModel#removeColumnModelListener(nextapp.echo2.app.event.TableColumnModelListener)
214: */
215: public void removeColumnModelListener(TableColumnModelListener l) {
216: listenerList.removeListener(TableColumnModelListener.class, l);
217: }
218: }
|