OOP2

Started on stockManagement2, got bored. Then I created a folder MVC template, which will store an MVC protocol for challenges.

Author
Maarten Vangeneugden
Date
Dec. 17, 2015, 5:15 p.m.
Hash
32118fc57410de3029d46fb88ad28662fc957f0b
Parent
d9e1e905f979f023565eb47a8da2451a91b0c5fe
Modified files
04/stockManagement2/Book.java
04/stockManagement2/BookList.java
MVC template/AbstractController.java
MVC template/AbstractView.java
MVC template/Clock.java
MVC template/ClockAnalogView.java
MVC template/ClockController.java
MVC template/ClockDigitalView.java
MVC template/ClockModel.java
MVC template/ClockTools.java
MVC template/ClockUpdate.java
MVC template/Controller.java
MVC template/DigitalClockController.java
MVC template/View.java
MVC template/readme.txt

04/stockManagement2/Book.java

37 additions and 0 deletions.

View changes Hide changes
+
1
+
2
	private Enum<BookType> bookType;
+
3
	private int price;
+
4
	private int inStock;
+
5
+
6
	public Book(Enum<BookType> bookType, int price, int inStock) { 
+
7
		this.bookType = bookType;
+
8
		this.price = price;
+
9
		this.inStock = inStock;
+
10
	}
+
11
+
12
	public void setBookType(Enum<BookType> bookType) {
+
13
		this.bookType = bookType;
+
14
	}
+
15
+
16
	public Enum<BookType> getBookType() {	
+
17
		return bookType;
+
18
	}
+
19
+
20
	public void setPrice(int price) {
+
21
		this.price = price;
+
22
	}
+
23
+
24
	public int getPrice() {	
+
25
		return price;
+
26
	}
+
27
+
28
	public void setInStock(int inStock) {
+
29
		this.inStock = inStock;
+
30
	}
+
31
+
32
	public int getInStock() {	
+
33
		return inStock;
+
34
	}
+
35
+
36
}
+
37

04/stockManagement2/BookList.java

28 additions and 0 deletions.

