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 // #ifdef includeCountViews
90 // [EF] Check if sort is true
91 if (sort) {
92 bubbleSort(images);
93 }
94 // #endif
95
96 //loop through array and add labels to list
97 for (int i = 0; i < images.length; i++) {
98 if (images[i] != null) {
99 //Add album name to menu list
100
101 // #ifdef includeFavourites
102 // [EF] Check if favorite is true
103 if (favorite) {
104 if (images[i].isFavorite())
105 imageList.append(images[i].getImageLabel(), null);
106 }
107 else
108 // #endif
109 imageList.append(images[i].getImageLabel(), null);
110
111 }
112 }
113 setCurrentScreen(imageList);
114 //currentMenu = "list";
115 }
116
117 // #ifdef includeCountViews
118 /**
119 * @param images
120 * @param pos1
121 * @param pos2
122 */
123 private void exchange(ImageData[] images, int pos1, int pos2) {
124 ImageData tmp = images[pos1];
125 images[pos1] = images[pos2];
126 images[pos2] = tmp;
127 }
128
129 /**
130 * Sorts an int array using basic bubble sort
131 *
132 * @param numbers the int array to sort
133 */
134 public void bubbleSort(ImageData[] images) {
135 System.out.print("Sorting by BubbleSort...");
136 // TODO implement bubbleSort
137 System.out.println("done.");
138 }
139 // #endif
140 }
|