Skip to content

Commit

Permalink
Episode 183
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesshore committed Apr 4, 2012
1 parent 18070e0 commit 708c285
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/com/jamesshore/finances/ui/ApplicationFrame.java
Expand Up @@ -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);
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
35 changes: 35 additions & 0 deletions 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);
}
}

}
18 changes: 12 additions & 6 deletions src/com/jamesshore/finances/ui/_ApplicationFrameTest.java
Expand Up @@ -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
Expand Down

0 comments on commit 708c285

Please sign in to comment.