1 package nl.tudelft.goal.ut2004.visualizer.gui.action;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.rmi.RemoteException;
6
7 import javax.swing.JComboBox;
8 import javax.swing.JOptionPane;
9 import javax.swing.JSpinner;
10 import javax.swing.JTextField;
11
12 import nl.tudelft.goal.ut2004.visualizer.connection.AddBotCommand;
13 import nl.tudelft.goal.ut2004.visualizer.connection.EnvironmentService;
14 import nl.tudelft.goal.ut2004.visualizer.controller.ServerController;
15 import nl.tudelft.goal.ut2004.visualizer.gui.widgets.WaypointBox;
16 import nl.tudelft.goal.ut2004.visualizer.util.SelectableEnvironment;
17
18 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
19 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.AddBot;
20 import cz.cuni.amis.pogamut.ut2004.communication.worldview.map.Waypoint;
21 import cz.cuni.amis.pogamut.ut2004.server.IUT2004Server;
22 import eis.exceptions.ManagementException;
23
24 public class AddUnrealGoalBotAction implements ActionListener {
25
26 private final JTextField botName;
27 private final nl.tudelft.goal.ut2004.visualizer.gui.widgets.WaypointBox location;
28 private final JSpinner skill;
29 private final JComboBox teamList;
30 private final JComboBox environmentList;
31
32 public AddUnrealGoalBotAction(JTextField botName, WaypointBox location,
33 JSpinner skill, JComboBox teamList, JComboBox environmentList) {
34 this.botName = botName;
35 this.location = location;
36 this.skill = skill;
37 this.teamList = teamList;
38 this.environmentList = environmentList;
39 }
40
41 @Override
42 public void actionPerformed(ActionEvent e) {
43
44 SelectableEnvironment se = (SelectableEnvironment) environmentList
45 .getSelectedItem();
46 final EnvironmentService environement = se.getItem();
47
48 final AddBotCommand addBot = new AddBotCommand();
49 addBot.setBotName(botName.getText());
50 addBot.setSkill((Integer) skill.getValue());
51
52 if (teamList.getSelectedItem().equals("Red")) {
53 addBot.setTeam(0);
54 } else if (teamList.getSelectedItem().equals("Blue")) {
55 addBot.setTeam(1);
56 } else {
57 addBot.setTeam(Math.random() > 0.5 ? 0 : 1);
58 }
59
60
61 new Thread(new Runnable() {
62
63 @Override
64 public void run() {
65 try {
66 environement.addBot(addBot);
67 } catch (RemoteException e1) {
68 e1.printStackTrace();
69
70 JOptionPane
71 .showMessageDialog(
72 null,
73 "We could not connect to the Unreal Goal environment. A stack strace has been printed to your console.",
74 "An Error has Occured",
75 JOptionPane.ERROR_MESSAGE);
76
77 } catch (ManagementException e2) {
78 e2.printStackTrace();
79 JOptionPane
80 .showMessageDialog(
81 null,
82 "The environment was unable to add a bot. A stack strace has been printed to your console.",
83 "An Error has Occured",
84 JOptionPane.ERROR_MESSAGE);
85 }
86 }
87 }).start();
88 }
89
90 }