Advertisement
Guest User

Word MVC Demo

a guest
Jan 26th, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. package WordMVC;
  2.  
  3. /** Application class that does wiring for MVC */
  4. public class AppMain
  5. {
  6.    public static void main(String [] args)
  7.    {
  8.         WordModel wm = new WordModel("exhilaration");
  9.         WordGUI gui = new WordGUI();
  10.         WordHTMLview view = new WordHTMLview();
  11.        
  12.         wm.addObserver(gui);
  13.         wm.addObserver(view);
  14.         gui.update(wm,null);
  15.         view.update(wm,null);
  16.         WordController controller = new WordController(wm);
  17.    }
  18.  
  19. }
  20. package WordMVC;
  21. import javax.swing.JFrame;
  22. import javax.swing.JButton;
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. /** Displays the buttons and responds to clicks */
  26. public class WordController extends JFrame implements ActionListener
  27. {
  28.     private WordModel model;
  29.  
  30.     private JButton left;
  31.     private JButton right;
  32.     public WordController(WordModel model)
  33.     {
  34.         this.model = model;
  35.         left = new JButton("<");
  36.         right = new JButton(">");
  37.         left.addActionListener(this);
  38.         right.addActionListener(this);
  39.         setLayout(new FlowLayout());
  40.         getContentPane().add(left);
  41.         getContentPane().add(right);
  42.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  43.  
  44.         pack();
  45.         setVisible(true);
  46.     }
  47.  
  48.    public void actionPerformed(ActionEvent evt)
  49.    {
  50.        if (evt.getSource() == left)
  51.        {
  52.            model.goLeft();
  53.        }
  54.        if (evt.getSource() == right)
  55.        {
  56.            model.goRight();
  57.        }
  58.        
  59.    }
  60.  
  61.  
  62.  
  63. }
  64.  
  65.  
  66. package WordMVC;
  67. import javax.swing.*;
  68. import java.util.Observer;
  69. import java.util.Observable;
  70. /** Graphical view of the word */
  71. public class WordGUI extends JFrame  implements Observer
  72. {
  73.     JLabel wordLabel = new JLabel();
  74.     public WordGUI()
  75.     {
  76.         wordLabel.setText("Text appears here");
  77.         getContentPane().add(wordLabel);
  78.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  79.         setLocation(0,100);
  80.         pack();
  81.         setVisible(true);
  82.     }      
  83.     public void update(Observable obs, Object obj)
  84.     {
  85.         wordLabel.setText(((WordModel)obs).getValue());
  86.     }  
  87.  
  88. }
  89. package WordMVC;
  90. import java.util.Observable;
  91.  
  92. public class WordHTMLview extends WordGUI
  93. {
  94.     public WordHTMLview()
  95.     {
  96.         super();
  97.         setLocation(200,100);
  98.     }
  99.    
  100.     @Override
  101.     public void update(Observable obs, Object obj)
  102.     {
  103.         String display = ((WordModel)obs).getValue();
  104.         wordLabel.setText("<HTML><I><BIG>" + display + "</BIG></I></HTML>");
  105.         pack();
  106.     }  
  107. }
  108. package WordMVC;
  109.  
  110. import java.util.Observable;
  111. /** A word that can be "rotated" left or right */
  112. public class WordModel extends Observable
  113. {
  114.    private String theWord;
  115.    public WordModel(String letters)
  116.    {
  117.       this.theWord = letters;
  118.    }
  119.    public void setValue(String letters)
  120.    {
  121.       this.theWord = letters;
  122.       setChanged();
  123.       notifyObservers();
  124.    }
  125.    public String getValue()
  126.    {
  127.       return new String(theWord);
  128.    }
  129.     public void goLeft()
  130.     {
  131.         theWord = theWord.substring(1) + theWord.charAt(0);
  132.         setChanged();
  133.         notifyObservers();
  134.     }
  135.     public void goRight()
  136.     {
  137.         theWord = theWord.charAt(theWord.length()-1) + theWord.substring(0,theWord.length()-1);
  138.         setChanged();
  139.         notifyObservers();
  140.     }
  141.  
  142. }
  143.  
  144. package WordMVC;
  145.  
  146.  
  147.  
  148. /**
  149.  * The test class WordModelTest.
  150.  *
  151.  * @author  jdalbey
  152.  * @version 2014.10.10
  153.  */
  154. public class WordModelTest extends junit.framework.TestCase implements java.util.Observer
  155. {
  156.     boolean updateFlag;
  157.  
  158.     public void testLeft()
  159.     {
  160.         updateFlag = false;
  161.         WordMVC.WordModel word1 = new WordMVC.WordModel("ABC");
  162.         word1.addObserver(this);
  163.         word1.goLeft();
  164.         assertTrue(updateFlag);
  165.         assertEquals("BCA", word1.getValue());
  166.     }
  167.  
  168.     public void testRight()
  169.     {
  170.         updateFlag = false;
  171.         WordMVC.WordModel word2 = new WordMVC.WordModel("XYZ");
  172.         word2.addObserver(this);
  173.         word2.goRight();
  174.         assertTrue(updateFlag);
  175.         assertEquals("ZXY", word2.getValue());
  176.     }
  177.     public void update(java.util.Observable obs, Object obj)
  178.     {
  179.         updateFlag =  true;
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement