-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added default GUI if there are no parameters (with Jon Dickinson)
- Loading branch information
skizz
committed
Jun 11, 2003
1 parent
905f2b1
commit 1cc27d6
Showing
5 changed files
with
298 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package biz.skizz.testdox; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: stevcc | ||
* Date: 11-Jun-2003 | ||
* Time: 20:17:50 | ||
* To change this template use Options | File Templates. | ||
*/ | ||
public interface Generator { | ||
void generate(File file); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package biz.skizz.testdox; | ||
|
||
import javax.swing.*; | ||
import javax.swing.event.DocumentListener; | ||
import javax.swing.event.DocumentEvent; | ||
import java.awt.*; | ||
import java.awt.event.ActionListener; | ||
import java.awt.event.ActionEvent; | ||
import java.io.File; | ||
import java.beans.PropertyChangeListener; | ||
import java.beans.PropertyChangeEvent; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: stevcc | ||
* Date: 11-Jun-2003 | ||
* Time: 19:08:11 | ||
* To change this template use Options | File Templates. | ||
*/ | ||
public class Gui extends JFrame { | ||
|
||
JButton browseButton; | ||
JButton goButton; | ||
JFileChooser fileChooser; | ||
JTextField path; | ||
Generator gen; | ||
|
||
private DocumentListener pathChangeListener = new DocumentListener() { | ||
public void insertUpdate(DocumentEvent e) { | ||
doPathChange(); | ||
} | ||
|
||
public void removeUpdate(DocumentEvent e) { | ||
doPathChange(); | ||
} | ||
|
||
public void changedUpdate(DocumentEvent e) { | ||
doPathChange(); | ||
} | ||
}; | ||
|
||
private void doPathChange() { | ||
File f = new File(path.getText()); | ||
goButton.setEnabled(f.exists()); | ||
if (goButton.isEnabled()) | ||
getRootPane().setDefaultButton(goButton); | ||
else | ||
getRootPane().setDefaultButton(browseButton); | ||
|
||
} | ||
|
||
public Gui(String title, Generator gen) throws HeadlessException { | ||
super(title); | ||
this.gen = gen; | ||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
setup(); | ||
} | ||
|
||
private ActionListener goActionListener = new ActionListener() { | ||
public void actionPerformed(ActionEvent e) { | ||
gen.generate(new File(path.getText())); | ||
} | ||
}; | ||
|
||
private ActionListener browseActionListener = new ActionListener() { | ||
public void actionPerformed(ActionEvent e) { | ||
doBrowseForFile(); | ||
} | ||
}; | ||
|
||
private void setup() { | ||
path = new JTextField(50); | ||
path.getDocument().addDocumentListener(pathChangeListener); | ||
browseButton = new JButton("Browse"); | ||
browseButton.addActionListener(browseActionListener); | ||
goButton = new JButton("Go"); | ||
goButton.setEnabled(false); | ||
goButton.addActionListener(goActionListener); | ||
|
||
Container pane = getContentPane(); | ||
pane.setLayout(new FlowLayout()); | ||
pane.add(path); | ||
pane.add(browseButton); | ||
pane.add(goButton); | ||
getRootPane().setDefaultButton(browseButton); | ||
pack(); | ||
} | ||
|
||
private void doBrowseForFile() { | ||
if (fileChooser == null) { | ||
fileChooser = new JFileChooser(); | ||
} | ||
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); | ||
if (fileChooser.showOpenDialog(this) != JFileChooser.CANCEL_OPTION) { | ||
File selectedFile = fileChooser.getSelectedFile(); | ||
path.setText(selectedFile.getPath()); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package biz.skizz.testdox; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Created by IntelliJ IDEA. | ||
* User: stevcc | ||
* Date: 11-Jun-2003 | ||
* Time: 19:09:15 | ||
* To change this template use Options | File Templates. | ||
*/ | ||
public class GuiTest extends TestCase { | ||
private Gui gui; | ||
private static JFileChooser selectSrcChooser = new JFileChooser() { | ||
public File getSelectedFile() { | ||
return selectedFile; | ||
} | ||
|
||
public int showOpenDialog(Component parent) throws HeadlessException { | ||
return JFileChooser.APPROVE_OPTION; | ||
} | ||
}; | ||
private static File selectedFile = new File("src"); | ||
private TestGenerator gen; | ||
|
||
protected void setUp() throws Exception { | ||
gen = new TestGenerator(); | ||
} | ||
|
||
protected void tearDown() throws Exception { | ||
super.tearDown(); | ||
} | ||
|
||
public void testShowsFileChooserIfBrowseIsClicked() { | ||
final boolean[] wasShown = new boolean[]{false}; | ||
|
||
JFileChooser chooser = new JFileChooser() { | ||
public int showOpenDialog(Component parent) throws HeadlessException { | ||
wasShown[0] = true; | ||
return JFileChooser.CANCEL_OPTION; | ||
} | ||
}; | ||
|
||
gui = new Gui("foo", gen); | ||
gui.fileChooser = chooser; | ||
gui.browseButton.doClick(); | ||
assertNotNull(gui.fileChooser); | ||
assertTrue(gui.fileChooser.isDirectorySelectionEnabled()); | ||
assertTrue(wasShown[0]); | ||
} | ||
|
||
public void testSomeGuiDefaults() { | ||
gui = new Gui("foo", gen); | ||
assertEquals(JFrame.EXIT_ON_CLOSE, gui.getDefaultCloseOperation()); | ||
} | ||
|
||
public void testSelectedFileIsShownInTextField() { | ||
|
||
gui = new Gui("foo", gen); | ||
gui.fileChooser = selectSrcChooser; | ||
gui.browseButton.doClick(); | ||
|
||
assertEquals("src", gui.path.getText()); | ||
} | ||
|
||
public void testSelectedFileIsNotShownIfUserClickedCancel() { | ||
JFileChooser chooser = new JFileChooser() { | ||
public File getSelectedFile() { | ||
return null; | ||
} | ||
|
||
public int showOpenDialog(Component parent) throws HeadlessException { | ||
return JFileChooser.CANCEL_OPTION; | ||
} | ||
}; | ||
|
||
gui = new Gui("foo", gen); | ||
gui.fileChooser = chooser; | ||
gui.browseButton.doClick(); | ||
|
||
assertEquals("", gui.path.getText()); | ||
} | ||
|
||
public void testGoButtonEnableUponUserFileSelection() { | ||
gui = new Gui("foo", gen); | ||
assertFalse(gui.goButton.isEnabled()); | ||
gui.fileChooser = selectSrcChooser; | ||
gui.browseButton.doClick(); | ||
assertTrue(gui.goButton.isEnabled()); | ||
} | ||
|
||
public static class TestGenerator implements Generator { | ||
private File file; | ||
|
||
public void generate(File file) { | ||
this.file = file; | ||
} | ||
|
||
public File getFile() { | ||
return file; | ||
} | ||
}; | ||
|
||
public void testClickGoButtonRunsIt() { | ||
|
||
gui = new Gui("foo", gen); | ||
gui.path.setText("src"); | ||
gui.goButton.setEnabled(true); | ||
gui.goButton.doClick(); | ||
|
||
assertEquals(selectedFile, gen.getFile()); | ||
} | ||
|
||
|
||
public void testGoButtonDisabledIfFileDoesNotExist() { | ||
gui = new Gui("foo", gen); | ||
gui.path.setText("non-existent-file"); | ||
assertFalse(gui.goButton.isEnabled()); | ||
} | ||
|
||
public void testEnteringPathFreehandEnablesGoButtonAndMakesItDefault() throws IOException, InterruptedException { | ||
gui = new Gui("foo", gen); | ||
gui.path.setText(selectedFile.getCanonicalPath()); | ||
assertTrue(gui.goButton.isEnabled()); | ||
assertTrue(gui.goButton.isDefaultButton()); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters