1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
50  
51  
52  
53  
54  public class AddUnrealGoalBotDialog extends JDialog {
55  
56  	
57  
58  
59  	private JTextField nameField;
60  
61  	
62  
63  
64  	private JSpinner levelSpinner;
65  
66  	
67  
68  
69  	private JComboBox teamList;
70  
71  	
72  
73  
74  	private WaypointBox location;
75  
76  	
77  
78  
79  	private JButton addButton;
80  
81  	
82  
83  
84  	private JButton closeButton;
85  
86  	private JComboBox environmentList;
87  
88  	
89  
90  
91  	private WindowPersistenceHelper persistenceHelper;
92  	
93  	
94  
95  
96  
97  
98  
99  
100 
101 
102 
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 		
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 		
181 		persistenceHelper = new WindowPersistenceHelper(this);
182 		persistenceHelper.load();
183 	}
184 }