1   package main;
2   
3   import intellego.Intellego;
4   import util.*;
5   import interfaces.*;
6   import real.*;
7   import simworldobjects.*;
8   import main.*;
9   import NetBeansResources.*;
10  
11  import java.awt.*;
12  import java.lang.*;
13  import java.awt.event.*;
14  import javax.swing.plaf.basic.*;
15  import javax.swing.*;
16  import java.io.*;
17  import java.util.*;
18  
19  /** Provides a dialog box to allow the user to interact with the robot in the
20   * simulation environment.
21   * @author Simon Zienkiewicz
22   */ 
23  public class RobotRotation extends JPanel implements ActionListener
24  {
25      private JLabel bearingL,title;
26      private JButton OK, cancel, tryW, remove;
27      private JSpinner bearingS;
28      private SimUI sim;
29      private SimRCX object;
30      private BasicSimWorld world=null;
31          
32      /** Creates an instance of RobotRotation
33       * @param sim the simulator window
34       */
35  
36      public RobotRotation(SimUI sim){
37          this.sim = sim;
38      }
39      
40      /** Creates the actual robot rotation dialog. */    
41      public void createRobotRotation(){
42          
43          //sets the properties of the popup window
44          setSize(210,165);
45          setLocation(400,400);
46         // setLocation(0,0);
47          setVisible(true);
48          this.setBorder(BasicBorders.getTextFieldBorder());
49          this.setLayout(new AbsoluteLayout());
50        
51          this.title = new JLabel("Robot Bearing:");
52          this.bearingL = new JLabel("XZ Rotation:");
53                
54          title.setForeground(Color.yellow);
55          bearingL.setForeground(Color.lightGray);
56          
57          this.bearingS = new JSpinner(new SpinnerNumberModel(0,0,360,1));
58          bearingS.setBackground(Color.yellow);
59          
60          
61          (OK=new JButton("OK")).addActionListener(this);
62          (tryW=new JButton("Try")).addActionListener(this);
63          (remove=new JButton("Del")).addActionListener(this);
64          remove.setMnemonic('d');
65          (cancel=new JButton("Cancel")).addActionListener(this);
66          
67          OK.setMargin(new Insets(1,1,1,1));
68          tryW.setMargin(new Insets(1,1,1,1));
69          remove.setMargin(new Insets(1,1,1,1));
70          cancel.setMargin(new Insets(1,1,1,1));
71          
72          //set color
73          OK.setBackground(Color.yellow);
74          OK.setForeground(Color.darkGray);
75          tryW.setBackground(Color.yellow);
76          tryW.setForeground(Color.darkGray);
77          remove.setBackground(Color.red);
78          remove.setForeground(Color.darkGray);
79          cancel.setBackground(Color.yellow);
80          cancel.setForeground(Color.darkGray);
81          
82          this.add(this.title,new AbsoluteConstraints(6,7));
83          this.add(this.bearingL,new AbsoluteConstraints(6,27));
84          this.add(this.bearingS,new AbsoluteConstraints(35,50));
85         
86          this.add(OK,new AbsoluteConstraints(5,85));
87          this.add(tryW,new AbsoluteConstraints(32,85));
88          this.add(remove,new AbsoluteConstraints(59,85));
89          this.add(cancel,new AbsoluteConstraints(86,85));
90  
91          this.setBackground(Color.darkGray);
92          
93          this.show();
94      }
95      /** Retrieves the dialog so the user can see it.
96       * @param robot the sim. world robot
97       * @param world the SimWorld where the robot exists
98       */    
99      public void updateRotation(SimRCX robot, SimWorld world){
100         this.object = robot;
101         this.world = (BasicSimWorld)world;
102         this.show();
103     }
104     
105     /** Updates the rotation which the user modified for the robot.
106      * @param robot the sim. world robot
107      */    
108     public void updateValue(SimRCX robot){
109         int value = (int)robot.getActualBearingXZ();
110         int div = (int)value/360;
111         value = value-(360*div);
112         if(value <0) value = value+360;
113         bearingS.setValue(new Integer(value));
114     }
115 
116     /**  Action event handler
117      * @param e the action event
118      */
119     public void actionPerformed(ActionEvent e)
120     {
121         if (e.getSource()==OK || e.getSource()==tryW)
122         {
123             try{
124                 int rotation=Integer.parseInt(bearingS.getValue().toString());
125                 this.object.setActualBearingXZ((long)rotation);
126             }
127             catch(Exception e2){}
128             
129             if(e.getSource()==OK){
130                     this.object.setSelected(false);                                      
131                     this.hide();
132             }
133             
134         }
135         else if(e.getSource()==remove){
136             LinkedList newList = this.world.getObjectList();
137             
138             for(int i=0;i<newList.size();i++){
139                 
140                 SimObject simObject = (SimObject)newList.get(i);
141                                
142                 if(simObject instanceof SimRCX){
143                     if(((SimRCX)simObject).equals(this.object)){
144                         newList.remove(this.object);
145                         i=(-1);
146                     }
147                }
148                 
149                 else if(simObject instanceof SimSensor){
150                     
151                     if((((SimSensor)simObject).getOwner()).equals(this.object)){
152                         SimObject a = (SimObject)newList.remove(i);
153                         i=(-1);
154                     }
155                 }
156             }
157             
158             this.world.updateObjectList(newList);
159             this.hide();
160         
161         }
162   
163         else{
164             this.object.setSelected(false);   
165             this.hide();
166         }
167         
168         try{sim.setSize(sim.getWidth(),sim.getHeight());}
169         catch(Exception e1){};
170     }
171 }