View Javadoc

1   /*
2    * Copyright (C) 2010 Unreal Visualizer Authors
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package nl.tudelft.goal.visualizer.gui;
18  
19  import java.awt.BorderLayout;
20  import java.awt.FlowLayout;
21  import java.awt.GridLayout;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.awt.event.KeyEvent;
25  
26  import javax.swing.JFrame;
27  import javax.swing.JMenu;
28  import javax.swing.JMenuBar;
29  import javax.swing.JMenuItem;
30  import javax.swing.JOptionPane;
31  import javax.swing.JPanel;
32  import javax.swing.SwingUtilities;
33  
34  import nl.tudelft.goal.unreal.util.vecmathcheck.VecmathCheck;
35  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
36  import nl.tudelft.goal.ut2004.visualizer.gui.action.PauseResumeAction;
37  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowDialogueAction;
38  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowServerDependentDialogueAction;
39  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowServerEnvironmentDependentDialogueAction;
40  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddNativeBotDialog;
41  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddUnrealGoalBotDialog;
42  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeGameSpeedDialog;
43  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeMapDialog;
44  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ListEnvironmentsDialog;
45  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ListPlayerDialog;
46  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ServerConnectionDialog;
47  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.SettingsDialog;
48  import nl.tudelft.goal.visualizer.gui.panels.MapPanel;
49  import nl.tudelft.goal.ut2004.visualizer.util.WindowPersistenceHelper;
50  import nl.tudelft.pogamut.base.server.ReconnectingServerDefinition;
51  import nl.tudelft.pogamut.base.server.ServerDefinition;
52  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
53  import java.awt.Dimension;
54  import java.awt.event.ComponentEvent;
55  import java.net.URI;
56  import java.net.URISyntaxException;
57  import java.util.logging.Level;
58  import java.util.logging.Logger;
59  
60  /**
61   * 
62   * This is the main class to start the UnrealVisualizer application. The
63   * UnrealVisualizer is started by calling the constructor for this class with an
64   * {@link UTServer} instance. All messaging from and to the server will be
65   * handled by the application itself.
66   * 
67   * Agents in Pogamut will be known as Bots inside the GUI.
68   * 
69   * @author Lennard de Rijk
70   * @author M.P. Korstanje
71   * 
72   */
73  public class UnrealVisualizerGUI extends JFrame {
74  
75  	/**
76  	 * The width of the application window.
77  	 */
78  	private static final int WINDOW_WIDTH = 800;
79  	/**
80  	 * The height of the application window.
81  	 */
82  	private static final int WINDOW_HEIGHT = 600;
83  
84  	/**
85  	 * The {@link MapPanel} in use by the {@link UnrealVisualizerGUI}.
86  	 */
87  	private final MapPanel mapPanel;
88  	
89  	/**
90  	 * Helper class to persist this window.
91  	 */
92  	private WindowPersistenceHelper persistenceHelper;
93  
94  	public UnrealVisualizerGUI() {
95  		super();
96  		
97  		// Create the controllers
98  		ServerController.createNewController();
99  		ServerController controller = ServerController.getInstance();
100 		ServerDefinition<IUT2004Server> serverDefinition = controller
101 				.getServerDefinition();
102 		
103 		// Set up the main window properties
104 		setTitle("Unreal Tournament Visualizer for GOAL");
105 		setResizable(true);
106 		setLayout(new BorderLayout());
107 		setDefaultCloseOperation(EXIT_ON_CLOSE);
108 
109 		// Setup persistence
110 		persistenceHelper = new WindowPersistenceHelper(this);
111 		persistenceHelper.load();
112 		
113 		// Set up the initial menu bar at the top
114 		setupMenuBar();
115 
116 		// Instantiate the MapPanel and add it to the tabbed pane
117 		mapPanel = new MapPanel();
118    
119 		// Instantiate the mainPanel that contains our tabbed pane
120 		JPanel mainPanel = new JPanel(new GridLayout(1, 1));
121 		mainPanel.add(mapPanel);
122 		add(mainPanel);
123 	}
124         
125         @Override
126         public Dimension getPreferredSize() {
127                 return new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
128         }
129 	
130 
131 	/**
132 	 * Setups the {@link JMenuBar} that is shown at the top of the screen.
133 	 */
134 	private void setupMenuBar() {
135 		// This is the main bar that goes on top of the screen
136 		JMenuBar menuBar = new JMenuBar();
137 		menuBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
138 		setJMenuBar(menuBar);
139 
140 		// Level 0 menu for the application itself. Contains for instance Exit
141 		// entry.
142 		JMenu applicationMenu = new JMenu("Visualizer");
143 		{
144 			// Add settings menu to Visualizer entry
145 			SettingsDialog settingsDialogue = new SettingsDialog(this);
146 			String name = "Settings";
147 			String description = "Change the settings of the visualizer.";
148 			ShowDialogueAction showSettings = new ShowDialogueAction(settingsDialogue, name, description);
149 			JMenuItem settings = new JMenuItem(showSettings);
150 			applicationMenu.add(settings);
151 			
152 			// Add Exit menu item to Visualizer entry
153 			JMenuItem exitVis = new JMenuItem("Exit", KeyEvent.VK_E);
154 			exitVis.setToolTipText("Exit the Visualizer");
155 			exitVis.addActionListener(new ActionListener() {
156 				@Override
157 				public void actionPerformed(ActionEvent e) {
158 					// Close the application
159 					dispose();
160 					System.exit(0);
161 				}
162 			});
163 			applicationMenu.add(exitVis);
164 			
165 		
166 		}
167 		menuBar.add(applicationMenu);
168 
169 		// Pause resume item
170 		JMenu server = new JMenu("Server");
171 		{
172 			{
173 				ServerController controller = ServerController.getInstance();
174 				ServerDefinition<IUT2004Server> serverDefinition = controller
175 						.getServerDefinition();
176 				ServerConnectionDialog connectionDialog = new ServerConnectionDialog(
177 						this, (ReconnectingServerDefinition) serverDefinition);
178 				String name = "Connection";
179 				String description = "Connect to an Unreal Tournament Server.";
180 				ShowDialogueAction action = new ShowDialogueAction(
181 						connectionDialog, name, description);
182 				JMenuItem connect = new JMenuItem(action);
183 				server.add(connect);
184 			}
185 
186 			JMenuItem pauseResume = new JMenuItem(new PauseResumeAction());
187 			server.add(pauseResume);
188 			{
189 				ChangeGameSpeedDialog gameSpeedDialog = new ChangeGameSpeedDialog(
190 						this);
191 				String name = "Game Speed";
192 				String description = "Change the speed of the game.";
193 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
194 						gameSpeedDialog, name, description);
195 				JMenuItem speed = new JMenuItem(action);
196 				server.add(speed);
197 			}
198 
199 			{
200 				ChangeMapDialog changeMapDialog = new ChangeMapDialog(this);
201 				String name = "Change Map";
202 				String description = "Change the current map.";
203 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
204 						changeMapDialog, name, description);
205 				JMenuItem change = new JMenuItem(action);
206 				server.add(change);
207 			}
208 			{
209 				AddNativeBotDialog addNativeBotDialog = new AddNativeBotDialog(
210 						this, null);
211 				String name = "Add Native Bot";
212 				String description = "Adds a native unreal bot to the game.";
213 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
214 						addNativeBotDialog, name, description);
215 				JMenuItem add = new JMenuItem(action);
216 				server.add(add);
217 			}
218 			{
219 				AddUnrealGoalBotDialog addUnrealGoalBotDialog = new AddUnrealGoalBotDialog(
220 						this, null);
221 				String name = "Add UnrealGoal Bot";
222 				String description = "Adds an UnrealGoal bot to the game.";
223 				ShowServerEnvironmentDependentDialogueAction action = new ShowServerEnvironmentDependentDialogueAction(
224 						addUnrealGoalBotDialog, name, description);
225 				JMenuItem add = new JMenuItem(action);
226 				server.add(add);
227 			}
228 			{
229 				ListPlayerDialog listPlayerDialog = new ListPlayerDialog(this);
230 				String name = "List Players";
231 				String description = "Lists all player in the game.";
232 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
233 						listPlayerDialog, name, description);
234 				JMenuItem add = new JMenuItem(action);
235 				server.add(add);
236 			}
237 		}
238 
239 		menuBar.add(server);
240 
241 		JMenu environments = new JMenu("Environments");
242 		{
243 			String name = "List Environments";
244 			String description = "List Goal Environments connected to the visualizier.";
245 			ListEnvironmentsDialog listDialog = new ListEnvironmentsDialog(this);
246 			ShowDialogueAction action = new ShowDialogueAction(listDialog,
247 					name, description);
248 			JMenuItem list = new JMenuItem(action);
249 
250 			environments.add(list);
251 		}
252 		menuBar.add(environments);
253 
254 	}
255 
256 	/**
257 	 * The interface is being closed. We will "try to" dispose all resources in
258 	 * use.
259 	 */
260 	@Override
261 	public void dispose() {
262 		super.dispose();
263 
264 		// The user interface has been disposed, dispose of all other
265 		// resources.
266 		ServerController.disposeController();
267 	}
268 
269 	/**
270 	 * Main method for testing purposes only since we need to launch with an
271 	 * active {@link UTServer} instance
272 	 * 
273 	 * @param args
274 	 */
275 	public static void main(String[] args) {
276 		
277 		if (!VecmathCheck.check()) {
278 			JOptionPane.showMessageDialog(null, VecmathCheck.getErrorMessage(),"Version Conflict",JOptionPane.ERROR_MESSAGE);
279 			
280 			return;
281 		}
282 		
283 		
284 		SwingUtilities.invokeLater(new Runnable() {
285 
286 			@Override
287 			public void run() {
288 				UnrealVisualizerGUI vizualiser = new UnrealVisualizerGUI();
289 				vizualiser.setVisible(true);
290 			}
291 		});
292 	}
293 
294 }