Package com.groovesquid.gui

Source Code of com.groovesquid.gui.ProgressCellRenderer

package com.groovesquid.gui;

import com.groovesquid.model.Track;
import java.awt.Color;
import java.awt.Component;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;


public class ProgressCellRenderer extends JProgressBar implements TableCellRenderer {
    public ProgressCellRenderer() {
        super(0, 100);
        setValue(0);
        setString("");
        setStringPainted(true);
        setOpaque(false);
    }

    @Override
    public boolean isDisplayable() {
        return true;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        String text = "";
        Track track = ((DownloadTableModel)table.getModel()).getSongDownloads().get(row);
       
        if(track != null) {
            Double downloadRate = track.getDownloadRate();
            if(track.getStatus() != null) {
                switch (track.getStatus()) {
                    case INITIALIZING:
                        text = "initializing...";
                        break;
                    case QUEUED:
                        text = "waiting...";
                        break;
                    case DOWNLOADING:
                        if (downloadRate != null) {
                            text = String.format("%1.0f%%, %d of %d kB, %1.0f kB/s",
                                    track.getProgress() * 1.0,
                                    track.getDownloadedBytes() / 1024,
                                    track.getTotalBytes() / 1024,
                                    downloadRate / 1024);
                        } else {
                            text = String.format("%1.0f%%, %d kB",
                                    track.getProgress() * 1.0,
                                    track.getTotalBytes() / 1024);
                        }
                        break;
                    case FINISHED:
                        downloadRate = track.getDownloadRate();
                        if (downloadRate != null) {
                            text = String.format("%d kB, %1.0f kB/s",
                                    track.getDownloadedBytes() / 1024,
                                    downloadRate / 1024);
                        } else {
                            text = String.format("%d kB",
                                    track.getDownloadedBytes() / 1024);
                        }
                        break;
                    case CANCELLED:
                        text = "cancelled";
                        break;
                    case ERROR:
                        text = "Error";
                        value = 100;
                        break;
                }
            }
        }
       
        this.setValue((Integer)value);
        this.setString(text);

        if (isSelected) {
            setBackground(table.getSelectionBackground());
        } else {
            if (row % 2 == 0) {
                setBackground(new Color(242,242,242));
            } else {
                setBackground(new Color(230,230,230));
            }
        }
       
        setOpaque(true);
        setBorderPainted(false);
        setForeground(new Color(243,156,18));
        setFont(table.getFont());
       
        return this;
    }
}
TOP

Related Classes of com.groovesquid.gui.ProgressCellRenderer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.