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

it.tidalwave.bluebill.mobile.android.observation.CountAndGenderActivity Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * 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: CountAndGenderActivity.java,v d808b6cb5d50 2010/07/10 21:19:33 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.observation;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import android.view.View;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.observation.Cardinality;
import it.tidalwave.bluebill.observation.Observable;
import it.tidalwave.bluebill.observation.Observation;
import it.tidalwave.bluebill.observation.TextNote;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.mobile.android.ui.ControlFlow;
import it.tidalwave.mobile.android.ui.DisplayableArrayAdapter;
import it.tidalwave.bluebill.mobile.observation.BirdGender;
import it.tidalwave.bluebill.mobile.observation.ObservationClipboard;
import it.tidalwave.bluebill.mobile.observation.Mode;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonObservable;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;

/***********************************************************************************************************************
 *
 * This activity allows to add an ObservationItem to the current Observation.
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class CountAndGenderActivity extends Activity
  {
    private static final String CLASS = CountAndGenderActivity.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    public static final Object ADD_MORE = "ADD_MORE";

    @CheckForNull
    private Taxon taxon;

    @CheckForNull
    private ObservationClipboard observationClipboard;
    
    private RadioButton rbNotCounted;

    private RadioButton rbSimpleCardinality;

    private RadioButton rbApproxCardinality;

    private RadioButton rbRangeCardinality;

    private EditText etSimpleCardinality;

    private EditText etApproxCardinality;

    private EditText etRangeCardinalityLower;

    private EditText etRangeCardinalityUpper;

    private Spinner spGender;

    private CheckBox cbSeen;

    private CheckBox cbHeard;

    private final AndroidTextNoteController textNoteController = new AndroidTextNoteController(this);

    private ControlFlow controlFlow;

    private final CommonOptionsMenuController commonOptionsMenuController = new CommonOptionsMenuController(this);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final OnCheckedChangeListener inputEnabler = new OnCheckedChangeListener()
      {
        public void onCheckedChanged (final @Nonnull CompoundButton button, final boolean checked)
          {
            enableComponents();

            if (checked) // FIXME: use a Group for this!
              {
                rbNotCounted.setChecked(button == rbNotCounted);
                rbSimpleCardinality.setChecked(button == rbSimpleCardinality);
                rbApproxCardinality.setChecked(button == rbApproxCardinality);
                rbRangeCardinality.setChecked(button == rbRangeCardinality);
              }
          }
      };

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void onCreate (final @Nonnull Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.count_and_gender_activity);

        final TextView textView = (TextView)findViewById(R.id.tvTaxon);
        final Button btOk = (Button)findViewById(R.id.btOk);
        final Button btAddMore = (Button)findViewById(R.id.btAddMore);
        observationClipboard = Locator.find(ObservationClipboard.class);

        rbNotCounted = (RadioButton)findViewById(R.id.rbNotCounted);
        rbSimpleCardinality = (RadioButton)findViewById(R.id.rbSimpleCardinality);
        rbApproxCardinality = (RadioButton)findViewById(R.id.rbApproxCardinality);
        rbRangeCardinality = (RadioButton)findViewById(R.id.rbRangeCardinality);

        rbNotCounted.setOnCheckedChangeListener(inputEnabler);
        rbSimpleCardinality.setOnCheckedChangeListener(inputEnabler);
        rbApproxCardinality.setOnCheckedChangeListener(inputEnabler);
        rbRangeCardinality.setOnCheckedChangeListener(inputEnabler);

        etSimpleCardinality = (EditText)findViewById(R.id.etSimpleCardinality);
        etApproxCardinality = (EditText)findViewById(R.id.etApproxCardinality);
        etRangeCardinalityLower = (EditText)findViewById(R.id.etRangeCardinalityLower);
        etRangeCardinalityUpper = (EditText)findViewById(R.id.etRangeCardinalityUpper);

        rbNotCounted.setChecked(true);

        spGender = (Spinner)findViewById(R.id.spGender);
        final ArrayAdapter adapter = new DisplayableArrayAdapter(this, android.R.layout.simple_spinner_item, BirdGender.values());
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spGender.setAdapter(adapter);

        cbSeen = (CheckBox)findViewById(R.id.cbSeen);
        cbHeard = (CheckBox)findViewById(R.id.cbHeard);
        cbSeen.setChecked(true);

        ((Button)findViewById(R.id.btAddNote)).setOnClickListener(new OnClickListener()
          {
            public void onClick (final @Nonnull View view)
              {
                textNoteController.editNote();
              }
          });

        enableComponents();
        
        try
          {
            taxon = TaxonIntentHelper.getTaxon(getIntent());
            textView.setText(Locator.find(TaxonomyPreferences.class).formatAsHtml(taxon));
          }
        catch (NotFoundException e)
          {
            throw new RuntimeException(e);
          }

        btOk.setOnClickListener(new OnClickListener()
          {
            public void onClick (final @Nonnull View view)
              {
                pickTaxon();
                controlFlow.toNextStep();
              }
          });
        btAddMore.setOnClickListener(new OnClickListener()
          {
            public void onClick (final @Nonnull View view)
              {
                pickTaxon();
                controlFlow.toNextStep(ADD_MORE);
              }
          });

        controlFlow = ControlFlow.forActivity(this);
      }

    /*******************************************************************************************************************
     *
     * Propagates the result back.
     *
     ******************************************************************************************************************/
    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data)
      {
        logger.warning("onActivityResult(%d, %d, %s)", requestCode, resultCode, data);

        if (resultCode == RESULT_OK)
          {
            setResult(resultCode, data);
            finish();
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onCreateOptionsMenu (final @Nonnull Menu menu)
      {
        getMenuInflater().inflate(R.menu.common_options_menu, menu);
        return true;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        return commonOptionsMenuController.onOptionsItemSelected(item);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void pickTaxon()
      {
        Cardinality cardinality = Cardinality.UNDEFINED;

        if (rbNotCounted.isChecked())
          {
            cardinality = Cardinality.UNDEFINED;
          }
        else if (rbSimpleCardinality.isChecked())
          {
            final int simpleCardinality = Integer.parseInt(etSimpleCardinality.getText().toString());
            cardinality = Cardinality.valueOf(simpleCardinality);
          }
        else if (rbApproxCardinality.isChecked())
          {
            final int approxCardinality = Integer.parseInt(etApproxCardinality.getText().toString());
            cardinality = Cardinality.approxValueOf(approxCardinality);
          }
        else if (rbRangeCardinality.isChecked())
          {
            final int lower = Integer.parseInt(etRangeCardinalityLower.getText().toString());
            final int upper = Integer.parseInt(etRangeCardinalityUpper.getText().toString());
            cardinality = Cardinality.rangeOf(lower, upper);
          }

        final Observation.Builder builder = observationClipboard.getBuilder();
        final List extras = new ArrayList();
        extras.add(spGender.getSelectedItem());

        if (cbSeen.isChecked())
          {
            extras.add(Mode.WATCHED);
          }

        if (cbHeard.isChecked())
          {
            extras.add(Mode.LISTENED);
          }

        final TextNote textNote = textNoteController.getTextNote();
        
        if (textNote != null)
          {
            extras.add(textNote);
          }

        final Observable observable = new TaxonObservable(taxon, extras.toArray());
        observationClipboard.setBuilder(builder.of(observable, cardinality));
        observationClipboard.addToTaxonHistory(taxon);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void enableComponents()
      {
        etSimpleCardinality.setEnabled(rbSimpleCardinality.isChecked());
        etApproxCardinality.setEnabled(rbApproxCardinality.isChecked());
        etRangeCardinalityLower.setEnabled(rbRangeCardinality.isChecked());
        etRangeCardinalityUpper.setEnabled(rbRangeCardinality.isChecked());
        
        etSimpleCardinality.setFocusableInTouchMode(rbSimpleCardinality.isChecked());
        etApproxCardinality.setFocusableInTouchMode(rbApproxCardinality.isChecked());
        etRangeCardinalityLower.setFocusableInTouchMode(rbRangeCardinality.isChecked());
        etRangeCardinalityUpper.setFocusableInTouchMode(rbRangeCardinality.isChecked());

        if (rbSimpleCardinality.isChecked())
          {
            etSimpleCardinality.requestFocus();
          }

        if (rbApproxCardinality.isChecked())
          {
            etApproxCardinality.requestFocus();
          }

        if (rbRangeCardinality.isChecked())
          {
            etRangeCardinalityLower.requestFocus();
          }
      }
  }