Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / table / DefaultTableColumnModel.java
blob3a8b1a3bf6d3cea86c23d9a4ee59d663fc4c3fc7
1 /* DefaultTableColumnModel.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing.table;
41 import java.beans.PropertyChangeEvent;
42 import java.beans.PropertyChangeListener;
43 import java.io.Serializable;
44 import java.util.Enumeration;
45 import java.util.EventListener;
46 import java.util.Vector;
48 import javax.swing.DefaultListSelectionModel;
49 import javax.swing.ListSelectionModel;
50 import javax.swing.event.ChangeEvent;
51 import javax.swing.event.EventListenerList;
52 import javax.swing.event.ListSelectionEvent;
53 import javax.swing.event.ListSelectionListener;
54 import javax.swing.event.TableColumnModelEvent;
55 import javax.swing.event.TableColumnModelListener;
57 /**
58 * DefaultTableColumnModel
59 * @author Andrew Selkirk
60 * @version 1.0
62 public class DefaultTableColumnModel
63 implements TableColumnModel, PropertyChangeListener, ListSelectionListener,
64 Serializable
66 private static final long serialVersionUID = 6580012493508960512L;
68 /**
69 * Columns that this model keeps track of.
71 protected Vector tableColumns;
73 /**
74 * Selection Model that keeps track of columns selection
76 protected ListSelectionModel selectionModel;
78 /**
79 * Space between two columns. By default it is set to 1
81 protected int columnMargin;
83 /**
84 * listenerList keeps track of all listeners registered with this model
86 protected EventListenerList listenerList = new EventListenerList();
88 /**
89 * changeEvent is fired when change occurs in one of the columns properties
91 protected transient ChangeEvent changeEvent = new ChangeEvent(this);
93 /**
94 * Indicates whether columns can be selected
96 protected boolean columnSelectionAllowed;
98 /**
99 * Total width of all the columns in this model
101 protected int totalColumnWidth;
104 * Constructor DefaultTableColumnModel
106 public DefaultTableColumnModel()
108 tableColumns = new Vector();
109 setSelectionModel(createSelectionModel());
110 columnMargin = 1;
111 columnSelectionAllowed = false;
115 * addColumn adds column to the model. This method fires ColumnAdded
116 * event to model's registered TableColumnModelListeners.
118 * @param col column to add
120 public void addColumn(TableColumn col)
122 tableColumns.add(col);
123 invalidateWidthCache();
124 fireColumnAdded(new TableColumnModelEvent(this,0,tableColumns.size()));
128 * removeColumn removes table column from the model. This method fires
129 * ColumnRemoved event to model's registered TableColumnModelListeners.
131 * @param col column to be removed
133 public void removeColumn(TableColumn col)
135 int index = getColumnIndex(col);
136 fireColumnRemoved(new TableColumnModelEvent(this,index,0));
137 tableColumns.remove(col);
138 invalidateWidthCache();
142 * moveColumn moves column at index i to index j. This method fires
143 * ColumnMoved event to model's registered TableColumnModelListeners.
145 * @param i index of the column that will be moved
146 * @param j index of column's new location
148 public void moveColumn(int i, int j)
150 Object tmp = tableColumns.get(i);
151 tableColumns.set(i, tableColumns.get(j));
152 tableColumns.set(j, tmp);
153 fireColumnAdded(new TableColumnModelEvent(this,i,j));
157 * setColumnMargin sets margin of the columns.
158 * @param m new column margin
160 public void setColumnMargin(int m)
162 columnMargin = m;
163 fireColumnMarginChanged();
167 * getColumnCount returns number of columns in the model
168 * @return int number of columns in the model
170 public int getColumnCount()
172 return tableColumns.size();
176 * getColumns
177 * @return Enumeration
179 public Enumeration getColumns()
181 return tableColumns.elements();
185 * getColumnIndex returns index of the specified column
187 * @param identifier identifier of the column
188 * @return int index of the given column
190 public int getColumnIndex(Object identifier)
192 return tableColumns.indexOf(identifier, 0);
196 * getColumn returns column at the specified index
197 * @param i index of the column
198 * @return TableColumn column at the specified index
200 public TableColumn getColumn(int i)
202 return (TableColumn) tableColumns.get(i);
206 * getColumnMargin returns column margin
207 * @return int column margin
209 public int getColumnMargin()
211 return columnMargin;
215 * getColumnIndexAtX returns column that contains specified x-coordinate.
216 * @param x x-coordinate that column should contain
217 * @return int index of the column that contains specified x-coordinate relative
218 * to this column model
220 public int getColumnIndexAtX(int x)
222 for (int i = 0; i < tableColumns.size(); ++i)
224 int w = ((TableColumn)tableColumns.get(i)).getWidth();
225 if (0 <= x && x < w)
226 return i;
227 else
228 x -= w;
230 return -1;
234 * getTotalColumnWidth returns total width of all the columns including
235 * column's margins.
237 * @return total width of all the columns
239 public int getTotalColumnWidth()
241 if (totalColumnWidth == -1)
242 recalcWidthCache();
243 return totalColumnWidth;
247 * setSelectionModel sets selection model that will be used by this ColumnTableModel
248 * to keep track of currently selected columns
250 * @param model new selection model
251 * @exception IllegalArgumentException if model is null
253 public void setSelectionModel(ListSelectionModel model)
255 if (model == null)
256 throw new IllegalArgumentException();
258 selectionModel = model;
259 selectionModel.addListSelectionListener(this);
263 * getSelectionModel returns selection model
264 * @return ListSelectionModel selection model
266 public ListSelectionModel getSelectionModel()
268 return selectionModel;
272 * setColumnSelectionAllowed sets whether column selection is allowed
273 * or not.
275 * @param flag true if column selection is allowed and false otherwise
277 public void setColumnSelectionAllowed(boolean flag)
279 columnSelectionAllowed = flag;
283 * getColumnSelectionAllowed indicates whether column selection is
284 * allowed or not.
286 * @return boolean true if column selection is allowed and false otherwise.
288 public boolean getColumnSelectionAllowed()
290 return columnSelectionAllowed;
294 * getSelectedColumns returns array containing indexes of currently
295 * selected columns
297 * @return int[] array containing indexes of currently selected columns
299 public int[] getSelectedColumns()
301 // FIXME: Implementation of this method was taken from private method
302 // JTable.getSelections(), which is used in various places in JTable
303 // including selected row calculations and cannot be simply removed.
304 // This design should be improved to illuminate duplication of code.
306 ListSelectionModel lsm = this.selectionModel;
307 int sz = getSelectedColumnCount();
308 int [] ret = new int[sz];
310 int lo = lsm.getMinSelectionIndex();
311 int hi = lsm.getMaxSelectionIndex();
312 int j = 0;
313 java.util.ArrayList ls = new java.util.ArrayList();
314 if (lo != -1 && hi != -1)
316 switch (lsm.getSelectionMode())
318 case ListSelectionModel.SINGLE_SELECTION:
319 ret[0] = lo;
320 break;
322 case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
323 for (int i = lo; i <= hi; ++i)
324 ret[j++] = i;
325 break;
327 case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
328 for (int i = lo; i <= hi; ++i)
329 if (lsm.isSelectedIndex(i))
330 ret[j++] = i;
331 break;
334 return ret;
338 * getSelectedColumnCount returns number of currently selected columns
339 * @return int number of currently selected columns
341 public int getSelectedColumnCount()
343 // FIXME: Implementation of this method was taken from private method
344 // JTable.countSelections(), which is used in various places in JTable
345 // including selected row calculations and cannot be simply removed.
346 // This design should be improved to illuminate duplication of code.
348 ListSelectionModel lsm = this.selectionModel;
349 int lo = lsm.getMinSelectionIndex();
350 int hi = lsm.getMaxSelectionIndex();
351 int sum = 0;
353 if (lo != -1 && hi != -1)
355 switch (lsm.getSelectionMode())
357 case ListSelectionModel.SINGLE_SELECTION:
358 sum = 1;
359 break;
361 case ListSelectionModel.SINGLE_INTERVAL_SELECTION:
362 sum = hi - lo + 1;
363 break;
365 case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION:
366 for (int i = lo; i <= hi; ++i)
367 if (lsm.isSelectedIndex(i))
368 ++sum;
369 break;
373 return sum;
377 * addColumnModelListener adds specified listener to the model's
378 * listener list
380 * @param listener the listener to add
382 public void addColumnModelListener(TableColumnModelListener listener)
384 listenerList.add(TableColumnModelListener.class, listener);
388 * removeColumnModelListener removes specified listener from the model's
389 * listener list.
391 * @param listener the listener to remove
393 public void removeColumnModelListener(TableColumnModelListener listener)
395 listenerList.remove(TableColumnModelListener.class, listener);
399 * @since 1.4
401 public TableColumnModelListener[] getColumnModelListeners()
403 return (TableColumnModelListener[])
404 listenerList.getListeners(TableColumnModelListener.class);
408 * fireColumnAdded fires TableColumnModelEvent to registered
409 * TableColumnModelListeners to indicate that column was added
411 * @param e TableColumnModelEvent
413 protected void fireColumnAdded(TableColumnModelEvent e)
415 TableColumnModelListener[] listeners = getColumnModelListeners();
417 for (int i=0; i< listeners.length; i++)
418 listeners[i].columnAdded(e);
422 * fireColumnAdded fires TableColumnModelEvent to registered
423 * TableColumnModelListeners to indicate that column was removed
425 * @param e TableColumnModelEvent
427 protected void fireColumnRemoved(TableColumnModelEvent e)
429 TableColumnModelListener[] listeners = getColumnModelListeners();
431 for (int i=0; i< listeners.length; i++)
432 listeners[i].columnRemoved(e);
436 * fireColumnAdded fires TableColumnModelEvent to registered
437 * TableColumnModelListeners to indicate that column was moved
439 * @param e TableColumnModelEvent
441 protected void fireColumnMoved(TableColumnModelEvent e)
443 TableColumnModelListener[] listeners = getColumnModelListeners();
445 for (int i=0; i< listeners.length; i++)
446 listeners[i].columnMoved(e);
450 * fireColumnSelectionChanged fires TableColumnModelEvent to model's
451 * registered TableColumnModelListeners to indicate that different column
452 * was selected.
454 * @param evt ListSelectionEvent
456 protected void fireColumnSelectionChanged(ListSelectionEvent evt)
458 EventListener [] listeners = getListeners(TableColumnModelListener.class);
459 for (int i = 0; i < listeners.length; ++i)
460 ((TableColumnModelListener)listeners[i]).columnSelectionChanged(evt);
464 * fireColumnMarginChanged fires TableColumnModelEvent to model's
465 * registered TableColumnModelListeners to indicate that column margin
466 * was changed.
468 protected void fireColumnMarginChanged()
470 EventListener [] listeners = getListeners(TableColumnModelListener.class);
471 for (int i = 0; i < listeners.length; ++i)
472 ((TableColumnModelListener)listeners[i]).columnMarginChanged(changeEvent);
476 * getListeners returns currently registered listeners with this model.
477 * @param listenerType type of listeners to return
479 * @return EventListener[] array of model's listeners of the specified type
481 public EventListener[] getListeners(Class listenerType)
483 return listenerList.getListeners(listenerType);
487 * propertyChange handles changes occuring in the properties of the
488 * model's columns.
490 * @param evt PropertyChangeEvent
492 public void propertyChange(PropertyChangeEvent evt)
494 if (evt.getPropertyName().equals(TableColumn.COLUMN_WIDTH_PROPERTY))
495 invalidateWidthCache();
499 * valueChanged handles changes in the selectionModel.
500 * @param e ListSelectionEvent
502 public void valueChanged(ListSelectionEvent e)
504 fireColumnSelectionChanged(e);
508 * createSelectionModel creates selection model that will keep track
509 * of currently selected column(s)
511 * @return ListSelectionModel selection model of the columns
513 protected ListSelectionModel createSelectionModel()
515 return new DefaultListSelectionModel();
519 * recalcWidthCache calculates total width of the columns.
520 * If the current cache of the total width is in invalidated state,
521 * then width is recalculated. Otherwise nothing is done.
523 protected void recalcWidthCache()
525 if (totalColumnWidth == -1)
527 totalColumnWidth = 0;
528 for (int i = 0; i < tableColumns.size(); ++i)
530 totalColumnWidth += ((TableColumn)tableColumns.get(i)).getWidth();
536 * invalidateWidthCache
538 private void invalidateWidthCache()
540 totalColumnWidth = -1;