1   package main;
2   
3   import intellego.Intellego;
4   import util.*;
5   import interfaces.*;
6   import real.*;
7   import simworldobjects.*;
8   import main.*;
9   
10  import java.awt.*;
11  import java.lang.*;
12  import java.awt.event.*;
13  import javax.swing.*;
14  import java.io.*;
15  
16  
17  /**
18  * Provides a dialog box to ask the user whether or not they want to download the lejos firmware
19  *
20  * @author Simon Zienkiewicz
21  * 
22  */ 
23  public class VerificationPopUp extends JDialog implements ActionListener, WindowFocusListener, WindowListener
24  {
25      private JLabel xLabel;
26      private JButton OK, cancel;
27      private CodeEditor window;
28      private int type;
29      private boolean exitWin = false;
30      
31      
32      /** Displays a dialog box to make the user verify that they want to upload the
33       * firmware onto the real robot.
34       * @param title the title of the diaglog
35       * @param length the lenth of the dialog box
36       * @param width the width of the dialog box
37       * @param message the message in the dialog
38       * @param t the type of dialog
39       * @param win the CodeEditor sending the request
40       */
41      public void createPopUpWindow(String title, int length, int width, String message, int t, CodeEditor win)
42      {
43          this.setDefaultLookAndFeelDecorated(false);
44          this.setUndecorated(true);
45          this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
46                  
47          window = win;
48          type = t;
49          setTitle(title);
50          setSize(length,width);
51          setLocation(400,400);
52          setVisible(true);
53          this.setResizable(false);
54                  
55          Container Panel1=new Container();
56          Panel1.setLayout(new FlowLayout());     
57  
58          Container Panel2=new Container();
59          Panel2.setLayout(new FlowLayout());  
60  
61          xLabel=new JLabel(message);
62          xLabel.setForeground(Color.lightGray);
63  
64          (OK=new JButton("OK")).addActionListener(this);
65          (cancel=new JButton("Cancel")).addActionListener(this);
66          this.addWindowFocusListener(this);
67          this.addWindowListener(this);
68          
69          //set color
70          OK.setBackground(Color.lightGray);
71          OK.setForeground(Color.darkGray);
72          cancel.setBackground(Color.lightGray);
73          cancel.setForeground(Color.darkGray);
74  
75          Panel2.add("South",OK);
76          Panel2.add("South",cancel);
77  
78          Panel1.add("North",xLabel);
79          Panel1.add("South",Panel2);
80          
81          this.getContentPane().setBackground(Color.darkGray);
82          this.getContentPane().add(Panel1);
83          this.show();
84          this.setLocationRelativeTo(window);
85          
86      }
87  
88      /**
89      *  Action event handler - downloads the lejos firmware to the robot
90      *
91      *  @param e the action event
92      */
93      public void actionPerformed(ActionEvent e)
94      {
95          
96          if (e.getSource()==OK)
97          {
98              switch(type){
99              
100                 case 0:  externalCommand("lejosfirmdl",2);
101                          
102                 case 1:  exitWin = true;
103                          this.window.saveFile();
104                          this.window.setVisible(false);
105         
106                         // and change the title
107                         this.window.setTitle("Code Editor:  (no file)");
108                         this.window.dispose();
109                 
110                 
111                 dispose();
112                                 
113             
114             }
115            
116         }      
117         else{
118             if(type ==1){
119                 this.window.setVisible(false);
120         // and change the title
121                 this.window.setTitle("Code Editor:  (no file)");
122                 exitWin = true;
123                 this.window.dispose();
124             }
125             
126             exitWin = true;
127             dispose();}
128     }
129             
130     /** Processes all external calls, i.e calls to lejos & lejosc
131      * @param cmd the command
132      * @param num the type of external command
133      */    
134     private void externalCommand(String cmd, int num)
135     {
136         int len;
137         byte buffer[] = new byte[1000];
138         Intellego.addToLog("CodeEditor.externalCommand(): Processing External Command: "+cmd);
139     try
140     {
141             Process external=Runtime.getRuntime().exec(cmd);
142             InputStream ees = external.getErrorStream();
143             try 
144             {
145                 ExternalMessager output=MainInterface.createExternalMessagerFrame(num);
146                 while ((len = ees.read(buffer)) != -1)
147         {
148                     String eo = new String (buffer, 0, len);
149                     output.append(eo);            
150                 }
151                 external.waitFor();
152              }             
153              catch (Exception e) 
154              {
155                 Intellego.addToLog("CodeEditor.externalCommand(): error: "+e.getMessage());
156              }
157     }
158     catch (Exception e) 
159     {
160             Intellego.addToLog("CodeEditor.externalCommand(): error: "+e.getMessage());
161             
162         }   
163     }
164      
165     public void windowGainedFocus(WindowEvent e) {
166     }
167     
168     public void windowLostFocus(WindowEvent e) {
169         if(exitWin == false){
170             //sound
171             this.getToolkit().beep();
172             this.show();
173         }
174         
175     }
176     
177     public void windowActivated(WindowEvent e) {
178     }
179     
180     public void windowClosed(WindowEvent e) {
181     }
182     
183     public void windowClosing(WindowEvent e) {
184          exitWin = true;
185          dispose();
186     }
187     
188     public void windowDeactivated(WindowEvent e) {
189     }
190     
191     public void windowDeiconified(WindowEvent e) {
192     }
193     
194     public void windowIconified(WindowEvent e) {
195     }
196     
197     public void windowOpened(WindowEvent e) {
198     }
199     
200 }
201