public class GraphFrame extends JFrame
1: import java.awt.*;
2: import java.awt.event.*;
3: import java.io.*;
4: import javax.swing.*;
5: import javax.swing.event.*;
7: /**
8: This frame shows the toolbar and the graph.
9: */
10: public class GraphFrame extends JFrame
11: {
12: /**
13: Constructs a graph frame that displays a given graph.
14: @param graph the graph to display
15: */
16: public GraphFrame(final Graph graph)
17: {
18: setSize(FRAME_WIDTH, FRAME_HEIGHT);
19: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
21: this.graph = graph;
23: constructFrameComponents();
24: // set up menus
26: JMenuBar menuBar = new JMenuBar();
27: setJMenuBar(menuBar);
28: JMenu fileMenu = new JMenu("File");
29: menuBar.add(fileMenu);
31: JMenuItem openItem = new JMenuItem("Open");
32: openItem.addActionListener(new
33: ActionListener()
34: {
35: public void actionPerformed(ActionEvent event)
36: {
37: openFile();
38: }
39: });
40: fileMenu.add(openItem);
42: JMenuItem saveItem = new JMenuItem("Save");
43: saveItem.addActionListener(new
44: ActionListener()
45: {
46: public void actionPerformed(ActionEvent event)
47: {
48: saveFile();
49: }
50: });
51: fileMenu.add(saveItem);
53: JMenuItem exitItem = new JMenuItem("Exit");
54: exitItem.addActionListener(new
55: ActionListener()
56: {
57: public void actionPerformed(ActionEvent event)
58: {
59: System.exit(0);
60: }
61: });
62: fileMenu.add(exitItem);
64: JMenuItem deleteItem = new JMenuItem("Delete");
65: deleteItem.addActionListener(new
66: ActionListener()
67: {
68: public void actionPerformed(ActionEvent event)
69: {
70: panel.removeSelected();
71: }
72: });
74: JMenuItem propertiesItem
75: = new JMenuItem("Properties");
76: propertiesItem.addActionListener(new
77: ActionListener()
78: {
79: public void actionPerformed(ActionEvent event)
80: {
81: panel.editSelected();
82: }
83: });
85: JMenu editMenu = new JMenu("Edit");
86: editMenu.add(deleteItem);
87: editMenu.add(propertiesItem);
88: menuBar.add(editMenu);
89: }
91: /**
92: Constructs the tool bar and graph panel.
93: */
94: private void constructFrameComponents()
95: {
96: toolBar = new ToolBar(graph);
97: panel = new GraphPanel(toolBar, graph);
98: scrollPane = new JScrollPane(panel);
99: this.add(toolBar, BorderLayout.NORTH);
100: this.add(scrollPane, BorderLayout.CENTER);
101: }
103: /**
104: Asks the user to open a graph file.
105: */
106: private void openFile()
107: {
108: // let user select file
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: }
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: }
166: private Graph graph;
167: private GraphPanel panel;
168: private JScrollPane scrollPane;
169: private ToolBar toolBar;
171: public static final int FRAME_WIDTH = 600;
172: public static final int FRAME_HEIGHT = 400;
173: }