Source of GraphFrame.java


  1: import java.awt.*;
  2: import java.awt.event.*;
  3: import java.io.*;
  4: import javax.swing.*;

  6: /**
  7:    This frame shows the toolbar and the graph.
  8: */
  9: public class GraphFrame extends JFrame
 10: {
 11:    /**
 12:       Constructs a graph frame that displays a given graph.
 13:       @param graph the graph to display
 14:    */
 15:    public GraphFrame(final Graph graph)
 16:    {
 17:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
 18:       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 20:       this.graph = graph;

 22:       constructFrameComponents();
 23:       // Set up menus

 25:       JMenuBar menuBar = new JMenuBar();
 26:       setJMenuBar(menuBar);
 27:       JMenu fileMenu = new JMenu("File");
 28:       menuBar.add(fileMenu);

 30:       JMenuItem openItem = new JMenuItem("Open");
 31:       openItem.addActionListener(new
 32:          ActionListener()
 33:          {
 34:             public void actionPerformed(ActionEvent event)
 35:             {
 36:                openFile();
 37:             }
 38:          });
 39:       fileMenu.add(openItem);

 41:       JMenuItem saveItem = new JMenuItem("Save");
 42:       saveItem.addActionListener(new
 43:          ActionListener()
 44:          {
 45:             public void actionPerformed(ActionEvent event)
 46:             {
 47:                saveFile();
 48:             }
 49:          });
 50:       fileMenu.add(saveItem);

 52:       JMenuItem exitItem = new JMenuItem("Exit");
 53:       exitItem.addActionListener(new
 54:          ActionListener()
 55:          {
 56:             public void actionPerformed(ActionEvent event)
 57:             {
 58:                System.exit(0);
 59:             }
 60:          });
 61:       fileMenu.add(exitItem);

 63:       JMenuItem deleteItem = new JMenuItem("Delete");
 64:       deleteItem.addActionListener(new
 65:          ActionListener()
 66:          {
 67:             public void actionPerformed(ActionEvent event)
 68:             {
 69:                panel.removeSelected();
 70:             }
 71:          });

 73:       JMenu editMenu = new JMenu("Edit");
 74:       editMenu.add(deleteItem);
 75:       menuBar.add(editMenu);
 76:    }

 78:    /**
 79:       Constructs the tool bar and graph panel.
 80:    */
 81:    private void constructFrameComponents()
 82:    {
 83:       toolBar = new ToolBar(graph);
 84:       panel = new GraphPanel(toolBar, graph);
 85:       scrollPane = new JScrollPane(panel);
 86:       this.add(toolBar, BorderLayout.NORTH);
 87:       this.add(scrollPane, BorderLayout.CENTER);
 88:    }

 90:    /**
 91:       Asks the user to open a graph file.
 92:    */
 93:    private void openFile()
 94:    {
 95:       // let user select file

 97:       JFileChooser fileChooser = new JFileChooser();
 98:       int r = fileChooser.showOpenDialog(this);
 99:       if (r == JFileChooser.APPROVE_OPTION)
100:       {
101:          // Open the file that the user selected
102:          try
103:          {
104:             File file = fileChooser.getSelectedFile();
105:             ObjectInputStream in = new ObjectInputStream(
106:                new FileInputStream(file));
107:             graph = (Graph) in.readObject();
108:             in.close();
109:             this.remove(scrollPane);
110:             this.remove(toolBar);
111:             constructFrameComponents();
112:             validate();
113:             repaint();
114:          }
115:          catch (IOException exception)
116:          {
117:             JOptionPane.showMessageDialog(null,
118:                exception);
119:          }
120:          catch (ClassNotFoundException exception)
121:          {
122:             JOptionPane.showMessageDialog(null,
123:                exception);
124:          }
125:       }
126:    }

128:    /**
129:       Saves the current graph in a file.
130:    */
131:    private void saveFile()
132:    {
133:       JFileChooser fileChooser = new JFileChooser();
134:       if (fileChooser.showSaveDialog(this)
135:             == JFileChooser.APPROVE_OPTION)
136:       {
137:          try
138:          {
139:             File file = fileChooser.getSelectedFile();
140:             ObjectOutputStream out = new ObjectOutputStream(
141:                   new FileOutputStream(file));
142:             out.writeObject(graph);
143:             out.close();
144:          }
145:          catch (IOException exception)
146:          {
147:             JOptionPane.showMessageDialog(null,
148:                   exception);
149:          }
150:       }
151:    }

153:    private Graph graph;
154:    private GraphPanel panel;
155:    private JScrollPane scrollPane;
156:    private ToolBar toolBar;

158:    public static final int FRAME_WIDTH = 600;
159:    public static final int FRAME_HEIGHT = 400;
160: }