1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.table;
21
22 import java.util.ArrayList;
23
24 import javax.swing.Icon;
25 import javax.swing.SwingUtilities;
26
27 import org.xnap.XNap;
28 import org.xnap.event.StateEvent;
29 import org.xnap.event.StateListener;
30 import org.xnap.gui.event.SwingListListener;
31 import org.xnap.peer.HotlistItem;
32 import org.xnap.peer.HotlistManager;
33
34 public class HotlistTableModel extends AbstractColumnTableModel
35 implements StateListener, SwingListListener
36 {
37
38
39
40
41
42 private UserFilter filter = null;
43 private ArrayList rows = new ArrayList();
44
45
46
47 public HotlistTableModel()
48 {
49 Column columns[] = new Column[] {
50 new Column("icon", XNap.tr("Icon"), Icon.class),
51 new Column("name", XNap.tr("Name"), String.class),
52 new Column("comment", XNap.tr("Comment"), String.class),
53 new Column("status", XNap.tr("Status"), String.class),
54 };
55 addColumns(columns);
56 }
57
58
59
60 /***
61 * Invoked when items have been added.
62 */
63 public void itemsAdded(Object[] items)
64 {
65 for (int i = 0; i < items.length; i++) {
66 HotlistItem item = (HotlistItem)items[i];
67 item.addStateListener(this);
68 add(item);
69 }
70 }
71
72 /***
73 * Invoked when items have been removed.
74 */
75 public void itemsRemoved(Object[] items)
76 {
77 for (int i = 0; i < items.length; i++) {
78 HotlistItem item = (HotlistItem)items[i];
79 item.removeStateListener(this);
80 remove(item);
81 }
82 }
83
84 public void add(HotlistItem item)
85 {
86 if (filter == null || filter.matches(item)) {
87 rows.add(item);
88 fireTableRowsInserted(rows.size() - 1, rows.size() - 1);
89 }
90 }
91
92 public HotlistItem get(int i)
93 {
94 return (HotlistItem)rows.get(mapToIndex(i));
95 }
96
97 public int getRowCount()
98 {
99 return rows.size();
100 }
101
102 public Object get(int i, int j)
103 {
104 HotlistItem h = (HotlistItem)rows.get(i);
105
106 switch (j) {
107 case 0:
108 return h.getIcon();
109 case 1:
110 return h.getName();
111 case 2:
112 return h.getComment();
113 case 3:
114 return h.getStatus();
115 }
116
117 return null;
118 }
119
120 public void remove(HotlistItem item)
121 {
122 int i = rows.indexOf(item);
123 if (i != -1) {
124 rows.remove(i);
125 fireTableRowsDeleted(i, i);
126 }
127 }
128
129 public void setFilter(UserFilter filter)
130 {
131 this.filter = filter;
132 reload();
133 }
134
135 public void stateChanged(StateEvent e)
136 {
137 HotlistItem item = (HotlistItem)e.getSource();
138 SwingUtilities.invokeLater(new StateEventHandler(item));
139 }
140
141 public void reload()
142 {
143 rows.clear();
144 HotlistItem[] items = HotlistManager.getInstance().getItems();
145 for (int i = 0; i < items.length; i++) {
146 if (filter == null || filter.matches(items[i])) {
147 rows.add(items[i]);
148 }
149 }
150 fireTableDataChanged();
151 }
152
153
154
155 private class StateEventHandler implements Runnable
156 {
157 HotlistItem u;
158
159 public StateEventHandler(HotlistItem u)
160 {
161 this.u = u;
162 }
163
164 public void run()
165 {
166 if (filter == null || filter.matches(u)) {
167 int i = rows.indexOf(u);
168 if (i != -1) {
169 fireTableRowsUpdated(i, i);
170 }
171 else {
172 add(u);
173 }
174 }
175 else {
176 remove(u);
177 }
178 }
179 }
180
181 public interface UserFilter
182 {
183 boolean matches(HotlistItem u);
184 }
185
186 public static class AllFilter implements UserFilter {
187
188 public boolean matches(HotlistItem u)
189 {
190 return true;
191 }
192
193 public String toString()
194 {
195 return XNap.tr("All");
196 }
197
198 }
199
200 public static class CategoryFilter implements UserFilter {
201
202 String category;
203
204 public CategoryFilter(String category)
205 {
206 this.category = category;
207 }
208
209 public boolean matches(HotlistItem u)
210 {
211 return u.getCategory().equals(category);
212 }
213
214 public String toString()
215 {
216 return category;
217 }
218
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public static class OtherFilter implements UserFilter {
249
250 UserFilter[] filters;
251
252 public OtherFilter(UserFilter[] filters)
253 {
254 this.filters = filters;
255 }
256
257 public boolean matches(HotlistItem u)
258 {
259 boolean accept = false;
260 for (int i = 0; i < filters.length && !accept; i++) {
261 accept |= filters[i].matches(u);
262 }
263
264 return !accept;
265 }
266
267 public String toString()
268 {
269 return XNap.tr("Other");
270 }
271
272 }
273
274 }