org.martin.ftp.model.TMFiles.java Source code

Java tutorial

Introduction

Here is the source code for org.martin.ftp.model.TMFiles.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.martin.ftp.model;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.LinkedList;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import org.apache.commons.net.ftp.FTPFile;
import org.martin.ftp.util.Utilities;

/**
 *
 * @author martin
 */
public class TMFiles implements TableModel {

    private LinkedList<FTPFile> files;
    private static final NumberFormat nf = new DecimalFormat("#0.0");
    private static final short BYTE_IN_KILOBYTE = 1024;

    public TMFiles(LinkedList<FTPFile> files) {
        this.files = files;
    }

    /**
     * Metodo que eleva cualquier numero entregandole el exponente
     * @param number numero a elevar
     * @param numberOfTimes exponente
     * @return resultado de la potencia
     */

    public int getIndex(String name) {

        int index = -1;

        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).getName().equalsIgnoreCase(name))
                return i;
        }
        return index;
    }

    private long raise(int number, int numberOfTimes) {

        for (int i = 1; i < numberOfTimes; i++)
            number *= number;

        return number;
    }

    public LinkedList<FTPFile> getFiles() {
        return files;
    }

    public void setFiles(LinkedList<FTPFile> files) {
        this.files = files;
    }

    public FTPFile getFile(int index) {
        return files.get(index);
    }

    @Override
    public int getRowCount() {
        return files.size();
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public String getColumnName(int columnIndex) {

        if (columnIndex == 0)
            return "Nombre";

        else if (columnIndex == 1)
            return "Tamao";

        else
            return "Ultima modificacion";
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {

        if (columnIndex == 0)
            return Object.class;

        else
            return String.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    FTPFile file = files.get(rowIndex);
    long tamao = file.getSize();
    Date timeStamp = file.getTimestamp().getTime();
        
    switch (columnIndex) {
        case 0:
            return file.getName();
        case 1:
            if (tamao < raise(BYTE_IN_KILOBYTE, 2))
                return nf.format((double) tamao / 1000) + "kB";
                
            else if (tamao < raise(BYTE_IN_KILOBYTE, 3))
                return nf.format((double) tamao / raise(BYTE_IN_KILOBYTE, 2)) + "MB";
                
            else
                return nf.format((double) tamao / raise(BYTE_IN_KILOBYTE, 3)) + "GB";
        default:
            return Utilities.getDateToString(timeStamp);
    }
        
}

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {

    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }
}