// #ifdef includePhoto
  // [NC] Added in the scenario 07
  public void showImage(String name) {
    // [EF] Instead of replicating this code, I change to use the method "getSelectedImageName()".
    Image storedImage = null;
    try {
      storedImage =
          ((ImageAlbumData) getAlbumData()).getImageFromRecordStore(getCurrentStoreName(), name);
    } catch (ImageNotFoundException e) {
      Alert alert =
          new Alert(
              "Error",
              "The selected photo was not found in the mobile device",
              null,
              AlertType.ERROR);
      Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      return;
    } catch (PersistenceMechanismException e) {
      Alert alert =
          new Alert("Error", "The mobile database can open this photo", null, AlertType.ERROR);
      Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      return;
    }

    // We can pass in the image directly here, or just the name/model pair and have it loaded
    PhotoViewScreen canv = new PhotoViewScreen(storedImage);
    canv.setCommandListener(this);
    AbstractController nextcontroller = this;
    // #if includeCopyMedia || includeSmsFeature
    // [NC] Changed in the scenario 07: just the first line below to support generic
    // AbstractController
    PhotoViewController controller =
        new PhotoViewController(
            midlet, getAlbumData(), (AlbumListScreen) getAlbumListScreen(), name);
    controller.setNextController(nextcontroller);
    canv.setCommandListener(controller);
    nextcontroller = controller;
    // #endif

    // #ifdef includeSmsFeature
    /* [NC] Added in scenario 06 */
    // [NC] Changed in the scenario 07: just the first line below to support generic
    // AbstractController
    SmsSenderController smscontroller =
        new SmsSenderController(
            midlet, getAlbumData(), (AlbumListScreen) getAlbumListScreen(), name);
    // this.setNextController(smscontroller);
    smscontroller.setNextController(nextcontroller);
    canv.setCommandListener(smscontroller);
    nextcontroller = smscontroller;
    // #endif

    setCurrentScreen(canv);
  }
  public boolean handleCommand(Command command) {
    String label = command.getLabel();
    System.out.println("<* MediaController.handleCommand() *> " + label);

    /** Case: Save Add photo * */
    if (label.equals("Add")) {
      ScreenSingleton.getInstance().setCurrentScreenName(Constants.ADDPHOTOTOALBUM_SCREEN);
      AddMediaToAlbum form = new AddMediaToAlbum("Add new item to Album");
      form.setCommandListener(this);
      setCurrentScreen(form);
      return true;

    }
    // #ifdef includePhoto
    // [NC] Added in the scenario 07
    else if (label.equals("View")) {
      String selectedImageName = getSelectedMediaName();
      showImage(selectedImageName);
      // #ifdef includeSorting
      // [EF] Added in the scenario 02
      incrementCountViews(selectedImageName);
      // #endif
      ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGE_SCREEN);
      return true;
    }
    // #endif

    // #ifdef includeMusic
    // [NC] Added in the scenario 07
    else if (label.equals("Play")) {
      String selectedMediaName = getSelectedMediaName();
      return playMusicMedia(selectedMediaName);
    }
    // #endif

    // #ifdef includeVideo
    // [NC] Added in the scenario 08
    else if (label.equals("Play Video")) {
      String selectedMediaName = getSelectedMediaName();
      return playVideoMedia(selectedMediaName);
    }
    // #endif

    // #ifdef captureVideo
    // [NC] Added in the scenario 08
    else if (label.equals("Capture Video")) {

      CaptureVideoScreen playscree =
          new CaptureVideoScreen(midlet, CaptureVideoScreen.CAPTUREVIDEO);
      playscree.setVisibleVideo();
      VideoCaptureController controller =
          new VideoCaptureController(
              midlet, getAlbumData(), (AlbumListScreen) getAlbumListScreen(), playscree);
      this.setNextController(controller);
      playscree.setCommandListener(this);
      return true;
    }
    // #endif

    // #ifdef capturePhoto
    // [NC] Added in the scenario 08
    else if (label.equals("Capture Photo")) {
      CaptureVideoScreen playscree =
          new CaptureVideoScreen(midlet, CaptureVideoScreen.CAPTUREPHOTO);
      playscree.setVisibleVideo();
      PhotoViewController controller =
          new PhotoViewController(
              midlet, getAlbumData(), (AlbumListScreen) getAlbumListScreen(), "New photo");
      controller.setCpVideoScreen(playscree);
      this.setNextController(controller);
      playscree.setCommandListener(this);
      return true;
    }
    // #endif

    else if (label.equals("Save Item")) {
      try {
        getAlbumData()
            .addNewMediaToAlbum(
                ((AddMediaToAlbum) getCurrentScreen()).getItemName(),
                ((AddMediaToAlbum) getCurrentScreen()).getPath(),
                getCurrentStoreName());
        // #ifdef includeMusic
        // [NC] Added in the scenario 07
        if (getAlbumData() instanceof MusicAlbumData) {
          getAlbumData().loadMediaDataFromRMS(getCurrentStoreName());
          MediaData mymedia =
              getAlbumData().getMediaInfo(((AddMediaToAlbum) getCurrentScreen()).getItemName());
          mymedia.setTypeMedia(((AddMediaToAlbum) getCurrentScreen()).getItemType());
          getAlbumData().updateMediaInfo(mymedia, mymedia);
          //					MultiMediaData mmedi = new MultiMediaData(mymedia, ((AddMediaToAlbum)
          // getCurrentScreen()).getItemType());
          //					getAlbumData().updateMediaInfo(mymedia, mmedi);
        }
        // #endif

      } catch (InvalidImageDataException e) {
        Alert alert = null;
        if (e instanceof ImagePathNotValidException)
          alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR);
        else alert = new Alert("Error", "The file format is not valid", null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
        return true;
        // alert.setTimeout(5000);
      } catch (PersistenceMechanismException e) {
        Alert alert = null;
        if (e.getCause() instanceof RecordStoreFullException)
          alert = new Alert("Error", "The mobile database is full", null, AlertType.ERROR);
        else
          alert =
              new Alert(
                  "Error", "The mobile database can not add a new photo", null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      }
      // #ifdef includeMusic
      // [NC] Added in the scenario 07
      catch (ImageNotFoundException e) {
        Alert alert =
            new Alert(
                "Error",
                "The selected item was not found in the mobile device",
                null,
                AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
        return true;
      }
      // #endif
      return goToPreviousScreen();
      /** Case: Delete selected Photo from recordstore * */
    } else if (label.equals("Delete")) {
      String selectedMediaName = getSelectedMediaName();
      try {
        getAlbumData().deleteMedia(getCurrentStoreName(), selectedMediaName);
      } catch (PersistenceMechanismException e) {
        Alert alert =
            new Alert(
                "Error", "The mobile database can not delete this item", null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
        return true;
      } catch (ImageNotFoundException e) {
        Alert alert =
            new Alert(
                "Error",
                "The selected item was not found in the mobile device",
                null,
                AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
        return true;
      }
      showMediaList(getCurrentStoreName(), false, false);
      ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);
      return true;

      /** Case: Edit photo label [EF] Added in the scenario 02 */
    } else if (label.equals("Edit Label")) {
      String selectedImageName = getSelectedMediaName();
      try {
        media = getAlbumData().getMediaInfo(selectedImageName);
        // PhotoController photoController = new PhotoController(image,
        // this);
        NewLabelScreen formScreen =
            new NewLabelScreen("Edit Label Item", NewLabelScreen.LABEL_PHOTO);
        formScreen.setCommandListener(this);
        this.setScreen(formScreen);
        setCurrentScreen(formScreen);
        formScreen = null;
      } catch (ImageNotFoundException e) {
        Alert alert =
            new Alert(
                "Error",
                "The selected item was not found in the mobile device",
                null,
                AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      }
      return true;

      // #ifdef includeSorting
      /** Case: Sort photos by number of views [EF] Added in the scenario 02 */
    } else if (label.equals("Sort by Views")) {
      showMediaList(getCurrentStoreName(), true, false);
      ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);

      return true;
      // #endif

      // #ifdef includeFavourites
      /** Case: Set photo as favorite [EF] Added in the scenario 03 */
    } else if (label.equals("Set Favorite")) {
      String selectedMediaName = getSelectedMediaName();
      try {
        MediaData media = getAlbumData().getMediaInfo(selectedMediaName);
        media.toggleFavorite();
        updateMedia(media);
        System.out.println(
            "<* BaseController.handleCommand() *> Image = "
                + selectedMediaName
                + "; Favorite = "
                + media.isFavorite());
      } catch (ImageNotFoundException e) {
        Alert alert =
            new Alert(
                "Error",
                "The selected item was not found in the mobile device",
                null,
                AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      } catch (InvalidImageDataException e) {
        Alert alert = null;
        if (e instanceof ImagePathNotValidException)
          alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR);
        else
          alert = new Alert("Error", "The image file format is not valid", null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      } catch (PersistenceMechanismException e) {
        Alert alert = null;
        if (e.getCause() instanceof RecordStoreFullException)
          alert = new Alert("Error", "The mobile database is full", null, AlertType.ERROR);
        else
          alert =
              new Alert(
                  "Error",
                  "The mobile database can not update new informations",
                  null,
                  AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      }
      return true;

      /** Case: View favorite photos [EF] Added in the scenario 03 */
    } else if (label.equals("View Favorites")) {
      showMediaList(getCurrentStoreName(), false, true);
      ScreenSingleton.getInstance().setCurrentScreenName(Constants.IMAGELIST_SCREEN);

      return true;
      // #endif

      /** Case: Save new Photo Label */
    } else if (label.equals("Save")) {
      System.out.println(
          "<* PhotoController.handleCommand() *> Save Photo Label = " + this.screen.getLabelName());
      this.getMedia().setMediaLabel(this.screen.getLabelName());
      try {
        updateMedia(media);
      } catch (InvalidImageDataException e) {
        Alert alert = null;
        if (e instanceof ImagePathNotValidException)
          alert = new Alert("Error", "The path is not valid", null, AlertType.ERROR);
        else
          alert = new Alert("Error", "The image file format is not valid", null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      } catch (PersistenceMechanismException e) {
        Alert alert =
            new Alert(
                "Error", "The mobile database can not update this photo", null, AlertType.ERROR);
        Display.getDisplay(midlet).setCurrent(alert, Display.getDisplay(midlet).getCurrent());
      }
      return goToPreviousScreen();

      /** Case: Go to the Previous or Fallback screen * */
    } else if (label.equals("Back")) {
      return goToPreviousScreen();

      /** Case: Cancel the current screen and go back one* */
    } else if (label.equals("Cancel")) {
      return goToPreviousScreen();
    }
    return false;
  }