View changes Hide changes
+
1
public class BookList {
+
2
+
3
	private List<Book> bookList;
+
4
+
5
	public BookList(List<Book> bookList) { 
+
6
		this.bookList = bookList;
+
7
+
8
	}
+
9
+
10
	private List<int> prices;
+
11
	public void setPrices(List<int> prices) {
+
12
		this.prices = prices;
+
13
	}
+
14
+
15
+
16
+
17
	private generateBookList() {
+
18
+
19
	public void setBookList(List<Book> bookList) {
+
20
		this.bookList = bookList;
+
21
	}
+
22
+
23
	public List<Book> getBookList() {	
+
24
		return bookList;
+
25
	}
+
26
+
27
}
+
28

MVC template/AbstractController.java

37 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 *
+
4
 * @author jvermeulen
+
5
 */
+
6
public abstract class AbstractController implements Controller {
+
7
    private Observable mModel;
+
8
    private View mView;
+
9
    
+
10
    public AbstractController(Observable model) {
+
11
        // Set the model.
+
12
        setModel(model);
+
13
    }
+
14
    
+
15
    
+
16
    @Override
+
17
    public void setView(View view) {
+
18
        mView = view;
+
19
    }
+
20
+
21
    @Override
+
22
    public View getView() {
+
23
        return mView;
+
24
    }
+
25
+
26
    @Override
+
27
    public void setModel(Observable model) {
+
28
        mModel = model;
+
29
    }
+
30
+
31
    @Override
+
32
    public Observable getModel() {
+
33
        return mModel;
+
34
    }
+
35
    
+
36
}
+
37

MVC template/AbstractView.java

73 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observer;
+
2
+
3
/**
+
4
 *
+
5
 * @author jvermeulen
+
6
 */
+
7
public abstract class AbstractView implements View, Observer {
+
8
+
9
    private Observable mModel;
+
10
    private Controller mController;
+
11
    
+
12
    /**
+
13
     * Empty constructor so that the model and controller can be set later.
+
14
     */
+
15
    public AbstractView() {        
+
16
    }
+
17
    
+
18
    public AbstractView(Observable model, Controller controller) {
+
19
        // Set the model.
+
20
        setModel(model);
+
21
        // If a controller was supplied, use it. Otherwise let the first call to 
+
22
        // getController() create the default controller.
+
23
        if (controller != null) {
+
24
            setController(controller);
+
25
        }
+
26
    }
+
27
    
+
28
    @Override
+
29
    public void setController(Controller controller) {
+
30
        mController = controller;
+
31
        // Tell the controller this object is its view.
+
32
        getController().setView(this);
+
33
    }
+
34
+
35
    @Override
+
36
    public Controller getController() {
+
37
        // If a controller hasn't been defined yet...
+
38
        if (mController == null) {
+
39
            // ...make one. Note that defaultController is normally overriden by 
+
40
            // the AbstractView subclass so that it returns the appropriate 
+
41
            // controller for the view.
+
42
            setController(defaultController(getModel()));
+
43
        }
+
44
        
+
45
        return mController;
+
46
    }
+
47
+
48
    @Override
+
49
    public void setModel(Observable model) {
+
50
        mModel = model;
+
51
    }
+
52
+
53
    @Override
+
54
    public Observable getModel() {
+
55
        return mModel;
+
56
    }
+
57
+
58
    @Override
+
59
    public Controller defaultController(Observable model) {
+
60
        return null;
+
61
    }
+
62
+
63
    /**
+
64
     * A do-nothing implementation of the Observer interface's update method.
+
65
     * Subclasses of AbstractView will provide a concrete implementation for 
+
66
     * this method.
+
67
     */
+
68
    @Override
+
69
    public void update(Observable o, Object arg) {    
+
70
    }
+
71
    
+
72
}
+
73

MVC template/Clock.java

78 additions and 0 deletions.

View changes Hide changes
+
1
import java.awt.event.WindowEvent;
+
2
import javax.swing.BoxLayout;
+
3
import javax.swing.JFrame;
+
4
+
5
/**
+
6
 * Main user interface for the application. Makes sure the model, views and
+
7
 * controllers are connected to each other.
+
8
 * @author jvermeulen
+
9
 */
+
10
public class Clock {
+
11
    // The clock data (i.e., the Model).
+
12
    private ClockModel mModel;
+
13
    
+
14
    // A digital view of the clock.
+
15
    private ClockDigitalView mDigitalView;
+
16
    
+
17
    // A toolbar for controlling the clock.
+
18
    private ClockTools mTools;
+
19
    
+
20
    // An analog view of the clock.
+
21
    private final ClockAnalogView mAnalogView;
+
22
    
+
23
    public Clock(int hour, int minute, int second) {
+
24
        // Create the data model.
+
25
        mModel = new ClockModel();
+
26
+
27
        // Create the digital clock view.
+
28
        mDigitalView = new ClockDigitalView(mModel, null);
+
29
        mModel.addObserver(mDigitalView);
+
30
+
31
        // Create the analog clock view.
+
32
        mAnalogView = new ClockAnalogView(mModel, null);
+
33
        mModel.addObserver(mAnalogView);
+
34
        
+
35
        // Create the clock tools view.
+
36
        mTools = new ClockTools(mModel, null);
+
37
        mModel.addObserver(mTools);
+
38
        
+
39
        // Set the time.
+
40
        mModel.setTime(hour, minute, second);
+
41
+
42
        // Start the clock ticking.
+
43
        mModel.start();
+
44
    }
+
45
    
+
46
    public void createGUI() {
+
47
        JFrame frame = new JFrame("Clock");
+
48
        frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
+
49
        frame.getContentPane().add(mDigitalView.getUI());
+
50
        frame.getContentPane().add(mAnalogView);
+
51
        frame.getContentPane().add(mTools.getUI());                
+
52
        frame.addWindowListener(new WindowAdapter() {
+
53
            @Override
+
54
            public void windowClosing(WindowEvent e) {
+
55
                System.exit(0);
+
56
            }
+
57
        });
+
58
        frame.pack();
+
59
        frame.setVisible(true);
+
60
    }
+
61
    
+
62
    private static void createAndShowGUI() {
+
63
        Clock clock = new Clock(9, 10, 22);
+
64
        clock.createGUI();
+
65
    }
+
66
    
+
67
    public static void main(String[] args) {
+
68
        //Schedule a job for the event-dispatching thread:
+
69
        //creating and showing this application's GUI.
+
70
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
+
71
            @Override
+
72
            public void run() {
+
73
                createAndShowGUI();
+
74
            }
+
75
        });
+
76
    }
+
77
}
+
78

MVC template/ClockAnalogView.java

218 additions and 0 deletions.

