1 /*
2 * Lancaster University
3 * Computing Department
4 *
5 * Created by Eduardo Figueiredo
6 * Date: 22 Jun 2007
7 *
8 */
9 package ubc.midp.mobilephoto.core.ui.controller;
10
11 import javax.microedition.lcdui.Alert;
12 import javax.microedition.lcdui.Command;
13 import javax.microedition.lcdui.CommandListener;
14 import javax.microedition.lcdui.Display;
15 import javax.microedition.lcdui.Displayable;
16
17 import ubc.midp.mobilephoto.core.ui.MainUIMidlet;
18 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData;
19 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen;
20
21 /**
22 * [EF] Added in scenario 04.
23 * Purpose: (i) to structure controllers and (ii) simplify method handleCommand.
24 * @author Eduardo Figueiredo
25 *
26 */
27 public abstract class AbstractController implements CommandListener, ControllerInterface {
28
29 protected MainUIMidlet midlet;
30
31 //Define a successor to implement the Chain of Responsibility design pattern
32 private ControllerInterface nextController;
33
34 private AlbumData albumData;
35
36 //Define the basic screens
37 private AlbumListScreen albumListScreen;
38
39 /**
40 * @param midlet
41 * @param nextController
42 * @param albumData
43 * @param albumListScreen
44 * @param currentScreenName
45 */
46 public AbstractController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) {
47 this.midlet = midlet;
48 this.albumData = albumData;
49 this.albumListScreen = albumListScreen;
50 // [EF] Senario 04: A singleton ScreenSingleton was created in order to all other access it.
51 // [EF] I think some data need to be unique (e.g. currentScreenName) to make them consistent for all controllers.
52 }
53
54 /* (non-Javadoc)
55 * @see ubc.midp.mobilephoto.core.ui.controller.ControllerInterface#postCommand(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
56 */
57 public void postCommand(Command command) {
58 System.out.println("AbstractController::postCommand - Current controller is: " + this.getClass().getName());
59 //If the current controller cannot handle the command, pass it to the next
60 //controller in the chain.
61 if (handleCommand(command) == false) {
62 ControllerInterface next = getNextController();
63 if (next != null) {
64 System.out.println("Passing to next controller in chain: " + next.getClass().getName());
65 next.postCommand(command);
66 } else {
67 System.out.println("AbstractController::postCommand - Reached top of chain. No more handlers for command: " + command);
68 }
69 }
70
71 }
72
73 /*
74 * Handle events. For now, this just passes control off to a 'wrapper'
75 * so we can ensure, in order to use it in the aspect advice
76 * (non-Javadoc)
77 * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
78 */
79 public void commandAction(Command c, Displayable d) {
80 postCommand(c);
81 }
82
83
84 public void setAlbumListAsCurrentScreen(Alert a) {
85 setCurrentScreen(a, albumListScreen);
86 }
87
88 /**
89 * Set the current screen for display, after alert
90 */
91 public void setCurrentScreen(Alert a, Displayable d) {
92 Display.getDisplay(midlet).setCurrent(a, d);
93 }
94
95 /**
96 * [EF] RENAMED in Scenario 04: remove "Name". Purpose: avoid method name conflict
97 * Get the current screen name that is displayed
98 */
99 public Displayable getCurrentScreen() {
100 return Display.getDisplay(midlet).getCurrent();
101 }
102
103 /**
104 * Set the current screen for display
105 */
106 public void setCurrentScreen(Displayable d) {
107 Display.getDisplay(midlet).setCurrent(d);
108 }
109
110 /**
111 * @return the albumData
112 */
113 public AlbumData getAlbumData() {
114 return albumData;
115 }
116
117 /**
118 * @param albumData the albumData to set
119 */
120 public void setAlbumData(AlbumData albumData) {
121 this.albumData = albumData;
122 }
123
124 /**
125 * @return the nextController
126 */
127 public ControllerInterface getNextController() {
128 return nextController;
129 }
130
131 /**
132 * @param nextController the nextController to set
133 */
134 public void setNextController(ControllerInterface nextController) {
135 this.nextController = nextController;
136 }
137
138 /**
139 * [EF] Scenario 04: Just forward method.
140 * @return the currentStoreName
141 */
142 public String getCurrentStoreName() {
143 return ScreenSingleton.getInstance().getCurrentStoreName();
144 }
145
146 /**
147 * @return the albumListScreen
148 */
149 public AlbumListScreen getAlbumListScreen() {
150 return albumListScreen;
151 }
152 }
|