1 /*
2 * Lancaster University
3 * Computing Department
4 *
5 * Created by Eduardo Figueiredo
6 * Date: 11 Aug 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
17 import lancs.midp.mobilephoto.lib.exceptions.UnavailablePhotoAlbumException;
18 import ubc.midp.mobilephoto.core.ui.MainUIMidlet;
19 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData;
20 import ubc.midp.mobilephoto.core.ui.datamodel.ImageData;
21 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen;
22 import ubc.midp.mobilephoto.core.ui.screens.PhotoListScreen;
23 import ubc.midp.mobilephoto.core.util.Constants;
24
25 /**
26 * @author Eduardo Figueiredo
27 *
28 */
29 public class PhotoListController extends AbstractController {
30
31 /**
32 * @param midlet
33 * @param nextController
34 * @param albumData
35 * @param albumListScreen
36 */
37 public PhotoListController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen) {
38 super(midlet, albumData, albumListScreen);
39 }
40
41 /* (non-Javadoc)
42 * @see ubc.midp.mobilephoto.core.ui.controller.ControllerInterface#handleCommand(java.lang.String)
43 */
44 public boolean handleCommand(Command command) {
45 String label = command.getLabel();
46 /** Case: Select PhotoAlbum to view **/
47 if (label.equals("Select")) {
48 // Get the name of the PhotoAlbum selected, and load image list from
49 // RecordStore
50 List down = (List) Display.getDisplay(midlet).getCurrent();
51 ScreenSingleton.getInstance().setCurrentStoreName(down.getString(down.getSelectedIndex()));
52 showImageList(getCurrentStoreName(), false, false);
53 ScreenSingleton.getInstance().setCurrentScreenName( Constants.IMAGELIST_SCREEN);
54 return true;
55 }
56
57 return false;
58 }
59
60 /**
61 * Show the list of images in the record store
62 * TODO: Refactor - Move this to ImageList class
63 */
64 public void showImageList(String recordName, boolean sort, boolean favorite) {
65
66 if (recordName == null)
67 recordName = getCurrentStoreName();
68
69 PhotoController photoController = new PhotoController(midlet, getAlbumData(), getAlbumListScreen());
70 photoController.setNextController(this);
71
72 PhotoListScreen imageList = new PhotoListScreen();
73 imageList.setCommandListener(photoController);
74
75 //Command selectCommand = new Command("Open", Command.ITEM, 1);
76 imageList.initMenu();
77
78 ImageData[] images = null;
79 try {
80 images = getAlbumData().getImages(recordName);
81 } catch (UnavailablePhotoAlbumException e) {
82 Alert alert = new Alert( "Error", "The list of photos can not be recovered", null, AlertType.ERROR);
83 Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
84 return;
85 }
86
87 if (images==null) return;
88
89 // [EF] Check if sort is true (Add in the Scenario 02)
90 if (sort) {
91 bubbleSort(images);
92 }
93
94 //loop through array and add labels to list
95 for (int i = 0; i < images.length; i++) {
96 if (images[i] != null) {
97 //Add album name to menu list
98
99 // [EF] Check if favorite is true (Add in the Scenario 03)
100 if (favorite) {
101 if (images[i].isFavorite())
102 imageList.append(images[i].getImageLabel(), null);
103 }
104 else
105 imageList.append(images[i].getImageLabel(), null);
106
107 }
108 }
109 setCurrentScreen(imageList);
110 //currentMenu = "list";
111 }
112
113 /**
114 * @param images
115 * @param pos1
116 * @param pos2
117 */
118 private void exchange(ImageData[] images, int pos1, int pos2) {
119 ImageData tmp = images[pos1];
120 images[pos1] = images[pos2];
121 images[pos2] = tmp;
122 }
123
124 /**
125 * Sorts an int array using basic bubble sort
126 *
127 * @param numbers the int array to sort
128 */
129 public void bubbleSort(ImageData[] images) {
130 System.out.print("Sorting by BubbleSort...");
131 //TODO implement bubbleSort
132 System.out.println("done.");
133 }
134 }
|