1 package main;
2
3 import simworldobjects.*;
4 import intellego.Intellego;
5 import util.*;
6 import interfaces.*;
7 import real.*;
8
9 import java.awt.*;
10 import java.lang.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.event.*;
14 import java.io.*;
15 import java.util.*;
16
17
22 public class SimUI extends JInternalFrame implements Runnable
23 {
24 private Container mainContainer;
25 private SimDisplay display=null;
26 private JLayeredPane mainPane;
27 private LinkedList controllerList;
28 private LinkedList robotList;
29 private SimWorld world=null;
30 private boolean running=false;
31 private boolean paused=false;
32 private int UPDATE_TIME=30;
33 private static int screenWidth=1000;
34 private static int screenHeight=800;
35
36 private static final String NO_CLASS="__NOCLASS__";
37
38
41 public SimUI()
42 {
43 super("Simulator:",true,true,true,true);
45 setSize(screenWidth,screenHeight);
46
47 setupWindow();
48
49 addInternalFrameListener(new InternalFrameAdapter()
51 {
52 public void internalFrameClosing(InternalFrameEvent e)
53 {
54 Intellego.addToLog("SimUI.init(): simulator quitting");
55
56 running=false;
58
59 for (int i=0;i<controllerList.size();i++)
61 {
62 Controller c=(Controller)controllerList.get(i);
63 c.halt();
64 }
65 }
66 });
67 }
68
69
74 public SimUI(String controllerClassName)
75 {
76 super("Simulator:",true,true,true,true);
78 setSize(screenWidth,screenHeight);
79
80 setupWindow();
81
82 selectWorld();
84
85 Controller c=createController(controllerClassName);
87 addController(c);
88 }
89
90
93 public void setupWindow()
94 {
95 mainContainer=getContentPane();
97 mainContainer.setLayout(new BoxLayout(mainContainer,BoxLayout.Y_AXIS));
98
99 JMenuBar mb=createMenuBar();
101 setJMenuBar(mb);
102
103 controllerList=new LinkedList();
105 robotList=new LinkedList();
106 }
107
108
114 private JMenuBar createMenuBar()
115 {
116 JMenuBar mb=new JMenuBar();
118
119 JMenu controlMenu=new JMenu("Controls");
121
122 JMenuItem pickWorld=new JMenuItem("Pick SimWorld");
124 JMenuItem addNewRobot=new JMenuItem("Add Robot");
125 JMenuItem startSim=new JMenuItem("Start Simulation");
126 JMenuItem pauseSim=new JMenuItem("Pause Simulation"); JMenuItem unpauseSim=new JMenuItem("Unpause Simulation"); JMenuItem stopSim=new JMenuItem("Stop Simulation");
129
130 pickWorld.addActionListener(new ActionListener()
132 {
133 public void actionPerformed(ActionEvent e)
134 {
135 selectWorld();
136 }
137 });
138
139 addNewRobot.addActionListener(new ActionListener()
140 {
141 public void actionPerformed(ActionEvent e)
142 {
143 addRobot();
144 }
145 });
146
147 startSim.addActionListener(new ActionListener()
148 {
149 public void actionPerformed(ActionEvent e)
150 {
151 startSimulation();
152 }
153 });
154
155 stopSim.addActionListener(new ActionListener()
156 {
157 public void actionPerformed(ActionEvent e)
158 {
159 stopSimulation();
160 }
161 });
162
163 controlMenu.add(pickWorld);
165 controlMenu.add(addNewRobot);
166 controlMenu.add(startSim);
167 controlMenu.add(stopSim);
168
169 mb.add(controlMenu);
171
172 return mb;
173 }
174
175
178 public void run()
179 {
180 if(world==null || display==null)
181 {
182 MainInterface.displayMessage("You must select a SimWorld and a SimDisplay first");
183 }
184 else
185 {
186 while (running)
187 {
188 world.tick();
190
191 display.repaint();
193
194 try{Thread.sleep(UPDATE_TIME);}catch(Exception e){}
196 }
197 }
198 }
199
200
203 public void selectWorld()
204 {
205 String className=getClassName("Pick a SimWorld class file:","simworlds");
207
208 if (className.equals(NO_CLASS))
210 {
211 }
213 else
214 {
215 if(world!=null)
217 {
218 stopSimulation();
219 world=null;
220 mainContainer.remove(display);
221 display=null;
222 }
223
224 SimWorld s=createSimWorld(className);
226
227 if(s!=null)
229 {
230 world=s;
232
233 display=new SimpleDisplay(world);
235
236 display.setVisible(true);
237
238 mainContainer.add(display, BorderLayout.CENTER);
240
241 setSize(screenWidth,screenHeight);
243 }
244 else {
246 MainInterface.displayMessage("Failed to create SimWorld");
247 Intellego.addToLog("SimUI.selectWorld(): Failed to create SimWorld with name "+className);
248 }
249 }
250 }
251
252
259 public SimWorld createSimWorld(String name)
260 {
261 SimWorld s=null;
262
263 try
264 {
265 Class simworldClass=Class.forName(name);
267
268 try
269 {
270 s=(SimWorld)simworldClass.newInstance();
273 }
274 catch (Exception e)
275 {
276 MainInterface.displayMessage("Error: this class is not a valid Intellego SimWorld");
277 }
278 }
279 catch (Exception e)
280 {
281 MainInterface.displayMessage("Error creating class");
283 Intellego.addToLog("SimUI.createSimWorld(): Error creating class: "+name+": "+e);
284 return null;
285 }
286
287 return s;
289 }
290
291
294 public void addRobot()
295 {
296 if(world==null || display==null)
297 {
298 MainInterface.displayMessage("You must select a SimWorld and a SimDisplay first");
299 }
300 else
301 {
302 String className=getClassName("Pick a Controller class file:","controllers");
304
305 if (className.equals(NO_CLASS))
307 {
308 }
310 else
311 {
312 Controller c=createController(className);
314
315 if(c!=null)
316 {
317 addController(c);
319 }
320 else
321 {
322 MainInterface.displayMessage("Failed to create Controller");
323 Intellego.addToLog("SimUI.addRobot(): Failed to create Controller with name "+className);
324 }
325 }
326 }
327 }
328
329
338 public String getClassName(String title,String base_dir)
339 {
340 String className=" ";
341
342 JFileChooser chooser=new JFileChooser(new File(System.getProperties().getProperty("user.dir"),base_dir));
344
345 String[] extensions={".class"};
347 chooser.addChoosableFileFilter(new FileChooserFilter(extensions,"Compiled Class File"));
348
349 chooser.setDialogTitle(title);
350 int returnVal=chooser.showOpenDialog(this);
351
352 try
353 {
354 if (returnVal==JFileChooser.APPROVE_OPTION) {
356 File file=chooser.getSelectedFile();
358
359 className=file.getName();
361
362 Intellego.addToLog("SimUI.getClassName(): opening file: "+className);
363
364 className=className.substring(0,file.getName().length() - 6);
366
367 Intellego.addToLog("SimUI.getClassName(): attempting to open class: "+className);
368
369 return (className);
371 }
372 else {
374 return NO_CLASS;
375 }
376 }
377 catch(Exception e)
378 {
379 return null;
380 }
381 }
382
383
390 public Controller createController(String name)
391 {
392 Controller c=null;
393
394 try
395 {
396 Class controllerClass=Class.forName(name);
398
399 try
400 {
401 c=(Controller)controllerClass.newInstance();
404 }
405 catch (Exception e)
406 {
407 MainInterface.displayMessage("Error: class is not a valid Intellego Controller class");
408 Intellego.addToLog("SimUI.createController(): Attempt to create Intellego Controller "+name+" failed. Not a valid Controller.");
409 }
410 }
411 catch (Exception e)
412 {
413 MainInterface.displayMessage("Error creating class");
415 Intellego.addToLog("SimUI.createController(): Error creating class "+name+": "+e);
416 return null;
417 }
418
419 return c;
421 }
422
423
428 public void addController(Controller c)
429 {
430 if(c!=null)
432 {
433 controllerList.add(c);
435
436 InitRobotDialog d=new InitRobotDialog();
438
439 d.createRobot(world,c,display);
441
442 repaint();
444 }
445 else {
447 MainInterface.displayMessage("Failed to add controller");
448 Intellego.addToLog("SimUI.addController: Failed to add controller");
449 }
450 }
451
452
455 public void startSimulation()
456 {
457 if (world==null || display==null)
459 {
460 MainInterface.displayMessage("You must pick a SimWorld and a SimDisplay first");
462 }
463 else
464 {
465 if(!running)
467 {
468 running=true;
470
471 Thread t=new Thread(this);
472 t.start();
473 }
474
475 for (int i=0;i<controllerList.size();i++)
477 {
478 Controller c=(Controller)controllerList.get(i);
479 Thread u=new Thread(c);
480 u.setPriority(Thread.MIN_PRIORITY);
481 u.start();
482 Intellego.addToLog("SimUI.run(): Started controller");
483 }
484 }
485 }
486
487
490 public void stopSimulation()
491 {
492 if (world==null || display==null)
494 {
495 MainInterface.displayMessage("No simulation to stop");
497 }
498 else
499 {
500 running=false;
501
502 for (int i=0;i<controllerList.size();i++)
504 {
505 Controller c=(Controller)controllerList.get(i);
506 c.halt();
507 }
508 }
509 }
510
511
512
513 }
514