it.tidalwave.mobile.util.Downloadable 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: Downloadable.java,v e2522453a1ad 2010/06/26 18:59:06 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.mobile.util;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.SortedSet;
import java.util.TreeSet;
import java.io.File;
import it.tidalwave.util.Displayable;
import it.tidalwave.util.Removable;
import it.tidalwave.util.logging.Logger;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public abstract class Downloadable implements Removable
{
private static final String CLASS = Downloadable.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public static enum Status implements Displayable
{
NOT_DOWNLOADED,
QUEUED,
DOWNLOADING,
DOWNLOADED,
OBSOLETE,
BROKEN;
private static final String BUNDLE_NAME = Status.class.getPackage().getName().replace('.', '/') + "/Bundle";
/* package */ static final Comparator LOCALE_COMPARATOR = new Comparator()
{
public int compare (final @Nonnull Locale l1, final @Nonnull Locale l2)
{
return l1.getLanguage().compareTo(l2.getLanguage());
}
};
@Nonnull
public String getDisplayName()
{
return getDisplayName(Locale.getDefault());
}
@Nonnull
public String getDisplayName (final @Nonnull Locale locale)
{
// final ResourceBundle bundle = NbBundle.getBundle(BUNDLE_NAME, locale); FIXME: crashes Android 1.5
final ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
return bundle.getString("status_" + toString());
}
@Nonnull
public Map getDisplayNames()
{
final Map result = new HashMap();
for (final Locale locale : getLocales())
{
result.put(locale, getDisplayName(locale));
}
return result;
}
@Nonnull
public SortedSet getLocales()
{
final TreeSet treeSet = new TreeSet(LOCALE_COMPARATOR);
treeSet.addAll(Arrays.asList(new Locale("en"), new Locale("it"), new Locale("fr")));
return treeSet;
}
}
public static final Class Downloadable = Downloadable.class;
public static final String PROP_DOWNLOAD_PROGRESS = "downloadProgress";
public static final String PROP_STATUS = "status";
public static final String PROP_CACHED = "cached";
@Nonnegative
protected float downloadProgress = 0f;
@Nonnull
protected Status status = Status.NOT_DOWNLOADED;
protected boolean cached = false;
private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnegative
public float getDownloadProgress()
{
return downloadProgress;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public Status getStatus()
{
return status;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Nonnull
public abstract File getFile();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public boolean isCached()
{
return cached;
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void addPropertyChangeListener (final @Nonnull PropertyChangeListener listener)
{
propertyChangeSupport.addPropertyChangeListener(listener);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public void removePropertyChangeListener (final @Nonnull PropertyChangeListener listener)
{
propertyChangeSupport.removePropertyChangeListener(listener);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract void download();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
public abstract void refresh();
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setDownloadProgress (final @Nonnull float downloadProgress)
{
final float oldDownloadProgress = this.downloadProgress;
this.downloadProgress = downloadProgress;
propertyChangeSupport.firePropertyChange(PROP_DOWNLOAD_PROGRESS, oldDownloadProgress, downloadProgress);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setStatus (final @Nonnull Status status)
{
logger.finer("setStatus(%s)", status);
final Status oldStatus = this.status;
this.status = status;
propertyChangeSupport.firePropertyChange(PROP_STATUS, oldStatus, status);
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
protected void setCached (final boolean cached)
{
final boolean oldCached = this.cached;
this.cached = cached;
propertyChangeSupport.firePropertyChange(PROP_CACHED, oldCached, cached);
}
}