1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.visualization.picking;
12
13 import java.awt.event.ItemEvent;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.LinkedHashSet;
18 import java.util.List;
19 import java.util.Set;
20
21
22
23
24
25
26
27
28
29
30 public class MultiPickedState<T> extends AbstractPickedState<T> implements PickedState<T> {
31
32
33
34 protected Set<T> picked = new LinkedHashSet<T>();
35
36
37
38
39 public boolean pick(T v, boolean state) {
40 boolean prior_state = this.picked.contains(v);
41 if (state) {
42 picked.add(v);
43 if(prior_state == false) {
44 fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
45 v, ItemEvent.SELECTED));
46 }
47
48 } else {
49 picked.remove(v);
50 if(prior_state == true) {
51 fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
52 v, ItemEvent.DESELECTED));
53 }
54
55 }
56 return prior_state;
57 }
58
59
60
61
62 public void clear() {
63 Collection<T> unpicks = new ArrayList<T>(picked);
64 for(T v : unpicks) {
65 pick(v, false);
66 }
67 picked.clear();
68
69 }
70
71
72
73
74 public Set<T> getPicked() {
75 return Collections.unmodifiableSet(picked);
76 }
77
78
79
80
81 public boolean isPicked(T e) {
82 return picked.contains(e);
83 }
84
85
86
87
88 @SuppressWarnings("unchecked")
89 public T[] getSelectedObjects() {
90 List<T> list = new ArrayList<T>(picked);
91 return (T[])list.toArray();
92 }
93
94 }