1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package nl.tudelft.goal.ut2004.visualizer.gui.widgets;
18
19 import java.util.Collection;
20
21 import javax.swing.JPanel;
22 import javax.swing.SwingUtilities;
23
24 import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
25 import nl.tudelft.goal.ut2004.visualizer.data.GameData;
26 import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
27 import nl.tudelft.goal.ut2004.visualizer.util.SelectableWayPoint;
28
29 import cz.cuni.amis.pogamut.ut2004.communication.worldview.map.Waypoint;
30 import cz.cuni.amis.utils.collections.ObservableCollection;
31
32
33
34
35
36
37
38 public class WaypointBox extends JPanel {
39
40 private final SuggestionField suggestionField;
41 private final Listener listener;
42 private final ObservableCollection<Waypoint> waypoints;
43 private final SuggestionModel suggestionModel;
44
45 public WaypointBox() {
46
47 this.listener = new Listener();
48 this.suggestionModel = new SuggestionModel();
49 this.suggestionField = new SuggestionField(suggestionModel);
50 this.add(suggestionField);
51
52 ServerController controller = ServerController.getInstance();
53 GameData data = controller.getGameData();
54 waypoints = data.getWaypoints();
55 waypoints.addCollectionListener(listener);
56 addAll(waypoints);
57
58 }
59
60
61
62
63
64
65 public Waypoint getSelected() {
66 SelectableWayPoint sw = (SelectableWayPoint) suggestionModel
67 .getSelectedItem();
68 if (sw != null) {
69 return sw.getWaypoint();
70 }
71
72 return null;
73 }
74
75 public void setSelected(Object item) {
76 if (item instanceof Waypoint) {
77 item = new SelectableWayPoint((Waypoint) item);
78 }
79 suggestionField.setSelectedItem(item);
80 }
81
82
83
84
85
86
87 private void addAll(Collection<Waypoint> waypoints) {
88 synchronized (waypoints) {
89 for (Waypoint w : waypoints) {
90 suggestionModel.addElement(new SelectableWayPoint(w));
91 }
92 }
93 }
94
95 private void removeAll(Collection<Waypoint> waypoints) {
96 synchronized (waypoints) {
97 for (Waypoint w : waypoints) {
98 suggestionModel.removeElement(new SelectableWayPoint(w));
99 }
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public void dispose() {
113 waypoints.removeCollectionListener(listener);
114 }
115
116 private class Listener extends CollectionEventAdaptor<Waypoint> {
117
118 @Override
119 public void postRemoveEvent(final Collection<Waypoint> alreadyRemoved,
120 Collection<Waypoint> whereWereRemoved) {
121 SwingUtilities.invokeLater(new Runnable() {
122
123 @Override
124 public void run() {
125 removeAll(alreadyRemoved);
126 }
127
128 });
129 }
130
131 @Override
132 public void postAddEvent(final Collection<Waypoint> alreadyAdded,
133 Collection<Waypoint> whereWereAdded) {
134 SwingUtilities.invokeLater(new Runnable() {
135
136 @Override
137 public void run() {
138 addAll(alreadyAdded);
139 }
140 });
141 }
142
143 }
144 }