View changes Hide changes
+
1
import java.awt.Dimension;
+
2
import java.awt.Graphics;
+
3
import java.awt.Graphics2D;
+
4
import java.awt.RenderingHints;
+
5
import java.awt.geom.AffineTransform;
+
6
import java.awt.geom.Ellipse2D;
+
7
import java.awt.geom.GeneralPath;
+
8
import java.util.Calendar;
+
9
import java.util.Observable;
+
10
import javax.swing.JComponent;
+
11
+
12
/**
+
13
 * An analog clock View for the ClockModel. This View has no user inputs, so
+
14
 * no controller is required. This class implements the View interface, and
+
15
 * subclasses JComponent directly, to illustrate that not all Views need to
+
16
 * inherit from AbstractView.
+
17
 *
+
18
 * Note: it is not necessary to understand all the details of the rendering 
+
19
 * of the analog clock.
+
20
 *
+
21
 * @author jvermeulen (inspired by IBM Java2D tutorial)
+
22
 */
+
23
public class ClockAnalogView extends JComponent implements View {
+
24
+
25
    private Observable mModel;
+
26
    private Controller mController;
+
27
+
28
    int mHour;
+
29
    int mMinute;
+
30
    int mSecond;
+
31
+
32
    ClockAnalogView(Observable model, Controller controller) {
+
33
        mModel = model;
+
34
        mController = controller;
+
35
    }
+
36
+
37
    @Override
+
38
    public void setController(Controller controller) {
+
39
        mController = controller;
+
40
    }
+
41
+
42
    @Override
+
43
    public Controller getController() {
+
44
        return mController;
+
45
    }
+
46
+
47
    @Override
+
48
    public void setModel(Observable model) {
+
49
        mModel = model;
+
50
    }
+
51
+
52
    @Override
+
53
    public Observable getModel() {
+
54
        return mModel;
+
55
    }
+
56
+
57
    @Override
+
58
    public Controller defaultController(Observable model) {
+
59
        return null;
+
60
    }
+
61
+
62
    @Override
+
63
    public void update(Observable o, Object info) {
+
64
        // Cast info to ClockUpdate type.
+
65
        ClockUpdate clockInfo = (ClockUpdate) info;
+
66
+
67
        // Store hour, minute, second for repainting.
+
68
        mHour = clockInfo.getHour()+5; // +5 Geeft een latere tijdzone weer. Dit is namelijk opdracht 3.
+
69
        mMinute = clockInfo.getMinute();
+
70
        mSecond = clockInfo.getSecond();
+
71
+
72
        // Using a little trigonometry, set the transforms to rotate
+
73
        // each hand into the proper position.  Center the rotation
+
74
        // around the pivot point (50, 50) instead of the origin
+
75
        hourTransform.setToRotation(((double) mHour) *
+
76
                                            (Math.PI / 6.0), 50, 50);
+
77
        minuteTransform.setToRotation(((double) mMinute) *
+
78
                                            (Math.PI / 30.0), 50, 50);
+
79
        secondTransform.setToRotation(((double) mSecond) *
+
80
                                            (Math.PI / 30.0), 50, 50);
+
81
+
82
        repaint();
+
83
    }
+
84
+
85
+
86
    /**
+
87
     * Sets this components preferred size to 150x150.
+
88
     * @return
+
89
     */
+
90
    @Override
+
91
    public Dimension getPreferredSize() {
+
92
        return new Dimension(100, 100);
+
93
    }
+
94
+
95
    // Create a shape for the face of the clock
+
96
    protected static Ellipse2D face = new Ellipse2D.Float(3, 3, 94, 94);
+
97
+
98
    // Create a path that represents a tick mark
+
99
    protected static GeneralPath tick = new GeneralPath();
+
100
    static // http://www.jusfortechies.com/java/core-java/static-blocks.php
+
101
    {
+
102
        tick.moveTo(100, 100);
+
103
        tick.moveTo(49, 0);
+
104
        tick.lineTo(51, 0);
+
105
        tick.lineTo(51, 6);
+
106
        tick.lineTo(49, 6);
+
107
        tick.lineTo(49, 0);
+
108
    }
+
109
+
110
    // Create a cool hour hand
+
111
    protected static GeneralPath hourHand = new GeneralPath();
+
112
    static
+
113
    {
+
114
        hourHand.moveTo(50, 15);
+
115
        hourHand.lineTo(53, 50);
+
116
        hourHand.lineTo(50, 53);
+
117
        hourHand.lineTo(47, 50);
+
118
        hourHand.lineTo(50, 15);
+
119
    }
+
120
+
121
    // Create a cool minute hand
+
122
    protected static GeneralPath minuteHand = new GeneralPath();
+
123
    static
+
124
    {
+
125
        minuteHand.moveTo(50, 2);
+
126
        minuteHand.lineTo(53, 50);
+
127
        minuteHand.lineTo(50, 58);
+
128
        minuteHand.lineTo(47, 50);
+
129
        minuteHand.lineTo(50, 2);
+
130
    }
+
131
+
132
    // And a cool second hand
+
133
    protected static GeneralPath secondHand = new GeneralPath();
+
134
    static
+
135
    {
+
136
        secondHand.moveTo(49, 5);
+
137
        secondHand.lineTo(51, 5);
+
138
        secondHand.lineTo(51, 62);
+
139
        secondHand.lineTo(49, 62);
+
140
        secondHand.lineTo(49, 5);
+
141
    }
+
142
+
143
    // Create some colors for the pieces of the clock
+
144
    protected static Color faceColor = new Color(220, 220, 220);
+
145
    protected static Color hourColor = Color.red.darker();
+
146
    protected static Color minuteColor = Color.blue.darker();
+
147
    protected static Color secondColor = new Color(180, 180, 0);
+
148
    protected static Color pinColor = Color.gray.brighter();
+
149
+
150
    // Create circles for the pivot and center pin
+
151
    protected Ellipse2D pivot = new Ellipse2D.Float(47, 47, 6, 6);
+
152
    protected Ellipse2D centerPin = new Ellipse2D.Float(49, 49, 2, 2);
+
153
+
154
+
155
    // Create three transforms that center around the pivot point
+
156
    protected AffineTransform hourTransform =
+
157
                    AffineTransform.getRotateInstance(0, 50, 50);
+
158
    protected AffineTransform minuteTransform =
+
159
                    AffineTransform.getRotateInstance(0, 50, 50);
+
160
    protected AffineTransform secondTransform =
+
161
                    AffineTransform.getRotateInstance(0, 50, 50);
+
162
+
163
    // Create a timer that fires once a second and a Calendar
+
164
    // instance for getting the time values
+
165
    // protected Timer timer = new Timer(1000, this);
+
166
    protected Calendar calendar = Calendar.getInstance();
+
167
+
168
    // This is an alternative to creating a UI delegate.  Since JPanel's
+
169
    // paint() method only paints the border and backgound, we can just
+
170
    // override the paint method of the component to do the graphics.
+
171
    public void paint(Graphics g)
+
172
    {
+
173
        // Call the superclass first to paint the border (if one is assigned)
+
174
        super.paint(g);
+
175
+
176
+
177
        // Get the graphics context and turn on anti-aliasing
+
178
        Graphics2D g2 = (Graphics2D) g;
+
179
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
+
180
                            RenderingHints.VALUE_ANTIALIAS_ON);
+
181
+
182
        // Set the paint for the clock face and fill it in
+
183
        g2.setPaint(faceColor);
+
184
        g2.fill(face);
+
185
+
186
        // Set the paint to black and draw the clock's outline
+
187
        g2.setPaint(Color.black);
+
188
        g2.draw(face);
+
189
+
190
        // Fill in the 12 ticks around the face of the clock
+
191
        for (double p = 0.0; p < 12.0; p += 1.0)
+
192
        {
+
193
            // This is probably terribly inefficient and should be
+
194
            // done statically or in the constructor - draw the
+
195
            // tick as a transformed shape that is rotated.
+
196
            g2.fill(tick.createTransformedShape(
+
197
                AffineTransform.getRotateInstance((Math.PI / 6.0)  * p,
+
198
                        50, 50)));
+
199
        }
+
200
+
201
        // Set the paint and draw the hour hand.  It is lowest in the
+
202
        // 'z-order' so will appear underneath the other hands.  Notice
+
203
        // how each hand is transformed by a different <AffineTransform>.
+
204
        g2.setPaint(hourColor);
+
205
        g2.fill(hourHand.createTransformedShape(hourTransform));
+
206
+
207
        // Set the paint and draw the minute hand, the second hand,
+
208
        // the pivot and the center pin
+
209
        g2.setPaint(minuteColor);
+
210
        g2.fill(minuteHand.createTransformedShape(minuteTransform));
+
211
        g2.setPaint(secondColor);
+
212
        g2.fill(secondHand.createTransformedShape(secondTransform));
+
213
        g2.fill(pivot);
+
214
        g2.setPaint(pinColor);
+
215
        g2.fill(centerPin);
+
216
    }
