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 javax.swing.plaf.basic.*;
14  import java.util.*;
15  import java.awt.event.*;
16  import javax.swing.*;
17  import java.io.*;
18  
19  /**
20  * Provides a dialog box to get color selection parameters from the user
21  * @author Simon Zienkiewicz
22  * 
23  */ 
24  public class ColorOptionDialog extends JPanel implements ActionListener
25  {
26      private JLabel colorL,rL,bL,gL, previewL, title;
27      private JButton OK, cancel, tryW;
28      private JTextField rT, bT, gT;
29      private JTextArea preview;
30      private JComboBox colorSelection;
31      private Color desiredColor;
32      private String[] availableColors = {"Black","Blue","Cyan","Gray","Green","Magenta","Orange","Pink","Red","White","Yellow"};
33      private int index;
34      private SimDisplay display;
35      private SimWorld  world;
36      private String objectName;
37      private SimGround simg;
38      private GridDisplay grid;
39      private SimUI sim;
40      
41      /** Used to change the colours of objects located within the Simulation environment.
42       * Note that one ColorOptionDialog is created for the entire Simulation Window.
43       * @param sim the SIMUI requesting a ColorOptionDialog
44       */
45      public ColorOptionDialog(SimUI sim){
46          this.sim = sim;
47          this.setBackground(Color.darkGray);
48      }
49      
50      /** Creates and displays the Popup dialog witht the Simulation environment */    
51      public void createColorPopUp()
52      {
53          setSize(150,245);
54          setLocation(400,400);
55          setVisible(true);
56          this.setBorder(BasicBorders.getTextFieldBorder());
57          this.setLayout(new AbsoluteLayout());
58  
59          title = new JLabel("Color Editor:");
60          previewL =new JLabel("Preview:");
61          colorL=new JLabel("Select Color:");
62          rL=new JLabel("R:");
63          bL=new JLabel("B:");
64          gL=new JLabel("G:");
65          //set colors
66          previewL.setForeground(Color.lightGray);
67          colorL.setForeground(Color.lightGray);
68          title.setForeground(Color.yellow);
69          rL.setForeground(Color.red);
70          bL.setForeground(Color.blue);
71          gL.setForeground(Color.green);
72                  
73          preview = new JTextArea(3,5);
74          preview.setSize(20,20);
75          preview.setBackground(Color.white);
76          preview.setEditable(false);
77          
78          rT=new JTextField(3);
79          bT=new JTextField(3);
80          gT=new JTextField(3);
81          rT.setEditable(false);
82          bT.setEditable(false);
83          gT.setEditable(false);
84          //set color
85          rT.setBackground(Color.yellow);
86          rT.setForeground(Color.darkGray);
87          bT.setBackground(Color.yellow);
88          bT.setForeground(Color.darkGray);
89          gT.setBackground(Color.yellow);
90          gT.setForeground(Color.darkGray);
91   
92          colorSelection = new JComboBox(availableColors);
93          colorSelection.addActionListener(this);
94          colorSelection.setSelectedIndex(9);
95          //set color
96          colorSelection.setBackground(Color.yellow);
97          colorSelection.setForeground(Color.darkGray);
98    
99          (OK=new JButton("OK")).addActionListener(this);
100         (tryW=new JButton("Try")).addActionListener(this);
101         (cancel=new JButton("Cancel")).addActionListener(this);
102         //set color
103         OK.setMargin(new Insets(1,1,1,1));
104         tryW.setMargin(new Insets(1,1,1,1));
105         cancel.setMargin(new Insets(1,1,1,1));
106         
107         OK.setBackground(Color.yellow);
108         OK.setForeground(Color.darkGray);
109         tryW.setBackground(Color.yellow);
110         tryW.setForeground(Color.darkGray);
111         cancel.setBackground(Color.yellow);
112         cancel.setForeground(Color.darkGray);
113 
114         this.add(title,new AbsoluteConstraints(6,7));
115         this.add(colorL,new AbsoluteConstraints(6,30));
116         this.add(colorSelection,new AbsoluteConstraints(50,50));
117         this.add(previewL,new AbsoluteConstraints(6,80));
118         this.add(preview,new AbsoluteConstraints(60,100));
119 
120         this.add(rL,new AbsoluteConstraints(25,160));
121         this.add(rT,new AbsoluteConstraints(15,180)); 
122         this.add(gL,new AbsoluteConstraints(65,160));
123         this.add(gT,new AbsoluteConstraints(55,180)); 
124         this.add(bL,new AbsoluteConstraints(105,160));
125         this.add(bT,new AbsoluteConstraints(95,180)); 
126         
127         this.add(OK,new AbsoluteConstraints(15,210)); 
128         this.add(tryW,new AbsoluteConstraints(45,210)); 
129         this.add(cancel,new AbsoluteConstraints(75,210)); 
130       
131         this.setBackground(Color.darkGray);
132         this.show();
133     }
134     
135     /** A method used to subsequantly call the ColorOptionDialog object in order to
136      * change the colour properties of a Simulation environment object.
137      * @param name the title of the object displayed in the Dialog
138      * @param index a number associated with the type of sim. world object
139      * @param d the SimDisplay object refrenced to the sim. world object
140      * @param w the SimWorld in which the object is in
141      * @param objectName the type of object
142      * @param simg the name of the SimGround object, if referenced object is NOT of that type, NULL
143      * is passed
144      * @param grid the name of the GridDisplay object, if referenced object is NOT of that type, NULL
145      * is passed
146      */    
147     public void updateColorPopUp(String name, int index, SimDisplay d, SimWorld w, String objectName, SimGround simg, GridDisplay grid){
148                
149         title.setText(name);
150         this.index = index;
151         this.display = d;
152         this.world = w;
153         this.objectName=objectName;
154         this.simg = simg;
155         this.grid = grid;
156                 
157         if(simg != null)deselectOtherObjects();
158         this.show();
159      }
160 
161     /** This method deselects all the other objects within the simulation environment,
162      * ensuring that only the current object is selected.
163      */    
164     private void deselectOtherObjects(){
165         
166         LinkedList newList = world.getObjectList();
167         
168         for(int i=0;i<newList.size();i++){
169             SimObject simObject2 = (SimObject)newList.get(i);
170             if(!this.simg.equals(simObject2)){
171                 if(simObject2 instanceof SimGround){
172                     ((SimGround)simObject2).setSelected(false);
173                     ((SimGround)simObject2).setOutlineColor(Color.black);
174                     display.repaint();
175                 }
176             }
177         }
178     }
179     
180     /**
181     *  Action event handler - sets up color code according to user selection
182     *
183     *  @param e the action event
184     */
185     public void actionPerformed(ActionEvent e)
186     {
187         //System.out.println("BEFORE ACTION EVENT:"+ this.isSelected());
188         if (e.getSource()==OK || e.getSource()==tryW)
189         {
190            switch(index){
191                case 0:  ((SimpleDisplay)display).setColorLibrary(this.objectName,this.desiredColor);
192                         if(this.objectName.equals("road")){
193                             for(int i=0;i<world.getObjectList().size();i++)
194                             {
195                                 SimObject o=(SimObject)world.getObjectList().get(i);
196                                 if(o instanceof SimRoad)((SimRoad)o).setColor(this.desiredColor);
197 
198                             }
199                         }
200                
201                         break;
202                
203                case 1: ((BasicSimWorld)world).setWorldColor(this.desiredColor);
204                         grid.updateWorldColor(this.desiredColor);
205                         grid.repaint();
206                         break;
207                
208                case 2:  simg.setColor(this.desiredColor);
209                         if(e.getSource()==OK){
210                             simg.setOutlineColor(Color.black);
211                             simg.setSelected(false);
212                         }
213                         
214                         //undo/redo purposes ***************
215                         sim.pushUndoItem(sim.copyList((LinkedList)world.getObjectList()));
216                
217                         break;
218                
219                case 6:  grid.updateGridColor(this.desiredColor);
220                                              
221            }
222            display.repaint();
223            
224            if(e.getSource()==OK )this.hide();
225 
226         }
227         //action handler for the JComboBox of colors
228         else if(e.getSource()==colorSelection){
229             Color color= Color.white;
230             switch(colorSelection.getSelectedIndex()){
231 
232                 case 0: color = Color.black;
233                         break;
234                 case 1: color = Color.blue;
235                         break;
236                 case 2: color = Color.cyan;
237                         break;
238                 case 3: color = Color.gray;
239                         break;
240                 case 4: color = Color.green;
241                         break;
242                 case 5: color = Color.magenta;
243                         break;
244                 case 6: color = Color.orange;
245                         break;
246                 case 7: color = Color.pink;
247                         break;
248                 case 8: color = Color.red;
249                         break;
250                 case 9: color = Color.white;
251                         break;
252                 case 10: color = Color.yellow;
253                          break;
254             }
255             //generates a preview of the selected color
256             this.preview.setBackground(color);
257             
258             //gets and sets the RBG values or the selected color
259             this.rT.setText(""+color.getRed());
260             this.bT.setText(""+color.getBlue());
261             this.gT.setText(""+color.getGreen());
262             
263             //sets the class variable
264             this.desiredColor = color;
265             
266         }
267 
268         else
269         {
270            if(index == 2){
271             simg.setSelected(false);
272             simg.setOutlineColor(Color.black);
273             display.repaint();
274            }
275            this.hide();
276         }
277         try{sim.setSize(sim.getWidth(),sim.getHeight());}
278         catch(Exception e1){};
279     }
280 }