FileSystemModel.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:3k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: FileSystemModel.java,v 1.2 2005/10/10 18:01:37 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.treetable;
  22. import java.io.File;
  23. import javax.swing.tree.TreeNode;
  24. import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
  25. /**
  26.  * FileSystemModel
  27.  *
  28.  * @author Ramesh Gupta
  29.  */
  30. public class FileSystemModel extends DefaultTreeTableModel {
  31.     protected boolean asksAllowsChildren;
  32.     public FileSystemModel() {
  33. this(new FileNode(new File(File.separator)));
  34.     }
  35.     public FileSystemModel(TreeNode root) {
  36. this(root, false);
  37.     }
  38.     public FileSystemModel(TreeNode root, boolean asksAllowsChildren) {
  39. super(root);
  40. this.asksAllowsChildren = asksAllowsChildren;
  41.     }
  42.     public Object getChild(Object parent, int index) {
  43. try {
  44.     return ((FileNode)parent).getChildren().get(index);
  45. }
  46. catch (Exception ex) {
  47.     return super.getChild(parent, index);
  48. }
  49.     }
  50.     public int getChildCount(Object parent) {
  51. try {
  52.     return ((FileNode)parent).getChildren().size();
  53. }
  54. catch (Exception ex) {
  55.     return super.getChildCount(parent);
  56. }
  57.     }
  58.     public int getColumnCount() {
  59. /**@todo Implement this org.jdesktopx.swing.treetable.TreeTableModel abstract method*/
  60. return 4;
  61.     }
  62.     public String getColumnName(int column) {
  63.         switch(column) {
  64. case 0:
  65.     return "Name";
  66. case 1:
  67.     return "Size";
  68. case 2:
  69.     return "Directory";
  70. case 3:
  71.     return "Modification Date";
  72. default:
  73.     return "Column " + column;
  74.         }
  75.     }
  76.     public Object getValueAt(Object node, int column) {
  77. final File file = ((FileNode)node).getFile();
  78. try {
  79.     switch(column) {
  80.     case 0:
  81. return file.getName();
  82.     case 1:
  83. return file.isFile() ? new Integer((int)file.length()) : ZERO;
  84.     case 2:
  85. return new Boolean(!file.isFile());
  86.     case 3:
  87. return new java.util.Date(file.lastModified());
  88.     }
  89. }
  90. catch  (Exception ex) {
  91.     ex.printStackTrace();
  92. }
  93. return null;
  94.     }
  95.     // The the returned file length for directories.
  96.     private static final Integer ZERO = new Integer(0);
  97. }