+
217
}
+
218

MVC template/ClockController.java

42 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Calendar;
+
2
+
3
/**
+
4
 * A clock controller that allows the clock to be started, stopped and reset.
+
5
 * @author jvermeulen
+
6
 */
+
7
+
8
//Toevoeging van Maarten V.: onSystemTime() voor updaten systeemtijd. Ook het aanpassen van ClockDigitalView wordt hier behandeld.
+
9
//Extra toevoeging voor OEF04: onEdit() als de gebruiker via het tekstveld een aanpassing doorvoert.
+
10
public class ClockController extends AbstractController {
+
11
    public ClockController(Observable model) {
+
12
        super(model);
+
13
    }
+
14
    
+
15
    public void onStart() {
+
16
        ((ClockModel)getModel()).start();
+
17
    }
+
18
    
+
19
    public void onStop() {
+
20
        ((ClockModel)getModel()).stop();
+
21
    }
+
22
    
+
23
    public void onReset() {
+
24
        ((ClockModel)getModel()).setTime(0,0,0);
+
25
    }
+
26
+
27
	public void onSystemTime() {
+
28
			Calendar calendar = Calendar.getInstance();
+
29
			((ClockModel)getModel()).setTime(calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
+
30
	}
+
31
+
32
    public void onEdit(String givenTime) {
+
33
			String[] parts = givenTime.split(":");
+
34
			System.out.println("Dit is al iets");
+
35
+
36
        ((ClockModel)getModel()).setTime(
+
37
			Integer.parseInt(parts[0]),
+
38
			Integer.parseInt(parts[1]),
+
39
			Integer.parseInt(parts[2]));
+
40
    }
+
41
}
+
42

MVC template/ClockDigitalView.java

84 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observable;
+
2
import javax.swing.JComponent;
+
3
import javax.swing.JTextField;
+
4
import java.awt.event.ActionEvent;
+
5
import java.awt.event.ActionListener;
+
6
+
7
/**
+
8
 * A digital clock View for the ClockModel. This View has no user inputs, so no 
+
9
 * Controller is required.
+
10
 * @author jvermeulen
+
11
 */
+
12
//BRING THE CHANGE! This should be updatable, so controllers and stuff will be added.
+
13
public class ClockDigitalView extends AbstractView {
+
14
    
+
15
    // The user interface for this view.
+
16
    private JTextField mClock;
+
17
    
+
18
    public ClockDigitalView(Observable model, DigitalClockController controller) {
+
19
        super(model, controller);
+
20
        init();
+
21
    }
+
22
    
+
23
    /**
+
24
     * Initializes the user interface.
+
25
     */
+
26
    private void init() {
+
27
        mClock = new JTextField();
+
28
		mClock.addActionListener(new ActionListener() {
+
29
				@Override
+
30
				public void actionPerformed(ActionEvent e) {
+
31
					((DigitalClockController)getController()).onEdit(mClock.getText());
+
32
				}
+
33
		});
+
34
    }
+
35
+
36
    /**
+
37
     * Updates the state of the on-screen digital clock.
+
38
     * Invoked automatically by ClockModel.
+
39
     * @param o The ClockModel object that is broadcasting an update
+
40
     * @param info A ClockUpdate instance describing the changes that have 
+
41
     * occurred in the ClockModel
+
42
     */
+
43
    @Override
+
44
    public void update(Observable o, Object info) {    
+
45
        // Cast info to ClockUpdate type.
+
46
        ClockUpdate clockInfo = (ClockUpdate) info;
+
47
        
+
48
        // Create a String representing the time.        
+
49
        String timeString = formatTime(clockInfo);
+
50
+
51
        // Display the new time in the clock text field.
+
52
        mClock.setText(timeString);
+
53
+
54
        // Fade the color of the display if the clock isn't running.
+
55
        if (clockInfo.isRunning()) {
+
56
          mClock.setBackground(Color.white);
+
57
        } else {
+
58
          mClock.setBackground(Color.gray);
+
59
        }
+
60
    }
+
61
    
+
62
    /**
+
63
     * Convenience method to return the user interface component. We don't need 
+
64
     * this if we implement View directly and directly subclass a GUI component.
+
65
     * @return the JComponent representing this View.
+
66
     */
+
67
	@Override
+
68
	public Controller defaultController(Observable model) {
+
69
			return new DigitalClockController(model);
+
70
	}
+
71
+
72
    public JComponent getUI() {
+
73
        return mClock;
+
74
    }
+
75
    
+
76
    private String formatTime(ClockUpdate info) {
+
77
        return String.format("%d:%d:%d", 
+
78
                             info.getHour(), 
+
79
                             info.getMinute(), 
+
80
                             info.getSecond());
+
81
    }        
+
82
    
+
83
}
+
84

MVC template/ClockModel.java

153 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Date;
+
2
import java.util.GregorianCalendar;
+
3
import java.util.Observable;
+
4
import java.util.Timer;
+
5
import java.util.TimerTask;
+
6
import javax.swing.SwingUtilities;
+
7
+
8
/**
+
9
 *
+
10
 * @author jvermeulen
+
11
 */
+
12
public class ClockModel extends Observable {
+
13
    private int mHour;
+
14
    private int mMinute;
+
15
    private int mSecond;
+
16
    
+
17
    private final long mTickInterval = 1000; // tick every second
+
18
    private Timer mTimer;
+
19
    private boolean mIsRunning;
+
20
    
+
21
    public ClockModel() {
+
22
        // By default, set the clock time to the current system time.
+
23
        Calendar now = new GregorianCalendar();        
+
24
        setTime(now.get(Calendar.HOUR_OF_DAY), now.get(Calendar.MINUTE), 
+
25
                now.get(Calendar.SECOND));        
+
26
    }
+
27
    
+
28
	public ClockModel(int hour, int minute, int second) {
+
29
			setTime(hour, minute, second);
+
30
		}
+
31
    /**
+
32
     * Helper class to run the tick() method multiple times 
+
33
     * (e.g., every 1 second).
+
34
     */
+
35
    class TickTask extends TimerTask {
+
36
        public void run() {
+
37
            tick();
+
38
        }
+
39
    }
+
40
    
+
41
    public void start() {
+
42
        if (!mIsRunning) {
+
43
            mIsRunning = true;
+
44
+
45
            // Schedule the tick() method to run every mTickInterval milliseconds.
+
46
            mTimer = new Timer();
+
47
            mTimer.schedule(new TickTask(), 0, mTickInterval); 
+
48
            
+
49
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
50
                                               mIsRunning);
+
51
            setChanged();
+
52
            notifyObservers(info);
+
53
        }
+
54
    }
+
55
    
+
56
    public void stop() {
+
57
        if (mIsRunning) {
+
58
            mIsRunning = false;
+
59
            
+
60
            // Stop the timer.
+
61
            mTimer.cancel();
+
62
            mTimer = null;
+
63
            
+
64
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
65
                                               mIsRunning);
+
66
            setChanged();
+
67
            notifyObservers(info);
+
68
        }
+
69
    }
