001package horstmann.ch08_graphed; 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 JMenu editMenu = new JMenu("Edit"); 059 editMenu.add(deleteItem); 060 menuBar.add(editMenu); 061 } 062 063 /** 064 Constructs the tool bar and graph panel. 065 */ 066 private void constructFrameComponents() 067 { 068 toolBar = new ToolBar(graph); 069 panel = new GraphPanel(toolBar, graph); 070 scrollPane = new JScrollPane(panel); 071 this.add(toolBar, BorderLayout.NORTH); 072 this.add(scrollPane, BorderLayout.CENTER); 073 } 074 075 /** 076 Asks the user to open a graph file. 077 */ 078 private void openFile() 079 { 080 // let user select file 081 082 JFileChooser fileChooser = new JFileChooser(); 083 int r = fileChooser.showOpenDialog(this); 084 if (r == JFileChooser.APPROVE_OPTION) 085 { 086 // Open the file that the user selected 087 try 088 { 089 File file = fileChooser.getSelectedFile(); 090 ObjectInputStream in = new ObjectInputStream( 091 new FileInputStream(file)); 092 graph = (Graph) in.readObject(); 093 in.close(); 094 this.remove(scrollPane); 095 this.remove(toolBar); 096 constructFrameComponents(); 097 validate(); 098 repaint(); 099 } 100 catch (IOException exception) 101 { 102 JOptionPane.showMessageDialog(null, 103 exception); 104 } 105 catch (ClassNotFoundException exception) 106 { 107 JOptionPane.showMessageDialog(null, 108 exception); 109 } 110 } 111 } 112 113 /** 114 Saves the current graph in a file. 115 */ 116 private void saveFile() 117 { 118 JFileChooser fileChooser = new JFileChooser(); 119 if (fileChooser.showSaveDialog(this) 120 == JFileChooser.APPROVE_OPTION) 121 { 122 try 123 { 124 File file = fileChooser.getSelectedFile(); 125 ObjectOutputStream out = new ObjectOutputStream( 126 new FileOutputStream(file)); 127 out.writeObject(graph); 128 out.close(); 129 } 130 catch (IOException exception) 131 { 132 JOptionPane.showMessageDialog(null, 133 exception); 134 } 135 } 136 } 137 138 private Graph graph; 139 private GraphPanel panel; 140 private JScrollPane scrollPane; 141 private ToolBar toolBar; 142 143 public static final int FRAME_WIDTH = 600; 144 public static final int FRAME_HEIGHT = 400; 145}