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