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