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  
18  /** Provides a dialog box to allow the user to set the their perferred setting for
19   * the grid display option, within the simulation environment.
20   * @author Simon Zienkiewicz
21   */ 
22  public class GridOptionDialog extends JPanel implements ActionListener
23  {
24      private JLabel gridDetailL, pixelL, title;
25      private JButton OK, cancel, tryW;
26      private JComboBox dimensionSelection, pixelD;
27      private Color desiredColor;
28      private String[] availableDimensions = {"NONE","25x25","50x50","100x100",};
29      private String[] availablePixelSizes = {"1","2","4","6"};
30      private SimDisplay display;
31      private SimWorld  world;
32      private String objectName;
33      private SimGround simg;
34      private GridDisplay grid;
35      private int gridSize;
36      private int pixelSize=4;
37      private boolean noGrid=true;
38      private SimUI simulator;
39          
40      /** Creates a GridOptionDialog object. */
41  
42      public GridOptionDialog(){
43      
44      }
45      
46      /** Creates and displays a grid popup menu.
47       * @param name the title of the popup menu
48       * @param grid the GridDisplay object associated with the popup menu
49       * @param display the SimDisplay object associated with the popup menu
50       * @param simulator the SimUI, or simulation environment to which this grid will be applied to
51       */    
52      public void createGridPopUp(String name, GridDisplay grid, SimDisplay display, SimUI simulator)
53      {
54          //setup variables
55          this.grid = grid;
56          this.display = display;
57          this.simulator = simulator;
58  
59          //sets the properties of the popup window
60          //setTitle(name);
61          setSize(210,165);
62          setLocation(400,400);
63          setVisible(true);
64          this.setBorder(BasicBorders.getTextFieldBorder());
65          this.setLayout(new AbsoluteLayout());
66        
67          this.title = new JLabel(name);
68          this.gridDetailL = new JLabel("Grid Spacing:");
69          gridDetailL.setForeground(Color.lightGray);
70          this.pixelL = new JLabel("Pixel Diameter:");
71          pixelL.setForeground(Color.lightGray);
72          title.setForeground(Color.yellow);
73          
74          this.dimensionSelection = new JComboBox(this.availableDimensions);
75          this.dimensionSelection.setSelectedIndex(0);
76          this.dimensionSelection.addActionListener(this);
77          this.pixelD = new JComboBox(this.availablePixelSizes);
78          this.pixelD.setSelectedIndex(2);
79          this.pixelD.addActionListener(this);
80          //set color
81          dimensionSelection.setBackground(Color.yellow);
82          dimensionSelection.setForeground(Color.darkGray);
83          pixelD.setBackground(Color.yellow);
84          pixelD.setForeground(Color.darkGray);
85          
86          (OK=new JButton("OK")).addActionListener(this);
87          (tryW=new JButton("Try")).addActionListener(this);
88          (cancel=new JButton("Cancel")).addActionListener(this);
89          OK.setMargin(new Insets(1,1,1,1));
90          tryW.setMargin(new Insets(1,1,1,1));
91          cancel.setMargin(new Insets(1,1,1,1));
92          
93          //set color
94          OK.setBackground(Color.yellow);
95          OK.setForeground(Color.darkGray);
96          tryW.setBackground(Color.yellow);
97          tryW.setForeground(Color.darkGray);
98          cancel.setBackground(Color.yellow);
99          cancel.setForeground(Color.darkGray);
100 
101         
102         this.add(this.title,new AbsoluteConstraints(6,7));
103         this.add(this.gridDetailL,new AbsoluteConstraints(6,27));
104         this.add(this.dimensionSelection,new AbsoluteConstraints(35,50));
105         this.add(this.pixelL,new AbsoluteConstraints(6,90));
106         this.add(this.pixelD,new AbsoluteConstraints(95,87));
107         
108         this.add(OK,new AbsoluteConstraints(15,125)); 
109         this.add(tryW,new AbsoluteConstraints(45,125)); 
110         this.add(cancel,new AbsoluteConstraints(75,125)); 
111 
112         this.setBackground(Color.darkGray);
113         
114         this.show();
115     }
116 
117     /**
118     *  Action event handler - sets up color code according to user selection
119     *
120     *  @param e the action event
121     */
122     public void actionPerformed(ActionEvent e)
123     {
124         if (e.getSource()==OK || e.getSource()==tryW)
125         {
126             this.simulator.setGrid(!noGrid);
127            
128             if(!noGrid){
129                 grid.setWantGrid(true);
130                 grid.updateGridSize(this.gridSize);
131                 grid.updateGridDotSize(this.pixelSize);
132             }
133             display.repaint();
134             if(e.getSource()==OK)this.hide();
135         }
136         //action handler for the JComboBox of colors
137         else if(e.getSource()==dimensionSelection){
138             switch(this.dimensionSelection.getSelectedIndex()){
139                 case 0:     this.noGrid = true;
140                             break;
141                 case 1:     this.gridSize = 25;
142                             this.noGrid = false;
143                             break;
144                 case 2:     this.gridSize = 50;
145                             this.noGrid = false;
146                             break;
147                 case 3:     this.gridSize = 100;
148                             this.noGrid = false;
149                             break;
150             }
151         }
152         
153         else if(e.getSource()==pixelD){
154             switch(this.pixelD.getSelectedIndex()){
155                 case 0:     this.pixelSize = 1;
156                             break;
157                 case 1:     this.pixelSize = 2;
158                             break;
159                 case 2:     this.pixelSize = 4;
160                             break;
161                 case 3:     this.pixelSize = 6;
162                             break;
163             }
164        }
165   
166        else this.hide();
167         
168        try{simulator.setSize(simulator.getWidth(),simulator.getHeight());}
169        catch(Exception e1){};
170     }
171 }