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