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

it.tidalwave.bluebill.mobile.android.taxonomy.SelectableTaxonomyViewRenderable 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.android.taxonomy;

import java.text.ParseException;
import javax.annotation.Nonnull;
import it.tidalwave.role.ui.android.ViewRenderable;
import android.view.View;
import android.widget.CheckedTextView;
import android.widget.TextView;
import it.tidalwave.android.javax.xml.datatype.XMLGregorianCalendar;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.ui.commonnames.CommonNamesPresentationModelCapabilitiesProvider;
import it.tidalwave.bluebill.taxonomy.mobile.Taxonomy;
import it.tidalwave.role.Displayable;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.semantic.EntityWithProperties;
import it.tidalwave.semantic.document.Document;
import it.tidalwave.semantic.vocabulary.DublinCoreVocabulary;
import it.tidalwave.util.Key;
import it.tidalwave.util.NotFoundException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import lombok.RequiredArgsConstructor;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor
public class SelectableTaxonomyViewRenderable implements ViewRenderable
  {
    private static final Key KEY_LANGUAGE = new Key("http://purl.org/dc/elements/1.1/language"); // FIXME
    private static final Key KEY_DATE = new Key("http://purl.org/dc/elements/1.1/date"); // FIXME
    
    private static final Class _ = CommonNamesPresentationModelCapabilitiesProvider.class;
    
    @Nonnull
    private final PresentationModel taxonomyPM;
    
    public void renderTo (final @Nonnull View view, final @Nonnull Object... extra) 
      {
        final CheckedTextView tvName = (CheckedTextView)view.findViewById(R.id.tvName);
        final TextView tvDescription = (TextView)view.findViewById(R.id.tvDescription);
        final TextView tvPublisher = (TextView)view.findViewById(R.id.tvPublisher);
        final TextView tvAuthors = (TextView)view.findViewById(R.id.tvAuthors);
        final TextView tvLicense = (TextView)view.findViewById(R.id.tvLicense);
        final TextView tvDate = (TextView)view.findViewById(R.id.tvDate);
        final TextView tvLanguages = (TextView)view.findViewById(R.id.tvLanguages);

        final Taxonomy taxonomy = taxonomyPM.as(Taxonomy.class); // FIXME: should work without this intermediate passage
        tvName.setText(taxonomy.as(Displayable.class).getDisplayName());
        
        x(tvDescription, "", taxonomy.as(Document.class), DublinCoreVocabulary.DC_DESCRIPTION);
        
        if (tvName.getText().toString().contains("Roberts"))
          {
            x2(tvLanguages, "Common names in: ", taxonomy.as(Document.class), KEY_LANGUAGE, _);
          }
        else
          {
            x(tvLanguages, "Common names in: ", taxonomy.as(Document.class), KEY_LANGUAGE, _);
          }
        
        x(tvPublisher, "Publisher: ", taxonomy.as(Document.class), DublinCoreVocabulary.DC_PUBLISHER);
        x(tvDate, "Publishing date: ", taxonomy.as(Document.class), KEY_DATE);
        x(tvAuthors, "Authors: ", taxonomy.as(Document.class), DublinCoreVocabulary.DC_CREATOR);
        x(tvLicense, "License: ", taxonomy.as(Document.class), DublinCoreVocabulary.DC_RIGHTS);
//        tvLanguages.setText(taxonomy.getLocales().toString());
      }    
    
    private static void x (final @Nonnull TextView textView, 
                           final @Nonnull String s,
                           final @Nonnull EntityWithProperties entity, 
                           final @Nonnull Key key)
      {
        x(textView, s, entity, key, null);
      }
    
    private static void x (final @Nonnull TextView textView, 
                           final @Nonnull String s,
                           final @Nonnull EntityWithProperties entity, 
                           final @Nonnull Key key,
                           final Class clazz)
      {
        final StringBuilder buffer = new StringBuilder(s);
        String separator = "";
        
        for (final Object value : entity.getMultiple(key))
          {
            String ss = "";

            try 
              {
                final DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd'Z'");
                final Date date = format1.parse(value.toString());
                final DateFormat format2 = new SimpleDateFormat("MMMMMMMMMMMMMMM yyyy");
                ss = format2.format(date); 
              } 
            catch (ParseException ex) 
              {
                ss = (clazz == null) ? value.toString() : NbBundle.getMessage(_, "lang_" + value.toString().toLowerCase());
              }
            
//            if (value instanceof XMLGregorianCalendar)
//              {
//                final Date date = ((XMLGregorianCalendar)value).toGregorianCalendar().getTime();
//                final DateFormat format = new SimpleDateFormat("MMMMMMMMMMMMMMM yyyy");
//                ss = format.format(date);
//              }
            
            if ("http://creativecommons.org/license/publicdomain/".equals(ss))
              {
                ss = "Public domain";  
              }
            
            buffer.append(separator).append(ss);
            separator = ", ";
          }

        textView.setText(buffer.toString());
      }

    private static void x2 (final @Nonnull TextView textView, 
                           final @Nonnull String s,
                           final @Nonnull EntityWithProperties entity, 
                           final @Nonnull Key key,
                           final Class clazz)
      {
        final StringBuilder buffer = new StringBuilder(s);
        String separator = "";
        
        for (final Object value : Arrays.asList("en", "de", "af"))
          {
            String ss = "";
            ss = (clazz == null) ? value.toString() : NbBundle.getMessage(_, "lang_" + value.toString().toLowerCase());
            buffer.append(separator).append(ss);
            separator = ", ";
          }

        textView.setText(buffer.toString());
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy