1 package nl.tudelft.goal.ut2004.visualizer.map;
2
3 import java.awt.BorderLayout;
4 import java.awt.event.ComponentAdapter;
5 import java.awt.event.ComponentEvent;
6
7 import javax.swing.JLabel;
8 import javax.swing.JPanel;
9 import javax.swing.SwingUtilities;
10
11 import nl.tudelft.goal.ut2004.visualizer.services.ISelectionHandler;
12 import nl.tudelft.pogamut.base.server.ServerDefinition;
13 import cz.cuni.amis.pogamut.unreal.server.IUnrealServer;
14 import cz.cuni.amis.utils.flag.FlagListener;
15
16
17
18
19
20
21 public class PureMapTopPanel extends JPanel {
22
23
24
25
26 private static final long serialVersionUID = -448000130214012550L;
27 protected final ServerDefinition<IUnrealServer> serverDefinition;
28 protected PureMapGLPanel mapPanel;
29 private ISelectionHandler selectionHandler;
30
31
32
33 public PureMapTopPanel(ServerDefinition<IUnrealServer> serverDefinition) {
34 this.serverDefinition = serverDefinition;
35 this.setLayout(new BorderLayout());
36
37
38 handleServerChanged();
39
40
41
42
43
44
45 handleVisibility();
46 handleServerName();
47 handleServer();
48 }
49
50
51
52
53
54 private void handleVisibility() {
55 this.addComponentListener(new ComponentAdapter() {
56
57 @Override
58 public void componentShown(ComponentEvent e) {
59 if (mapPanel != null) {
60 mapPanel.startDisplayLoop();
61 }
62 }
63
64 @Override
65 public void componentHidden(ComponentEvent e) {
66 if (mapPanel != null) {
67 mapPanel.stopDisplayLoop();
68 }
69 }
70 });
71 }
72
73 private void handleServerName() {
74 setPanelName();
75 serverDefinition.getServerNameFlag().addListener(
76 new FlagListener<String>() {
77
78 @Override
79 public void flagChanged(String changedValue) {
80 SwingUtilities.invokeLater(new Runnable() {
81
82 @Override
83 public void run() {
84 setPanelName();
85 }
86 });
87 }
88 });
89
90 }
91
92 protected void setPanelName() {
93 String map = "no map";
94 if (serverDefinition.getServerFlag().getFlag() != null) {
95 map = serverDefinition.getServerFlag().getFlag().getMapName();
96 }
97
98 this.setName(serverDefinition.getServerName() + " [" + map + "]");
99
100 }
101
102 private void handleServer() {
103 serverDefinition.getServerFlag().addListener(
104 new FlagListener<IUnrealServer>() {
105
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 @Override
113 public void flagChanged(IUnrealServer server) {
114 SwingUtilities.invokeLater(new Runnable() {
115
116 @Override
117 public void run() {
118 handleServerChanged();
119 }
120
121 });
122 }
123 });
124
125 }
126
127
128
129
130
131 private void handleServerChanged() {
132 removeAll();
133 setPanelName();
134 if (serverDefinition.getServerFlag().getFlag() != null) {
135 setUpMapPanel();
136 } else {
137 add(new JLabel("Not connected."), BorderLayout.PAGE_START);
138 }
139 revalidate();
140 repaint();
141 }
142
143 @SuppressWarnings("unchecked")
144 protected void setUpMapPanel() {
145
146
147 if (mapPanel != null) {
148 remove(mapPanel);
149 mapPanel.destroy();
150 }
151
152
153 IUnrealServer server = serverDefinition.getServerFlag().getFlag();
154 mapPanel = new PureMapGLPanel(server.getMap(), server);
155 mapPanel.setSelectionHandler(selectionHandler);
156
157
158 if (isVisible()) {
159 mapPanel.startDisplayLoop();
160 }
161
162
163 add(mapPanel);
164 }
165
166 public void setSelectionHandler(ISelectionHandler selectionHandler) {
167 this.selectionHandler = selectionHandler;
168 if (mapPanel != null) {
169 mapPanel.setSelectionHandler(selectionHandler);
170 }
171 }
172
173 }