001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.frame.progress;
020
021import icy.math.RateMeter;
022import icy.math.UnitUtil;
023import icy.util.StringUtil;
024
025/**
026 * @author stephane
027 */
028public class DownloadFrame extends CancelableProgressFrame
029{
030    /**
031     * calculated download rate
032     */
033    private double rate;
034
035    /**
036     * internal
037     */
038    private final RateMeter meter;
039
040    public DownloadFrame()
041    {
042        this("", 0);
043    }
044
045    public DownloadFrame(String path)
046    {
047        this(path, 0);
048    }
049
050    public DownloadFrame(String path, double length)
051    {
052        super(StringUtil.isEmpty(path) ? "" : StringUtil.limit("Downloading " + path, 64));
053
054        meter = new RateMeter();
055        this.length = length;
056        rate = 0;
057    }
058
059    public void setPath(String path)
060    {
061        setMessage(StringUtil.limit("Downloading " + path, 64));
062    }
063
064    /**
065     * @deprecated use setMessage(..) instead
066     */
067    @Deprecated
068    public void setMessageBase(String messageBase)
069    {
070        setMessage(messageBase);
071    }
072
073    @Override
074    protected String buildMessage(String text)
075    {
076        String mess = text + "  [";
077
078        // information on position
079        if (position != -1d)
080            mess += UnitUtil.getBytesString(position);
081        else
082            mess += "???";
083
084        mess += " / ";
085
086        if (length > 0d)
087            mess += UnitUtil.getBytesString(length);
088        else
089            mess += "???";
090
091        if (rate > 0)
092            mess += " - " + UnitUtil.getBytesString(rate) + "/s";
093
094        mess += "]";
095
096        return super.buildMessage(mess);
097    }
098
099    @Override
100    public void setLength(double length)
101    {
102        if (getLength() != length)
103            meter.reset();
104
105        super.setLength(length);
106    }
107
108    @Override
109    public void setPosition(double position)
110    {
111        // update rate
112        if (getPosition() < position)
113            rate = meter.updateFromTotal(position);
114        else
115            rate = 0;
116
117        super.setPosition(position);
118    }
119
120}