[3030] | 1 | package nu.fw.jeti.ui.models; |
---|
| 2 | |
---|
| 3 | import javax.swing.table.*; |
---|
| 4 | import java.util.*; |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | /* |
---|
| 8 | * Jeti, a Java Jabber client, Copyright (C) 2002 E.S. de Boer |
---|
| 9 | * |
---|
| 10 | * This program is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | * (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This program is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | * GNU General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with this program; if not, write to the Free Software |
---|
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 23 | * |
---|
| 24 | * For questions, comments etc, |
---|
| 25 | * use the website at http://jeti.jabberstudio.org |
---|
| 26 | * or mail/IM me at jeti@jabber.org |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | public class ListTableModel extends AbstractTableModel |
---|
| 30 | { |
---|
| 31 | private String[] columnNames; |
---|
| 32 | private List plugins; |
---|
| 33 | |
---|
| 34 | public ListTableModel(String[] columnNames,List plugins) |
---|
| 35 | { |
---|
| 36 | this.columnNames = columnNames; |
---|
| 37 | this.plugins = plugins; |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public void reload(List list) |
---|
| 41 | { |
---|
| 42 | plugins = list; |
---|
| 43 | fireTableDataChanged(); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | public List getPlugins() |
---|
| 47 | { |
---|
| 48 | return plugins; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /* |
---|
| 52 | public synchronized void remove(int row) |
---|
| 53 | { |
---|
| 54 | //System.out.println(row); |
---|
| 55 | data.remove(row); |
---|
| 56 | fireTableDataChanged(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | public Object getRow(int row) |
---|
| 61 | { |
---|
| 62 | return data.get(row); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | public void delete(Object list) |
---|
| 66 | { |
---|
| 67 | data.remove(data.indexOf(list)); |
---|
| 68 | fireTableDataChanged(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | public void sort(int column, boolean asc) |
---|
| 73 | { |
---|
| 74 | ascending =asc; |
---|
| 75 | if (data.isEmpty())return; |
---|
| 76 | if ((((List)data.get(0)).get(column)).getClass() == String.class) |
---|
| 77 | { |
---|
| 78 | sortStrings(column); |
---|
| 79 | } |
---|
| 80 | else if ((((List)data.get(0)).get(column)).getClass() == Double.class) |
---|
| 81 | { |
---|
| 82 | sortDoubles(column); |
---|
| 83 | } |
---|
| 84 | else if ((((List)data.get(0)).get(column)).getClass() == Boolean.class) |
---|
| 85 | { |
---|
| 86 | sortBooleans(column); |
---|
| 87 | } |
---|
| 88 | else |
---|
| 89 | { |
---|
| 90 | sortIntegers(column); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public void sortIntegers(int column) |
---|
| 95 | { |
---|
| 96 | final int col = column; |
---|
| 97 | Collections.sort(data, new Comparator() |
---|
| 98 | { |
---|
| 99 | public int compare(Object o1, Object o2) |
---|
| 100 | { |
---|
| 101 | int result = ((Integer)((List)o1).get(col)).compareTo(((Integer)((List)o2).get(col))); |
---|
| 102 | if (result != 0) |
---|
| 103 | { |
---|
| 104 | return ascending ? result : -result; |
---|
| 105 | } |
---|
| 106 | return 0; |
---|
| 107 | } |
---|
| 108 | }); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | public void sortDoubles(int column) |
---|
| 112 | { |
---|
| 113 | final int col = column; |
---|
| 114 | Collections.sort(data, new Comparator() |
---|
| 115 | { |
---|
| 116 | public int compare(Object o1, Object o2) |
---|
| 117 | { |
---|
| 118 | int result = ((Double)((List)o1).get(col)).compareTo(((Double)((List)o2).get(col))); |
---|
| 119 | if (result != 0) |
---|
| 120 | { |
---|
| 121 | return ascending ? result : -result; |
---|
| 122 | } |
---|
| 123 | return 0; |
---|
| 124 | } |
---|
| 125 | }); |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | public void sortStrings(int column) |
---|
| 129 | { |
---|
| 130 | final int col = column; |
---|
| 131 | Collections.sort(data, new Comparator() |
---|
| 132 | { |
---|
| 133 | public int compare(Object o1, Object o2) |
---|
| 134 | { |
---|
| 135 | int result = ((String)((List)o1).get(col)).compareTo(((String)((List)o2).get(col))); |
---|
| 136 | if (result != 0) |
---|
| 137 | { |
---|
| 138 | return ascending ? result : -result; |
---|
| 139 | } |
---|
| 140 | return 0; |
---|
| 141 | } |
---|
| 142 | }); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | public void sortBooleans(int column) |
---|
| 146 | { |
---|
| 147 | final int col = column; |
---|
| 148 | Collections.sort(data, new Comparator() |
---|
| 149 | { |
---|
| 150 | public int compare(Object o1, Object o2) |
---|
| 151 | { |
---|
| 152 | int result =0; |
---|
| 153 | if ((((List)o1).get(col)).equals(((List)o2).get(col))) return 0; |
---|
| 154 | if (((Boolean)((List)o1).get(col)).booleanValue()) result=-1; |
---|
| 155 | else result=1; |
---|
| 156 | return ascending ? result : -result; |
---|
| 157 | } |
---|
| 158 | }); |
---|
| 159 | } |
---|
| 160 | */ |
---|
| 161 | |
---|
| 162 | public boolean isCellEditable(int row, int col) |
---|
| 163 | { |
---|
| 164 | if (col == 1) return true; |
---|
| 165 | if (col == 3) return true; |
---|
| 166 | return false; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | public void setValueAt(Object value, int row, int col) |
---|
| 170 | { |
---|
| 171 | ((Object[])plugins.get(row))[col] = value; |
---|
| 172 | fireTableCellUpdated(row, col); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | public Class getColumnClass(int col) |
---|
| 176 | { |
---|
| 177 | return ((Object[])plugins.get(0))[col].getClass(); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | public String getColumnName(int col) |
---|
| 181 | { |
---|
| 182 | return columnNames[col]; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | //required methods by abstractTableModel |
---|
| 186 | public int getColumnCount() |
---|
| 187 | { |
---|
| 188 | return columnNames.length; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | public int getRowCount() |
---|
| 192 | { |
---|
| 193 | return plugins.size(); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | public Object getValueAt(int row, int col) |
---|
| 197 | { |
---|
| 198 | return ((Object[])plugins.get(row))[col]; |
---|
| 199 | } |
---|
| 200 | } |
---|
| 201 | /* |
---|
| 202 | * Overrides for emacs |
---|
| 203 | * Local variables: |
---|
| 204 | * tab-width: 4 |
---|
| 205 | * End: |
---|
| 206 | */ |
---|