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

it.tidalwave.bluebill.mobile.observation.DefaultObservationsUIController Maven / Gradle / Ivy

There is a newer version: 1.0.15
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: DefaultObservationsUIController.java,v cc1386b5bca7 2010/05/29 09:11:06 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.observation;

import javax.annotation.Nonnull;
import java.util.Calendar;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.bluebill.observation.Observation;
import it.tidalwave.bluebill.observation.Observation.Builder;
import it.tidalwave.bluebill.observation.ObservationItem;
import it.tidalwave.bluebill.observation.ObservationManager;
import it.tidalwave.bluebill.observation.ObservationSet;
import static it.tidalwave.bluebill.taxonomy.Taxon.Taxon;
import it.tidalwave.mobile.LocationFinder;
import it.tidalwave.bluebill.mobile.share.Report;
import it.tidalwave.bluebill.mobile.share.SharingPreferences;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public abstract class DefaultObservationsUIController implements ObservationUIController
  {
    private static final String CLASS = DefaultObservationsUIController.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private final Lookup lookup = Lookup.getDefault();

    private final TaxonomyPreferences taxonomyPreferences = lookup.lookup(TaxonomyPreferences.class);
    
    private final SharingPreferences sharingPreferences = lookup.lookup(SharingPreferences.class);

    private ObservationClipboard observationClipboard;

    private ObservationSet observationSet;

    @Nonnull
    private final ObservationsUI ui;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public DefaultObservationsUIController (final @Nonnull ObservationsUI ui)
      {
        this.ui = ui;
        observationClipboard = lookup.lookup(ObservationClipboard.class);
        observationSet = lookup.lookup(ObservationManager.class).findOrCreateObservationSetById(null);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void startNewObservationSequence()
      {
        logger.info("startNewObservationSequence()");
        observationClipboard.clear();
        final Builder builder = observationClipboard.getBuilder();
        observationClipboard.setBuilder(builder.date(Calendar.getInstance().getTime()));
        lookup.lookup(LocationFinder.class).start(); // search needs time, and we pre-start it
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void browseToFactSheet()
      {
        // nothing to do by default
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void commitNewObservation()
      {
        logger.info("commitNewObservation()");
        final Observation.Builder builder = observationClipboard.getBuilder();

        if (builder.getItemCount() > 0)
          {
            builder.build();
            final String message = NbBundle.getMessage(DefaultObservationsUIController.class, "addedNewObservation");
            ui.getCommonUITasks().showTemporaryMessage(message);
            ui.highlightLatestObservation();
          }

        observationClipboard.clear();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void cancelNewObservation()
      {
        lookup.lookup(LocationFinder.class).stop(); // search needs time, and we pre-start it
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void deleteAllObservations()
      {
        final String title = NbBundle.getMessage(DefaultObservationsUIController.class, "confirm");
        final String message = NbBundle.getMessage(DefaultObservationsUIController.class, "confirmEraseAllObservations");

        ui.getCommonUITasks().alertDialog(title, message, new Runnable()
          {
            public void run() 
              {
                observationSet.clear();
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void deleteObservationItem (final @Nonnull ObservationItem observationItem)
      {
        final String title = NbBundle.getMessage(DefaultObservationsUIController.class, "confirm");
        final String message = NbBundle.getMessage(DefaultObservationsUIController.class, 
                                                   "confirmDeleteObservationItem",
                                                   taxonomyPreferences.format(observationItem.getObservable().as(Taxon)));
        final String itemDeleted = NbBundle.getMessage(DefaultObservationsUIController.class, "itemDeleted");
        ui.getCommonUITasks().alertDialog(title, message, new Runnable()
          {
            public void run()
              {
                try
                  {
                    observationItem.getObservation().replace().removeItem(observationItem).apply();
                    ui.getCommonUITasks().showTemporaryMessage(itemDeleted);
                  }
                catch (Exception e)
                  {
                    throw new RuntimeException(e);
                  }
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void shareObservations()
      {
        shareObservations(new Report()
                .withSubject(NbBundle.getMessage(DefaultObservationsUIController.class, "observations"))
                .withRecipients(sharingPreferences.getDefaultRecipients())
                .withBody(observationSet.as(ObservationSetVisitor.class).visit(new ReportGenerator()).getMessage()));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected abstract void shareObservations (@Nonnull Report report);
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy