/** * Title: GPS Simulator * Copyright: Copyright (c) 2002 * Company: University of Oregon Computer Science Dept. * * @author Jason Prideaux * @version 1.0 * */ package gui; import gui.tray.*; import controller.*; import agent.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; import java.util.*; import java.io.*; /* ====================================================================== */ /** This class is a frame for the Simulator. It contains a map which is the * JComponent in which agents are added to and moved around. This frame * also contains menu items, and is the parent for a Tray JDialog object. * */ public class SimulatorFrame extends JFrame { /** The controller which controls the simulation and this frame. */ private Controller controller; /** The JComponent which is an image of a map. */ private ChartComp chart; /** The scroll pane to put the ChartComp into. */ private ScrollPane pane; /** A dialog for an about window. */ private JDialog aboutDialog; /** A dialog for an exit confirmation. */ private JDialog quitDialog; // Menu components. private JMenuBar mnuBar = new JMenuBar(); private JMenu mnuFile = new JMenu(); private JMenu mnuEdit = new JMenu(); private JMenu mnuHelp = new JMenu(); private JMenuItem mnuItmAbout = new JMenuItem(); private JMenuItem mnuItmExit = new JMenuItem(); private JMenuItem mnuItmTray = new JMenuItem(); /* ===================================================================== */ /** This constructor initializes the frame by taking in the controller/owner * of this frame, and the ChartComp to be added to this frame. * * @param controller The simulator controller. * @param chart The map image panel to be displayed in frame. * */ public SimulatorFrame(Controller controller, ChartComp chart){ super("iSIM"); this.chart = chart; this.controller = controller; // Initialize the components of this frame. init(); // Close the JVM on exit. this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } //Constructor /* ===================================================================== */ /** This method sets up the components for the frame, the menus, and the * CompChart to dispaly the map. * */ public void init(){ // Create the menus mnuFile.setMnemonic('F'); mnuFile.setText("File"); mnuFile.setMnemonic(KeyEvent.VK_F); mnuItmExit.setMnemonic('X'); mnuItmExit.setText("Exit"); mnuItmExit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { mnuItmExit_actionPerformed(); } }); mnuEdit.setMnemonic('T'); mnuEdit.setText("Tray"); mnuItmTray.setMnemonic('S'); mnuItmTray.setText("Show Tray"); mnuItmTray.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { mnuItmTray_actionPerformed(e); } }); mnuHelp.setMnemonic('H'); mnuHelp.setText("Help"); mnuItmAbout.setMnemonic('A'); mnuItmAbout.setText("About..."); mnuItmAbout.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { mnuItmAbout_actionPerformed(e); } }); mnuBar.add(mnuFile); mnuBar.add(mnuEdit); mnuBar.add(mnuHelp); mnuFile.add(mnuItmExit); mnuEdit.add(mnuItmTray); mnuHelp.add(mnuItmAbout); // Create the panels JPanel mainpanel = new JPanel(new BorderLayout()); JPanel chartpanel = new JPanel(new BorderLayout()); // Set Borders mainpanel.setBorder(BorderFactory.createEmptyBorder( 10, 5, 5, 5)); chartpanel.setBorder(TTBorder.getBorder("Map")); // Create scroll pane for map pane = new ScrollPane(); pane.add(chart); // Add stuff to panels chartpanel.add(pane, BorderLayout.CENTER); mainpanel.add(chartpanel, BorderLayout.CENTER); // Add menu and mainpanel to this Frame this.getContentPane().add(mnuBar, BorderLayout.NORTH); this.getContentPane().add(mainpanel, BorderLayout.CENTER); // Show it all and set the size this.pack(); this.setSize(900,700); //Set the location to be center Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width - this.getWidth()) / 2; int y = (screen.height - this.getHeight()) / 2; this.setLocation(x,y); //Now show this frame. //this.setVisible(true); } //method: init // ======================================================================== /** This method is called to ask for exit confirmation. * */ public void mnuItmExit_actionPerformed() { //Setup a current/new JDialog. quitDialog = new JDialog(this, "Exit Confirmation"); // Create buttons and the labels. JLabel lbl = new JLabel("Are you sure you want to exit?"); JButton btnAdd = new JButton( " EXIT "); JButton btnCancel = new JButton( "CANCEL"); JPanel btnpanel = new JPanel(new GridBagLayout()); JPanel lblpanel = new JPanel(new BorderLayout()); lblpanel.setBorder( BorderFactory.createEmptyBorder(15, 15, 15, 15) ); btnpanel.add(btnAdd, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0)); btnpanel.add(btnCancel, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 10, 0, 0), 0, 0)); //Add components to the panels, then add panels to the JDialog. lblpanel.add(lbl, BorderLayout.NORTH); quitDialog.getContentPane().add(lblpanel, BorderLayout.CENTER); quitDialog.getContentPane().add(btnpanel, BorderLayout.SOUTH); //Make a button listener to close the dialog. btnAdd.setMnemonic(KeyEvent.VK_A); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); btnCancel.setMnemonic(KeyEvent.VK_C); btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { quitDialog.dispose(); } }); //Pack it all together quitDialog.pack(); //Center this dialog on the screen. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = ( screen.width / 2) - (quitDialog.getSize().width /2 ); int y = ( screen.height / 2) - (quitDialog.getSize().height /2 ); quitDialog.setLocation( x, y); //Now show this dialog. quitDialog.setVisible(true); } //method: mnuItmExit_actionPerformed /* ===================================================================== */ /** This method is the performs the action when the user selects About * from this frame menu. A JDialog is built and shown on the screen. * * @param e The ActionEvent for the menu selection. * */ public void mnuItmAbout_actionPerformed(ActionEvent e){ // Setup a the JDialog. aboutDialog = new JDialog(this, "About"); // Create a button and the labels. JLabel lbl = new JLabel(); JButton btn = new JButton( " CLOSE "); // Set the texxt to display lbl.setText("<html><body>" + "<center>GPS & WiFi Simulator version " + 1.1 + " build " + 5 + "<br>Copyright (C) 2002.</center>" + "<p><b><u>Author</u></b>:</p>" + "<blockquote>" + "Jason Prideaux<BR>" + "</blockquote>" + "</body></html>" ); // Create panels for hold the Components JPanel btnpanel = new JPanel(); JPanel lblpanel = new JPanel(new BorderLayout()); // Set borders lblpanel.setBorder( BorderFactory.createEmptyBorder(20, 20, 20, 20) ); // Add components to the panels, then add panels to the JDialog. lblpanel.add(lbl, BorderLayout.NORTH); btnpanel.add(btn); // Add panels to the dialog aboutDialog.getContentPane().add(lblpanel, BorderLayout.CENTER); aboutDialog.getContentPane().add(btnpanel, BorderLayout.SOUTH); // Make a button listener to close the dialog. btn.setMnemonic(KeyEvent.VK_C); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { aboutDialog.dispose(); } }); // Pack it all together aboutDialog.pack(); // Center this dialog on the screen. Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = ( screen.width / 2) - (aboutDialog.getSize().width /2 ); int y = ( screen.height / 2) - (aboutDialog.getSize().height /2 ); aboutDialog.setLocation( x, y); // Now show this dialog. aboutDialog.setVisible(true); } //method: mnuItmAbout_actionPerformed /* ===================================================================== */ /** This method is the performs the action when the user selects Tray * from this frame menu. It first checks to see if the Tray is alrady * visible in the interface, and if not it creates a new Tray, by informing * Controller. * * @param e The ActionEvent for the menu selection. * */ public void mnuItmTray_actionPerformed(ActionEvent e){ // Check if exists already. if( !(controller.isTrayAlive()) ){ controller.makeTray(); } } //method: mnuItmTray_actionPerformed /* ===================================================================== */ /** This method take the given Point and changes the scroll position of * the ScrollPane inorder to make the given Point the center of the view. * * @param pos The Point to be the new center of displayed chart. * */ public void centerScreen(Point pos){ Point p = pane.getScrollPosition(); Dimension d = pane.getViewportSize(); Point p2=new Point(p); p2.y=pos.y-(d.height)/2; p2.x=pos.x-(d.width)/2; if (!p2.equals(p)){ pane.setScrollPosition(p2); } } //method: centerScreen /* ===================================================================== */ /** This method changes the map image/data which the ChartComp is currently * displaying. The method mearly checks that the map data file exists, then * call on ChartComp. * * @param chartName The chart filename to put into the ChartComp. * @param scale The scale of the map image. * @param precision The precision of the map image for the ChartComp * */ public void changeChart(String chartName){ if (!(new File("files/data/"+chartName)).exists()){ System.err.println("Could not find file: "+chartName); } chart.loadChart(chartName); this.show(); } //method: changeChart /* ===================================================================== */ /** This method returns the ScrollPane kept inside this frame. The main * purpose is for the controller to get position information from it. * * @return ScrollPane The main scrollPane inside this frame. * */ public ScrollPane getChartPane(){ return pane; } //method: getChartPane } //class: SimulatorFrame