+
70
    
+
71
    /**
+
72
     * Sets the current time. Notifies observers of any change in time.
+
73
     * @param hour The new hour.
+
74
     * @param minute The new minute
+
75
     * @param second The new second
+
76
     */
+
77
    public void setTime(int hour, int minute, int second) {
+
78
        if (hour != mHour && isValidHour(hour)) {
+
79
            mHour = hour;
+
80
            setChanged();
+
81
        }
+
82
        
+
83
        if (minute != mMinute && isValidMinute(minute)) {
+
84
            mMinute = minute;
+
85
            setChanged();
+
86
        }
+
87
                
+
88
        if (second != mSecond && isValidSecond(second)) {
+
89
            mSecond = second;
+
90
            setChanged();
+
91
        }
+
92
        
+
93
        // If the model has changed, notify Views.
+
94
        if (hasChanged()) {
+
95
            ClockUpdate info = new ClockUpdate(mHour, mMinute, mSecond, 
+
96
                                               mIsRunning);
+
97
            notifyObservers(info);
+
98
        }
+
99
    }    
+
100
    
+
101
    /**
+
102
     * Checks to see if a number is a valid hour (i.e. in [0,23]).
+
103
     * @param hour The hour to check
+
104
     * @return True if this is a valid hour
+
105
     */
