diff options
Diffstat (limited to 'libjava/javax/swing/table/DefaultTableColumnModel.java')
-rw-r--r-- | libjava/javax/swing/table/DefaultTableColumnModel.java | 370 |
1 files changed, 245 insertions, 125 deletions
diff --git a/libjava/javax/swing/table/DefaultTableColumnModel.java b/libjava/javax/swing/table/DefaultTableColumnModel.java index b66b6951a99..3a8b1a3bf6d 100644 --- a/libjava/javax/swing/table/DefaultTableColumnModel.java +++ b/libjava/javax/swing/table/DefaultTableColumnModel.java @@ -66,37 +66,37 @@ public class DefaultTableColumnModel private static final long serialVersionUID = 6580012493508960512L; /** - * tableColumns + * Columns that this model keeps track of. */ protected Vector tableColumns; /** - * selectionModel + * Selection Model that keeps track of columns selection */ protected ListSelectionModel selectionModel; /** - * columnMargin + * Space between two columns. By default it is set to 1 */ protected int columnMargin; /** - * listenerList + * listenerList keeps track of all listeners registered with this model */ protected EventListenerList listenerList = new EventListenerList(); /** - * changeEvent + * changeEvent is fired when change occurs in one of the columns properties */ protected transient ChangeEvent changeEvent = new ChangeEvent(this); /** - * columnSelectionAllowed + * Indicates whether columns can be selected */ protected boolean columnSelectionAllowed; /** - * totalColumnWidth + * Total width of all the columns in this model */ protected int totalColumnWidth; @@ -106,103 +106,116 @@ public class DefaultTableColumnModel public DefaultTableColumnModel() { tableColumns = new Vector(); - setSelectionModel(new DefaultListSelectionModel()); + setSelectionModel(createSelectionModel()); columnMargin = 1; columnSelectionAllowed = false; } /** - * addColumn - * @param value0 TODO + * addColumn adds column to the model. This method fires ColumnAdded + * event to model's registered TableColumnModelListeners. + * + * @param col column to add */ public void addColumn(TableColumn col) { tableColumns.add(col); invalidateWidthCache(); + fireColumnAdded(new TableColumnModelEvent(this,0,tableColumns.size())); } /** - * removeColumn - * @param value0 TODO + * removeColumn removes table column from the model. This method fires + * ColumnRemoved event to model's registered TableColumnModelListeners. + * + * @param col column to be removed */ public void removeColumn(TableColumn col) { + int index = getColumnIndex(col); + fireColumnRemoved(new TableColumnModelEvent(this,index,0)); tableColumns.remove(col); invalidateWidthCache(); } /** - * moveColumn - * @param value0 TODO - * @param value1 TODO + * moveColumn moves column at index i to index j. This method fires + * ColumnMoved event to model's registered TableColumnModelListeners. + * + * @param i index of the column that will be moved + * @param j index of column's new location */ public void moveColumn(int i, int j) { Object tmp = tableColumns.get(i); tableColumns.set(i, tableColumns.get(j)); tableColumns.set(j, tmp); + fireColumnAdded(new TableColumnModelEvent(this,i,j)); } /** - * setColumnMargin - * @param value0 TODO + * setColumnMargin sets margin of the columns. + * @param m new column margin */ public void setColumnMargin(int m) { columnMargin = m; + fireColumnMarginChanged(); } - /** - * getColumnCount - * @return int - */ + /** + * getColumnCount returns number of columns in the model + * @return int number of columns in the model + */ public int getColumnCount() { return tableColumns.size(); } - /** - * getColumns + /** + * getColumns * @return Enumeration - */ + */ public Enumeration getColumns() { return tableColumns.elements(); } - /** - * getColumnIndex - * @param value0 TODO - * @return int + /** + * getColumnIndex returns index of the specified column + * + * @param identifier identifier of the column + * @return int index of the given column */ - public int getColumnIndex(Object obj) + public int getColumnIndex(Object identifier) { - return tableColumns.indexOf(obj, 0); + return tableColumns.indexOf(identifier, 0); } - /** - * getColumn - * @param value0 TODO - * @return TableColumn + /** + * getColumn returns column at the specified index + * @param i index of the column + * @return TableColumn column at the specified index */ public TableColumn getColumn(int i) { return (TableColumn) tableColumns.get(i); } - /** - * getColumnMargin - * @return int - */ + /** + * getColumnMargin returns column margin + * @return int column margin + */ public int getColumnMargin() { return columnMargin; } - /** - * getColumnIndexAtX - * @param value0 TODO - * @return int + /** + * getColumnIndexAtX returns column that contains specified x-coordinate. + * @param x x-coordinate that column should contain + * @return int index of the column that contains specified x-coordinate relative + * to this column model */ public int getColumnIndexAtX(int x) { @@ -217,10 +230,12 @@ public class DefaultTableColumnModel return -1; } - /** - * getTotalColumnWidth - * @return int - */ + /** + * getTotalColumnWidth returns total width of all the columns including + * column's margins. + * + * @return total width of all the columns + */ public int getTotalColumnWidth() { if (totalColumnWidth == -1) @@ -229,8 +244,10 @@ public class DefaultTableColumnModel } /** - * setSelectionModel - * @param model TODO + * setSelectionModel sets selection model that will be used by this ColumnTableModel + * to keep track of currently selected columns + * + * @param model new selection model * @exception IllegalArgumentException if model is null */ public void setSelectionModel(ListSelectionModel model) @@ -242,53 +259,124 @@ public class DefaultTableColumnModel selectionModel.addListSelectionListener(this); } - /** - * getSelectionModel - * @return ListSelectionModel - */ + /** + * getSelectionModel returns selection model + * @return ListSelectionModel selection model + */ public ListSelectionModel getSelectionModel() { return selectionModel; } /** - * setColumnSelectionAllowed - * @param value0 TODO + * setColumnSelectionAllowed sets whether column selection is allowed + * or not. + * + * @param flag true if column selection is allowed and false otherwise */ - public void setColumnSelectionAllowed(boolean a) + public void setColumnSelectionAllowed(boolean flag) { - columnSelectionAllowed = a; + columnSelectionAllowed = flag; } - /** - * getColumnSelectionAllowed - * @return boolean - */ + /** + * getColumnSelectionAllowed indicates whether column selection is + * allowed or not. + * + * @return boolean true if column selection is allowed and false otherwise. + */ public boolean getColumnSelectionAllowed() { return columnSelectionAllowed; } - /** - * getSelectedColumns - * @return int[] - */ + /** + * getSelectedColumns returns array containing indexes of currently + * selected columns + * + * @return int[] array containing indexes of currently selected columns + */ public int[] getSelectedColumns() { - return null; // TODO + // FIXME: Implementation of this method was taken from private method + // JTable.getSelections(), which is used in various places in JTable + // including selected row calculations and cannot be simply removed. + // This design should be improved to illuminate duplication of code. + + ListSelectionModel lsm = this.selectionModel; + int sz = getSelectedColumnCount(); + int [] ret = new int[sz]; + + int lo = lsm.getMinSelectionIndex(); + int hi = lsm.getMaxSelectionIndex(); + int j = 0; + java.util.ArrayList ls = new java.util.ArrayList(); + if (lo != -1 && hi != -1) + { + switch (lsm.getSelectionMode()) + { + case ListSelectionModel.SINGLE_SELECTION: + ret[0] = lo; + break; + + case ListSelectionModel.SINGLE_INTERVAL_SELECTION: + for (int i = lo; i <= hi; ++i) + ret[j++] = i; + break; + + case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: + for (int i = lo; i <= hi; ++i) + if (lsm.isSelectedIndex(i)) + ret[j++] = i; + break; + } + } + return ret; } - /** - * getSelectedColumnCount - * @return int - */ + /** + * getSelectedColumnCount returns number of currently selected columns + * @return int number of currently selected columns + */ public int getSelectedColumnCount() { - return 0; // TODO + // FIXME: Implementation of this method was taken from private method + // JTable.countSelections(), which is used in various places in JTable + // including selected row calculations and cannot be simply removed. + // This design should be improved to illuminate duplication of code. + + ListSelectionModel lsm = this.selectionModel; + int lo = lsm.getMinSelectionIndex(); + int hi = lsm.getMaxSelectionIndex(); + int sum = 0; + + if (lo != -1 && hi != -1) + { + switch (lsm.getSelectionMode()) + { + case ListSelectionModel.SINGLE_SELECTION: + sum = 1; + break; + + case ListSelectionModel.SINGLE_INTERVAL_SELECTION: + sum = hi - lo + 1; + break; + + case ListSelectionModel.MULTIPLE_INTERVAL_SELECTION: + for (int i = lo; i <= hi; ++i) + if (lsm.isSelectedIndex(i)) + ++sum; + break; + } + } + + return sum; } /** - * addColumnModelListener + * addColumnModelListener adds specified listener to the model's + * listener list + * * @param listener the listener to add */ public void addColumnModelListener(TableColumnModelListener listener) @@ -297,7 +385,9 @@ public class DefaultTableColumnModel } /** - * removeColumnModelListener + * removeColumnModelListener removes specified listener from the model's + * listener list. + * * @param listener the listener to remove */ public void removeColumnModelListener(TableColumnModelListener listener) @@ -315,35 +405,53 @@ public class DefaultTableColumnModel } /** - * fireColumnAdded - * @param value0 TODO + * fireColumnAdded fires TableColumnModelEvent to registered + * TableColumnModelListeners to indicate that column was added + * + * @param e TableColumnModelEvent */ - protected void fireColumnAdded(TableColumnModelEvent value0) - { - // TODO + protected void fireColumnAdded(TableColumnModelEvent e) + { + TableColumnModelListener[] listeners = getColumnModelListeners(); + + for (int i=0; i< listeners.length; i++) + listeners[i].columnAdded(e); } - /** - * fireColumnRemoved - * @param value0 TODO - */ - protected void fireColumnRemoved(TableColumnModelEvent value0) + /** + * fireColumnAdded fires TableColumnModelEvent to registered + * TableColumnModelListeners to indicate that column was removed + * + * @param e TableColumnModelEvent + */ + protected void fireColumnRemoved(TableColumnModelEvent e) { - // TODO + TableColumnModelListener[] listeners = getColumnModelListeners(); + + for (int i=0; i< listeners.length; i++) + listeners[i].columnRemoved(e); } - /** - * fireColumnMoved - * @param value0 TODO - */ - protected void fireColumnMoved(TableColumnModelEvent value0) + /** + * fireColumnAdded fires TableColumnModelEvent to registered + * TableColumnModelListeners to indicate that column was moved + * + * @param e TableColumnModelEvent + */ + protected void fireColumnMoved(TableColumnModelEvent e) { - // TODO + TableColumnModelListener[] listeners = getColumnModelListeners(); + + for (int i=0; i< listeners.length; i++) + listeners[i].columnMoved(e); } /** - * fireColumnSelectionChanged - * @param value0 TODO + * fireColumnSelectionChanged fires TableColumnModelEvent to model's + * registered TableColumnModelListeners to indicate that different column + * was selected. + * + * @param evt ListSelectionEvent */ protected void fireColumnSelectionChanged(ListSelectionEvent evt) { @@ -352,54 +460,66 @@ public class DefaultTableColumnModel ((TableColumnModelListener)listeners[i]).columnSelectionChanged(evt); } - /** - * fireColumnMarginChanged - */ + /** + * fireColumnMarginChanged fires TableColumnModelEvent to model's + * registered TableColumnModelListeners to indicate that column margin + * was changed. + */ protected void fireColumnMarginChanged() { - // TODO + EventListener [] listeners = getListeners(TableColumnModelListener.class); + for (int i = 0; i < listeners.length; ++i) + ((TableColumnModelListener)listeners[i]).columnMarginChanged(changeEvent); } - /** - * getListeners - * @param value0 TODO - * @return EventListener[] + /** + * getListeners returns currently registered listeners with this model. + * @param listenerType type of listeners to return + * + * @return EventListener[] array of model's listeners of the specified type */ - public EventListener[] getListeners(Class klass) + public EventListener[] getListeners(Class listenerType) { - return listenerList.getListeners(klass); + return listenerList.getListeners(listenerType); } - /** - * propertyChange - * @param value0 TODO - */ - public void propertyChange(PropertyChangeEvent value0) + /** + * propertyChange handles changes occuring in the properties of the + * model's columns. + * + * @param evt PropertyChangeEvent + */ + public void propertyChange(PropertyChangeEvent evt) { - // TODO + if (evt.getPropertyName().equals(TableColumn.COLUMN_WIDTH_PROPERTY)) + invalidateWidthCache(); } - /** - * valueChanged - * @param value0 TODO - */ - public void valueChanged(ListSelectionEvent value0) + /** + * valueChanged handles changes in the selectionModel. + * @param e ListSelectionEvent + */ + public void valueChanged(ListSelectionEvent e) { - fireColumnSelectionChanged(value0); + fireColumnSelectionChanged(e); } - /** - * createSelectionModel - * @return ListSelectionModel - */ + /** + * createSelectionModel creates selection model that will keep track + * of currently selected column(s) + * + * @return ListSelectionModel selection model of the columns + */ protected ListSelectionModel createSelectionModel() - { - return null; // TODO + { + return new DefaultListSelectionModel(); } - /** - * recalcWidthCache - */ + /** + * recalcWidthCache calculates total width of the columns. + * If the current cache of the total width is in invalidated state, + * then width is recalculated. Otherwise nothing is done. + */ protected void recalcWidthCache() { if (totalColumnWidth == -1) @@ -412,9 +532,9 @@ public class DefaultTableColumnModel } } - /** - * invalidateWidthCache - */ + /** + * invalidateWidthCache + */ private void invalidateWidthCache() { totalColumnWidth = -1; |