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.ut2004.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.JPanel;
31  import javax.swing.SwingUtilities;
32  
33  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
34  import nl.tudelft.goal.ut2004.visualizer.gui.action.PauseResumeAction;
35  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowDialogueAction;
36  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowServerDependentDialogueAction;
37  import nl.tudelft.goal.ut2004.visualizer.gui.action.ShowServerEnvironmentDependentDialogueAction;
38  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddNativeBotDialog;
39  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.AddUnrealGoalBotDialog;
40  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeGameSpeedDialog;
41  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ChangeMapDialog;
42  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ListEnvironmentsDialog;
43  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ListPlayerDialog;
44  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.ServerConnectionDialog;
45  import nl.tudelft.goal.ut2004.visualizer.gui.dialogs.SettingsDialog;
46  import nl.tudelft.goal.ut2004.visualizer.gui.panels.MapPanel;
47  import nl.tudelft.goal.ut2004.visualizer.util.WindowPersistenceHelper;
48  import nl.tudelft.pogamut.base.server.ReconnectingServerDefinition;
49  import nl.tudelft.pogamut.base.server.ServerDefinition;
50  import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
51  
52  /**
53   * 
54   * This is the main class to start the UnrealVisualizer application. The
55   * UnrealVisualizer is started by calling the constructor for this class with an
56   * {@link UTServer} instance. All messaging from and to the server will be
57   * handled by the application itself.
58   * 
59   * Agents in Pogamut will be known as Bots inside the GUI.
60   * 
61   * @author Lennard de Rijk
62   * @author M.P. Korstanje
63   * 
64   */
65  public class UnrealVisualizerGUI extends JFrame {
66  
67  	/**
68  	 * The width of the application window.
69  	 */
70  	private static final int WINDOW_WIDTH = 800;
71  	/**
72  	 * The height of the application window.
73  	 */
74  	private static final int WINDOW_HEIGHT = 600;
75  
76  	/**
77  	 * The {@link MapPanel} in use by the {@link UnrealVisualizerGUI}.
78  	 */
79  	private final MapPanel mapPanel;
80  	
81  	/**
82  	 * Helper class to persist this window.
83  	 */
84  	private WindowPersistenceHelper persistenceHelper;
85  
86  	public UnrealVisualizerGUI() {
87  		super();
88  
89  		// Create the controllers
90  		ServerController.createNewController();
91  		ServerController controller = ServerController.getInstance();
92  		ServerDefinition<IUT2004Server> serverDefinition = controller
93  				.getServerDefinition();
94  		
95  		// Set up the main window properties
96  		setTitle("Unreal Tournament 2004 Visualizer for GOAL");
97  		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
98  		setResizable(true);
99  		setLayout(new BorderLayout());
100 		setDefaultCloseOperation(EXIT_ON_CLOSE);
101 
102 		// Setup persistence
103 		persistenceHelper = new WindowPersistenceHelper(this);
104 		persistenceHelper.load();
105 		
106 		// Set up the initial menu bar at the top
107 		setupMenuBar();
108 
109 		// Instantiate the MapPanel and add it to the tabbed pane
110 		mapPanel = new MapPanel();
111 
112 		// Instantiate the mainPanel that contains our tabbed pane
113 		JPanel mainPanel = new JPanel(new GridLayout(1, 1));
114 		mainPanel.add(mapPanel);
115 		add(mainPanel);
116 	}
117 
118 	
119 
120 	/**
121 	 * Setups the {@link JMenuBar} that is shown at the top of the screen.
122 	 */
123 	private void setupMenuBar() {
124 		// This is the main bar that goes on top of the screen
125 		JMenuBar menuBar = new JMenuBar();
126 		menuBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
127 		setJMenuBar(menuBar);
128 
129 		// Level 0 menu for the application itself. Contains for instance Exit
130 		// entry.
131 		JMenu applicationMenu = new JMenu("Visualizer");
132 		{
133 			// Add settings menu to Visualizer entry
134 			SettingsDialog settingsDialogue = new SettingsDialog(this);
135 			String name = "Settings";
136 			String description = "Change the settings of the visualizer.";
137 			ShowDialogueAction showSettings = new ShowDialogueAction(settingsDialogue, name, description);
138 			JMenuItem settings = new JMenuItem(showSettings);
139 			applicationMenu.add(settings);
140 			
141 			// Add Exit menu item to Visualizer entry
142 			JMenuItem exitVis = new JMenuItem("Exit", KeyEvent.VK_E);
143 			exitVis.setToolTipText("Exit the Visualizer");
144 			exitVis.addActionListener(new ActionListener() {
145 				@Override
146 				public void actionPerformed(ActionEvent e) {
147 					// Close the application
148 					dispose();
149 					System.exit(0);
150 				}
151 			});
152 			applicationMenu.add(exitVis);
153 			
154 		
155 		}
156 		menuBar.add(applicationMenu);
157 
158 		// Pause resume item
159 		JMenu server = new JMenu("Server");
160 		{
161 			{
162 				ServerController controller = ServerController.getInstance();
163 				ServerDefinition<IUT2004Server> serverDefinition = controller
164 						.getServerDefinition();
165 				ServerConnectionDialog connectionDialog = new ServerConnectionDialog(
166 						this, (ReconnectingServerDefinition) serverDefinition);
167 				String name = "Connection";
168 				String description = "Connect to an Unreal Tournament Server.";
169 				ShowDialogueAction action = new ShowDialogueAction(
170 						connectionDialog, name, description);
171 				JMenuItem connect = new JMenuItem(action);
172 				server.add(connect);
173 			}
174 
175 			JMenuItem pauseResume = new JMenuItem(new PauseResumeAction());
176 			server.add(pauseResume);
177 			{
178 				ChangeGameSpeedDialog gameSpeedDialog = new ChangeGameSpeedDialog(
179 						this);
180 				String name = "Game Speed";
181 				String description = "Change the speed of the game.";
182 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
183 						gameSpeedDialog, name, description);
184 				JMenuItem speed = new JMenuItem(action);
185 				server.add(speed);
186 			}
187 
188 			{
189 				ChangeMapDialog changeMapDialog = new ChangeMapDialog(this);
190 				String name = "Change Map";
191 				String description = "Change the current map.";
192 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
193 						changeMapDialog, name, description);
194 				JMenuItem change = new JMenuItem(action);
195 				server.add(change);
196 			}
197 			{
198 				AddNativeBotDialog addNativeBotDialog = new AddNativeBotDialog(
199 						this, null);
200 				String name = "Add Native Bot";
201 				String description = "Adds a native unreal bot to the game.";
202 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
203 						addNativeBotDialog, name, description);
204 				JMenuItem add = new JMenuItem(action);
205 				server.add(add);
206 			}
207 			{
208 				AddUnrealGoalBotDialog addUnrealGoalBotDialog = new AddUnrealGoalBotDialog(
209 						this, null);
210 				String name = "Add UnrealGoal Bot";
211 				String description = "Adds an UnrealGoal bot to the game.";
212 				ShowServerEnvironmentDependentDialogueAction action = new ShowServerEnvironmentDependentDialogueAction(
213 						addUnrealGoalBotDialog, name, description);
214 				JMenuItem add = new JMenuItem(action);
215 				server.add(add);
216 			}
217 			{
218 				ListPlayerDialog listPlayerDialog = new ListPlayerDialog(this);
219 				String name = "List Players";
220 				String description = "Lists all player in the game.";
221 				ShowServerDependentDialogueAction action = new ShowServerDependentDialogueAction(
222 						listPlayerDialog, name, description);
223 				JMenuItem add = new JMenuItem(action);
224 				server.add(add);
225 			}
226 		}
227 
228 		menuBar.add(server);
229 
230 		JMenu environments = new JMenu("Environments");
231 		{
232 			String name = "List Environments";
233 			String description = "List Goal Environments connected to the visualizier.";
234 			ListEnvironmentsDialog listDialog = new ListEnvironmentsDialog(this);
235 			ShowDialogueAction action = new ShowDialogueAction(listDialog,
236 					name, description);
237 			JMenuItem list = new JMenuItem(action);
238 
239 			environments.add(list);
240 		}
241 		menuBar.add(environments);
242 
243 	}
244 
245 	/**
246 	 * The interface is being closed. We will "try to" dispose all resources in
247 	 * use.
248 	 */
249 	@Override
250 	public void dispose() {
251 		super.dispose();
252 
253 		// The user interface has been disposed, dispose of all other
254 		// resources.
255 		ServerController.disposeController();
256 	}
257 
258 	/**
259 	 * Main method for testing purposes only since we need to launch with an
260 	 * active {@link UTServer} instance
261 	 * 
262 	 * @param args
263 	 */
264 	public static void main(String[] args) {
265 		SwingUtilities.invokeLater(new Runnable() {
266 
267 			@Override
268 			public void run() {
269 				UnrealVisualizerGUI vizualiser = new UnrealVisualizerGUI();
270 				vizualiser.setVisible(true);
271 			}
272 		});
273 	}
274 
275 }