it.tidalwave.bluebill.mobile.android.taxonomy.browser.AndroidPickTaxonController 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: AndroidPickTaxonController.java,v 8f6c7d15bf61 2010/08/07 16:16:37 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.Parameters;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.Taxon;
import it.tidalwave.bluebill.taxonomy.Taxonomy.Type;
import it.tidalwave.mobile.android.ui.ControlFlow;
import it.tidalwave.bluebill.mobile.observation.ObservationClipboard;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonAdapter;
import static it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class AndroidPickTaxonController
{
private static final String CLASS = AndroidPickTaxonController.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
private final TaxonomyPreferences preferences = Locator.find(TaxonomyPreferences.class);
private final ObservationClipboard observationClipboard = Locator.find(ObservationClipboard.class);
private TaxonAdapter taxonHistoryAdapter;
private final PickTaxonActivity activity;
private ControlFlow controlFlow;
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public AndroidPickTaxonController (final @Nonnull PickTaxonActivity activity)
{
Parameters.checkNonNull(activity, "activity");
this.activity = activity;
controlFlow = ControlFlow.forActivity(activity);
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
@Nonnull
public TaxonAdapter getTaxonHistoryAdapter()
{
return taxonHistoryAdapter;
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public void pickHistoricTaxon (final @Nonnegative int position)
{
try
{
final Taxon taxon = taxonHistoryAdapter.getItem(position);
controlFlow.toNextStep(intentFor(taxon));
}
catch (NotFoundException e)
{
throw new RuntimeException(e);
}
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public void startBrowsingByType (final @Nonnull Type type)
{
controlFlow.toNextStep(intentFor(type), ControlFlow.USE_INTENT_FILTER);
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public void bindTaxa()
{
final List recentTaxa = observationClipboard.getRecentTaxa();
final long time = System.currentTimeMillis();
Collections.sort(recentTaxa, preferences.getTaxonComparator());
logger.info("%d taxa sorted in %d msec", recentTaxa.size(), System.currentTimeMillis() - time);
taxonHistoryAdapter = new TaxonAdapter(activity, preferences, recentTaxa);
activity.updateList();
}
/*******************************************************************************************************************
*
*
*
******************************************************************************************************************/
public void clearTaxonHistory()
{
observationClipboard.clearTaxonHistory();
bindTaxa();
}
}