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