    001: import java.awt.*;
    002: import java.awt.event.*;
    003: import java.io.*;
    004: import javax.swing.*;
    005: import javax.swing.event.*;
    006:
    007: /**
    008:    This frame shows the toolbar and the graph.
    009: */
    010: public class GraphFrame extends JFrame
    011: {
    012:    /**
    013:       Constructs a graph frame that displays a given graph.
    014:       @param graph the graph to display
    015:    */
    016:    public GraphFrame(final Graph graph)
    017:    {
    018:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
    019:       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    020:
    021:       this.graph = graph;
    022:
    023:       constructFrameComponents();
    024:       // set up menus
    025:
    026:       JMenuBar menuBar = new JMenuBar();
    027:       setJMenuBar(menuBar);
    028:       JMenu fileMenu = new JMenu("File");
    029:       menuBar.add(fileMenu);
    030:
    031:       JMenuItem openItem = new JMenuItem("Open");
    032:       openItem.addActionListener(new
    033:          ActionListener()
    034:          {
    035:             public void actionPerformed(ActionEvent event)
    036:             {
    037:                openFile();
    038:             }
    039:          });
    040:       fileMenu.add(openItem);
    041:
    042:       JMenuItem saveItem = new JMenuItem("Save");
    043:       saveItem.addActionListener(new
    044:          ActionListener()
    045:          {
    046:             public void actionPerformed(ActionEvent event)
    047:             {
    048:                saveFile();
    049:             }
    050:          });
    051:       fileMenu.add(saveItem);
    052:
    053:       JMenuItem exitItem = new JMenuItem("Exit");
    054:       exitItem.addActionListener(new
    055:          ActionListener()
    056:          {
    057:             public void actionPerformed(ActionEvent event)
    058:             {
    059:                System.exit(0);
    060:             }
    061:          });
    062:       fileMenu.add(exitItem);
    063:
    064:       JMenuItem deleteItem = new JMenuItem("Delete");
    065:       deleteItem.addActionListener(new
    066:          ActionListener()
    067:          {
    068:             public void actionPerformed(ActionEvent event)
    069:             {
    070:                panel.removeSelected();
    071:             }
    072:          });
    073:
    074:       JMenuItem propertiesItem
    075:          = new JMenuItem("Properties");
    076:       propertiesItem.addActionListener(new
    077:          ActionListener()
    078:          {
    079:             public void actionPerformed(ActionEvent event)
    080:             {
    081:                panel.editSelected();
    082:             }
    083:          });
    084:
    085:       JMenu editMenu = new JMenu("Edit");
    086:       editMenu.add(deleteItem);
    087:       editMenu.add(propertiesItem);
    088:       menuBar.add(editMenu);
    089:    }
    090:
    091:    /**
    092:       Constructs the tool bar and graph panel.
    093:    */
    094:    private void constructFrameComponents()
    095:    {
    096:       toolBar = new ToolBar(graph);
    097:       panel = new GraphPanel(toolBar, graph);
    098:       scrollPane = new JScrollPane(panel);
    099:       this.add(toolBar, BorderLayout.NORTH);
    100:       this.add(scrollPane, BorderLayout.CENTER);
    101:    }
    102:
    103:    /**
    104:       Asks the user to open a graph file.
    105:    */
    106:    private void openFile()
    107:    {
    108:       // let user select file
    109:
    110:       JFileChooser fileChooser = new JFileChooser();
    111:       int r = fileChooser.showOpenDialog(this);
    112:       if (r == JFileChooser.APPROVE_OPTION)
    113:       {
    114:          // open the file that the user selected
    115:          try
    116:          {
    117:             File file = fileChooser.getSelectedFile();
    118:             ObjectInputStream in = new ObjectInputStream(
    119:                new FileInputStream(file));
    120:             graph = (Graph) in.readObject();
    121:             in.close();
    122:             this.remove(scrollPane);
    123:             this.remove(toolBar);
    124:             constructFrameComponents();
    125:             validate();
    126:             repaint();
    127:          }
    128:          catch (IOException exception)
    129:          {
    130:             JOptionPane.showMessageDialog(null,
    131:                exception);
    132:          }
    133:          catch (ClassNotFoundException exception)
    134:          {
    135:             JOptionPane.showMessageDialog(null,
    136:                exception);
    137:          }
    138:       }
    139:    }
    140:
    141:    /**
    142:       Saves the current graph in a file.
    143:    */
    144:    private void saveFile()
    145:    {
    146:       JFileChooser fileChooser = new JFileChooser();
    147:       if (fileChooser.showSaveDialog(this)
    148:          == JFileChooser.APPROVE_OPTION)
    149:       {
    150:          try
    151:          {
    152:             File file = fileChooser.getSelectedFile();
    153:             ObjectOutputStream out = new ObjectOutputStream(
    154:                new FileOutputStream(file));
    155:             out.writeObject(graph);
    156:             out.close();
    157:          }
    158:          catch (IOException exception)
    159:          {
    160:             JOptionPane.showMessageDialog(null,
    161:                exception);
    162:          }
    163:       }
    164:    }
    165:
    166:    private Graph graph;
    167:    private GraphPanel panel;
    168:    private JScrollPane scrollPane;
    169:    private ToolBar toolBar;
    170:
    171:    public static final int FRAME_WIDTH = 600;
    172:    public static final int FRAME_HEIGHT = 400;
    173: }