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

it.tidalwave.bluebill.mobile.android.taxonomy.browser.TaxonPickerActivity 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.android.taxonomy.browser;

import java.util.concurrent.ExecutorService;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuControllerProvider;
import it.tidalwave.netbeans.util.Locator;
import javax.annotation.Nonnull;
import java.util.Arrays;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.ui.FlowController;
import it.tidalwave.mobile.android.ui.AndroidFlowController;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon.Rank;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.mobile.taxonomy.ui.spi.DefaultTaxonPickerViewController;
import it.tidalwave.bluebill.mobile.taxonomy.ui.TaxonPickerViewController;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import it.tidalwave.bluebill.mobile.android.R;
import lombok.extern.slf4j.Slf4j;
import static it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper.*;

/***********************************************************************************************************************
 *
 * This activity allows to pick a Taxon (actually a species). It offers the user the capability of choosing where to
 * start browsing from (in the range from Order to Species, bounds included).
 *
 * @stereotype Activity
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@Slf4j
public class TaxonPickerActivity extends Activity
  {
    public static final Class TaxonPickerActivity = TaxonPickerActivity.class;

    private final AndroidFlowController controlFlow = AndroidFlowController.forActivity(this);
    
    @Nonnull
    private final ExecutorService executorService = Locator.find(ExecutorService.class);

    private AndroidTaxonPickerView view;
    
    private TaxonPickerViewController controller;
    
    private final CommonOptionsMenuController commonOptionsMenuController = Locator.find(CommonOptionsMenuControllerProvider.class).createCommonOptionsMenuController(this);  

    /*******************************************************************************************************************
     *
     * 
     *
     ******************************************************************************************************************/
    private final FlowController controlFlowDecorator = new FlowController() 
      {
        public void finish() 
          {
            controlFlow.finish();
          }

        public void toNextStep (final @Nonnull Object... args)
          {
            if (args[0] instanceof Taxon)
              {
                try 
                  {
                    controlFlow.toNextStep(intentFor((Taxon)args[0]));
                  }
                catch (NotFoundException e) 
                  {
                    throw new RuntimeException(e);
                  }
              }
            else if (args[0] instanceof Rank)
              {
                controlFlow.toNextStep(intentFor((Rank)args[0]), AndroidFlowController.USE_INTENT_FILTER);
              }
            else
              {
                throw new IllegalArgumentException(Arrays.toString(args));  
              }
          } 
      };
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void onCreate (final @Nonnull Bundle savedInstanceState)
      {
        log.info("onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_taxon_picker);
        view = (AndroidTaxonPickerView)findViewById(R.id.viewTaxonPicker);
        controller = new DefaultTaxonPickerViewController(view, controlFlowDecorator);
        view.initialize(controller);
        registerForContextMenu(view.getLiRecentTaxa());
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    protected void onResume()
      {
        log.info("onResume()");
        super.onResume();
        
        executorService.submit(new Runnable()
          {
            @Override
            public void run()
              {
                // always reinitialize, even if you have previously loaded data, because e.g. we changed the primary
                // taxon locale, or the taxonomy, etc...
                controller.initialize();
              }
          });
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public boolean onCreateOptionsMenu (final @Nonnull Menu menu)
      {
        getMenuInflater().inflate(R.menu.pick_taxon_options_menu, menu);
        getMenuInflater().inflate(R.menu.common_options_menu, menu);
        return true;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        switch (item.getItemId())
          {
            case R.id.clearTaxonHistory:
                controller.clearTaxonHistory();
                return true;

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

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void onCreateContextMenu (final @Nonnull ContextMenu menu,
                                     final @Nonnull View view,
                                     final @Nonnull ContextMenuInfo menuInfo)
      {
        this.view.getAdapter().onCreateContextMenu(menu, view, menuInfo);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public boolean onContextItemSelected (final @Nonnull MenuItem item)
      {
        return view.getAdapter().onContextItemSelected(item);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     * Propagates the result back.
     *
     ******************************************************************************************************************/
    @Override
    protected void onActivityResult (final int requestCode, final int resultCode, final @Nonnull Intent data)
      {
        log.info("onActivityResult({}, {}, {})", new Object[] { requestCode, resultCode, data } );
        
        if (resultCode == RESULT_OK)
          {
            setResult(resultCode, data);
            finish();
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy