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.awt.GridLayout;
20 import java.util.Collection;
21
22 import javax.swing.JPanel;
23 import javax.swing.SwingUtilities;
24
25 import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
26 import nl.tudelft.goal.ut2004.visualizer.data.GameData;
27 import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
28 import nl.tudelft.goal.ut2004.visualizer.util.SelectableMapList;
29
30 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.MapList;
31 import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
32 import cz.cuni.amis.utils.collections.ObservableCollection;
33 import cz.cuni.amis.utils.flag.FlagListener;
34
35
36
37
38
39
40
41
42 public class MapBox extends JPanel {
43
44 private final SuggestionField suggestionField;
45 private final Listener listener;
46 private final ObservableCollection<MapList> maps;
47 private final SuggestionModel suggestionModel;
48
49 public MapBox() {
50 this.setLayout(new GridLayout(1, 1));
51 this.listener = new Listener();
52 this.suggestionModel = new SuggestionModel();
53 this.suggestionField = new SuggestionField(suggestionModel);
54 this.add(suggestionField);
55
56 ServerController controller = ServerController.getInstance();
57 GameData data = controller.getGameData();
58 maps = data.getAvailableMaps();
59 maps.addCollectionListener(listener);
60 addAll(maps);
61
62 controller.getServerDefinition().getServerFlag()
63 .addListener(new FlagListener<IUT2004Server>() {
64
65 @Override
66 public void flagChanged(IUT2004Server changedValue) {
67 if (changedValue != null) {
68 suggestionField.setSelectedItem(changedValue
69 .getMapName());
70 }
71 }
72 });
73
74 IUT2004Server server = controller.getServer();
75 if (server != null) {
76 suggestionField.setSelectedItem(server.getMapName());
77 }
78 }
79
80
81
82
83
84
85 public MapList getSelected() {
86 SelectableMapList sw = (SelectableMapList) suggestionModel
87 .getSelectedItem();
88 if (sw != null) {
89 return sw.getMapList();
90 }
91
92 return null;
93 }
94
95 public void setSelected(MapList item) {
96 suggestionField.setSelectedItem(item);
97 }
98
99
100
101
102
103
104 private void addAll(Collection<MapList> maps) {
105 synchronized (maps) {
106 for (MapList w : maps) {
107 suggestionModel.addElement(new SelectableMapList(w));
108 }
109 }
110 }
111
112
113
114
115
116
117 private void removeAll(Collection<MapList> maps) {
118 synchronized (maps) {
119 for (MapList w : maps) {
120 suggestionModel.removeElement(new SelectableMapList(w));
121 }
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134 public void dispose() {
135 maps.removeCollectionListener(listener);
136 }
137
138 private class Listener extends CollectionEventAdaptor<MapList> {
139
140 @Override
141 public void postRemoveEvent(final Collection<MapList> alreadyRemoved,
142 Collection<MapList> whereWereRemoved) {
143 SwingUtilities.invokeLater(new Runnable() {
144
145 @Override
146 public void run() {
147 removeAll(alreadyRemoved);
148
149 }
150 });
151 }
152
153 @Override
154 public void postAddEvent(final Collection<MapList> alreadyAdded,
155 Collection<MapList> whereWereAdded) {
156 SwingUtilities.invokeLater(new Runnable() {
157
158 @Override
159 public void run() {
160 addAll(alreadyAdded);
161
162 }
163 });
164 }
165
166 }
167
168 }