Permalink
Browse files

Merge pull request #1 from KroneckerDelta/thomas

Thomas
  • Loading branch information...
2 parents 93cfdd6 + 7379445 commit a0e4cceaeaec0ff947b175acc4dc839db3e6b462 @schauder committed Feb 11, 2014
@@ -0,0 +1,25 @@
+package de.schauderhaft.architecture.example;
+
+import javax.swing.JFrame;
+
+public class CrossWordController {
+
+ private InputView view;
+ private CrosswordGame crosswordGame;
+
+ public CrossWordController(InputView view, CrosswordGame crosswordGame) {
+ super();
+ this.view = view;
+ this.crosswordGame = crosswordGame;
+ }
+
+ public void start() {
+
+ view.addActionForSubmit(new WordInputActionListener(view, crosswordGame));
+
+ view.getInputView().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ view.getInputView().setVisible(true);
+
+ }
+
+}
@@ -0,0 +1,20 @@
+package de.schauderhaft.architecture.example;
+
+import java.util.Arrays;
+import java.util.HashSet;
+
+public class GuiStarter {
+
+ public static void main(String[] args) {
+
+ InputView view = new InputView();
+
+ CrosswordGame crosswordGame = new CrosswordGame(new HashSet<String>(
+ Arrays.asList("Haus", "Maus", "Auto")));
+
+ CrossWordController controller = new CrossWordController(view,
+ crosswordGame);
+ controller.start();
+
+ }
+}
@@ -0,0 +1,51 @@
+package de.schauderhaft.architecture.example;
+
+import java.awt.Dimension;
+import java.awt.FlowLayout;
+import java.awt.event.ActionListener;
+
+import javax.swing.JButton;
+import javax.swing.JFrame;
+import javax.swing.JTextField;
+
+public class InputView {
+
+ JFrame frame = new JFrame("CrossWord");
+
+ JTextField inputfield = new JTextField();
+ JButton submit = new JButton("submit");
+ ScoreBoard score = new ScoreBoard();
+
+ public InputView() {
+ inputfield.setPreferredSize(new Dimension(80, 20));
+ frame.getContentPane().setLayout(new FlowLayout());
+ frame.getContentPane().add(inputfield);
+ frame.getContentPane().add(submit);
+ frame.getContentPane().add(score.getScorePanel());
+ frame.pack();
+ }
+
+ public JFrame getInputView() {
+ return frame;
+ }
+
+ public void addActionForSubmit(ActionListener action) {
+ submit.addActionListener(action);
+ }
+
+ /**
+ * Access to inputfield
+ *
+ * @return
+ */
+ public String getInput() {
+ return inputfield.getText();
+
+ }
+
+ public void newScore(int points) {
+ score.newScore(points);
+
+ }
+
+}
@@ -0,0 +1,31 @@
+package de.schauderhaft.architecture.example;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+
+public class ScoreBoard {
+
+ JPanel container = new JPanel();
+ JLabel text = new JLabel("Points:");
+ JLabel points = new JLabel("0");
+
+ public ScoreBoard() {
+ container.add(text);
+ container.add(points);
+
+ }
+
+ public JPanel getScorePanel() {
+ return container;
+ }
+
+ /**
+ * Add new value to label.
+ *
+ * @param newScore
+ */
+ public void newScore(int newScore) {
+ points.setText(Integer.valueOf(newScore).toString());
+ }
+
+}
@@ -0,0 +1,25 @@
+package de.schauderhaft.architecture.example;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+
+public class WordInputActionListener implements ActionListener {
+
+ InputView view;
+ CrosswordGame game;
+
+ public WordInputActionListener(InputView view, CrosswordGame game) {
+ super();
+ this.view = view;
+ this.game = game;
+ }
+
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ String word = view.getInput().trim();
+ int points = game.submit(word);
+ view.newScore(points);
+
+ }
+
+}
@@ -0,0 +1,54 @@
+package de.schauderhaft.architecture.example;
+
+import static org.junit.Assert.assertTrue;
+
+import java.awt.Component;
+
+import javax.swing.JButton;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+
+import org.junit.Test;
+
+public class InputViewTest {
+
+ @Test
+ public void viewShouldHaveInputField() {
+ InputView view = new InputView();
+ Component[] components = view.getInputView().getContentPane()
+ .getComponents();
+ componentsAssertTrue(components, JTextField.class);
+
+ }
+
+ @Test
+ public void viewShouldHaveSubmitButton() {
+ InputView view = new InputView();
+ Component[] components = view.getInputView().getContentPane()
+ .getComponents();
+ componentsAssertTrue(components, JButton.class);
+
+ }
+
+ @Test
+ public void inputViewShouldHavePanelForScore() {
+ InputView view = new InputView();
+ Component[] components = view.getInputView().getContentPane()
+ .getComponents();
+ componentsAssertTrue(components, JPanel.class);
+
+ }
+
+ private <T extends Component> void componentsAssertTrue(
+ Component[] components, Class<T> clazz) {
+ boolean fieldExists = false;
+
+ for (Component c : components) {
+ if (clazz.isInstance(c)) {
+ fieldExists = true;
+ }
+ }
+ assertTrue(fieldExists);
+ }
+
+}
@@ -0,0 +1,25 @@
+package de.schauderhaft.architecture.example;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.awt.Component;
+
+import org.junit.Test;
+
+public class ScoreBoardTest {
+
+ ScoreBoard underTest = new ScoreBoard();
+
+ @Test
+ public void panelIsNotNull() {
+ assertNotNull(underTest.getScorePanel());
+ }
+
+ @Test
+ public void scoreBoardShouldHaveTwoComponents() throws Exception {
+
+ Component[] components = underTest.getScorePanel().getComponents();
+ assertEquals(2, components.length);
+ }
+}

0 comments on commit a0e4cce

Please sign in to comment.