1 /*
2 * Lancaster University
3 * Computing Department
4 *
5 * Created by Eduardo Figueiredo
6 * Date: 17 Jun 2007
7 *
8 */
9 package ubc.midp.mobilephoto.core.ui.controller;
10
11 import javax.microedition.lcdui.Alert;
12 import javax.microedition.lcdui.AlertType;
13 import javax.microedition.lcdui.Command;
14 import javax.microedition.lcdui.Display;
15 import javax.microedition.lcdui.Image;
16 import javax.microedition.lcdui.List;
17 import javax.microedition.rms.RecordStoreFullException;
18
19 import lancs.midp.mobilephoto.lib.exceptions.ImageNotFoundException;
20 import lancs.midp.mobilephoto.lib.exceptions.ImagePathNotValidException;
21 import lancs.midp.mobilephoto.lib.exceptions.InvalidImageDataException;
22 import lancs.midp.mobilephoto.lib.exceptions.NullAlbumDataReference;
23 import lancs.midp.mobilephoto.lib.exceptions.PersistenceMechanismException;
24 import ubc.midp.mobilephoto.core.ui.MainUIMidlet;
25 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData;
26 import ubc.midp.mobilephoto.core.ui.datamodel.ImageData;
27 import ubc.midp.mobilephoto.core.ui.screens.AddPhotoToAlbum;
28 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen;
29 import ubc.midp.mobilephoto.core.ui.screens.NewLabelScreen;
30 import ubc.midp.mobilephoto.core.ui.screens.PhotoViewScreen;
31 import ubc.midp.mobilephoto.core.util.Constants;
32
33
34 import ubc.midp.mobilephoto.sms.SmsSenderController;
35
36 /**
37 * @author Eduardo Figueiredo
38 */
39 public class PhotoController extends PhotoListController {
40
41 private ImageData image;
42 private NewLabelScreen screen;
43
44 public PhotoController (MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) {
45 super(midlet, albumData, albumListScreen);
46 }
47
48 public boolean handleCommand(Command command) {
49 String label = command.getLabel();
50 System.out.println( "<* PhotoController.handleCommand() *> " + label);
51
52 if (label.equals("View")) {
53 String selectedImageName = getSelectedImageName();
54 showImage(selectedImageName);
55
56 try {
57 ImageData image = getAlbumData().getImageAccessor().getImageInfo(selectedImageName);
58 image.increaseNumberOfViews();
59 updateImage(image);
60 System.out.println("<* BaseController.handleCommand() *> Image = " + selectedImageName + "; # views = " + image.getNumberOfViews());
61 } catch (ImageNotFoundException e) {
62 Alert alert = new Alert("Error", "The selected photo was not found in the mobile device", null, AlertType.ERROR);
63 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
64 } catch (NullAlbumDataReference e) {
65 this.setAlbumData( new AlbumData() );
66 Alert alert = new Alert( "Error", "The operation is not available. Try again later !", null, AlertType.ERROR);
67 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
68 }catch (InvalidImageDataException e) {
69 Alert alert = new Alert( "Error", "The image data is not valid", null, AlertType.ERROR);
70 alert.setTimeout(5000);
71 } catch (PersistenceMechanismException e) {
72 Alert alert = new Alert( "Error", "It was not possible to recovery the selected image", null, AlertType.ERROR);
73 alert.setTimeout(5000);
74 }
75
76
77 ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGE_SCREEN);
78 return true;
79
80 /** Case: Add photo * */
81 } else if (label.equals("Add")) {
82 ScreenSingleton.getInstance().setCurrentScreenName(Constants.ADDPHOTOTOALBUM_SCREEN);
83 AddPhotoToAlbum form = new AddPhotoToAlbum("Add new Photo to Album");
84 form.setCommandListener(this);
85 setCurrentScreen(form);
86 return true;
87 /** Case: Save Add photo * */
88 } else if (label.equals("Save Photo")) {
89 try {
90 getAlbumData().addNewPhotoToAlbum(((AddPhotoToAlbum) getCurrentScreen()).getPhotoName(),
91 ((AddPhotoToAlbum) getCurrentScreen()).getPath(), getCurrentStoreName());
92 } catch (InvalidImageDataException e) {
93 Alert alert = null;
94 if (e instanceof ImagePathNotValidException)
95 alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR);
96 else
97 alert = new Alert("Error", "The image file format is not valid", null, AlertType.ERROR);
98 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
99 return true;
100 // alert.setTimeout(5000);
101 } catch (PersistenceMechanismException e) {
102 Alert alert = null;
103 if (e.getCause() instanceof RecordStoreFullException)
104 alert = new Alert("Error", "The mobile database is full", null, AlertType.ERROR);
105 else
106 alert = new Alert("Error", "The mobile database can not add a new photo", null, AlertType.ERROR);
107 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
108 }
109 return goToPreviousScreen();
110 /** Case: Delete selected Photo from recordstore * */
111 } else if (label.equals("Delete")) {
112 String selectedImageName = getSelectedImageName();
113 try {
114 getAlbumData().deleteImage(getCurrentStoreName(), selectedImageName);
115 } catch (PersistenceMechanismException e) {
116 Alert alert = new Alert("Error", "The mobile database can not delete this photo", null, AlertType.ERROR);
117 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
118 return true;
119 } catch (ImageNotFoundException e) {
120 Alert alert = new Alert("Error", "The selected photo was not found in the mobile device", null, AlertType.ERROR);
121 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
122 return true;
123 }
124 showImageList(getCurrentStoreName(), false, false);
125 ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
126 return true;
127
128 /** Case: Edit photo label
129 */
130 } else if (label.equals("Edit Label")) {
131 String selectedImageName = getSelectedImageName();
132 try {
133 image = getAlbumData().getImageAccessor().getImageInfo(
134 selectedImageName);
135 // PhotoController photoController = new PhotoController(image,
136 // this);
137 NewLabelScreen formScreen = new NewLabelScreen(
138 "Edit Label Photo", NewLabelScreen.LABEL_PHOTO);
139 formScreen.setCommandListener(this);
140 this.setScreen(formScreen);
141 setCurrentScreen(formScreen);
142 formScreen = null;
143 } catch (ImageNotFoundException e) {
144 Alert alert = new Alert(
145 "Error",
146 "The selected photo was not found in the mobile device",
147 null, AlertType.ERROR);
148 Display.getDisplay(midlet).setCurrent(alert,
149 Display.getDisplay(midlet).getCurrent());
150 } catch (NullAlbumDataReference e) {
151 this.setAlbumData( new AlbumData() );
152 Alert alert = new Alert( "Error", "The operation is not available. Try again later !", null, AlertType.ERROR);
153 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
154 }
155 return true;
156
157 /**
158 * Case: Sort photos by number of views
159 */
160 } else if (label.equals("Sort by Views")) {
161 showImageList(getCurrentStoreName(), true, false);
162 ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
163
164 return true;
165
166 /**
167 * Case: Set photo as favorite
168 */
169 } else if (label.equals("Set Favorite")) {
170 String selectedImageName = getSelectedImageName();
171 try {
172 ImageData image = getAlbumData().getImageAccessor().getImageInfo(selectedImageName);
173 image.toggleFavorite();
174 updateImage(image);
175 System.out.println("<* BaseController.handleCommand() *> Image = "+ selectedImageName + "; Favorite = " + image.isFavorite());
176 } catch (ImageNotFoundException e) {
177 Alert alert = new Alert("Error", "The selected photo was not found in the mobile device", null, AlertType.ERROR);
178 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
179 } catch (NullAlbumDataReference e) {
180 this.setAlbumData(new AlbumData());
181 Alert alert = new Alert( "Error", "The operation is not available. Try again later !", null, AlertType.ERROR);
182 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
183 } catch (InvalidImageDataException e) {
184 Alert alert = null;
185 if (e instanceof ImagePathNotValidException)
186 alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR);
187 else
188 alert = new Alert("Error", "The image file format is not valid", null, AlertType.ERROR);
189 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
190 } catch (PersistenceMechanismException e) {
191 Alert alert = null;
192 if (e.getCause() instanceof RecordStoreFullException)
193 alert = new Alert( "Error", "The mobile database is full", null, AlertType.ERROR);
194 else
195 alert = new Alert( "Error", "The mobile database can not update new informations", null, AlertType.ERROR);
196 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
197 }
198 return true;
199
200 /**
201 * Case: View favorite photos
202 */
203 } else if (label.equals("View Favorites")) {
204 showImageList(getCurrentStoreName(), false, true);
205 ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
206
207 return true;
208
209 /** Case: Save new Photo Label */
210 } else if (label.equals("Save")) {
211 System.out
212 .println("<* PhotoController.handleCommand() *> Save Photo Label = "
213 + this.screen.getLabelName());
214 this.getImage().setImageLabel(this.screen.getLabelName());
215 try {
216 updateImage(image);
217 } catch (InvalidImageDataException e) {
218 Alert alert = null;
219 if (e instanceof ImagePathNotValidException)
220 alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR);
221 else
222 alert = new Alert("Error", "The image file format is not valid", null, AlertType.ERROR);
223 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
224 } catch (PersistenceMechanismException e) {
225 Alert alert = new Alert("Error", "The mobile database can not update this photo", null, AlertType.ERROR);
226 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
227 }
228 return goToPreviousScreen();
229
230 /** Case: Go to the Previous or Fallback screen * */
231 } else if (label.equals("Back")) {
232 return goToPreviousScreen();
233
234 /** Case: Cancel the current screen and go back one* */
235 } else if (label.equals("Cancel")) {
236 return goToPreviousScreen();
237
238 }
239
240 return false;
241 }
242
243 void updateImage(ImageData image) throws InvalidImageDataException, PersistenceMechanismException {
244 getAlbumData().getImageAccessor().updateImageInfo(image, image);
245
246 }
247
248 /**
249 * Get the last selected image from the Photo List screen.
250 * TODO: This really only gets the selected List item.
251 * So it is only an image name if you are on the PhotoList screen...
252 * Need to fix this
253 */
254 public String getSelectedImageName() {
255 List selected = (List) Display.getDisplay(midlet).getCurrent();
256 if (selected == null)
257 System.out.println("Current List from display is NULL!");
258 String name = selected.getString(selected.getSelectedIndex());
259 return name;
260 }
261
262 /**
263 * Show the current image that is selected
264 */
265 public void showImage(String name) {
266 // [EF] Instead of replicating this code, I change to use the method "getSelectedImageName()".
267 Image storedImage = null;
268 try {
269 storedImage = getAlbumData().getImageFromRecordStore(getCurrentStoreName(), name);
270 } catch (ImageNotFoundException e) {
271 Alert alert = new Alert( "Error", "The selected photo was not found in the mobile device", null, AlertType.ERROR);
272 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
273 return;
274 } catch (PersistenceMechanismException e) {
275 Alert alert = new Alert( "Error", "The mobile database can open this photo", null, AlertType.ERROR);
276 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
277 return;
278 }
279
280 //We can pass in the image directly here, or just the name/model pair and have it loaded
281 PhotoViewScreen canv = new PhotoViewScreen(storedImage);
282 canv.setCommandListener( this );
283 AbstractController nextcontroller = this;
284 PhotoViewController controller = new PhotoViewController(midlet, getAlbumData(), getAlbumListScreen(), name);
285 controller.setNextController(nextcontroller);
286 canv.setCommandListener(controller);
287 nextcontroller = controller;
288
289 SmsSenderController smscontroller = new SmsSenderController(midlet, getAlbumData(), getAlbumListScreen(), name);
290 //this.setNextController(smscontroller);
291 smscontroller.setNextController(nextcontroller);
292 canv.setCommandListener(smscontroller);
293 nextcontroller = smscontroller;
294
295 setCurrentScreen(canv);
296 }
297
298 /**
299 * TODO [EF] update this method or merge with method of super class.
300 * Go to the previous screen
301 */
302 private boolean goToPreviousScreen() {
303 System.out.println("<* PhotoController.goToPreviousScreen() *>");
304 String currentScreenName = ScreenSingleton.getInstance().getCurrentScreenName();
305 if (currentScreenName.equals(Constants.ALBUMLIST_SCREEN)) {
306 System.out.println("Can't go back here...Should never reach this spot");
307 } else if (currentScreenName.equals(Constants.IMAGE_SCREEN)) {
308 //Go to the image list here, not the main screen...
309 showImageList(getCurrentStoreName(), false, false);
310 ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
311 return true;
312 }
313 else if (currentScreenName.equals(Constants.ADDPHOTOTOALBUM_SCREEN)) {
314 showImageList(getCurrentStoreName(), false, false);
315 ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
316 return true;
317 }
318 return false;
319 }
320
321 /**
322 * @param image the image to set
323 */
324 public void setImage(ImageData image) {
325 this.image = image;
326 }
327
328 /**
329 * @return the image
330 */
331 public ImageData getImage() {
332 return image;
333 }
334
335 public void setScreen(NewLabelScreen screen) {
336 this.screen = screen;
337 }
338
339 public NewLabelScreen getScreen() {
340 return screen;
341 }
342
343 }
|