OOP2

Worked on a basic MVC framework thingy.

Author
Maarten Vngngdn
Date
Dec. 17, 2015, 11:05 p.m.
Hash
4a4a80b00250b8ae025039fedac1bc2569219e2b
Parent
32118fc57410de3029d46fb88ad28662fc957f0b
Modified files
MVC template/AbstractController.java
MVC template/ClockDigitalView.java
New MVC/AbstractController.java
New MVC/AbstractModel.java
New MVC/AbstractView.java
New MVC/Controller.java
New MVC/MainWindow.java

MVC template/AbstractController.java

1 addition and 4 deletions.

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

MVC template/ClockDigitalView.java

1 addition and 1 deletion.

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

New MVC/AbstractController.java

47 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observer; // Idem for Observers.
+
2
+
3
abstract class Controller {
+
4
+
5
	// Members
+
6
	private Observable model;
+
7
	private Observer view;
+
8
+
9
	// Constructor
+
10
	public Controller(Observable model, Observer view) { 
+
11
		this.model = model;
+
12
		this.view = view;
+
13
	}
+
14
+
15
	/**
+
16
	 * Sets the model for this controller.
+
17
	 * @param model The model that is to be controlled.
+
18
	 */
+
19
	public void setModel(Observable model) {
+
20
		this.model = model;
+
21
	}
+
22
+
23
	/**
+
24
	 * Returns the model that is being observed.
+
25
	 * @return The model being observed.
+
26
	 */
+
27
	public Observable getModel() {	
+
28
		return model;
+
29
	}
+
30
+
31
	/**
+
32
	 * Sets the view for this controller.
+
33
	 * @param view The view for this controller.
+
34
	 */
+
35
	public void setView(Observer view) {
+
36
		this.view = view;
+
37
	}
+
38
+
39
	/**
+
40
	 * Returns the view for this controller.
+
41
	 * @return The view for this controller.
+
42
	 */
+
43
	public Observer getView() {	
+
44
		return view;
+
45
	}
+
46
}
+
47

New MVC/AbstractModel.java

13 additions and 0 deletions.

View changes Hide changes
+
1
+
2
/**
+
3
 * AbstractModel - An abstraction layer for a model in an MVC-architecture.
+
4
 * @author Maarten Vangeneugden - 1438256
+
5
 */
+
6
public class AbstractModel {
+
7
+
8
	public AbstractModel() { 
+
9
+
10
	}
+
11
+
12
}
+
13

New MVC/AbstractView.java

62 additions and 0 deletions.

View changes Hide changes
+
1
import java.util.Observer;
+
2
+
3
/**
+
4
 * AbstractView - An abstraction layer for a view class in an MVC architecture.
+
5
 * @author Maarten Vangeneugden - 1438256
+
6
 */
+
7
abstract class AbstractView implements Observer {
+
8
+
9
	// Members
+
10
	private Observable model;
+
11
	private Controller controller;
+
12
+
13
	// Constructor
+
14
	public AbstractView(Observable model, Controller controller) { 
+
15
		this.model = model;
+
16
		this.controller = controller;
+
17
	}
+
18
+
19
	/**
+
20
	 * Sets the model associated with this view.
+
21
	 * @param model The model associated with this view.
+
22
	 */
+
23
	public void setModel(Observable model) {
+
24
		this.model = model;
+
25
	}
+
26
+
27
	/**
+
28
	 * Returns the model associated with this view.
+
29
	 * @return The model associated with this view.
+
30
	 */
+
31
	public Observable getModel() {	
+
32
		return model;
+
33
	}
+
34
+
35
	/**
+
36
	 * Sets the controller associated with this view.
+
37
	 * @param controller The controller associated with this view.
+
38
	 */
+
39
	public void setController(Controller controller) {
+
40
		this.controller = controller;
+
41
	}
+
42
+
43
	/**
+
44
	 * Returns the controller associated with this view.
+
45
	 * @return The controller associated with this view.
+
46
	 */
+
47
	public Controller getController() {	
+
48
		return controller;
+
49
	}
+
50
+
51
	/**
+
52
	 * This is an abstract method, in that it must be overridden in order to use this class. The method itself is an override of Observer.update().
+
53
	 * Please note that this function is not supposed to be called because that's something you can do with functions (methods, whatevs); This function is automatically triggered by the Observable class whenever its state is modified. So what is supposed to happen here, is that the subclass of this class takes the trigger class, and asks about its state to update his own stuff.
+
54
	 * @see Observable.update
+
55
	 * @param observable The class that is being observed. This is also the class that triggers this function. It's the observed class screaming it had an update, if you will. It literally sends itself as a parameter.
+
56
	 * @param information An Object containing additional information. This can be any class, so it's up to the subclass to deal with this, or not to deal with it at all.
+
57
	 * @post The View is updated so that it is a correct representation of its associated Model.
+
58
	 */
+
59
	@Override
+
60
	abstract void update(Observable observable, Object information);
+
61
}
+
62

