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:       Container contentPane = getContentPane();
087:       contentPane.add(toolBar, BorderLayout.NORTH);
088:       contentPane.add(scrollPane, BorderLayout.CENTER);
089:    }
090: 
091:    /**
092:       Asks the user to open a graph file.
093:    */
094:    private void openFile()
095:    {  
096:       // let user select file
097: 
098:       JFileChooser fileChooser = new JFileChooser();
099:       int r = fileChooser.showOpenDialog(this);
100:       if (r == JFileChooser.APPROVE_OPTION)
101:       {  
102:          // open the file that the user selected
103:          try
104:          {
105:             File file = fileChooser.getSelectedFile();
106:             ObjectInputStream in = new ObjectInputStream(
107:                new FileInputStream(file));
108:             graph = (Graph) in.readObject();
109:             in.close();
110:             Container contentPane = getContentPane();
111:             contentPane.remove(scrollPane);
112:             contentPane.remove(toolBar);
113:             constructFrameComponents();
114:             validate();
115:             repaint();
116:          }
117:          catch (IOException exception)
118:          {
119:             JOptionPane.showMessageDialog(null, 
120:                exception);
121:          }
122:          catch (ClassNotFoundException exception)
123:          {
124:             JOptionPane.showMessageDialog(null, 
125:                exception);
126:          }
127:       }
128:    }
129: 
130:    /**
131:       Saves the current graph in a file. 
132:    */
133:    private void saveFile()
134:    {
135:       JFileChooser fileChooser = new JFileChooser();
136:       if (fileChooser.showSaveDialog(this) 
137:          == JFileChooser.APPROVE_OPTION)
138:       {
139:          try
140:          {
141:             File file = fileChooser.getSelectedFile();
142:             ObjectOutputStream out = new ObjectOutputStream(
143:                new FileOutputStream(file));
144:             out.writeObject(graph);
145:             out.close();
146:          }
147:          catch (IOException exception)
148:          {
149:             JOptionPane.showMessageDialog(null, 
150:                exception);
151:          }
152:       }
153:    }
154: 
155:    private Graph graph;
156:    private GraphPanel panel;
157:    private JScrollPane scrollPane;
158:    private ToolBar toolBar;
159: 
160:    public static final int FRAME_WIDTH = 600;
161:    public static final int FRAME_HEIGHT = 400;
162: }