Package nixonftp.ui

Source Code of nixonftp.ui.QueueCellRenderer

/* nixonFTP
* FTP client version 0.1
* Copyright (C) 2010 NIXON Development Corporation.
* All rights reserved.
* http://members.shaw.ca/nixon.com
*/

package nixonftp.ui;
import nixonftp.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.ListCellRenderer;
import javax.swing.SpringLayout;
import nixonftp.list.NXDefaultListModel;


public class NXQueuePanel extends JPanel {
  private final JList list;
  private NXDefaultListModel listModel;
  private JLabel lblProgress;
  private final JLabel lblTime;
  private NXQueueItem currentItem;
  private final JLabel lblTotalProgress;
  private final JLabel lblTotalTime;
  private JScrollPane listScroll;
  private JToggleButton tool;
  private JPanel panel;
  private JLabel lblAll;
  SpringLayout layout;

  public NXQueuePanel() {
    super();
    setLayout(new BorderLayout());
    panel = new JPanel(new GridBagLayout()); //top panel

    JLabel l;
    GridBagConstraints c = new GridBagConstraints();
    JLabel lblCurrent = new JLabel("Current");
    Font f = lblCurrent.getFont().deriveFont(Float.valueOf("14")).deriveFont(Font.BOLD);
    lblCurrent.setFont(f);
    Insets defaultInsets = new Insets(2,24,2,24);
    c.gridx = 0;
    c.gridy = 3;
    c.insets = defaultInsets;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    panel.add(lblCurrent, c);

    c.gridy = 0;
    lblAll = new JLabel("All");
    lblAll.setFont(f);
    panel.add(lblAll, c);

    /*c.gridx = 0;
    //c.insets = new Insets(4,4,4,4);
    c.anchor = GridBagConstraints.FIRST_LINE_END;
    l = new JLabel("Progress:");
    c.gridy = 1;
    panel.add(l, c);

    l = new JLabel("Time (Remain):");
    c.gridy = 2;
    panel.add(l, c);

    l = new JLabel("Speed:");
    c.gridy = 3;
    panel.add(l, c);*/

    c.gridx = 0;
    c.insets = defaultInsets;
    c.anchor = GridBagConstraints.FIRST_LINE_START;

    lblProgress = new JLabel("0 bytes");
    Font smallFont = lblProgress.getFont().deriveFont(11F);
    lblProgress.setFont(smallFont);
    c.gridy = 4;
    panel.add(lblProgress, c);
   
    lblTime = new JLabel("0 seconds");
    lblTime.setFont(smallFont);
    c.gridy = 5;
    panel.add(lblTime, c);

    c.gridy = 1;
    lblTotalProgress = new JLabel("0 bytes");
    lblTotalProgress.setFont(smallFont);
    panel.add(lblTotalProgress, c);

    lblTotalTime = new JLabel("0 seconds");
    lblTotalTime.setFont(smallFont);
    c.gridy = 2;
    panel.add(lblTotalTime, c);

    add(panel, BorderLayout.NORTH);

    list = new JList(); //queue
    listScroll = new JScrollPane(list);
    add(listScroll, BorderLayout.CENTER);
   
    listModel = new NXDefaultListModel();
    list.setModel(listModel);
    list.setCellRenderer(new QueueCellRenderer());
    list.setPrototypeCellValue(new NXQueueItem("index", "", "/", ""));
  }

  public void addItem(NXQueueItem item, int pos) {
    if (pos == -1) {
      listModel.addElement(item);
    } else {
      listModel.add(pos, item);
    }
  }

  public NXQueueItem getItem(NXQueueItem item) {
    return (NXQueueItem) listModel.get(listModel.indexOf(item));
  }

  public void removeItem(NXQueueItem item) {
    listModel.removeElement(item);
  }

  public void updateActivity(NXQueueItem item) {
    String activity = "Unknown";

    if (item.activity.equals("list")) {
      activity = "Directory Listing";
    } else {
      activity = item.activity;
    }
    //lblAction.setText(activity);
    //lblRemoteFile.setText(item.remoteFile);
    //lblLocalFile.setText(item.localFile);
    currentItem = item;
  }

  public void updateProgress(long currentProgress, int currentSeconds, int totalTransferred, int totalSeconds, long totalSize) {
    lblProgress.setText(NXFormat.fileSize(currentProgress) + " / " + NXFormat.fileSize(currentItem.totalSize));

    int bps = 0;
    if (currentSeconds != 0) bps = (int) (currentProgress / currentSeconds);
   
    int eta = NXFormat.getEta(currentItem.totalSize, currentProgress, bps);
    lblTime.setText(NXFormat.parseTime(currentSeconds) + NXFormat.eta(eta));
    //if (currentSeconds != 0) lblSpeed.setText(NXFormat.fileSize(bps, true) + "/sec");

    lblTotalProgress.setText(NXFormat.fileSize(totalTransferred) + " / " + NXFormat.fileSize(totalSize));

    if (totalSeconds != 0) bps = (int) (totalTransferred / totalSeconds);
    else bps = 0;
    eta = NXFormat.getEta(totalSize, totalTransferred, bps);
    lblTotalTime.setText(NXFormat.parseTime(totalSeconds));
    if (totalSeconds != 0) lblAll.setText("All at " + NXFormat.fileSize(bps) + "/s");
  }
}
class QueueCellRenderer extends JPanel implements ListCellRenderer {

  public Component getListCellRendererComponent(JList list,
    Object value,
    int index,
    boolean isSelected,
    boolean cellHasFocus) {
    removeAll();

    NXQueueItem queueItem = (NXQueueItem) value;
    JLabel lblActivity = new JLabel(queueItem.activity + " " + queueItem.remoteFile);
    JLabel lblSize = new JLabel(NXFormat.fileSize(queueItem.totalSize, true));

    add(lblActivity);
    add(lblSize);

    return this;
  }
}
TOP

Related Classes of nixonftp.ui.QueueCellRenderer

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.