001package horstmann.ch08_graphed2;
002import java.awt.BorderLayout;
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.io.ObjectInputStream;
008import java.io.ObjectOutputStream;
009
010import javax.swing.JFileChooser;
011import javax.swing.JFrame;
012import javax.swing.JMenu;
013import javax.swing.JMenuBar;
014import javax.swing.JMenuItem;
015import javax.swing.JOptionPane;
016import javax.swing.JScrollPane;
017
018/**
019   This frame shows the toolbar and the graph.
020 */
021@SuppressWarnings("serial")
022public class GraphFrame extends JFrame
023{
024        /**
025      Constructs a graph frame that displays a given graph.
026      @param graph the graph to display
027         */
028        public GraphFrame(final Graph graph)
029        {
030                setSize(FRAME_WIDTH, FRAME_HEIGHT);
031                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
032
033                this.graph = graph;
034
035                constructFrameComponents();
036                // set up menus
037
038                JMenuBar menuBar = new JMenuBar();
039                setJMenuBar(menuBar);
040                JMenu fileMenu = new JMenu("File");
041                menuBar.add(fileMenu);
042
043                JMenuItem openItem = new JMenuItem("Open");
044                openItem.addActionListener(event -> openFile());
045                fileMenu.add(openItem);
046
047                JMenuItem saveItem = new JMenuItem("Save");
048                saveItem.addActionListener(event -> saveFile());
049                fileMenu.add(saveItem);
050
051                JMenuItem exitItem = new JMenuItem("Exit");
052                exitItem.addActionListener(event -> System.exit(0));
053                fileMenu.add(exitItem);
054
055                JMenuItem deleteItem = new JMenuItem("Delete");
056                deleteItem.addActionListener(event -> panel.removeSelected());
057
058                JMenuItem propertiesItem
059                = new JMenuItem("Properties");
060                propertiesItem.addActionListener(event -> panel.editSelected());
061
062                JMenu editMenu = new JMenu("Edit");
063                editMenu.add(deleteItem);
064                editMenu.add(propertiesItem);
065                menuBar.add(editMenu);
066        }
067
068        /**
069      Constructs the tool bar and graph panel.
070         */
071        private void constructFrameComponents()
072        {
073                toolBar = new ToolBar(graph);
074                panel = new GraphPanel(toolBar, graph);
075                scrollPane = new JScrollPane(panel);
076                this.add(toolBar, BorderLayout.NORTH);
077                this.add(scrollPane, BorderLayout.CENTER);
078        }
079
080        /**
081      Asks the user to open a graph file.
082         */
083        private void openFile()
084        {
085                // let user select file
086
087                JFileChooser fileChooser = new JFileChooser();
088                int r = fileChooser.showOpenDialog(this);
089                if (r == JFileChooser.APPROVE_OPTION)
090                {
091                        // open the file that the user selected
092                        try
093                        {
094                                File file = fileChooser.getSelectedFile();
095                                ObjectInputStream in = new ObjectInputStream(
096                                                new FileInputStream(file));
097                                graph = (Graph) in.readObject();
098                                in.close();
099                                this.remove(scrollPane);
100                                this.remove(toolBar);
101                                constructFrameComponents();
102                                validate();
103                                repaint();
104                        }
105                        catch (IOException exception)
106                        {
107                                JOptionPane.showMessageDialog(null,
108                                                exception);
109                        }
110                        catch (ClassNotFoundException exception)
111                        {
112                                JOptionPane.showMessageDialog(null,
113                                                exception);
114                        }
115                }
116        }
117
118        /**
119      Saves the current graph in a file.
120         */
121        private void saveFile()
122        {
123                JFileChooser fileChooser = new JFileChooser();
124                if (fileChooser.showSaveDialog(this)
125                                == JFileChooser.APPROVE_OPTION)
126                {
127                        try
128                        {
129                                File file = fileChooser.getSelectedFile();
130                                ObjectOutputStream out = new ObjectOutputStream(
131                                                new FileOutputStream(file));
132                                out.writeObject(graph);
133                                out.close();
134                        }
135                        catch (IOException exception)
136                        {
137                                JOptionPane.showMessageDialog(null,
138                                                exception);
139                        }
140                }
141        }
142
143        private Graph graph;
144        private GraphPanel panel;
145        private JScrollPane scrollPane;
146        private ToolBar toolBar;
147
148        public static final int FRAME_WIDTH = 600;
149        public static final int FRAME_HEIGHT = 400;
150}