New MVC/Controller.java

47 additions and 0 deletions.

View changes Hide changes
+
1
+
2
class Controller {
+
3
+
4
	// Members
+
5
	private Observable model;
+
6
	private Observer view;
+
7
+
8
	// Constructor
+
9
	public Controller() { 
+
10
		this.model = null;
+
11
		this.view = null;
+
12
	}
+
13
+
14
	/**
+
15
	 * Sets the model for this controller.
+
16
	 * @param model The model that is to be controlled.
+
17
	 */
+
18
	public void setModel(Observable model) {
+
19
		this.model = model;
+
20
	}
+
21
+
22
	/**
+
23
	 * Returns the model that is being observed.
+
24
	 * @return The model being observed.
+
25
	 */
+
26
	public Observable getModel() {	
+
27
		return model;
+
28
	}
+
29
+
30
	/**
+
31
	 * Sets the view for this controller.
+
32
	 * @param view The view for this controller.
+
33
	 */
+
34
	public void setView(Observer view) {
+
35
		this.view = view;
+
36
	}
+
37
+
38
	/**
+
39
	 * Returns the view for this controller.
+
40
	 * @return The view for this controller.
+
41
	 */
+
42
	public Observer getView() {	
+
43
		return view;
+
44
	}
+
45
+
46
}
+
47

New MVC/MainWindow.java

70 additions and 0 deletions.

View changes Hide changes
+
1
import javax.swing.*;
+
2
import java.util.List;
+
3
+
4
import java.awt.event.WindowAdapter;
+
5
import java.awt.event.WindowEvent;
+
6
/**
+
7
 * MainWindow - Abstraction layer for a window in an MVC architecture. It abstracts a lot of fluff caused by Swing being Swing. It also takes care of the state and layout of the window itself, allows for dialog creating, therefore making 80% of what's most used in simple programs with a GUI possible in just 20% of the time it used to take.
+
8
 * @author Maarten Vangeneugden - 1438256
+
9
 */
+
10
class MainWindow extends AbstractView {
+
11
	
+
12
	private JFrame window;
+
13
+
14
	/**
+
15
	 * Constructor. It creates an empty window. Period.
+
16
	 * @param windowTitle The title of the created window.
+
17
	 * @param widgets A list of widgets that can be added to the window.
+
18
	 */
+
19
	public MainWindow(String windowTitle, List<JComponent> widgets) {
+
20
+
21
		super(null, null);
+
22
		// First, we create the window itself:
+
23
		JFrame window = new JFrame(windowTitle); // Create the new window.
+
24
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set its close action. (Laymen's terms: Make it close down when you close it down.)
+
25
		setWindow(window);
+
26
+
27
		// Then, we add the given widgets to it:
+
28
		if(widgets != null) {
+
29
			for (JComponent widget : widgets) {
+
30
				addWidget(widget);
+
31
			}
+
32
		}
+
33
	}
+
34
+
35
	public void setWindow(JFrame window) {
+
36
		this.window = window;
+
37
	}
+
38
+
39
	public JFrame getWindow() {
+
40
		return window;
+
41
	}
+
42
+
43
	/**
+
44
	 * Adds a given widget to the window.
+
45
	 * It's highly recommended to have the widget's fluff (i.e. Actionlisteners) all prepped before adding.
+
46
	 * @param widget The widget to be added to the window.
+
47
	 */
+
48
	public void addWidget(JComponent widget) {
+
49
		getWindow().getContentPane().add(widget);
+
50
	}
+
51
+
52
	/**
+
53
	 * Returns a list containing all widgets in this window.
+
54
	 * @return A list of widgets.
+
55
	 */
+
56
	public List<JComponent> getWidgets() {
+
57
		return new ArrayList(getWindow().getContentPane().getComponents());
+
58
	}
+
59
+
60
	/**
+
61
	 * Removes the given widget from the window.
+
62
	 * @param widget The widget that should be removed.
+
63
	 * @post The given widget is no longer in the window.
+
64
	 */
+
65
	public void removeWidget(JComponent widget) {
+
66
		getWindow().getContentPane().removeComponent(widget);
+
67
	}
+
68
+
69
}
+
70