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

it.tidalwave.bluebill.mobile.observation.DefaultObservationPreferences Maven / Gradle / Ivy

There is a newer version: 1.0.15
Show newest version
/***********************************************************************************************************************
 *
 * 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: DefaultObservationPreferences.java,v e1fc277b8b8f 2010/05/30 17:05:49 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.observation;

import java.util.Collection;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.annotation.Nonnull;
import org.openide.util.Lookup;
import org.openide.util.lookup.ServiceProvider;
import it.tidalwave.util.AsException;
import it.tidalwave.util.Parameters;
import it.tidalwave.bluebill.observation.Cardinality;
import it.tidalwave.bluebill.observation.ObservationItem;
import it.tidalwave.bluebill.observation.TextNote;
import static it.tidalwave.bluebill.observation.TextNote.TextNote;
import static it.tidalwave.bluebill.taxonomy.Taxon.Taxon;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import static it.tidalwave.bluebill.mobile.observation.BirdGender.BirdGender;
import static it.tidalwave.bluebill.mobile.observation.BirdGender.*;
import static it.tidalwave.bluebill.mobile.observation.Mode.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
@ServiceProvider(service=ObservationPreferences.class)
public class DefaultObservationPreferences implements ObservationPreferences
  {
    private @Nonnull TaxonomyPreferences taxonomyPreferences = Lookup.getDefault().lookup(TaxonomyPreferences.class);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public CharSequence format (final @Nonnull ObservationItem observationItem, final @Nonnull Extra... extra)
      {
        return taxonomyPreferences.format(observationItem.getObservable().as(Taxon), formatExtra(observationItem, extra));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public CharSequence formatAsHtml (final @Nonnull ObservationItem observationItem, final @Nonnull Extra... extra)
      {
        return taxonomyPreferences.formatAsHtml(observationItem.getObservable().as(Taxon), formatExtra(observationItem, extra));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    private String formatExtra (final @Nonnull ObservationItem observationItem, final @Nonnull Extra... extra)
      {
        final Locale locale = taxonomyPreferences.getTaxonomyLocales().get(0);
        final StringBuilder buffer = new StringBuilder();
        String leading = " (";
        String trailing = "";
        final Collection extras = Parameters.find(Extra.class, extra);

        if (extras.contains(Extra.CARDINALITY))
          {
            try
              {
                final Cardinality cardinality = observationItem.getCardinality();

                if (!cardinality.equals(Cardinality.UNDEFINED))
                  {
                    buffer.append(leading);
                    buffer.append(cardinality.toString());
                    leading = " ";
                    trailing = ")";
                  }
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }

        if (extras.contains(Extra.GENDER))
          {
            try
              {
                final BirdGender gender = observationItem.getObservable().as(BirdGender);

                if (!gender.equals(NOT_RECORDED))
                  {
                    final ResourceBundle bundle = ResourceBundle.getBundle("it/tidalwave/bluebill/mobile/observation/Bundle", locale);
                    final String shortGender = bundle.getString("shortGender_" + gender.toString());

                    if (!shortGender.startsWith(" ") && leading.endsWith(" "))
                      {
                        leading = leading.substring(0, leading.length() - 1);
                      }
                    
                    buffer.append(leading).append(shortGender.trim());
                    trailing = ")";
                  }
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }

        buffer.append(trailing);

        if (extras.contains(Extra.MODE))
          {
            final Collection modes = observationItem.getObservable().getLookup().lookupAll(Mode.class);

            if ((modes.size() > 1) || !modes.contains(WATCHED))
              {
                for (final Mode mode : modes)
                  {
                    buffer.append(", ").append(mode.getDisplayName(locale));
                  }
              }
          }

        if (extras.contains(Extra.NOTE_FULL) || extras.contains(Extra.NOTE_REFERENCE))
          {
            try
              {
                final TextNote textNote = observationItem.getObservable().as(TextNote);

                if (extras.contains(Extra.NOTE_FULL))
                  {
                    buffer.append(" - ").append(textNote.stringValue());
                  }
                else
                  {
                    buffer.append(" [note]");
                  }
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }

        return buffer.toString();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy