1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dishevelled.piccolo.eventlist.view;
25
26 import ca.odell.glazedlists.EventList;
27 import ca.odell.glazedlists.ListSelection;
28
29 import ca.odell.glazedlists.event.ListEvent;
30 import ca.odell.glazedlists.event.ListEventListener;
31
32 import java.awt.event.ActionEvent;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import javax.swing.AbstractAction;
38
39 import org.dishevelled.eventlist.view.EventListView;
40 import org.dishevelled.eventlist.view.EventListViewSupport;
41
42 import org.dishevelled.identify.IdentifiableAction;
43
44 import static org.dishevelled.iconbundle.tango.TangoProject.*;
45
46 import org.piccolo2d.PNode;
47
48
49
50
51
52
53
54
55 public abstract class AbstractEventListNode<E>
56 extends PNode
57 implements EventListView<E>
58 {
59
60 private final EventListViewSupport<E> eventListViewSupport;
61
62
63 private final IdentifiableAction selectAllAction = new IdentifiableAction("Select all", EDIT_SELECT_ALL)
64 {
65 @Override
66 public void actionPerformed(final ActionEvent event)
67 {
68 selectAll();
69 }
70 };
71
72
73 private final AbstractAction clearSelectionAction = new AbstractAction("Clear selection")
74 {
75 @Override
76 public void actionPerformed(final ActionEvent event)
77 {
78 clearSelection();
79 }
80 };
81
82
83 private final AbstractAction invertSelectionAction = new AbstractAction("Invert selection")
84 {
85 @Override
86 public void actionPerformed(final ActionEvent event)
87 {
88 invertSelection();
89 }
90 };
91
92
93 private final IdentifiableAction cutAction = new IdentifiableAction("Cut", EDIT_CUT)
94 {
95 @Override
96 public void actionPerformed(final ActionEvent event)
97 {
98 cut();
99 }
100 };
101
102
103 private final IdentifiableAction copyAction = new IdentifiableAction("Copy", EDIT_COPY)
104 {
105 @Override
106 public void actionPerformed(final ActionEvent event)
107 {
108 copy();
109 }
110 };
111
112
113 private final IdentifiableAction pasteAction = new IdentifiableAction("Paste", EDIT_PASTE)
114 {
115 @Override
116 public void actionPerformed(final ActionEvent event)
117 {
118 paste();
119 }
120 };
121
122
123 private final IdentifiableAction addAction = new IdentifiableAction("Add", LIST_ADD)
124 {
125 @Override
126 public void actionPerformed(final ActionEvent event)
127 {
128 add();
129 }
130 };
131
132
133 private final IdentifiableAction removeAction = new IdentifiableAction("Remove", LIST_REMOVE)
134 {
135 @Override
136 public void actionPerformed(final ActionEvent event)
137 {
138 remove();
139 }
140 };
141
142
143 private final AbstractAction removeAllAction = new AbstractAction("Remove all")
144 {
145 @Override
146 public void actionPerformed(final ActionEvent event)
147 {
148 removeAll();
149 }
150 };
151
152
153 private ListEventListener<E> listener = new ListEventListener<E>()
154 {
155 @Override
156 public void listChanged(final ListEvent<E> event)
157 {
158 updateListActions();
159 }
160 };
161
162
163 private ListSelection.Listener selectionListener = new ListSelection.Listener()
164 {
165 @Override
166 public void selectionChanged(final int index0, final int index1)
167 {
168 updateSelectionActions();
169 }
170 };
171
172
173
174
175
176
177
178 protected AbstractEventListNode(final EventList<E> model)
179 {
180 super();
181 eventListViewSupport = new EventListViewSupport<E>(model);
182 getModel().addListEventListener(listener);
183 getSelectionModel().addSelectionListener(selectionListener);
184 updateListActions();
185 updateSelectionActions();
186 }
187
188
189
190
191
192 private void updateListActions()
193 {
194 selectAllAction.setEnabled(!isEmpty());
195 invertSelectionAction.setEnabled(!isEmpty());
196 removeAllAction.setEnabled(!isEmpty());
197 }
198
199
200
201
202 private void updateSelectionActions()
203 {
204 copyAction.setEnabled(!isSelectionEmpty());
205 cutAction.setEnabled(!isSelectionEmpty());
206 removeAction.setEnabled(!isSelectionEmpty());
207 clearSelectionAction.setEnabled(!isSelectionEmpty());
208 }
209
210
211
212
213
214
215 public final boolean isEmpty()
216 {
217 return getModel().isEmpty();
218 }
219
220
221
222
223
224
225 public final boolean isSelectionEmpty()
226 {
227 return getSelectionModel().getSelected().isEmpty();
228 }
229
230
231
232
233 public final void selectAll()
234 {
235 getSelectionModel().selectAll();
236 }
237
238
239
240
241 public final void clearSelection()
242 {
243 getSelectionModel().deselectAll();
244 }
245
246
247
248
249 public final void invertSelection()
250 {
251 getSelectionModel().invertSelection();
252 }
253
254
255
256
257 public final void cut()
258 {
259 if (!isSelectionEmpty())
260 {
261 cut(new ArrayList<E>(getSelectionModel().getSelected()));
262 }
263 }
264
265
266
267
268 public final void copy()
269 {
270 if (!isSelectionEmpty())
271 {
272 copy(new ArrayList<E>(getSelectionModel().getSelected()));
273 }
274 }
275
276
277
278
279
280
281 protected abstract void cut(List<E> toCut);
282
283
284
285
286
287
288 protected abstract void copy(List<E> toCopy);
289
290
291
292
293 public abstract void add();
294
295
296
297
298 public abstract void paste();
299
300
301
302
303 public final void remove()
304 {
305 getSelectionModel().getSelected().clear();
306 }
307
308
309
310
311 public final void removeAll()
312 {
313 getModel().clear();
314 }
315
316
317
318
319
320
321 protected final IdentifiableAction getSelectAllAction()
322 {
323 return selectAllAction;
324 }
325
326
327
328
329
330
331 protected final AbstractAction getClearSelectionAction()
332 {
333 return clearSelectionAction;
334 }
335
336
337
338
339
340
341 protected final AbstractAction getInvertSelectionAction()
342 {
343 return invertSelectionAction;
344 }
345
346
347
348
349
350
351 protected final IdentifiableAction getCutAction()
352 {
353 return cutAction;
354 }
355
356
357
358
359
360
361 protected final IdentifiableAction getCopyAction()
362 {
363 return copyAction;
364 }
365
366
367
368
369
370
371 protected final IdentifiableAction getPasteAction()
372 {
373 return pasteAction;
374 }
375
376
377
378
379
380
381 protected final IdentifiableAction getAddAction()
382 {
383 return addAction;
384 }
385
386
387
388
389
390
391 protected final IdentifiableAction getRemoveAction()
392 {
393 return removeAction;
394 }
395
396
397
398
399
400
401 protected final AbstractAction getRemoveAllAction()
402 {
403 return removeAllAction;
404 }
405
406 @Override
407 public final EventList<E> getModel()
408 {
409 return eventListViewSupport.getModel();
410 }
411
412 @Override
413 public final ListSelection<E> getSelectionModel()
414 {
415 return eventListViewSupport.getSelectionModel();
416 }
417
418
419
420
421
422
423 public void dispose()
424 {
425 getModel().removeListEventListener(listener);
426 getSelectionModel().removeSelectionListener(selectionListener);
427 }
428 }