+
106
    private boolean isValidHour(int hour) {
+
107
        return (hour >= 0 && hour <= 23);
+
108
    }
+
109
    
+
110
    /**
+
111
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
112
     * @param minute The minute to check
+
113
     * @return True if this is a valid minute
+
114
     */
+
115
    private boolean isValidMinute(int minute) {
+
116
        return (minute >= 0 && minute <= 59);
+
117
    }
+
118
    
+
119
    /**
+
120
     * Checks to see if a number is a valid minute (i.e. in [0,59]).
+
121
     * @param second The second to check
+
122
     * @return True if this is a valid second
+
123
     */
+
124
    private boolean isValidSecond(int second) {
+
125
        return (second >= 0 && second <= 59);
+
126
    }
+
127
    
+
128
    private void tick() {
+
129
        // Get the current time
+
130
        int h = mHour;
+
131
        int m = mMinute;
+
132
        int s = mSecond;
+
133
        
+
134
        // Increment the current second, adjusting the minute and hour 
+
135
        // if necessary.
+
136
        s += 1;
+
137
        if (s > 59) {
+
138
            s = 0;
+
139
            m += 1;
+
140
            if (m > 59) {
+
141
                m = 0;
+
142
                h += 1;
+
143
                if (h > 23) {
+
144
                    h = 0;
+
145
                }
+
146
            }
+
147
        }
+
148
+
149
        // Set the new time.
+
150
        setTime(h, m, s);
+
151
    }
