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

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.name.NameAdapter 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: NameAdapter.java,v d808b6cb5d50 2010/07/10 21:19:33 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.name;

import it.tidalwave.util.NotFoundException;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nonnegative;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.bluebill.taxonomy.Taxon;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.netbeans.util.Locator;
import java.util.Collections;
import org.openide.util.NbBundle;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class NameAdapter extends BaseAdapter
  {
    private static final String CLASS = NameAdapter.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final LayoutInflater layoutInflater;

    @Nonnull
    private final Taxon taxon;

    @Nonnull
    private final List lans = new ArrayList();

    private final int resourceId;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public NameAdapter (final @Nonnull Context context,
                        final @Nonnull Taxon taxon,
                        final int resourceId)
      {
        this.layoutInflater = LayoutInflater.from(context);
        this.resourceId = resourceId;
        this.taxon = taxon;
        
        for (final Locale locale : Locator.find(TaxonomyPreferences.class).getTaxonomy().getLocales())
          {
            try
              {
                final String language = NbBundle.getMessage(NameAdapter.class, "lang_" + locale.getLanguage());
                final String name = taxon.getDisplayName(locale);
                lans.add(new LanguageAndName(language, name));
              }
            catch (NotFoundException e)
              {
                // ok, no name
              }
          }

        Collections.sort(lans);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public LanguageAndName getItem (final @Nonnegative int index)
      {
        return lans.get(index);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public long getItemId (final @Nonnegative int id)
      {
        return id;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnegative
    public int getCount()
      {
        return lans.size();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public View getView (final @Nonnegative int index,
                         @CheckForNull View view,
                         final @Nonnull ViewGroup viewGroup)
      {
        NameViewHelper nameViewHelper;

        if (view == null)
          {
            view = layoutInflater.inflate(resourceId, null);
            view.setTag(nameViewHelper = new NameViewHelper(view));
          }
        else
          {
            nameViewHelper = (NameViewHelper)view.getTag();
          }

        nameViewHelper.set(getItem(index));

        return view;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean hasStableIds()
      {
        return super.hasStableIds();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy