1   package main;
2   
3   import intellego.Intellego;
4   import util.*;
5   import interfaces.*;
6   import real.*;
7   import NetBeansResources.*;
8   
9   import java.awt.*;
10  import java.lang.*;
11  import java.awt.event.*;
12  import javax.swing.*;
13  import java.io.*;
14  import javax.imageio.*;
15  import java.awt.image.*;
16  
17  /** The main GUI.
18   * @author Graham Ritchie
19   * @modifyer Simon Zienkiewcz
20   */
21  public class MainInterface extends JFrame
22  {
23      /** main desktop pane */    
24      private static JDesktopPane mainPane;
25      /** the simulator frame */    
26      private static SimUI frame = null;
27      /** the help dialog frame */    
28      private HelpDialog helpDialog=null;
29      /** the about dialog */    
30      private About about = null;
31      /**
32      * Sets up the JDesktopPane
33      */
34      public MainInterface()
35      {
36          super("Intellego 2.0");
37  
38          // Make the main window indented 50 pixels from each edge of the screen.
39          int inset = 50;
40          
41          Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
42          setBounds(inset, inset, screenSize.width - inset*2, screenSize.height-inset*2);
43  
44          //load icon image from a file
45          try{ 
46                  BufferedImage simIcon= ImageIO.read(new File("images\\lego.png"));
47                  this.setIconImage(simIcon);
48          }
49          catch(Exception e){}
50  
51          // Quit the whole program when the main window closes.
52          addWindowListener(new WindowAdapter() 
53          {
54              public void windowClosing(WindowEvent e) 
55              {
56                  Intellego.addToLog("MainInterface.init(): System quitting");
57                  System.exit(0);
58              }
59          });
60  
61          // set up the main pane
62          mainPane=new JDesktopPane();
63  
64          setContentPane(mainPane);
65          mainPane.setBackground(Color.darkGray);
66          setJMenuBar(createMenuBar());
67          
68          this.setDefaultLookAndFeelDecorated(false);
69          this.setUndecorated(true);
70          this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
71  
72      }
73  
74      /**
75      * Creates a new code editor window
76      */
77      public static void createCodeEditorFrame() 
78      {
79          
80          CodeEditor frame = new CodeEditor();
81          frame.setVisible(true);
82          mainPane.add(frame);
83          
84          try 
85          {
86              frame.setSelected(true);
87          }
88          catch (Exception e) 
89          {
90              Intellego.addToLog("MainInterface.createFrame(): Failed to create internal code editor frame properly: "+e);
91          }
92      }
93  
94      /**
95      * Creates a new simulator window
96      */
97      public void createSimulatorFrame() 
98      {
99          if(frame == null || !frame.isVisible()){
100             frame = new SimUI();
101             frame.setVisible(true);
102             mainPane.add(frame);
103             try 
104             {
105                 frame.setSelected(true);
106             }
107             catch (Exception e) 
108             {
109                 Intellego.addToLog("MainInterface.createFrame(): Failed to create internal simulator frame properly: "+e);
110             }
111         }
112         else displayMessage("ERROR: more than one active SIMULATOR WINDOW | ONLY ONE is allowed");
113     }
114     
115      /** Creates a new simulator window with the specified controller preloaded
116      * @param file the controller file wanting to be opened in the simulator window
117      */
118     public static void createSimulatorFrame(File file)//String className) 
119     {
120         // prompt the user to open a new simworld and then open 
121         // the controller in that simworld
122         if(frame == null || !frame.isShowing()){
123             frame = new SimUI(file);
124             frame.setVisible(true);
125             mainPane.add(frame);
126             try 
127             {
128                 frame.setSelected(true);
129             }
130             catch (Exception e) 
131             {
132                 Intellego.addToLog("MainInterface.createFrame(): Failed to create internal simulator frame properly: "+e);
133             }
134         }
135         
136         // open the controller fine in the simworld that is 
137         // opened in the simulator window
138         else 
139         {
140             
141             frame.openCurrentController(file);
142             
143             
144             
145         }
146         //displayMessage("ERROR: more than one active SIMULATOR WINDOW | ONLY ONE is allowed");
147     }
148 
149     /** Creates a new window to display messages from external processes
150      * @return the ExternalMessager frame
151      * @param num the index, indicating which type of external messenger is being used
152      */
153     public static ExternalMessager createExternalMessagerFrame(int num) 
154     {
155         ExternalMessager frame = new ExternalMessager(num);
156         frame.setVisible(true);
157         mainPane.add(frame);
158         try 
159         {
160             frame.setSelected(true);
161         }
162         catch (Exception e) 
163         {
164             Intellego.addToLog("MainInterface.createFrame(): Failed to create internal frame properly: "+e);
165         }
166         return frame;
167     }
168 
169     /**
170     * Creates the menu bar for the main desktop pane
171     *
172     * @return the menu bar
173     */
174     private JMenuBar createMenuBar() 
175     {
176         JMenuBar menuBar = new JMenuBar();
177         JMenu toolsMenu = new JMenu("Tools");
178         toolsMenu.setMnemonic(KeyEvent.VK_T);
179 
180         JMenu help = new JMenu("Help");
181         help.setMnemonic(KeyEvent.VK_H);
182         
183         JMenuItem codeEditor = new JMenuItem(" Code Editor",new ImageIcon("images/code.gif"));
184         codeEditor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.ALT_MASK));
185         JMenuItem simulator = new JMenuItem(" Simulator",new ImageIcon("images/execute.gif"));
186         simulator.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,ActionEvent.ALT_MASK));
187         
188         JMenuItem helpI = new JMenuItem(" Help",new ImageIcon("images/help.gif"));
189         helpI.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));
190         JMenuItem aboutI = new JMenuItem("About Intellego 2.0");
191                         
192         //set colors
193         toolsMenu.setBackground(Color.darkGray);
194         toolsMenu.setForeground(Color.lightGray);
195         help.setBackground(Color.darkGray);
196         help.setForeground(Color.lightGray);
197         helpI.setBackground(Color.darkGray);
198         helpI.setForeground(Color.lightGray);
199         aboutI.setBackground(Color.darkGray);
200         aboutI.setForeground(Color.lightGray);
201         codeEditor.setBackground(Color.darkGray);
202         codeEditor.setForeground(Color.red.darker());
203         simulator.setBackground(Color.darkGray);
204         simulator.setForeground(Color.yellow);
205 
206         helpI.addActionListener(new ActionListener() 
207         {
208             public void actionPerformed(ActionEvent e) 
209             {
210                 if(helpDialog == null){
211                     //create the help JFrame
212                     helpDialog = new HelpDialog();
213                 }
214                 
215                 else{
216                     helpDialog.setVisible(true);
217                 }
218             }
219         });
220         aboutI.addActionListener(new ActionListener() 
221         {
222             public void actionPerformed(ActionEvent e) 
223             {
224                 if(about == null){
225                     //create the about JFrame
226                     about = new About();
227                 }
228                 
229                 else{
230                     about.setVisible(true);
231                 }
232             }
233         });
234         codeEditor.addActionListener(new ActionListener() 
235         {
236             public void actionPerformed(ActionEvent e) 
237             {
238                 createCodeEditorFrame();
239             }
240         });
241 
242         simulator.addActionListener(new ActionListener() 
243         {
244             public void actionPerformed(ActionEvent e) 
245             {
246                 createSimulatorFrame();
247             }
248         });
249 
250         toolsMenu.add(codeEditor);
251         toolsMenu.addSeparator();
252         toolsMenu.add(simulator);
253         
254         help.add(helpI);
255         help.addSeparator();
256         help.add(aboutI);
257         
258         menuBar.setLayout(new AbsoluteLayout());
259         menuBar.setBackground(Color.DARK_GRAY);
260         menuBar.add(toolsMenu,new AbsoluteConstraints(3,0));
261         menuBar.add(help,new AbsoluteConstraints(50,0));
262         
263         //strictly used to extend the menu bar to proper height
264         JButton sizeButton = new JButton("hi");
265         sizeButton.setSize(new Dimension(20,10));
266         sizeButton.setMargin(new Insets(0,0,0,0));
267         sizeButton.setVisible(false);
268         menuBar.add(sizeButton,new AbsoluteConstraints(263,0));    
269         
270         return menuBar;
271     }
272 
273     /**
274     * Displays messages to the user in a dialog box
275     *
276     * @param message the message to be displayed to the user
277     */
278     public static void displayMessage(String message)
279     {
280             // pop up a dialog box with the message
281             IntellegoDialog dialog=new IntellegoDialog(message);
282 
283     }
284 }
285