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