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

it.tidalwave.bluebill.mobile.observation.ui.ObservationItemRenderingOption Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * 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://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.observation.ui;

import javax.annotation.Nonnull;
import javax.inject.Provider;
import java.util.Collection;
import java.util.Locale;
import it.tidalwave.util.AsException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.observation.Cardinality;
import it.tidalwave.observation.ObservationItem;
import it.tidalwave.observation.TextNote;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import it.tidalwave.observation.bluebill.BirdGender;
import it.tidalwave.observation.bluebill.ObservationType;
import static it.tidalwave.observation.TextNote.TextNote;
import static it.tidalwave.observation.bluebill.BirdGender.*;
import static it.tidalwave.observation.bluebill.ObservationType.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public enum ObservationItemRenderingOption 
  {
    CARDINALITY
      {
        public void render (final @Nonnull Builder builder, final @Nonnull ObservationItem observationItem)
          {
            try
              {
                final Cardinality cardinality = observationItem.getCardinality();

                if (!cardinality.equals(Cardinality.UNDEFINED))
                  {
                    builder.beginSectionInParentheses();
                    builder.append(builder.separator).append(cardinality.toString());
                    builder.endSection(this);
                  }           
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }        
      },
    
    GENDER
      {
        public void render (final @Nonnull Builder builder, final @Nonnull ObservationItem observationItem)
          {
            try
              {
                final BirdGender gender = observationItem.getObservable().as(BirdGender);

                if (!gender.equals(NOT_RECORDED))
                  {
                    builder.beginSectionInParentheses();
                    String shortGender = gender.getShortDisplayName(getLocale());

                    if (!CARDINALITY.equals(builder.prevOption))
                      {
//                        if (!shortGender.startsWith(" ") && builder.separator.endsWith(" "))
//                          {
//                            builder.separator = builder.separator.substring(0, builder.separator.length() - 1);
//                          }

                        builder.append(builder.separator);
                        shortGender = shortGender.trim();
                      }
                    
                    builder.append(shortGender);
                    builder.endSection(this);
                  }
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }        
      },
    
    MODE
      {
        public void render (final @Nonnull Builder builder, final @Nonnull ObservationItem observationItem)
          {
            final Collection observationTypes = 
                    observationItem.getObservable().getLookup().lookupAll(ObservationType.class);

            if ((observationTypes.size() > 1) || !observationTypes.contains(WATCHED))
              {
                builder.beginSectionOutsideParentheses();
                String separator = builder.separator;
                
                for (final ObservationType observationType : observationTypes)
                  {
                    builder.append(separator).append(observationType.getDisplayName(getLocale()));
                    separator = ", ";
                  }
                
                builder.endSection(this);
              }
          }        
      },
    
    NOTE_FULL
      {
        public void render (final @Nonnull Builder builder, final @Nonnull ObservationItem observationItem)
          {
            try
              {
                final TextNote textNote = observationItem.getObservable().as(TextNote);
                builder.beginSectionOutsideParentheses();
                builder.append(builder.separator.replace(", ", " - ")).append(textNote.stringValue());
                builder.endSection(this);
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }        
      },
    
    NOTE_REFERENCE
      {
        public void render (final @Nonnull Builder builder, final @Nonnull ObservationItem observationItem)
          {
            try
              {
                observationItem.getObservable().as(TextNote); // probes the presence of a TextNote
                builder.beginSectionOutsideParentheses();
                builder.append(builder.separator.replace(", ", " - ")).append("[note]");
                builder.endSection(this);
              }
            catch (AsException e)
              {
                // not present, never mind
              }
          }        
      };

    private static final @Nonnull Provider taxonomyPreferences = Locator.createProviderFor(TaxonomyPreferences.class);

    public abstract void render(Builder builder, ObservationItem observationItem);
    
    private static Locale getLocale()
      {
        return taxonomyPreferences.get().getTaxonomyLocales().get(0);
      }
    
    public static class Builder
      {
        private final StringBuilder builder = new StringBuilder();
        private String separator = "";
        private boolean paren = false;
        private ObservationItemRenderingOption prevOption;
        
        @Nonnull
        public StringBuilder append (final @Nonnull String string)
          {
            return builder.append(string);  
          }
        
        @Nonnull
        public String asString()
          {
            beginSectionOutsideParentheses();
            return builder.toString();  
          }

        private void beginSectionInParentheses()
          {
            if (!paren)
              {
                paren = true;
                
                if (!"".equals(separator))
                  {
                    builder.append(" ");  
                  }
                
                builder.append("(");
                separator = "";
              }
          }

        private void beginSectionOutsideParentheses() 
          {
            if (paren)
              {
                paren = false;
                builder.append(")");
                separator = ", ";
              }
          }

        private void endSection (final @Nonnull ObservationItemRenderingOption option) 
          {
            prevOption = option;
            separator = ", ";
          }
      }    
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy