1 29 30 package nextapp.echo2.app.table; 31 32 import java.io.Serializable ; 33 import java.util.ArrayList ; 34 import java.util.EventListener ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 import nextapp.echo2.app.event.EventListenerList; 39 import nextapp.echo2.app.event.TableColumnModelEvent; 40 import nextapp.echo2.app.event.TableColumnModelListener; 41 42 45 public class DefaultTableColumnModel 46 implements Serializable , TableColumnModel { 47 48 51 private List columns = new ArrayList (); 52 53 56 protected EventListenerList listenerList = new EventListenerList(); 57 58 61 public DefaultTableColumnModel() { 62 super(); 63 } 64 65 68 public void addColumn(TableColumn column) { 69 columns.add(column); 70 fireColumnAdded(new TableColumnModelEvent(this, -1, columns.size() - 1)); 71 } 72 73 76 public void addColumnModelListener(TableColumnModelListener l) { 77 listenerList.addListener(TableColumnModelListener.class, l); 78 } 79 80 86 protected void fireColumnAdded(TableColumnModelEvent e) { 87 EventListener [] 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 100 protected void fireColumnMoved(TableColumnModelEvent e) { 101 EventListener [] 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 114 protected void fireColumnRemoved(TableColumnModelEvent e) { 115 EventListener [] 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 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 136 public int getColumnCount() { 137 return columns.size(); 138 } 139 140 143 public int getColumnIndex(Object identifier) { 144 if (identifier == null) { 145 throw new IllegalArgumentException ("Null not allowed as identifier value."); 146 } 147 148 Iterator 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 ("No column found with specified identifier: " + identifier); 160 } 161 162 165 public Iterator getColumns() { 166 return columns.iterator(); 167 } 168 169 172 public void moveColumn(int columnIndex, int newIndex) { 173 if (columnIndex < 0 || columnIndex >= columns.size()) { 174 throw new IllegalArgumentException ("No column exists at index: " + columnIndex); 175 } 176 if (newIndex < 0 || newIndex >= columns.size()) { 177 throw new IllegalArgumentException ("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 188 public void removeColumn(TableColumn column) { 189 int columnIndex = columns.indexOf(column); 190 if (columnIndex == -1) { 191 return; 193 } 194 columns.remove(columnIndex); 195 fireColumnAdded(new TableColumnModelEvent(this, columnIndex, -1)); 196 } 197 198 201 public void removeColumnModelListener(TableColumnModelListener l) { 202 listenerList.removeListener(TableColumnModelListener.class, l); 203 } 204 } 205 | Popular Tags |