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 objects in the
20   * simulation environment.
21   * @author Simon Zienkiewicz
22   */ 
23  public class ObjectEditDialog extends JPanel implements ActionListener
24  {
25      private JLabel pointL,xL,yL,zL, sizeL, widthL, lengthL, orientationL, bearingL, heightL, title;
26      private JButton OK, cancel, tryW, delW,clone;
27      private JSpinner xS,yS,zS,widthS,lengthS,bearingS,heightS;
28      private String shapeNames[] = {"robot","light","sensorTouch","sensorLight","wall","ground"};
29      private int index;
30      private SimDisplay display;
31      private BasicSimObject simObject;
32      private BasicSimWorld simWorld;
33      private int objectIndex;
34      private SimUI sim;
35    
36      /** Displays a dialog box for the user to interact with the simulation envirionment objects.
37       * @param sim the simulator window
38       */
39  
40      public ObjectEditDialog(SimUI sim){
41          this.sim = sim;
42          this.setBackground(Color.darkGray);
43      }
44      
45      /** Creates the actual popup dialog. */    
46      public void createObjectEditorPopUp()
47      {
48          setSize(355,225);
49          setLocation(400,400);
50          setVisible(true);
51          this.setBorder(BasicBorders.getTextFieldBorder());
52          this.setLayout(new AbsoluteLayout());
53        
54          this.title = new JLabel("Object Editor:");
55          this.pointL = new JLabel("Center Point:");
56          this.xL = new JLabel("X:");
57          this.yL = new JLabel("Y:");
58          this.zL = new JLabel("Z:");
59          this.sizeL = new JLabel("Dimensions:");
60          this.widthL = new JLabel("Width:");
61          this.heightL = new JLabel("Height:");
62          this.lengthL = new JLabel("Length:");
63          this.orientationL = new JLabel("Orientation:");
64          this.bearingL = new JLabel("XZ Rotation:");
65          //set colors
66          title.setForeground(Color.yellow);
67          pointL.setForeground(Color.lightGray);
68          xL.setForeground(Color.yellow);
69          yL.setForeground(Color.yellow);
70          zL.setForeground(Color.yellow);
71          sizeL.setForeground(Color.lightGray);
72          widthL.setForeground(Color.yellow);
73          heightL.setForeground(Color.yellow);
74          lengthL.setForeground(Color.yellow);
75          orientationL.setForeground(Color.lightGray);
76          bearingL.setForeground(Color.yellow);
77          
78          this.xS = new JSpinner(new SpinnerNumberModel(0,0,1000,1));
79          this.xS.setBackground(Color.yellow);
80          this.yS = new JSpinner(new SpinnerNumberModel(0,0,1000,1));
81          this.yS.setBackground(Color.yellow);
82          this.yS.setEnabled(false);
83          this.zS = new JSpinner(new SpinnerNumberModel(0,0,1000,1));
84          this.zS.setBackground(Color.yellow);
85                         
86          this.widthS = new JSpinner(new SpinnerNumberModel(10,10,500,1));
87          this.heightS = new JSpinner(new SpinnerNumberModel(0,0,500,1));
88          this.heightS.setEnabled(false);
89          this.lengthS = new JSpinner(new SpinnerNumberModel(10,10,500,1));
90          //set colors
91          widthS.setBackground(Color.yellow);
92          heightS.setBackground(Color.yellow);
93          lengthS.setBackground(Color.yellow);
94          
95          this.bearingS = new JSpinner(new SpinnerNumberModel(0,0,360,1));
96          //this.bearingS.setEnabled(false);
97          bearingS.setBackground(Color.yellow);
98                          
99          (OK=new JButton("OK")).addActionListener(this);
100         (tryW=new JButton("Try")).addActionListener(this);
101         (delW=new JButton("Del")).addActionListener(this);
102         delW.setMnemonic('d');
103                 
104         (cancel=new JButton("Cancel")).addActionListener(this); 
105         (clone=new JButton()).addActionListener(this); 
106         
107         ImageIcon copyI = new ImageIcon("images\\copy.gif");
108         Image image3 = (copyI.getImage().getScaledInstance(18,18,0));
109         clone.setIcon(new ImageIcon(image3));
110         clone.setMnemonic('c');
111                 
112         clone.setToolTipText("clone current object");
113         delW.setToolTipText("delete current object");
114         
115         OK.setMargin(new Insets(1,1,1,1));
116         tryW.setMargin(new Insets(1,1,1,1));
117         delW.setMargin(new Insets(1,1,1,1));
118         cancel.setMargin(new Insets(1,1,1,1));
119         clone.setMargin(new Insets(1,1,1,1));
120         
121         OK.setBackground(Color.yellow);
122         OK.setForeground(Color.darkGray);
123         tryW.setBackground(Color.yellow);
124         tryW.setForeground(Color.darkGray);
125         delW.setBackground(Color.red);
126         delW.setForeground(Color.darkGray);
127         cancel.setBackground(Color.yellow);
128         cancel.setForeground(Color.darkGray);
129         clone.setBackground(Color.red);
130         clone.setForeground(Color.darkGray);
131                 
132         this.add(this.title,new AbsoluteConstraints(6,7));
133         this.add(this.pointL,new AbsoluteConstraints(6,30));
134         
135         this.add(this.xL,new AbsoluteConstraints(35,57));
136         this.add(this.xS,new AbsoluteConstraints(50,53));
137         this.add(this.yL,new AbsoluteConstraints(35,87));
138         this.add(this.yS,new AbsoluteConstraints(50,83));
139         this.add(this.zL,new AbsoluteConstraints(35,117));
140         this.add(this.zS,new AbsoluteConstraints(50,113));
141         
142         this.add(this.sizeL,new AbsoluteConstraints(6,143)); 
143         this.add(this.widthL,new AbsoluteConstraints(20,170)); 
144         this.add(this.widthS,new AbsoluteConstraints(70,166));
145         this.add(this.heightL,new AbsoluteConstraints(20,200));
146         this.add(this.heightS,new AbsoluteConstraints(70,196));
147         this.add(this.lengthL,new AbsoluteConstraints(20,230));
148         this.add(this.lengthS,new AbsoluteConstraints(70,226));
149         
150         this.add(orientationL,new AbsoluteConstraints(6,256));
151         this.add(this.bearingL,new AbsoluteConstraints(20,282));
152         this.add(this.bearingS,new AbsoluteConstraints(70,310));
153                 
154         this.add(OK,new AbsoluteConstraints(5,345));
155         this.add(tryW,new AbsoluteConstraints(32,345));
156         this.add(delW,new AbsoluteConstraints(59,345));
157         this.add(cancel,new AbsoluteConstraints(86,345));
158         this.add(clone,new AbsoluteConstraints(105,5));
159        
160         this.setBackground(Color.DARK_GRAY);
161      
162         this.show();
163     }
164     /** Updates the object after the user makes changes in the dialog window.
165      * @param simObject the object that was modified
166      * @param d the SimDisplay object associated with the modified object
167      * @param objectIndex the index of the object within the object list
168      * @param world the SimWorld which contains the object
169      */    
170     public void updateObjectPopUp(SimObject simObject, SimDisplay d, int objectIndex, SimWorld world){
171          //setup variables
172         this.display = d;
173         this.simObject = (BasicSimObject)simObject;
174         this.simWorld = (BasicSimWorld)world;
175         this.objectIndex = objectIndex;
176         
177         this.title.setText("Editing: " + ((this.simObject.getType()).substring(0,1)).toUpperCase()+(this.simObject.getType()).substring(1)+" "+ objectIndex);
178         
179         ((SpinnerNumberModel)(xS.getModel())).setValue(new Integer((int)this.simObject.getXCoord()));
180         ((SpinnerNumberModel)(xS.getModel())).setMaximum(new Integer(simWorld.getWorldDimensions()[0]));     
181         
182         ((SpinnerNumberModel)(yS.getModel())).setValue(new Integer((int)this.simObject.getYCoord()));
183         
184         ((SpinnerNumberModel)(zS.getModel())).setValue(new Integer((int)this.simObject.getZCoord()));
185         ((SpinnerNumberModel)(zS.getModel())).setMaximum(new Integer(simWorld.getWorldDimensions()[2]));     
186         
187         ((SpinnerNumberModel)(heightS.getModel())).setValue(new Integer((int)this.simObject.getHeight()));
188         ((SpinnerNumberModel)(lengthS.getModel())).setValue(new Integer((int)this.simObject.getLength()));
189         ((SpinnerNumberModel)(lengthS.getModel())).setMaximum(new Integer(simWorld.getWorldDimensions()[2]/2));
190         ((SpinnerNumberModel)(widthS.getModel())).setValue(new Integer((int)this.simObject.getWidth()));
191         ((SpinnerNumberModel)(widthS.getModel())).setMaximum(new Integer(simWorld.getWorldDimensions()[0]/2));     
192         
193         ((SpinnerNumberModel)(bearingS.getModel())).setValue(new Integer((int)this.simObject.getActualBearingXZ()));
194         
195         deselectOtherObjects();
196         this.show();
197      }
198 
199     /** Deselects all other objects. */    
200     private void deselectOtherObjects(){
201         
202         LinkedList newList = this.simWorld.getObjectList();
203         
204         for(int i=0;i<newList.size();i++){
205             SimObject simObject2 = (SimObject)newList.get(i);
206             if(!this.simObject.equals(simObject2)){
207                 if(simObject2 instanceof SimGround){
208                     ((SimGround)simObject2).setSelected(false);
209                     ((SimGround)simObject2).setOutlineColor(Color.black);
210                     display.repaint();
211                 }
212                 else if(simObject2 instanceof SimWall){
213                     ((SimWall)simObject2).setSelected(false);
214                     ((SimWall)simObject2).setOutlineColor(Color.black);
215                     display.repaint();
216                 }
217             }
218         }
219     }
220     /** Adds a new object to the simulation world
221      * @param type the type of object, either wall or ground
222      */    
223      private void addObjectToWorld(String type){
224             if(type.equals("wall")){
225                 SimWall newWall = new SimWall(this.simObject.getXCoord(),simObject.getYCoord(),simObject.getZCoord(),simObject.getActualBearingXZ(),simObject.getWidth(),simObject.getLength());
226                 simWorld.addObjecttoFront(newWall);
227                 
228             }
229             else{
230                 SimGround newGround = new SimGround(this.simObject.getXCoord(),simObject.getYCoord(),simObject.getZCoord(),simObject.getActualBearingXZ(),simObject.getWidth(),simObject.getLength(),((SimGround)simObject).getColor());
231                 simWorld.addObjecttoFront(newGround);
232             }
233            display.repaint();
234         }
235        
236     
237     /**
238     *  Action event handler - sets up color code according to user selection
239     *
240     *  @param e the action event
241     */
242     public void actionPerformed(ActionEvent e)
243     {
244         if (e.getSource()==OK || e.getSource()==tryW)
245         {
246             try{
247                 int x=Integer.parseInt(xS.getValue().toString());
248                 int z=Integer.parseInt(zS.getValue().toString());
249                 int width=Integer.parseInt(widthS.getValue().toString());
250                 int length=Integer.parseInt(lengthS.getValue().toString());
251                 int rotation=Integer.parseInt(bearingS.getValue().toString());
252 
253                 this.simObject.setXCoord((long)x);
254                 this.simObject.setZCoord((long)z);
255                 this.simObject.updateWidth((long)width);
256                 this.simObject.updateLength((long)length);
257                 this.simObject.setActualBearingXZ((long)rotation);
258                                 
259                 display.repaint();
260     
261                 //only dispose if ok was clicked
262                 if(e.getSource()==OK){
263                     if(this.simObject instanceof SimGround){
264                         ((SimGround)this.simObject).setSelected(false);
265                         ((SimGround)this.simObject).setOutlineColor(Color.black);
266                     }
267                     else{
268                         ((SimWall)this.simObject).setSelected(false);
269                         ((SimWall)this.simObject).setOutlineColor(Color.black);
270                     }
271                                         
272                     this.hide();
273                 }
274                 
275                 //undo/redo purposes ********************************************************************************
276                 sim.pushUndoItem(sim.copyList((LinkedList)simWorld.getObjectList()));
277          
278             }
279             catch(Exception a){
280                 main.MainInterface.displayMessage("ERROR: Incorrect Input Type | FIX: Please try again, only entering INTEGERs");
281                 if(this.simObject instanceof SimGround){
282                         ((SimGround)this.simObject).setSelected(false);
283                         ((SimGround)this.simObject).setOutlineColor(Color.black);
284                 }
285                 else{
286                     ((SimWall)this.simObject).setSelected(false);
287                     ((SimWall)this.simObject).setOutlineColor(Color.black);
288                 }
289 
290                this.hide();
291             }
292             
293         }
294         else if(e.getSource()==delW){
295            
296             LinkedList newList = this.simWorld.getObjectList();
297             newList.remove(this.objectIndex);
298             this.simWorld.updateObjectList(newList);
299             display.repaint();
300             this.hide();
301             
302             //undo/redo purposes ********************************************************************************
303             sim.pushUndoItem(sim.copyList((LinkedList)simWorld.getObjectList()));
304        
305         }
306         else if(e.getSource()==clone){
307             if(simObject instanceof SimWall) addObjectToWorld("wall");
308             else addObjectToWorld("ground");
309             
310             //undo/redo purposes ********************************************************************************
311             sim.pushUndoItem(sim.copyList((LinkedList)simWorld.getObjectList()));
312         
313         }
314         else
315         {
316             if(this.simObject instanceof SimGround){
317                 ((SimGround)this.simObject).setSelected(false);
318                 ((SimGround)this.simObject).setOutlineColor(Color.black);
319             }
320             else{
321                 ((SimWall)this.simObject).setSelected(false);
322                 ((SimWall)this.simObject).setOutlineColor(Color.black);
323             }
324             
325             display.repaint();
326             this.hide(); 
327         }
328         try{sim.setSize(sim.getWidth(),sim.getHeight());}
329         catch(Exception e1){};
330     }
331 }