001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.table;
014:
015: import java.io.Serializable;
016: import java.util.Collection;
017: import java.util.Iterator;
018: import java.util.LinkedList;
019: import java.util.List;
020:
021: import javax.swing.event.ChangeEvent;
022: import javax.swing.event.EventListenerList;
023:
024: import org.wings.event.STableColumnModelEvent;
025: import org.wings.event.STableColumnModelListener;
026:
027: /*
028: * @see STableColumnModel
029: */
030: public class SDefaultTableColumnModel implements STableColumnModel,
031: Serializable {
032:
033: private List columns = new LinkedList();
034: private int columnMargin;
035: private String totalColumnWidth;
036:
037: /** Change event (only one instance needed) */
038: transient protected ChangeEvent changeEvent = null;
039:
040: /** List of STableColumnModelListener */
041: protected EventListenerList listenerList = new EventListenerList();
042:
043: public void addColumn(STableColumn column) {
044: if (column == null)
045: throw new IllegalArgumentException("Column is null");
046:
047: columns.add(column);
048:
049: fireColumnAdded(new STableColumnModelEvent(this , 0,
050: getColumnCount() - 1));
051: }
052:
053: public void removeColumn(STableColumn column) {
054: if (column == null)
055: throw new IllegalArgumentException("Column is null");
056:
057: int columnIndex = columns.indexOf(column);
058:
059: if (columnIndex != -1) {
060: columns.remove(columnIndex);
061: fireColumnRemoved(new STableColumnModelEvent(this ,
062: columnIndex, 0));
063: }
064: }
065:
066: public void moveColumn(int columnIndex, int newIndex) {
067: if ((columnIndex < 0) || (columnIndex >= getColumnCount())
068: || (newIndex < 0) || (newIndex >= getColumnCount()))
069: throw new IllegalArgumentException(
070: "moveColumn() - Index out of range");
071:
072: STableColumn column = (STableColumn) columns
073: .remove(columnIndex);
074: columns.add(newIndex, column);
075:
076: fireColumnMoved(new STableColumnModelEvent(this , columnIndex,
077: newIndex));
078: }
079:
080: public void setColumnMargin(int newMargin) {
081: if (columnMargin != newMargin) {
082: columnMargin = newMargin;
083: fireColumnMarginChanged();
084: }
085: }
086:
087: public int getColumnCount() {
088: return columns.size();
089: }
090:
091: public Collection getColumns() {
092: return columns;
093: }
094:
095: public int getColumnIndex(Object columnIdentifier) {
096: int index = 0;
097: for (Iterator iterator = columns.iterator(); iterator.hasNext();) {
098: STableColumn column = (STableColumn) iterator.next();
099: if (columnIdentifier.equals(column.getIdentifier()))
100: return index;
101:
102: index++;
103: }
104:
105: return -1;
106: }
107:
108: public STableColumn getColumn(int columnIndex) {
109: if (columns == null || columnIndex >= columns.size()
110: || columnIndex < 0)
111: return null;
112: else
113: return (STableColumn) columns.get(columnIndex);
114: }
115:
116: public int getColumnMargin() {
117: return columnMargin;
118: }
119:
120: public String getTotalColumnWidth() {
121: return totalColumnWidth;
122: }
123:
124: public void setTotalColumnWidth(String totalColumnWidth) {
125: this .totalColumnWidth = totalColumnWidth;
126: }
127:
128: /**
129: * Indicates if the given column is hidden.
130: *
131: *
132: * @return <code>true</code> if the given column is invisible
133: */
134: public boolean isColumnHidden(STableColumn column) {
135: return column.isHidden();
136: }
137:
138: /**
139: * Indicates if the given column should be hidden.
140: *
141: * @param hidden <code>true</code> if the given column should be invisible
142: */
143: public void setColumnHidden(STableColumn column, boolean hidden) {
144: if (column.isHidden() != hidden) {
145: column.setHidden(hidden);
146: if (hidden)
147: fireColumnHidden(column);
148: else
149: fireColumnShown(column);
150: }
151: }
152:
153: /**
154: * Adds a listener for table column model events.
155: * @param x a <code>STableColumnModelListener</code> object
156: */
157: public void addColumnModelListener(STableColumnModelListener x) {
158: listenerList.add(STableColumnModelListener.class, x);
159: }
160:
161: /**
162: * Removes a listener for table column model events.
163: * @param x a <code>STableColumnModelListener</code> object
164: */
165: public void removeColumnModelListener(STableColumnModelListener x) {
166: listenerList.remove(STableColumnModelListener.class, x);
167: }
168:
169: /**
170: * Notifies all listeners that have registered interest for
171: * notification on this event type.
172: * @param e the event received
173: * @see EventListenerList
174: */
175: protected void fireColumnAdded(STableColumnModelEvent e) {
176: Object[] listeners = listenerList.getListenerList();
177: for (int i = listeners.length - 2; i >= 0; i -= 2) {
178: if (listeners[i] == STableColumnModelListener.class) {
179: ((STableColumnModelListener) listeners[i + 1])
180: .columnAdded(e);
181: }
182: }
183: }
184:
185: /**
186: * Notifies all listeners that have registered interest for
187: * notification on this event type.
188: * @param e the event received
189: * @see EventListenerList
190: */
191: protected void fireColumnRemoved(STableColumnModelEvent e) {
192: Object[] listeners = listenerList.getListenerList();
193: for (int i = listeners.length - 2; i >= 0; i -= 2) {
194: if (listeners[i] == STableColumnModelListener.class) {
195: ((STableColumnModelListener) listeners[i + 1])
196: .columnRemoved(e);
197: }
198: }
199: }
200:
201: /**
202: * Notifies all listeners that have registered interest for
203: * notification on this event type.
204: * @param e the event received
205: * @see EventListenerList
206: */
207: protected void fireColumnMoved(STableColumnModelEvent e) {
208: Object[] listeners = listenerList.getListenerList();
209: for (int i = listeners.length - 2; i >= 0; i -= 2) {
210: if (listeners[i] == STableColumnModelListener.class) {
211: ((STableColumnModelListener) listeners[i + 1])
212: .columnMoved(e);
213: }
214: }
215: }
216:
217: /**
218: * Notifies all listeners that have registered interest for
219: * notification on this event type.
220: * @see EventListenerList
221: */
222: protected void fireColumnMarginChanged() {
223: Object[] listeners = listenerList.getListenerList();
224: for (int i = listeners.length - 2; i >= 0; i -= 2) {
225: if (listeners[i] == STableColumnModelListener.class) {
226: // Lazily create the event:
227: if (changeEvent == null)
228: changeEvent = new ChangeEvent(this );
229: ((STableColumnModelListener) listeners[i + 1])
230: .columnMarginChanged(changeEvent);
231: }
232: }
233: }
234:
235: /**
236: * Notifies all listeners that have registered interest for
237: * notification on this event type.
238: * @see EventListenerList
239: */
240: protected void fireColumnShown(STableColumn column) {
241: Object[] listeners = listenerList.getListenerList();
242: for (int i = listeners.length - 2; i >= 0; i -= 2) {
243: if (listeners[i] == STableColumnModelListener.class) {
244: ((STableColumnModelListener) listeners[i + 1])
245: .columnShown(new ChangeEvent(column));
246: }
247: }
248: }
249:
250: /**
251: * Notifies all listeners that have registered interest for
252: * notification on this event type.
253: * @see EventListenerList
254: */
255: protected void fireColumnHidden(STableColumn column) {
256: Object[] listeners = listenerList.getListenerList();
257: for (int i = listeners.length - 2; i >= 0; i -= 2) {
258: if (listeners[i] == STableColumnModelListener.class) {
259: ((STableColumnModelListener) listeners[i + 1])
260: .columnHidden(new ChangeEvent(column));
261: }
262: }
263: }
264: }
|