All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.taxonomy.factsheet.impl.sound.DeleteAction Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.taxonomy.factsheet.impl.sound;

import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.awt.event.ActionEvent;
import it.tidalwave.util.ui.UserNotificationWithFeedback.Feedback;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.io.MasterFileSystem;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.sound.DefaultTaxonSoundFactSheetViewController;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.sound.TaxonSoundFactSheetView;
import lombok.extern.slf4j.Slf4j;
import static org.openide.util.NbBundle.*;
import static it.tidalwave.util.ui.UserNotificationWithFeedback.notificationWithFeedback;
import static it.tidalwave.util.ui.UserNotification.notification;
import static it.tidalwave.mobile.util.Downloadable.*;
import static it.tidalwave.mobile.util.Downloadable.Status.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@SuppressWarnings("serial") @Slf4j
public class DeleteAction extends TaxonSoundFactSheetActionSupport
  {
    private static final double MEGA = 1024.0 * 1024.0;
    
    @Nonnull
    private final DefaultTaxonSoundFactSheetViewController controller;
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private final Provider masterFileSystem = Locator.createProviderFor(MasterFileSystem.class);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public DeleteAction (final @Nonnull Media media,
                         final @Nonnull DefaultTaxonSoundFactSheetViewController controller, 
                         final @Nonnull TaxonSoundFactSheetView view) 
      {
        super(media, view);
        this.controller = controller;
        putValue(NAME, getMessage(_, "deleteMedia"));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void actionPerformed (final @Nonnull ActionEvent event)
      {
        log.info("actionPerformed({}) - media: {}", event, media);
        
        view.confirmToDeleteMedia(notificationWithFeedback().withCaption(_, "confirmDeletionTitle")
                                                            .withText(_, "confirmDeletionMessage")
                                                            .withFeedback(new Feedback()
          {
            @Override
            public void onConfirm()
              {
                try
                  {
                    media.as(Removable).remove();
                    final double freeSpace = masterFileSystem.get().getExternalFileSystem().getFreeSpace() / MEGA;
                    view.notifyCompletion(notification().withText(_, "deletionCompleted", freeSpace));
                  }
                catch (Exception e)
                  {
                    log.warn("Can't delete sound", e);
                    view.notifyError(notification().withText(_, "deletionFailed"));
                  }

                // Needed because at this point the PropertyChangeListener has been detached.
                // Maybe it would be better to keep the listener attached, implement a dispose() method that
                // detaches it at the end.
                controller.notifyStatusChanged();
              }
          }));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean isEnabled() 
      {
        return isMediaInStatus(DOWNLOADED, OBSOLETE, BROKEN);
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy