-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraphFrame.java
160 lines (145 loc) · 4.02 KB
/
GraphFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
/**
This frame shows the toolbar and the graph.
*/
public class GraphFrame extends JFrame
{
/**
Constructs a graph frame that displays a given graph.
@param graph the graph to display
*/
public GraphFrame(final Graph graph)
{
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.graph = graph;
constructFrameComponents();
// Set up menus
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem openItem = new JMenuItem("Open");
openItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
openFile();
}
});
fileMenu.add(openItem);
JMenuItem saveItem = new JMenuItem("Save");
saveItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
saveFile();
}
});
fileMenu.add(saveItem);
JMenuItem exitItem = new JMenuItem("Exit");
exitItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
fileMenu.add(exitItem);
JMenuItem deleteItem = new JMenuItem("Delete");
deleteItem.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
panel.removeSelected();
}
});
JMenu editMenu = new JMenu("Edit");
editMenu.add(deleteItem);
menuBar.add(editMenu);
}
/**
Constructs the tool bar and graph panel.
*/
private void constructFrameComponents()
{
toolBar = new ToolBar(graph);
panel = new GraphPanel(toolBar, graph);
scrollPane = new JScrollPane(panel);
this.add(toolBar, BorderLayout.NORTH);
this.add(scrollPane, BorderLayout.CENTER);
}
/**
Asks the user to open a graph file.
*/
private void openFile()
{
// let user select file
JFileChooser fileChooser = new JFileChooser();
int r = fileChooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION)
{
// Open the file that the user selected
try
{
File file = fileChooser.getSelectedFile();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file));
graph = (Graph) in.readObject();
in.close();
this.remove(scrollPane);
this.remove(toolBar);
constructFrameComponents();
validate();
repaint();
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(null,
exception);
}
catch (ClassNotFoundException exception)
{
JOptionPane.showMessageDialog(null,
exception);
}
}
}
/**
Saves the current graph in a file.
*/
private void saveFile()
{
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showSaveDialog(this)
== JFileChooser.APPROVE_OPTION)
{
try
{
File file = fileChooser.getSelectedFile();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(graph);
out.close();
}
catch (IOException exception)
{
JOptionPane.showMessageDialog(null,
exception);
}
}
}
private Graph graph;
private GraphPanel panel;
private JScrollPane scrollPane;
private ToolBar toolBar;
public static final int FRAME_WIDTH = 600;
public static final int FRAME_HEIGHT = 400;
}