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.AlertType;
13 import javax.microedition.lcdui.Command;
14 import javax.microedition.lcdui.Display;
15 import javax.microedition.lcdui.List;
16 import javax.microedition.rms.RecordStoreFullException;
17
18 import lancs.midp.mobilephoto.lib.exceptions.InvalidPhotoAlbumNameException;
19 import lancs.midp.mobilephoto.lib.exceptions.PersistenceMechanismException;
20 import ubc.midp.mobilephoto.core.ui.MainUIMidlet;
21 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData;
22 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen;
23 import ubc.midp.mobilephoto.core.ui.screens.NewLabelScreen;
24 import ubc.midp.mobilephoto.core.util.Constants;
25
26 /**
27 * @author Eduardo Figueiredo
28 * Purpose: simplify method handleCommand() in the BaseController.
29 */
30 public class AlbumController extends AbstractController {
31
32 public AlbumController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) {
33 super(midlet, albumData, albumListScreen);
34 }
35
36 /* (non-Javadoc)
37 * @see ubc.midp.mobilephoto.core.ui.controller.ControllerInterface#handleCommand(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
38 */
39 public boolean handleCommand(Command command) {
40 String label = command.getLabel();
41 System.out.println( "<* AlbumController.handleCommand() *>: " + label);
42
43 if (label.equals("Reset")) {
44 System.out.println("<* BaseController.handleCommand() *> Reset Photo Album");
45 resetImageData();
46 ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN);
47 return true;
48 /** Case: Create PhotoAlbum **/
49 }else if (label.equals("New Photo Album")) {
50 System.out.println("Create new Photo Album here");
51 ScreenSingleton.getInstance().setCurrentScreenName(Constants.NEWALBUM_SCREEN);
52 NewLabelScreen canv = new NewLabelScreen("Add new Photo Album", NewLabelScreen.NEW_ALBUM);
53 canv.setCommandListener(this);
54 setCurrentScreen(canv);
55 canv = null;
56 return true;
57 /** Case: Delete Album Photo**/
58 }else if (label.equals("Delete Album")) {
59 System.out.println("Delete Photo Album here");
60 List down = (List) Display.getDisplay(midlet).getCurrent();
61 ScreenSingleton.getInstance().setCurrentScreenName(Constants.CONFIRMDELETEALBUM_SCREEN);
62 ScreenSingleton.getInstance().setCurrentStoreName(down.getString(down.getSelectedIndex()));
63 String message = "Would you like to remove the album "+ScreenSingleton.getInstance().getCurrentStoreName();
64 Alert deleteConfAlert = new Alert("Delete Photo Album", message,null,AlertType.CONFIRMATION);
65 deleteConfAlert.setTimeout(Alert.FOREVER);
66 deleteConfAlert.addCommand(new Command("Yes - Delete", Command.OK, 2));
67 deleteConfAlert.addCommand(new Command("No - Delete", Command.CANCEL, 2));
68 setAlbumListAsCurrentScreen(deleteConfAlert);
69 deleteConfAlert.setCommandListener(this);
70 return true;
71 /**
72 * TODO [EF] I think this confirmation questions are complicating the implementation
73 * [EF] How do you know that "Yes - Delete" is to delete Photo Album instead of Photo?
74 * Case: Yes delete Photo Album **/
75 }else if (label.equals("Yes - Delete")) {
76 try {
77 getAlbumData().deletePhotoAlbum(ScreenSingleton.getInstance().getCurrentStoreName());
78 } catch (PersistenceMechanismException e) {
79 Alert alert = new Alert( "Error", "The mobile database can not delete this photo album", null, AlertType.ERROR);
80 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
81 }
82 goToPreviousScreen();
83 return true;
84 /**
85 * [EF] Same question. How do you know that "No - Delete" is to delete Photo Album instead of Photo?
86 * Case: No delete Photo Album **/
87 }else if (label.equals("No - Delete")) {
88 goToPreviousScreen();
89 return true;
90 /**
91 * [EF] Again, see [EF] comments above.
92 * Case: Save new Photo Album **/
93 } else if (label.equals("Save")) {
94 try {
95 if (getCurrentScreen() instanceof NewLabelScreen) {
96 NewLabelScreen currentScreen = (NewLabelScreen)getCurrentScreen();
97 if (currentScreen.getFormType() == NewLabelScreen.NEW_ALBUM)
98 getAlbumData().createNewPhotoAlbum(currentScreen.getLabelName());
99 else if (currentScreen.getFormType() == NewLabelScreen.LABEL_PHOTO) {
100
101 }
102 }
103 } catch (PersistenceMechanismException e) {
104 Alert alert = null;
105 if (e.getCause() instanceof RecordStoreFullException)
106 alert = new Alert( "Error", "The mobile database is full", null, AlertType.ERROR);
107 else
108 alert = new Alert( "Error", "The mobile database can not add a new photo album", null, AlertType.ERROR);
109 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
110 return true;
111 } catch (InvalidPhotoAlbumNameException e) {
112 Alert alert = new Alert( "Error", "You have provided an invalid Photo Album name", null, AlertType.ERROR);
113 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
114 return true;
115 }
116 goToPreviousScreen();
117 return true;
118 }
119
120 return false;
121 }
122
123 /**
124 * This option is mainly for testing purposes. If the record store
125 * on the device or emulator gets into an unstable state, or has too
126 * many images, you can reset it, which clears the record stores and
127 * re-creates them with the default images bundled with the application
128 */
129 private void resetImageData() {
130 try {
131 getAlbumData().resetImageData();
132 } catch (PersistenceMechanismException e) {
133 Alert alert = null;
134 if (e.getCause() instanceof RecordStoreFullException)
135 alert = new Alert( "Error", "The mobile database is full", null, AlertType.ERROR);
136 else
137 alert = new Alert( "Error", "It is not possible to reset the database", null, AlertType.ERROR);
138 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent()); // TODO [EF] weird
139 return;
140 }
141
142 //Clear the names from the album list
143 for (int i = 0; i < getAlbumListScreen().size(); i++) {
144 getAlbumListScreen().delete(i);
145 }
146
147 //Get the default ones from the album
148 String[] albumNames = getAlbumData().getAlbumNames();
149 for (int i = 0; i < albumNames.length; i++) {
150 if (albumNames[i] != null) {
151 //Add album name to menu list
152 getAlbumListScreen().append(albumNames[i], null);
153 }
154 }
155 setCurrentScreen(getAlbumListScreen());
156 }
157
158 private void goToPreviousScreen() {
159 System.out.println("<* AlbumController.goToPreviousScreen() *>");
160 getAlbumListScreen().repaintListAlbum(getAlbumData().getAlbumNames());
161 setCurrentScreen( getAlbumListScreen() );
162 ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN);
163 }
164 }
|