diff --git a/src/com/jamesshore/finances/ui/ApplicationFrame.java b/src/com/jamesshore/finances/ui/ApplicationFrame.java index e671f02..d6216dd 100644 --- a/src/com/jamesshore/finances/ui/ApplicationFrame.java +++ b/src/com/jamesshore/finances/ui/ApplicationFrame.java @@ -13,7 +13,7 @@ public class ApplicationFrame extends JFrame { public static final Dimension INITIAL_SIZE = new Dimension(900, 400); private ApplicationModel model; - private FileDialog saveAsDialog; + private SaveAsDialog saveAsDialog; public static void newWindow() { new ApplicationFrame(new ApplicationModel()).setVisible(true); @@ -39,7 +39,7 @@ private void addComponents() { contentPane.add(BorderLayout.NORTH, configurationPanel()); setJMenuBar(menuBar()); - saveAsDialog = new FileDialog(ApplicationFrame.this, "Save As", FileDialog.SAVE); + saveAsDialog = new SaveAsDialog(ApplicationFrame.this, model); } private Component forecastTable() { @@ -82,13 +82,13 @@ private JMenuItem saveAsMenuItem() { return menuItem("Save As...", KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.SHIFT_MASK | InputEvent.META_MASK), new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { - saveAsDialog.setVisible(true); - doSave(); // this line of code is untested + saveAsDialog.display(); } }); } // non-private for testing purposes + // TODO: delete me (moved to SaveAsDialog) void doSave() { try { String directory = saveAsDialog.getDirectory(); diff --git a/src/com/jamesshore/finances/ui/SaveAsDialog.java b/src/com/jamesshore/finances/ui/SaveAsDialog.java new file mode 100644 index 0000000..cd1c0cc --- /dev/null +++ b/src/com/jamesshore/finances/ui/SaveAsDialog.java @@ -0,0 +1,35 @@ +package com.jamesshore.finances.ui; + +import java.awt.*; +import java.io.*; +import javax.swing.*; + +public class SaveAsDialog extends FileDialog { + private static final long serialVersionUID = 1L; + + private ApplicationModel model; + + public SaveAsDialog(Frame parentWindow, ApplicationModel model) { + super(parentWindow, "Save As", FileDialog.SAVE); + this.model = model; + } + + public void display() { + this.setVisible(true); + this.doSave(); // this line of code is untested + } + + // non-private for testing purposes + void doSave() { + try { + String directory = this.getDirectory(); + String file = this.getFile(); + + if (file != null) model.save(new File(directory, file)); + } + catch (IOException e) { + JOptionPane.showMessageDialog(this, "Could not save file: " + e.getLocalizedMessage(), "Save File", JOptionPane.WARNING_MESSAGE); + } + } + +} diff --git a/src/com/jamesshore/finances/ui/_ApplicationFrameTest.java b/src/com/jamesshore/finances/ui/_ApplicationFrameTest.java index f93d213..e95a0b1 100644 --- a/src/com/jamesshore/finances/ui/_ApplicationFrameTest.java +++ b/src/com/jamesshore/finances/ui/_ApplicationFrameTest.java @@ -127,12 +127,18 @@ public void closeMenuItemShouldCloseTheWindow() throws Throwable { // This test sometimes fails saying frame isn't disposed. Can't reliably reproduce; seems to be race condition // that appears when Swing tests are running slow. // Tried: invokeAndWait around doClick (did not work) - // Try next? Run whole test on event handler thread using invokeAndWait? - - frame.setVisible(true); - assertTrue("before disposable, frame is displayable", frame.isDisplayable()); - closeMenuItem.doClick(); - assertTrue("frame should have been disposed", !frame.isDisplayable()); + // Currently trying: run whole test on event handler thread using invokeAndWait - put in place in episode 183; + // if it hasn't recurred by episode 200, assume it's fixed. (And if it is, that's a really good sign that we + // need to put something similar in place for all Swing tests. + SwingUtilities.invokeAndWait(new Runnable() { + @Override + public void run() { + frame.setVisible(true); + assertTrue("before disposable, frame is displayable", frame.isDisplayable()); + closeMenuItem.doClick(); + assertTrue("frame should have been disposed", !frame.isDisplayable()); + } + }); } @Test