001: import java.awt.*;
002: import java.awt.event.*;
003: import java.io.*;
004: import javax.swing.*;
005: 
006: /**
007:    This frame shows the toolbar and the graph.
008: */
009: public class GraphFrame extends JFrame
010: {
011:    /**
012:       Constructs a graph frame that displays a given graph.
013:       @param graph the graph to display
014:    */
015:    public GraphFrame(final Graph graph)
016:    {
017:       setSize(FRAME_WIDTH, FRAME_HEIGHT);
018:       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
019: 
020:       this.graph = graph;
021: 
022:       constructFrameComponents();
023:       // Set up menus
024: 
025:       JMenuBar menuBar = new JMenuBar();
026:       setJMenuBar(menuBar);
027:       JMenu fileMenu = new JMenu("File");
028:       menuBar.add(fileMenu);
029: 
030:       JMenuItem openItem = new JMenuItem("Open");
031:       openItem.addActionListener(new
032:          ActionListener()
033:          {
034:             public void actionPerformed(ActionEvent event)
035:             {
036:                openFile();
037:             }
038:          });
039:       fileMenu.add(openItem);
040: 
041:       JMenuItem saveItem = new JMenuItem("Save");
042:       saveItem.addActionListener(new
043:          ActionListener()
044:          {
045:             public void actionPerformed(ActionEvent event)
046:             {
047:                saveFile();
048:             }
049:          });
050:       fileMenu.add(saveItem);
051: 
052:       JMenuItem exitItem = new JMenuItem("Exit");
053:       exitItem.addActionListener(new
054:          ActionListener()
055:          {
056:             public void actionPerformed(ActionEvent event)
057:             {
058:                System.exit(0);
059:             }
060:          });
061:       fileMenu.add(exitItem);
062: 
063:       JMenuItem deleteItem = new JMenuItem("Delete");
064:       deleteItem.addActionListener(new
065:          ActionListener()
066:          {
067:             public void actionPerformed(ActionEvent event)
068:             {
069:                panel.removeSelected();
070:             }
071:          });
072: 
073:       JMenu editMenu = new JMenu("Edit");
074:       editMenu.add(deleteItem);
075:       menuBar.add(editMenu);
076:    }
077: 
078:    /**
079:       Constructs the tool bar and graph panel.
080:    */
081:    private void constructFrameComponents()
082:    {
083:       toolBar = new ToolBar(graph);
084:       panel = new GraphPanel(toolBar, graph);
085:       scrollPane = new JScrollPane(panel);
086:       this.add(toolBar, BorderLayout.NORTH);
087:       this.add(scrollPane, BorderLayout.CENTER);
088:    }
089: 
090:    /**
091:       Asks the user to open a graph file.
092:    */
093:    private void openFile()
094:    {
095:       // let user select file
096: 
097:       JFileChooser fileChooser = new JFileChooser();
098:       int r = fileChooser.showOpenDialog(this);
099:       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:    }
127: 
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:    }
152: 
153:    private Graph graph;
154:    private GraphPanel panel;
155:    private JScrollPane scrollPane;
156:    private ToolBar toolBar;
157: 
158:    public static final int FRAME_WIDTH = 600;
159:    public static final int FRAME_HEIGHT = 400;
160: }