/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull public TaxonomyFinder findTaxonomies() { return taxonomyManager.findTaxonomies(); }
@Nonnull private String getDisplayName (final @Nonnull Taxon taxon) { try { return taxon.getDisplayName(primaryTaxonLocale); } catch (NotFoundException e) { // We prepend some zeds to scientific names, so if they are compared against common names, // they go to the bottom of the list. // FIXME: in some locales, Z is not the last letter! return "ZZZZZZZ" + taxon.getScientificName(); } } };
public int compare (final @Nonnull Taxon taxon1, final @Nonnull Taxon taxon2) { return collator.compare(taxon1.getScientificName(), taxon2.getScientificName()); } };
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull public static Intent intentFor (final @Nonnull Taxon taxon) throws NotFoundException { // FIXME: you should really use the Uri, but it fails to find the Intent // FIXME: probably it needs a further declaration in the Android manifest // return new Intent(ACTION_TAXON, Uri.parse(taxon.getId().stringValue())); final Intent intent = new Intent(getActionPackage() + taxon.getRank().getNarrower().name()); intent.putExtra(TAXON_ID, taxon.getId().stringValue()); return intent; }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Override public void readExternal (final @Nonnull ObjectInput oi) throws IOException, ClassNotFoundException { final Id id = (Id)oi.readObject(); try { final Taxonomy taxonomy = Locator.find(TaxonomyPreferences.class).getTaxonomy(); taxon = taxonomy.findTaxa().withId(id).result(); } catch (NotFoundException e) { throw new IOException(e.toString()); // Java 5 / Android compatibility } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Override public void writeExternal (final @Nonnull ObjectOutput oo) throws IOException { oo.writeObject(taxon.getId()); }
/******************************************************************************************************************* * * Loads the taxa. If a {@link Taxon} is contained in the {@link Intent}, the sub taxa will be loaded and the cache * disabled since there are no taxa with a number of sub taxa so large to create performance problems while sorting. * If no {@code Taxon} is contained in the {@code Intent}, all the taxa with the {@link Rank} bound to this * controller will be loaded; and the cache enabled if the rank is {@code SPECIES} since there can be so many * items to create performance problems when sorting. The latter behaviour also happens if the {@code Intent} * contains a {@code Taxon}, but it's not from the current taxonomy. * * TODO: This management of cache is cumbersome. Using Finders with sorting capabilities could be a solution, * moving all the caching logics to the Finder themselves (and eventually could opaquely implement sorting * based on OpenSesame query that could be fast enough to avoid caching). * ******************************************************************************************************************/ @Nonnull private void loadTaxa (final @Nonnull Intent intent) { taxa.clear(); cacheEnabled = false; try { taxa.addAll(TaxonIntentHelper.getTaxon(intent).findSubTaxa().results()); } catch (NotFoundException e) // no Taxon in the Intent { taxa.addAll(taxonomyPreferences.get().getTaxonomy().findTaxa().withRank(rank).results()); cacheEnabled = rank == Rank.SPECIES; } ensureTaxaAreSorted(); }
@Override public void run() { log.info("renderTaxon({})", taxon); taxon.as(TextViewRenderable).renderTo(tvTaxon); } });
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull protected Taxonomy loadTaxonomy (final @Nonnull Id taxonomyId) throws Exception { log.info("loadTaxonomy({})", taxonomyId); final long time = System.currentTimeMillis(); final Taxonomy taxonomy = taxonomyManager.findTaxonomies().withId(taxonomyId).result(); log.info(">>>> taxonomy loaded in {} msecs", System.currentTimeMillis() - time); return taxonomy; }
@Nonnull public String getTaxonomyDisplayName() { return taxonomyPreferences.getTaxonomy().as(Displayable.class).getDisplayName(); }
/******************************************************************************************************************* * * ******************************************************************************************************************/ @Nonnull public static Intent intentFor (final @Nonnull Rank rank) { return new Intent(getActionPackage() + rank.name()); }
public CommonNamesPresentationModel (final @Nonnull Taxon taxon, final @Nonnull Locale locale) throws NotFoundException { super(new Object[] { taxon }); language = getMessage(_, "lang_" + locale.getLanguage()); name = taxon.getDisplayName(locale); scientificName = taxon.getScientificName(); } }
/******************************************************************************************************************* * * Retrieves the {@link Taxon} parameter from an {@link Intent}. * * @return the {@code Taxon} * @throws NotFoundException if no {@code Taxon} has been passed as a parameter * ******************************************************************************************************************/ @Nonnull public static Taxon getTaxon (final @Nonnull Intent intent) throws NotFoundException { NotFoundException.throwWhenNull(intent, "No Intent"); final String id = NotFoundException.throwWhenNull(intent.getExtras(), "No extras").getString(TAXON_ID); NotFoundException.throwWhenNull(id, "No taxonId"); return Locator.find(TaxonomyPreferences.class).getTaxonomy().findTaxa().withId(new Id(id)).result(); }
@Nonnull public String getLocalizedName (final @Nonnull Taxon taxon) throws NotFoundException { // FIXME: string concatenation here kills the performance - make it "No name for %s", taxon); // return NotFoundException.throwWhenNull(map.get(taxon.getId()), "No name for " + taxon); return NotFoundException.throwWhenNull(map.get(taxon.getId()), "No name for taxon"); }
public void run() { log.info("renderTaxon({})", taxon); taxon.as(TextViewRenderable).renderTo(tvTaxon, TaxonRenderingOptions.PRIMARY_AND_SCIENTIFIC_NAME); } });
/******************************************************************************************************************* * * ******************************************************************************************************************/ private boolean matchesScientificName (final @Nonnull Taxon taxon, final @Nonnull CharSequence match, final @Nonnull NameMatcher nameMatcher) { return nameMatcher.matches(taxon.getScientificName(), match, Locale.ENGLISH); } }
/******************************************************************************************************************* * * ******************************************************************************************************************/ public void addToTaxonHistory (final @Nonnull Taxon taxon) { final Id id = taxon.getId(); taxonIdHistory.remove(id); taxonIdHistory.add(0, id); while (taxonIdHistory.size() > maxHistorySize) { taxonIdHistory.remove(maxHistorySize); } save(); }
public void renderTo (final @Nonnull TextView textView, final @Nonnull Object ... args) { textView.setText(Html.fromHtml(taxon.as(HtmlRenderable).render(args))); } }