KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > table > DefaultTableColumnModel


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app.table;
31
32 import java.io.Serializable JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.EventListener JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37
38 import nextapp.echo2.app.event.EventListenerList;
39 import nextapp.echo2.app.event.TableColumnModelEvent;
40 import nextapp.echo2.app.event.TableColumnModelListener;
41
42 /**
43  * The default <code>TableColumnModel</code> implementation.
44  */

45 public class DefaultTableColumnModel
46 implements Serializable JavaDoc, TableColumnModel {
47     
48     /**
49      * A collection of all columns in the column model in order.
50      */

51     private List JavaDoc columns = new ArrayList JavaDoc();
52     
53     /**
54      * A listener storage facility.
55      */

56     protected EventListenerList listenerList = new EventListenerList();
57     
58     /**
59      * Creates a new DefaultTableColumnModel.
60      */

61     public DefaultTableColumnModel() {
62         super();
63     }
64     
65     /**
66      * @see nextapp.echo2.app.table.TableColumnModel#addColumn(nextapp.echo2.app.table.TableColumn)
67      */

68     public void addColumn(TableColumn column) {
69         columns.add(column);
70         fireColumnAdded(new TableColumnModelEvent(this, -1, columns.size() - 1));
71     }
72     
73     /**
74      * @see nextapp.echo2.app.table.TableColumnModel#addColumnModelListener(nextapp.echo2.app.event.TableColumnModelListener)
75      */

76     public void addColumnModelListener(TableColumnModelListener l) {
77         listenerList.addListener(TableColumnModelListener.class, l);
78     }
79         
80     /**
81      * Notifies <code>TableColumnModelListener</code>s that a column was
82      * added.
83      *
84      * @param e the <code>TableColumnModelEvent</code> to fire
85      */

86     protected void fireColumnAdded(TableColumnModelEvent e) {
87         EventListener JavaDoc[] listeners = listenerList.getListeners(TableColumnModelListener.class);
88         
89         for (int index = 0; index < listeners.length; ++index) {
90             ((TableColumnModelListener) listeners[index]).columnAdded(e);
91         }
92     }
93     
94     /**
95      * Notifies <code>TableColumnModelListener</code>s that a column was
96      * moved.
97      *
98      * @param e the <code>TableColumnModelEvent</code> to fire
99      */

100     protected void fireColumnMoved(TableColumnModelEvent e) {
101         EventListener JavaDoc[] listeners = listenerList.getListeners(TableColumnModelListener.class);
102         
103         for (int index = 0; index < listeners.length; ++index) {
104             ((TableColumnModelListener) listeners[index]).columnMoved(e);
105         }
106     }
107     
108     /**
109      * Notifies <code>TableColumnModelListener</code>s that a column was
110      * removed.
111      *
112      * @param e the <code>TableColumnModelEvent</code> to fire
113      */

114     protected void fireColumnRemoved(TableColumnModelEvent e) {
115         EventListener JavaDoc[] listeners = listenerList.getListeners(TableColumnModelListener.class);
116         
117         for (int index = 0; index < listeners.length; ++index) {
118             ((TableColumnModelListener) listeners[index]).columnRemoved(e);
119         }
120     }
121     
122     /**
123      * @see nextapp.echo2.app.table.TableColumnModel#getColumn(int)
124      */

125     public TableColumn getColumn(int index) {
126         if ((getColumnCount() - 1) < index) {
127             return null;
128         }
129
130         return (TableColumn) columns.get(index);
131     }
132     
133     /**
134      * @see nextapp.echo2.app.table.TableColumnModel#getColumnCount()
135      */

136     public int getColumnCount() {
137         return columns.size();
138     }
139     
140     /**
141      * @see nextapp.echo2.app.table.TableColumnModel#getColumnIndex(java.lang.Object)
142      */

143     public int getColumnIndex(Object JavaDoc identifier) {
144         if (identifier == null) {
145             throw new IllegalArgumentException JavaDoc("Null not allowed as identifier value.");
146         }
147     
148         Iterator JavaDoc it = columns.iterator();
149         int index = 0;
150         
151         while (it.hasNext()) {
152             TableColumn column = (TableColumn) it.next();
153             if (identifier.equals(column.getIdentifier())) {
154                 return index;
155             }
156             ++index;
157         }
158         
159         throw new IllegalArgumentException JavaDoc("No column found with specified identifier: " + identifier);
160     }
161     
162     /**
163      * @see nextapp.echo2.app.table.TableColumnModel#getColumns()
164      */

165     public Iterator JavaDoc getColumns() {
166         return columns.iterator();
167     }
168     
169     /**
170      * @see nextapp.echo2.app.table.TableColumnModel#moveColumn(int, int)
171      */

172     public void moveColumn(int columnIndex, int newIndex) {
173         if (columnIndex < 0 || columnIndex >= columns.size()) {
174             throw new IllegalArgumentException JavaDoc("No column exists at index: " + columnIndex);
175         }
176         if (newIndex < 0 || newIndex >= columns.size()) {
177             throw new IllegalArgumentException JavaDoc("Attempt to move column to invalid index: " + newIndex);
178         }
179         
180         TableColumn column = (TableColumn) columns.remove(columnIndex);
181         columns.add(newIndex, column);
182         fireColumnMoved(new TableColumnModelEvent(this, columnIndex, newIndex));
183     }
184     
185     /**
186      * @see nextapp.echo2.app.table.TableColumnModel#removeColumn(nextapp.echo2.app.table.TableColumn)
187      */

188     public void removeColumn(TableColumn column) {
189         int columnIndex = columns.indexOf(column);
190         if (columnIndex == -1) {
191             // Do nothing, column is not in model.
192
return;
193         }
194         columns.remove(columnIndex);
195         fireColumnAdded(new TableColumnModelEvent(this, columnIndex, -1));
196     }
197     
198     /**
199      * @see nextapp.echo2.app.table.TableColumnModel#removeColumnModelListener(nextapp.echo2.app.event.TableColumnModelListener)
200      */

201     public void removeColumnModelListener(TableColumnModelListener l) {
202         listenerList.removeListener(TableColumnModelListener.class, l);
203     }
204 }
205
Popular Tags