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 63ea8727ef65 2010/08/17 18:54:51 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.browser;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
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.R;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;
import org.openide.util.NbBundle;
/***********************************************************************************************************************
*
* 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 TextView tvTaxonomy;
private TextView tvTaxonLanguages;
private final TaxonCountFormat xFormat;
private final CommonOptionsMenuController commonOptionsMenuController = new CommonOptionsMenuController(this);
private final TaxonomyFooterController taxonomyFooterController = new TaxonomyFooterController();
@CheckForNull
private String taxonomyName; // FIXME: replace with the id
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
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);
list = (ListView)findViewById(android.R.id.list);
tvFooter = (TextView)findViewById(R.id.tvFooter);
tvTaxonomy = (TextView)findViewById(R.id.tvTaxonomy);
tvTaxonLanguages = (TextView)findViewById(R.id.tvPrimaryLanguage);
((ImageButton)findViewById(R.id.btFilter)).setOnClickListener(AndroidUtilities.createShowSoftKeyboardOnClickListener(list));
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void onResume()
{
logger.info("onResume()");
super.onResume();
eventuallyLoadTaxonomyOrReSortTaxa(); // in case it has been changed by preferences
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void onDestroy()
{
logger.info("onDestroy()");
if (controller != null)
{
controller.dispose();
}
super.onDestroy();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@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);
}
/*******************************************************************************************************************
*
* Propagates the result back.
*
******************************************************************************************************************/
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
logger.info("onActivityResult(%d, %d, %s)", requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
setResult(resultCode, data);
finish();
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void eventuallyLoadTaxonomyOrReSortTaxa()
{
final TaxonomyPreferences taxonomyPreferences = Locator.find(TaxonomyPreferences.class);
final Taxonomy taxonomy = taxonomyPreferences.getTaxonomy();
final String newTaxonomyName = taxonomy.getDisplayName();
logger.info(">>>> current taxonomy: %s new taxonomy: %s", taxonomyName, newTaxonomyName);
final boolean taxonomyChanged = (taxonomyName == null) || !taxonomyName.equals(newTaxonomyName);
final boolean needsResorting = (controller != null) && controller.needsResorting();
if (taxonomyChanged || needsResorting)
{
final String message = NbBundle.getMessage(TaxonomyBrowserController.class, "pleaseWait");
final ProgressDialog dialog = ProgressDialog.show(this, "", message, true);
final Thread thread = new Thread()
{
@Override
public void run()
{
if (taxonomyChanged)
{
logger.info("(re)loading taxonomy");
taxonomyName = newTaxonomyName;
if (controller != null)
{
controller.dispose();
}
controller = new TaxonomyBrowserController(TaxonomyBrowserActivity.this, taxonomyPreferences, type, taxonomy, getIntent());
}
else if (needsResorting)
{
controller.ensureTaxaAreSorted();
}
runOnUiThread(new Runnable()
{
public void run()
{
final BaseAdapter taxonBrowserAdapter = (BaseAdapter)controller.getTaxonBrowserAdapter();
// TODO: if you can avoid re-creating the controller, this section can be moved to onCreate()
if (!taxonomyChanged)
{
taxonBrowserAdapter.notifyDataSetChanged();
}
else
{
list.setAdapter(taxonBrowserAdapter);
list.setOnItemClickListener(controller.getTaxonBrowserListener());
taxonBrowserAdapter.registerDataSetObserver(new DataSetObserver()
{
@Override
public void onChanged()
{
updateHeaderAndFooter();
}
});
}
updateHeaderAndFooter();
dialog.dismiss();
}
});
}
};
thread.start();
}
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
private void updateHeaderAndFooter()
{
tvFooter.setText(xFormat.format(list.getAdapter().getCount()));
tvTaxonomy.setText(taxonomyFooterController.getTaxonomyDisplayName());
tvTaxonLanguages.setText(taxonomyFooterController.getTaxonLanguages());
}
}