import javax.swing.*; import java.awt.event.*; import java.io.*; public class FileSaveDialog extends JFileChooser implements ActionListener { WorkSpace workSpace; DrawingCanvas canvas; public FileSaveDialog (WorkSpace workSpace, DrawingCanvas canvas) { this.workSpace = workSpace; this.canvas = canvas; } public void actionPerformed(ActionEvent e) { File path = null; try { showSaveDialog(null); path = getSelectedFile(); FileOutputStream outFile = new FileOutputStream(path); ObjectOutputStream outObj = new ObjectOutputStream(outFile); /* * It appears that there may be some problem with saving with a * shape selected, so we'll toggle it off then back on when * saving. This is a bit tricky for 102 students, but we won't * hold then too it. */ Shape selection = workSpace.getSelection(); workSpace.setSelection(null); outObj.writeObject(workSpace); workSpace.setSelection(selection); } catch (NullPointerException npe) { // Just ignore this; it means the user didn't select a file. } catch (FileNotFoundException fnfe) { new JOptionPane(). showMessageDialog(null, "File " + path + " not found.", "", JOptionPane.ERROR_MESSAGE); } catch (IOException ioe) { new JOptionPane(). showMessageDialog(null, "I/o exception:" + ioe, "", JOptionPane.ERROR_MESSAGE); } } }