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

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.MediaViewHelper Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * 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: MediaViewHelper.java,v 4007ee5c1e71 2010/06/26 01:14:22 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet;

import android.content.Context;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import org.openide.util.NbBundle;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.Key;
import it.tidalwave.mobile.util.Downloadable.Status;
import it.tidalwave.mobile.util.Downloadable;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.mobile.media.Entity;
import it.tidalwave.bluebill.observation.Observation;
import static it.tidalwave.mobile.util.Downloadable.Downloadable;
import android.view.View;
import android.widget.TextView;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.netbeans.util.Locator;
import org.xml.sax.ext.Locator2;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public abstract class MediaViewHelper
  {
    private final static Map colorMapByStatus = new HashMap();

    protected final static Entity UNKNOWN_AUTHOR = Entity.namedEntity("");
    protected final static Entity UNKNOWN_LOCATION = Entity.namedEntity("");

    @Nonnull
    private final TextView tvStatus;

    @CheckForNull
    private Downloadable downloadable;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    static
      {
        colorMapByStatus.put(Status.BROKEN,         R.color.download_status_broken);
        colorMapByStatus.put(Status.DOWNLOADED,     R.color.download_status_downloaded);
        colorMapByStatus.put(Status.DOWNLOADING,    R.color.download_status_downloading);
        colorMapByStatus.put(Status.NOT_DOWNLOADED, R.color.download_status_not_downloaded);
        colorMapByStatus.put(Status.OBSOLETE,       R.color.download_status_obsolete);
        colorMapByStatus.put(Status.QUEUED,         R.color.download_status_queued);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public MediaViewHelper (final @Nonnull View view)
      {
        tvStatus = (TextView)view.findViewById(R.id.tvStatus);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void setObservation (final @Nonnull Observation observation)
      throws NotFoundException
      {
        final Media media = observation.as(Media.class);
        downloadable = media.as(Downloadable);
        renderStatus();
        downloadable.addPropertyChangeListener(new PropertyChangeListener()
          {
            public void propertyChange (final @Nonnull PropertyChangeEvent event)
              {
                tvStatus.post(new Runnable()
                  {
                    public void run()
                      {
                        renderStatus();
                      }
                  });
              }
          });
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void renderStatus()
      {
        final Status status = downloadable.getStatus();
        final StringBuilder buffer = new StringBuilder();
        buffer.append(status.getDisplayName());

        if (status == Status.DOWNLOADING)
          {
            buffer.append(String.format(" (%d%%)", Math.round(100 * downloadable.getDownloadProgress())));
          }

        tvStatus.setText(buffer.toString());
        tvStatus.setTextColor(Locator.find(Context.class).getResources().getColor(colorMapByStatus.get(status)));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static  T get (final @Nonnull Media media, final @Nonnull Key key, final @Nonnull T defaultValue)
      {
        try
          {
            return media.get(key);
          }
        catch (NotFoundException e)
          {
            return defaultValue;
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void renderLicense (final @Nonnull TextView tv, final @Nonnull Media media)
      {
        try
          {
            tv.setText(NbBundle.getMessage(MediaViewHelper.class, "licenseShort-" + media.get(Media.RIGHTS)));
          }
        catch (MissingResourceException e)
          {
            tv.setText("???");
          }
        catch (Exception e)
          {
            tv.setText(NbBundle.getMessage(MediaViewHelper.class, "unknownLicense"));
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy