
it.tidalwave.bluebill.mobile.taxonomy.impl.TaxonHtmlRenderable 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://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.taxonomy.impl;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.Parameters;
import it.tidalwave.role.HtmlRenderable;
import it.tidalwave.role.spi.StringRenderableSupport;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonRenderingOptions;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import lombok.RequiredArgsConstructor;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@Immutable @RequiredArgsConstructor
public class TaxonHtmlRenderable extends StringRenderableSupport implements HtmlRenderable
{
private final Provider preferences = Locator.createProviderFor(TaxonomyPreferences.class);
@Nonnull
private final Taxon taxon;
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public String render (final @Nonnull Object ... args)
{
final TaxonRenderingOptions option = Parameters.find(TaxonRenderingOptions.class, TaxonRenderingOptions.STANDARD, args);
final List taxonLocales = preferences.get().getTaxonomyLocales();
switch (option) // FIXME: replace the switch with polymorphism
{
case STANDARD:
return render(taxonLocales, preferences.get().isScientificNamesRenderingEnabled());
case PRIMARY_AND_SCIENTIFIC_NAME:
return render(Collections.singletonList(taxonLocales.get(0)), true);
case HIERARCHY:
return renderHierarchy();
}
throw new IllegalArgumentException(option.toString());
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private String render (final @Nonnull Collection locales,
final boolean scientificNamesRendered)
{
final StringBuilder buffer = new StringBuilder();
String separator = "";
String style1 = "";
String style2 = "";
String size1 = "";
String size2 = "";
boolean atLeastOneDisplayName = false;
for (final Locale locale : locales)
{
try
{
final String displayName = taxon.getDisplayName(locale);
buffer.append(separator + size1 + style1 + displayName + style2 + size2);
style1 = style2 = "";
separator = "
";
size1 = "";
size2 = "";
atLeastOneDisplayName = true;
}
catch (NotFoundException e)
{
}
}
if (!atLeastOneDisplayName || scientificNamesRendered)
{
buffer.append(separator);
buffer.append(String.format("%s", taxon.getScientificName()));
}
return buffer.toString();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
private String renderHierarchy()
{
final StringBuilder buffer = new StringBuilder();
for (final Taxon taxon : getPath())
{
buffer.append(String.format("%s
", taxon.getScientificName()));
}
return buffer.toString();
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
protected List getPath()
{
final List path = new ArrayList();
try
{
Taxon parent = taxon.getParent().getParent(); // Skip Genus, it's already rendered as part of the name
for (;;)
{
path.add(parent);
parent = parent.getParent();
}
}
catch (NotFoundException e)
{
// ok, done
}
Collections.reverse(path);
return path;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy