1 /* 
  2  * Lancaster University 
  3  * Computing Department 
  4  *  
  5  * Created by Eduardo Figueiredo 
  6  * Date: 22 Jul 2007 
  7  *  
  8  */ 
  9 // #if includeCopyPhoto || includeSmsFeature 
 10  
 11 package ubc.midp.mobilephoto.core.ui.controller; 
 12  
 13 import javax.microedition.lcdui.Alert; 
 14 import javax.microedition.lcdui.AlertType; 
 15 import javax.microedition.lcdui.Command; 
 16 import javax.microedition.lcdui.Display; 
 17  
 18 import javax.microedition.rms.RecordStoreFullException; 
 19  
 20 import lancs.midp.mobilephoto.lib.exceptions.ImageNotFoundException; 
 21 import lancs.midp.mobilephoto.lib.exceptions.ImagePathNotValidException; 
 22 import lancs.midp.mobilephoto.lib.exceptions.InvalidImageDataException; 
 23 import lancs.midp.mobilephoto.lib.exceptions.NullAlbumDataReference; 
 24 import lancs.midp.mobilephoto.lib.exceptions.PersistenceMechanismException; 
 25 import ubc.midp.mobilephoto.core.ui.MainUIMidlet; 
 26 import ubc.midp.mobilephoto.core.ui.datamodel.AlbumData; 
 27 import ubc.midp.mobilephoto.core.ui.datamodel.ImageAccessor; 
 28 import ubc.midp.mobilephoto.core.ui.datamodel.ImageData; 
 29 import ubc.midp.mobilephoto.core.ui.screens.AddPhotoToAlbum; 
 30 import ubc.midp.mobilephoto.core.ui.screens.AlbumListScreen; 
 31 import ubc.midp.mobilephoto.core.util.Constants; 
 32  
 33 //#ifdef includeSmsFeature 
 34 import ubc.midp.mobilephoto.core.ui.screens.PhotoViewScreen; 
 35 import javax.microedition.lcdui.Image; 
 36 //#endif 
 37 /** 
 38  * @author Eduardo Figueiredo 
 39  */ 
 40 public class PhotoViewController extends AbstractController { 
 41  
 42   String imageName = ""; 
 43    
 44   /** 
 45    * @param midlet 
 46    * @param nextController 
 47    * @param albumData 
 48    * @param albumListScreen 
 49    * @param currentScreenName 
 50    */ 
 51   public PhotoViewController(MainUIMidlet midlet, AlbumData albumData, AlbumListScreen albumListScreen, String imageName) { 
 52     super(midlet, albumData, albumListScreen); 
 53     this.imageName = imageName; 
 54   } 
 55  
 56   /* (non-Javadoc) 
 57    * @see ubc.midp.mobilephoto.core.ui.controller.ControllerInterface#handleCommand(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) 
 58    */ 
 59   public boolean handleCommand(Command c) { 
 60     String label = c.getLabel(); 
 61     System.out.println( "<* PhotoViewController.handleCommand() *> " + label); 
 62  
 63     /** Case: Copy photo to a different album */ 
 64     if (label.equals("Copy")) { 
 65       AddPhotoToAlbum copyPhotoToAlbum = new AddPhotoToAlbum("Copy Photo to Album"); 
 66       copyPhotoToAlbum.setPhotoName(imageName); 
 67       copyPhotoToAlbum.setLabePhotoPath("Copy to Album:"); 
 68       copyPhotoToAlbum.setCommandListener(this); 
 69  
 70       // #ifdef includeSmsFeature 
 71       if (((PhotoViewScreen)this.getCurrentScreen()).isFromSMS()) 
 72         {copyPhotoToAlbum.setImage(((PhotoViewScreen)this.getCurrentScreen()).getImage());} 
 73       //#endif   
 74        
 75           Display.getDisplay(midlet).setCurrent(copyPhotoToAlbum); 
 76        
 77       return true; 
 78     } 
 79      
 80     /** Case: Save a copy in a new album */ 
 81     else if (label.equals("Save Photo")) { 
 82       try { 
 83         ImageData imageData = null;   
 84         // #ifdef includeSmsFeature 
 85         Image img = ((AddPhotoToAlbum)this.getCurrentScreen()).getImage(); 
 86          
 87         if (img == null) 
 88         //#endif 
 89         { 
 90           try { 
 91             imageData = getAlbumData().getImageAccessor().getImageInfo(imageName); 
 92           } catch (ImageNotFoundException e) { 
 93             Alert alert = new Alert("Error", "The selected photo was not found in the mobile device", null, AlertType.ERROR); 
 94             Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent()); 
 95           } catch (NullAlbumDataReference e) { 
 96             this.setAlbumData( new AlbumData() ); 
 97             Alert alert = new Alert( "Error", "The operation is not available. Try again later !", null, AlertType.ERROR); 
 98             Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent()); 
 99           } 
100         } 
101          
102          
103         String photoname = ((AddPhotoToAlbum) getCurrentScreen()).getPhotoName(); 
104         String albumname = ((AddPhotoToAlbum) getCurrentScreen()).getPath(); 
105         ImageAccessor imageAccessor = getAlbumData().getImageAccessor(); 
106          
107         // #ifdef includeSmsFeature 
108         if (img != null) 
109           imageAccessor.addImageData(photoname, img, albumname); 
110         // #endif  
111          
112         // #if includeCopyPhoto && includeSmsFeature 
113         if (img == null) 
114         //#endif 
115            
116         // #if includeCopyPhoto  
117         imageAccessor.addImageData(photoname, imageData, albumname);  
118         // #endif  
119          
120        
121          
122       } catch (InvalidImageDataException e) { 
123         Alert alert = null; 
124         if (e instanceof ImagePathNotValidException) 
125           alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR); 
126         else 
127           alert = new Alert("Error", "The image file format is not valid", null, AlertType.ERROR); 
128         Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent()); 
129         return true; 
130         // alert.setTimeout(5000); 
131       } catch (PersistenceMechanismException e) { 
132         Alert alert = null; 
133         if (e.getCause() instanceof RecordStoreFullException) 
134           alert = new Alert("Error", "The mobile database is full", null, AlertType.ERROR); 
135         else 
136           alert = new Alert("Error", "The mobile database can not add a new photo", null, AlertType.ERROR); 
137         Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent()); 
138       } 
139       //((PhotoController)this.getNextController()).showImageList(ScreenSingleton.getInstance().getCurrentStoreName(), false, false); 
140         //ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN); 
141         getAlbumListScreen().repaintListAlbum(getAlbumData().getAlbumNames()); 
142       setCurrentScreen( getAlbumListScreen() ); 
143       ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN); 
144       return true; 
145     }else if ((label.equals("Cancel")) || (label.equals("Back"))){ 
146       getAlbumListScreen().repaintListAlbum(getAlbumData().getAlbumNames()); 
147       setCurrentScreen( getAlbumListScreen() ); 
148       ScreenSingleton.getInstance().setCurrentScreenName(Constants.ALBUMLIST_SCREEN); 
149       return true; 
150     } 
151      
152     return false; 
153   } 
154 } 
155 //#endif    
    |