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

it.tidalwave.bluebill.mobile.android.taxonomy.AndroidTaxonomyPickerViewController Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * 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://kenai.com/hg/bluebill~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy;

import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyDescription;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.mobile.taxonomy.ui.DefaultTaxonomyPickerViewController;
import it.tidalwave.bluebill.mobile.taxonomy.spi.TaxonomyPreferencesSupport;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import it.tidalwave.bluebill.mobile.android.R;

/***********************************************************************************************************************
 *
 * @stereotype Controller
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class AndroidTaxonomyPickerViewController extends DefaultTaxonomyPickerViewController
  {
    private static final String CLASS = AndroidTaxonomyPickerViewController.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final SharedPreferences preferences = Locator.find(SharedPreferences.class);

    private final TaxonomyPickerActivity view;
    
    private TaxonomyAdapter taxonomyAdapter;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AndroidTaxonomyPickerViewController (final @Nonnull TaxonomyPickerActivity view)
      {
        super(view);
        this.view = view;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void selectTaxonomy (final @Nonnull TaxonomyDescription taxonomyDescription)
      {
        final String taxonomyId = taxonomyDescription.getId();
        logger.info("Selected taxonomy id: %s", taxonomyId);

        if (!taxonomyId.equals(preferences.getString(TaxonomyPreferencesSupport.PREF_TAXONOMY, "")))
          {
            final Editor editor = preferences.edit();
            editor.putString(TaxonomyPreferencesSupport.PREF_TAXONOMY, taxonomyId);
            editor.commit();
          }

        view.dismiss();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public synchronized TaxonomyAdapter getListAdapter()
      {
        if (taxonomyAdapter == null)
          {
            taxonomyAdapter = new TaxonomyAdapter(view, R.layout.taxonomy_list_row, taxonomies);
          }

        return taxonomyAdapter;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnegative
    public int getSelectedIndex()
      {
        final String selectedTaxonomyId = preferences.getString(TaxonomyPreferencesSupport.PREF_TAXONOMY, "");

        for (int index = 0; index < taxonomies.size(); index++)
          {
            final String taxonomyId = taxonomies.get(index).getId();

            if (taxonomyId.equals(selectedTaxonomyId))
              {
                return index;
              }
          }

        return 0;
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy