Package de.kout.wlFxp.view

Source Code of de.kout.wlFxp.view.MainTableModel

/**
* Copyright (C) 2003 Alexander Kout
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

package de.kout.wlFxp.view;

import javax.swing.*;
import javax.swing.table.*;
import java.io.*;
import javax.swing.event.*;
import java.util.*;

import de.kout.wlFxp.ftp.FtpFile;
import de.kout.wlFxp.Utilities;

/**
*  the special implementation of a list model for file lists
*
*@author     Alexander Kout
*@created    30. M�rz 2002
*/

public class MainTableModel
     extends AbstractTableModel {
  Vector files;
  MainPanel panel;
  boolean isAscent = true;
  int column;


  /**
   *  Constructor for the MainListModel object
   *
   *@param  panel       Description of Parameter
   *@param  files       Description of Parameter
   *@param  showHidden  Description of the Parameter
   */
  public MainTableModel(MainPanel panel, boolean showHidden) {
    this.panel = panel;
  }


  /**
   *  Gets the rowCount attribute of the MainTableModel object
   *
   *@return    The rowCount value
   */
  public int getRowCount() {
    if (panel.mode == panel.FTP && !panel.ftpSession.connected()) {
      return 0;
    }
    if (files != null) {
      return files.size() + 1;
    } else {
      return 0;
    }
  }


  /**
   *  Gets the columnCount attribute of the MainTableModel object
   *
   *@return    The columnCount value
   */
  public int getColumnCount() {
    return 3;
  }


  /**
   *  Gets the columnName attribute of the MainTableModel object
   *
   *@param  columnIndex  Description of the Parameter
   *@return              The columnName value
   */
  public String getColumnName(int columnIndex) {
    if (columnIndex == 0) {
      return "Name";
    } else if (columnIndex == 1) {
      return "Size";
    } else if (columnIndex == 2) {
      return "Mode";
    } else if (columnIndex == 3) {
      return "Date";
    } else {
      return "";
    }
  }


  /**
   *  Gets the columnClass attribute of the MainTableModel object
   *
   *@param  columnIndex  Description of the Parameter
   *@return              The columnClass value
   */
  public Class getColumnClass(int columnIndex) {
    switch (columnIndex) {
            case 0:
              return FtpFile.class;
            case 1:
              return String.class;
            case 2:
              return String.class;
            case 3:
              return String.class;
            default:
              return Object.class;
    }
  }


  /**
   *  Gets the cellEditable attribute of the MainTableModel object
   *
   *@param  rowIndex     Description of the Parameter
   *@param  columnIndex  Description of the Parameter
   *@return              The cellEditable value
   */
  public boolean isCellEditable(int rowIndex, int columnIndex) {
    return false;
  }


  /**
   *  Gets the elementAt attribute of the MainListModel object
   *
   *@param  rowIndex     Description of the Parameter
   *@param  columnIndex  Description of the Parameter
   *@return              The elementAt value
   */
  public Object getValueAt(int rowIndex, int columnIndex) {
    if (rowIndex == 0) {
      if (columnIndex > 0) {
        return "";
      }
      return new FtpFile("..");
    }
    if (files.size() >= rowIndex && rowIndex > 0) {
      if (columnIndex == 0) {
        return (FtpFile) files.elementAt(rowIndex - 1);
      } else if (columnIndex == 1) {
        return String.valueOf(((FtpFile) files.elementAt(rowIndex - 1)).hSize);
      } else if (columnIndex == 2) {
        return ((FtpFile) files.elementAt(rowIndex - 1)).getMode();
      } else if (columnIndex == 3) {
        return ((FtpFile) files.elementAt(rowIndex - 1)).getDate();
      }
    }
    return new FtpFile();
  }


  /**
   *  Sets the valueAt attribute of the MainTableModel object
   *
   *@param  aValue       The new valueAt value
   *@param  rowIndex     The new valueAt value
   *@param  columnIndex  The new valueAt value
   */
  public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
    if (rowIndex == 0) {
      return;
    }
    ((FtpFile) files.elementAt(rowIndex - 1)).setName((String) aValue);
  }

  public void removeElementAt(int i) {
    if (i > -1) {
      files.removeElementAt(i);
    }
  }

  public void removeElement(FtpFile f) {
    for (int i = 0; i < files.size(); i++)
      if (((FtpFile) files.elementAt(i)).getAbsolutePath().equals(f.getAbsolutePath())) {
        files.removeElementAt(i);
        return;
      }
  }

  public void addElement(FtpFile f) {
    files.addElement(f);
    fireTableDataChanged();
  }


  /**
   *  Sets the files attribute of the MainTableModel object
   *
   *@param  files       The new files value
   *@param  showHidden  The new files value
   */
  public void setFiles(Vector files, boolean showHidden) {
    if (showHidden) {
      this.files = files;
    } else {
      for (int i = files.size()-1; i >= 0; i--) {
        if (!((FtpFile) files.elementAt(i)).isVisible()) {
          files.removeElementAt(i);
        } else {
          if (((FtpFile) files.elementAt(i)).isFile()) {
            panel.dirSize += ((FtpFile) files.elementAt(i)).getSize();
          }
        }
      }
      this.files = files;
    }
    sortBy(column, isAscent);
  }


  /**
   *  Description of the Method
   *
   *@param  column    Description of the Parameter
   *@param  isAscent  Description of the Parameter
   */
  public void sortBy(int column, boolean isAscent) {
    this.column = column;
    this.isAscent = isAscent;
    String sortby = "Name";
    if (column == 0 && isAscent) {
      sortby = "Name";
    } else if (column == 0 && !isAscent) {
      sortby = "IName";
    } else if (column == 1 && isAscent) {
      sortby = "Size";
    } else if (column == 1 && !isAscent) {
      sortby = "ISize";
    } else if (column == 3 && isAscent) {
      sortby = "Date";
    } else if (column == 3 && !isAscent) {
      sortby = "IDate";
    }
    if (files != null)
      Utilities.sortFiles(files, sortby, false, null);
    fireTableDataChanged();
  }
}
TOP

Related Classes of de.kout.wlFxp.view.MainTableModel

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.