+
152
}
+
153

MVC template/ClockTools.java

110 additions and 0 deletions.

View changes Hide changes
+
1
import java.awt.event.ActionEvent;
+
2
import java.awt.event.ActionListener;
+
3
import java.util.Observable;
+
4
import javax.swing.JButton;
+
5
import javax.swing.JComponent;
+
6
import javax.swing.JPanel;
+
7
+
8
/**
+
9
 *
+
10
 * @author jvermeulen
+
11
 */
+
12
public class ClockTools extends AbstractView {
+
13
    // The user interface for this view.
+
14
    private JPanel mTools;
+
15
    private JButton mStart;
+
16
    private JButton mStop;
+
17
    private JButton mReset;
+
18
	//Toevoeging van Vngngdn: Knop toevoegen die de klok op huidige systeemtijd instelt:
+
19
	private JButton mSystemTime;
+
20
    
+
21
    public ClockTools(Observable model, ClockController controller) {
+
22
        super(model, controller);
+
23
        init();
+
24
    }
+
25
+
26
    private void init() {
+
27
        mTools = new JPanel();
+
28
        mTools.setLayout(new GridLayout(1, 3));
+
29
        mStart = new JButton("Start");
+
30
        mStart.setEnabled(false);
+
31
        mStop = new JButton("Stop");
+
32
        mStop.setEnabled(false);
+
33
        mReset = new JButton("Reset");
+
34
		mSystemTime = new JButton("Systeemtijd");
+
35
        
+
36
        // handle events: pass to controller
+
37
        mStart.addActionListener(new ActionListener() {
+
38
+
39
            @Override
+
40
            public void actionPerformed(ActionEvent e) {
+
41
                ((ClockController)getController()).onStart();
+
42
            }
+
43
        });
+
44
        
+
45
        mStop.addActionListener(new ActionListener() {
+
46
+
47
            @Override
+
48
            public void actionPerformed(ActionEvent e) {
+
49
                ((ClockController)getController()).onStop();
+
50
            }
+
51
        });
+
52
                
+
53
        mReset.addActionListener(new ActionListener() {
+
54
+
55
            @Override
+
56
            public void actionPerformed(ActionEvent e) {
+
57
                ((ClockController)getController()).onReset();
+
58
            }
+
59
        });
+
60
        
+
61
        mSystemTime.addActionListener(new ActionListener() {
+
62
+
63
            @Override
+
64
            public void actionPerformed(ActionEvent e) {
+
65
                ((ClockController)getController()).onSystemTime();
+
66
            }
+
67
		});
+
68
+
69
        mTools.add(mStart);
+
70
        mTools.add(mStop);
+
71
        mTools.add(mReset);       
+
72
 		mTools.add(mSystemTime);
+
73
	}
+
74
    
+
75
        /**
+
76
     * Updates the state of the clock tools.
+
77
     * Invoked automatically by ClockModel.
+
78
     * @param o The ClockModel object that is broadcasting an update
+
79
     * @param info A ClockUpdate instance describing the changes that have 
+
80
     * occurred in the ClockModel
+
81
     */
+
82
    @Override
+
83
    public void update(Observable o, Object info) {    
+
84
        // Cast info to ClockUpdate type.
+
85
        ClockUpdate clockInfo = (ClockUpdate) info;
+
86
        
+
87
        if (clockInfo.isRunning()) {
+
88
            mStop.setEnabled(true);
+
89
            mStart.setEnabled(false);
+
90
        } else {
+
91
            mStart.setEnabled(true);
+
92
            mStop.setEnabled(false);
+
93
        }
+
94
    }
+
95
    
+
96
    @Override
+
97
    public Controller defaultController(Observable model) {
+
98
        return new ClockController(model);        
+
99
    }
+
100
    
+
101
    /**
+
102
     * Convenience method to return the user interface component. We don't need 
+
103
     * this if we implement View directly and directly subclass a GUI component.
+
104
     * @return 
+
105
     */
+
106
    public JComponent getUI() {
+
107
        return mTools;
+
108
    }
+
109
}
+
110

