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

it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonomyBrowserActivity 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: TaxonomyBrowserActivity.java,v 97140ea1539e 2010/06/11 14:13:55 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageButton;
import android.widget.ListView;
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.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.bluebill.taxonomy.Taxonomy.Type;
import it.tidalwave.mobile.android.ui.AndroidUtilities;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonCountFormat;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.android.preferences.BlueBillPreferenceActivity;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.about.AboutActivity;

/***********************************************************************************************************************
 *
 * TODO: should be refactored so it can dynamically handle all the levels in a single class, instead of requiring
 * subclassing for each level type. For this, it needs control of the back button.
 * 
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public abstract class TaxonomyBrowserActivity extends ListActivity
  {
    private static final String CLASS = TaxonomyBrowserActivity.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final Type type;

    @CheckForNull
    private TaxonomyBrowserController controller;

    private ListView list;

    private TextView tvFooter;

    private final TaxonCountFormat xFormat;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public TaxonomyBrowserActivity (final @Nonnull Type type)
      {
        this.type = type;
        this.xFormat = new TaxonCountFormat(type);
      }

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

        final TaxonomyPreferences taxonomyPreferences = Locator.find(TaxonomyPreferences.class);
        final Taxonomy taxonomy = taxonomyPreferences.getTaxonomy();
        final List taxa = computeTaxa(taxonomy);
        controller = new TaxonomyBrowserController(this, taxonomyPreferences, type, taxa);

        list = (ListView)findViewById(android.R.id.list);
        list.setAdapter(controller.getTaxonBrowserAdapter());
        list.setOnItemClickListener(controller.getTaxonBrowserListener());

        tvFooter = (TextView)findViewById(R.id.tvFooter);
        updateFooter();

        list.getAdapter().registerDataSetObserver(new DataSetObserver()
          {
            @Override
            public void onChanged()
              {
                updateFooter();
              }
          });

        ((ImageButton)findViewById(R.id.btFilter)).setOnClickListener(AndroidUtilities.createShowSoftKeyboardOnClickListener(list));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void onResume()
      {
        super.onResume();
        logger.info("onDestroy()");
        controller.ensureTaxaAreSorted();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void onDestroy()
      {
        logger.info("onDestroy()");
        controller.dispose();
        super.onDestroy();
      }

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        switch (item.getItemId())
          {
            case R.id.preferences:
                startActivity(new Intent(getBaseContext(), BlueBillPreferenceActivity.class));
                return true;

            case R.id.about:
                startActivity(new Intent(getBaseContext(), AboutActivity.class));
                return true;

            default:
                return super.onOptionsItemSelected(item);
          }
      }

    /*******************************************************************************************************************
     *
     * 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();
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void updateFooter()
      {
        tvFooter.setText(xFormat.format(list.getAdapter().getCount()));
      }

    /*******************************************************************************************************************
     *
     * FIXME: move to the Controller.
     *
     ******************************************************************************************************************/
    @Nonnull
    private List computeTaxa (final @Nonnull Taxonomy taxonomy)
      {
        final List taxa = new ArrayList();

        try
          {
            final Taxon taxon = TaxonIntentHelper.getTaxon(getIntent());
            taxa.addAll(taxon.getChildren());
          }
        catch (NotFoundException e)
          {
            taxa.addAll(taxonomy.findTaxaByType(type));
          }

        return taxa;
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy