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.dialogs;
18  
19  import java.awt.FlowLayout;
20  import java.awt.Frame;
21  import java.util.Collection;
22  
23  import javax.swing.DefaultComboBoxModel;
24  import javax.swing.JButton;
25  import javax.swing.JComboBox;
26  import javax.swing.JDialog;
27  import javax.swing.JLabel;
28  import javax.swing.JList;
29  import javax.swing.JSpinner;
30  import javax.swing.JTextField;
31  import javax.swing.SpinnerNumberModel;
32  import javax.swing.SwingUtilities;
33  
34  import nl.tudelft.goal.ut2004.util.Team;
35  import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
36  import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
37  import nl.tudelft.goal.ut2004.visualizer.data.EnvironmentData;
38  import nl.tudelft.goal.ut2004.visualizer.gui.action.AddUnrealGoalBotAction;
39  import nl.tudelft.goal.ut2004.visualizer.gui.widgets.WaypointBox;
40  import nl.tudelft.goal.ut2004.visualizer.util.CollectionEventAdaptor;
41  import nl.tudelft.goal.ut2004.visualizer.util.SelectableEnvironment;
42  import nl.tudelft.goal.ut2004.visualizer.util.WindowPersistenceHelper;
43  import cz.cuni.amis.pogamut.unreal.communication.worldview.map.IUnrealWaypoint;
44  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.NavPoint;
45  import cz.cuni.amis.utils.collections.ObservableSet;
46  import cz.cuni.amis.utils.exception.PogamutException;
47  
48  /**
49   * Dialog for adding an Epic Bot to the server.
50   * 
51   * @author Lennard de Rijk
52   * 
53   */
54  public class AddUnrealGoalBotDialog extends JDialog {
55  
56  	/**
57  	 * Field for the name of the bot.
58  	 */
59  	private JTextField nameField;
60  
61  	/**
62  	 * {@link JSpinner} for the difficulty level of the bot.
63  	 */
64  	private JSpinner levelSpinner;
65  
66  	/**
67  	 * {@link JList} which contains a choice between red or blue team.
68  	 */
69  	private JComboBox teamList;
70  
71  	/**
72  	 * {@link WaypointBox} for choosing the location where the bot should spawn.
73  	 */
74  	private WaypointBox location;
75  
76  	/**
77  	 * Button which adds a bot when clicked.
78  	 */
79  	private JButton addButton;
80  
81  	/**
82  	 * Button which closes this dialog when clicked.
83  	 */
84  	private JButton closeButton;
85  
86  	private JComboBox environmentList;
87  
88  	/**
89  	 * Helper class to persist this window.
90  	 */
91  	private WindowPersistenceHelper persistenceHelper;
92  	
93  	/**
94  	 * Creates a {@link AddUnrealGoalBotDialog} which allows the user to spawn
95  	 * an Epic Bot.
96  	 * 
97  	 * @param parent
98  	 *            Parent Frame, may be null
99  	 * @param navPoint
100 	 * @throws PogamutException
101 	 *             iff the {@link NavPoint} could not be retrieved from the
102 	 *             server.
103 	 */
104 	public AddUnrealGoalBotDialog(Frame parent, IUnrealWaypoint navPoint) {
105 		super(parent, false);
106 
107 		setTitle("Add UnrealGoal bot");
108 		setLayout(new FlowLayout());
109 
110 		add(new JLabel("Name"));
111 		this.nameField = new JTextField();
112 		this.nameField.setColumns(15);
113 		add(nameField);
114 
115 		// Add a spinner from 1 to 7 with increments of 1
116 		add(new JLabel("Level"));
117 		SpinnerNumberModel levelModel = new SpinnerNumberModel(4, 1, 7, 1);
118 		this.levelSpinner = new JSpinner(levelModel);
119 		add(levelSpinner);
120 
121 		add(new JLabel("Team"));
122 		this.teamList = new JComboBox(Team.values());
123 		this.teamList.setSelectedIndex(0);
124 		add(teamList);
125 
126 		location = new WaypointBox();
127 		if (navPoint != null)
128 			location.setSelected(navPoint);
129 		add(location);
130 
131 		ServerController controller = ServerController.getInstance();
132 		EnvironmentData data = controller.getEnvironmentData();
133 		ObservableSet<EnvironmentService> clients = data.getEnvironments();
134 		Collection<SelectableEnvironment> s = SelectableEnvironment
135 				.fromCollection(clients);
136 		this.environmentList = new JComboBox(s.toArray());
137 		clients.addCollectionListener(new CollectionEventAdaptor<EnvironmentService>() {
138 
139 			@Override
140 			public void postAddEvent(
141 					Collection<EnvironmentService> alreadyAdded,
142 					final Collection<EnvironmentService> whereWereAdded) {
143 				SwingUtilities.invokeLater(new Runnable() {
144 
145 					@Override
146 					public void run() {
147 						Collection<SelectableEnvironment> s = SelectableEnvironment
148 								.fromCollection(whereWereAdded);
149 						environmentList.setModel(new DefaultComboBoxModel(s
150 								.toArray()));
151 					}
152 				});
153 			}
154 
155 			@Override
156 			public void postRemoveEvent(
157 					Collection<EnvironmentService> alreadyRemoved,
158 					final Collection<EnvironmentService> whereWereRemoved) {
159 				SwingUtilities.invokeLater(new Runnable() {
160 
161 					@Override
162 					public void run() {
163 						Collection<SelectableEnvironment> s = SelectableEnvironment
164 								.fromCollection(whereWereRemoved);
165 						environmentList.setModel(new DefaultComboBoxModel(s
166 								.toArray()));
167 					}
168 				});
169 			}
170 		});
171 
172 		add(environmentList);
173 
174 		this.addButton = new JButton("Add Bot");
175 		addButton.addActionListener(new AddUnrealGoalBotAction(nameField,
176 				location, levelSpinner, teamList, environmentList));
177 		add(addButton);
178 
179 		setSize(400, 225);
180 		// Setup persistence
181 		persistenceHelper = new WindowPersistenceHelper(this);
182 		persistenceHelper.load();
183 	}
184 }