MVC template/ClockUpdate.java

75 additions and 0 deletions.

View changes Hide changes
+
1
 * Represent a clock update including all necessary information.
+
2
 * @author jvermeulen
+
3
 */
+
4
public class ClockUpdate {
+
5
    private int mHour;
+
6
    private int mMinute;
+
7
    private int mSecond;
+
8
    private boolean mIsRunning; 
+
9
    
+
10
    
+
11
    ClockUpdate(int hour, int minute, int second, boolean isRunning) {
+
12
        mHour = hour;
+
13
        mMinute = minute;
+
14
        mSecond = second;
+
15
        mIsRunning = isRunning;
+
16
    }
+
17
+
18
    /**
+
19
     * @return the mHour
+
20
     */
+
21
    public int getHour() {
+
22
        return mHour;
+
23
    }
+
24
+
25
    /**
+
26
     * @param hour the hour to set
+
27
     */
+
28
    public void setHour(int hour) {
+
29
        this.mHour = hour;
+
30
    }
+
31
+
32
    /**
+
33
     * @return the mMinute
+
34
     */
+
35
    public int getMinute() {
+
36
        return mMinute;
+
37
    }
+
38
+
39
    /**
+
40
     * @param minute the minute to set
+
41
     */
+
42
    public void setMinute(int minute) {
+
43
        this.mMinute = minute;
+
44
    }
+
45
+
46
    /**
+
47
     * @return the mSecond
+
48
     */
+
49
    public int getSecond() {
+
50
        return mSecond;
+
51
    }
+
52
+
53
    /**
+
54
     * @param second the second to set
+
55
     */
+
56
    public void setSecond(int second) {
+
57
        this.mSecond = second;
+
58
    }
+
59
+
60
    /**
+
61
     * @return the mIsRunning
+
62
     */
+
63
    public boolean isRunning() {
+
64
        return mIsRunning;
+
65
    }
+
66
+
67
    /**
+
68
     * @param isRunning the isRunning to set
+
69
     */
+
70
    public void setRunning(boolean isRunning) {
+
71
        this.mIsRunning = isRunning;
+
72
    }
+
73
    
+
74
}
+
75

MVC template/Controller.java

13 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 *
+
4
 * @author jvermeulen
+
5
 */
+
6
public interface Controller {
+
7
+
8
    void setView(View view);
+
9
    View getView();
+
10
    void setModel(Observable model);
+
11
    Observable getModel();
+
12
}
+
13

MVC template/DigitalClockController.java

23 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * A clock controller that allows the clock to be started, stopped and reset.
+
4
 * @author jvermeulen
+
5
 */
+
6
+
7
//Toevoeging van Maarten V.: onSystemTime() voor updaten systeemtijd. Ook het aanpassen van ClockDigitalView wordt hier behandeld.
+
8
//Extra toevoeging voor OEF04: onEdit() als de gebruiker via het tekstveld een aanpassing doorvoert.
+
9
public class DigitalClockController extends AbstractController {
+
10
    public DigitalClockController(Observable model) {
+
11
        super(model);
+
12
    }
+
13
+
14
    public void onEdit(String givenTime) {
+
15
			String[] parts = givenTime.split(":");
+
16
+
17
        ((ClockModel)getModel()).setTime(
+
18
			Integer.parseInt(parts[0]),
+
19
			Integer.parseInt(parts[1]),
+
20
			Integer.parseInt(parts[2]));
+
21
    }
+
22
}
+
23

MVC template/View.java

17 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observer;
+
2
+
3
/**
+
4
 *
+
5
 * @author jvermeulen
+
6
 */
+
7
public interface View extends Observer {
+
8
      
+
9
    void setController(Controller controller);
+
10
    Controller getController();
+
11
    
+
12
    void setModel(Observable model);
+
13
    Observable getModel();
+
14
    
+
15
    Controller defaultController(Observable model);
+
16
}
+
17

MVC template/readme.txt

3 additions and 0 deletions.

View changes Hide changes
+
1
+
2
Aanpassen gebeurt direct na RETURN. Om te voorkomen dat de cursor steeds verspringt, zet de klok op pauze.
+
3