View Javadoc

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   * This panel handles connecting to an unreal server and rendering it.
18   * 
19   * @author M.P. Korstanje
20   */
21  public class PureMapTopPanel extends JPanel {
22  
23  	/**
24  	 * Default generated.
25  	 */
26  	private static final long serialVersionUID = -448000130214012550L;
27  	protected final ServerDefinition<IUnrealServer> serverDefinition;
28  	protected PureMapGLPanel mapPanel;
29  	private ISelectionHandler selectionHandler;
30  
31  	// protected InstanceContent lookupContent;
32  
33  	public PureMapTopPanel(ServerDefinition<IUnrealServer> serverDefinition) {
34  		this.serverDefinition = serverDefinition;
35  		this.setLayout(new BorderLayout());
36  
37  		// Trigger server changed sync with serverDefintion.
38  		handleServerChanged();
39  
40  		// Associate our lookup with this TC
41  		// lookupContent = new InstanceContent();
42  		// associateLookup(new AbstractLookup(lookupContent));
43  
44  		// set proper name
45  		handleVisibility();
46  		handleServerName();
47  		handleServer();
48  	}
49  
50  	/**
51  	 * The map updates if it's shown, it doesn't when it's not.
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  		// setDisplayName(serverDef.getServerName() + " [" + map + "]");
100 	}
101 
102 	private void handleServer() {
103 		serverDefinition.getServerFlag().addListener(
104 				new FlagListener<IUnrealServer>() {
105 
106 					/**
107 					 * When server changes, get message
108 					 * 
109 					 * @param changedValue
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 	 * Clears the panel and updates the map panel.
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 		// Clear up any previous panels.
147 		if (mapPanel != null) {
148 			remove(mapPanel);
149 			mapPanel.destroy();
150 		}
151 
152 		// Create map panel
153 		IUnrealServer server = serverDefinition.getServerFlag().getFlag();
154 		mapPanel = new PureMapGLPanel(server.getMap(), server);
155 		mapPanel.setSelectionHandler(selectionHandler);
156 
157 		// Don't show unless visible.
158 		if (isVisible()) {
159 			mapPanel.startDisplayLoop();
160 		}
161 
162 		// add map panel to the TC
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 }