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

it.tidalwave.bluebill.mobile.observation.ui.spi.DefaultCountAndGenderViewController 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.observation.ui.spi;

import it.tidalwave.bluebill.mobile.observation.ui.CountAndGenderFormModel;
import it.tidalwave.bluebill.mobile.observation.ui.CountAndGenderView;
import it.tidalwave.bluebill.mobile.observation.ui.CountAndGenderViewController;
import java.beans.PropertyChangeEvent;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.observation.Cardinality;
import it.tidalwave.observation.Observable;
import it.tidalwave.observation.Observation;
import it.tidalwave.observation.TextNote;
import it.tidalwave.observation.bluebill.ObservationClipboard;
import it.tidalwave.observation.bluebill.ObservationType;
import it.tidalwave.util.ui.FlowController;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonObservable;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonHistory;
import it.tidalwave.util.ui.FormModel;
import java.beans.PropertyChangeListener;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static org.openide.util.NbBundle.*;

/***********************************************************************************************************************
 *
 * @stereotype Controller
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor @Slf4j
public class DefaultCountAndGenderViewController implements CountAndGenderViewController
  {
    private static final Class _ = DefaultCountAndGenderViewController.class;
    
    @Nonnull
    private final CountAndGenderView view;
    
    @Nonnull
    private final FlowController controlFlow;
    
    private final CountAndGenderFormModel model = new CountAndGenderFormModel();
    
    private Taxon taxon;
    
    private Observation.Builder builder;
    
    private final Provider observationClipboard = Locator.createProviderFor(ObservationClipboard.class);
            
    @CheckForNull
    private final Provider taxonHistory = Locator.createProviderFor(TaxonHistory.class);

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Getter
    private final Action okAction = new AbstractAction(getMessage(_, "ok")) 
      {
        public void actionPerformed (final @Nonnull ActionEvent event) 
          {
            pickTaxon();
            controlFlow.toNextStep();
          }
      };
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    @Getter
    private final Action addMoreAction = new AbstractAction(getMessage(_, "addMore"))
      {
        public void actionPerformed (final @Nonnull ActionEvent event) 
          {
            pickTaxon();
            controlFlow.toNextStep(ADD_MORE__);
          }
      };
    
    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
      { 
        model.addPropertyChangeListener(new PropertyChangeListener() 
         {
            public void propertyChange (final @Nonnull PropertyChangeEvent event) 
              {
                if (FormModel.PROP_VALID.equals(event.getPropertyName()))
                  {
                    okAction.setEnabled(model.isValid());                        
                    addMoreAction.setEnabled(model.isValid());                        
                  }
              }
         });
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void initialize (final @Nonnull Taxon taxon) 
      {
        log.info("initialize({})", taxon);
        this.taxon = taxon;
        builder = observationClipboard.get().getBuilder();
        view.bindModel(model);
        view.renderTaxon(taxon);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    private void pickTaxon() 
      {
        final List extras = new ArrayList();

        extras.add(model.getGender().getValue());
        
        if (model.getNote().getValue() != null)
          {
            extras.add(new TextNote(model.getNote().getValue()));
          }
        
        if (model.getWatched().getValue())
          {
            extras.add(ObservationType.WATCHED);
          }

        if (model.getListened().getValue())
          {
            extras.add(ObservationType.LISTENED);
          }
          
        
//        final Observable observable = taxon.as(ObservableBuilder).with(extras).create();
        final Observable observable = new TaxonObservable(taxon, extras.toArray());
        observationClipboard.get().setBuilder(builder.of(observable, getCardinality(model)));
        taxonHistory.get().addToTaxonHistory(taxon);
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    /* package */ static Cardinality getCardinality (final @Nonnull CountAndGenderFormModel model)
      {
        switch (model.getCountType().getValue())
          {
            case NOT_COUNTED:
                return Cardinality.UNDEFINED;
                
            case EXACT:
                final int simpleCardinality = Integer.parseInt(model.getCount().getValue());
                return Cardinality.valueOf(simpleCardinality);
                
            case APPROXIMATED:
                final int approxCardinality = Integer.parseInt(model.getCount().getValue());
                return Cardinality.approxValueOf(approxCardinality);
                
            case RANGE:
                final int lower = Integer.parseInt(model.getCount().getValue());
                final int upper = Integer.parseInt(model.getMaxCount().getValue());
                return Cardinality.rangeOf(lower, upper);
          }
        
        throw new RuntimeException("Unexpected value: " + model.getCountType().getValue());
      }
  }