TestJTreeTable.java
001 /*
002  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
003  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
004  *
005  *  This file is part of GATE (see http://gate.ac.uk/), and is free
006  *  software, licenced under the GNU Library General Public License,
007  *  Version 2, June 1991 (in the distribution as file licence.html,
008  *  and also available at http://gate.ac.uk/gate/licence.html).
009  *
010  *  Valentin Tablan, 16 Jan 2008
011  *
012  *  $Id: TestJTreeTable.java 12006 2009-12-01 17:24:28Z thomas_heitz $
013  */
014 package gate.swing;
015 
016 import gate.gui.MainFrame;
017 
018 import java.io.File;
019 import java.util.Date;
020 
021 import javax.swing.*;
022 import javax.swing.tree.DefaultTreeCellRenderer;
023 
024 /**
025  * This class is used to demonstrate the functionality of {@link JTreeTable}.
026  */
027 public class TestJTreeTable {
028   
029   private JFrame mainFrame;
030   
031   private JTreeTable treeTable;
032   
033   private TreeTableModel treeTableModel;
034   
035   private class FileTTModel extends AbstractTreeTableModel{
036     private File root;
037     private String[] columnNames = {"NAME""SIZE""DATE"};
038     private static final int NAME_COLUMN = 0;
039     private static final int SIZE_COLUMN = 1;
040     private static final int DATE_COLUMN = 2;
041     
042     public FileTTModel(File root){
043       super(root);
044       this.root = root;
045     }
046     
047     @Override
048     public Object getChild(Object parent, int index) {
049       if(parent instanceof File){
050         File parentFile = (File)parent;
051         if(parentFile.isDirectory()){
052           File[] children = parentFile.listFiles();
053           if(children != null && children.length > index){
054             return children[index];
055           }else{
056             return null;
057           }
058         }else{
059           throw new RuntimeException("Not a directory!");
060         }
061       }else{
062         throw new RuntimeException("Not a file!");
063       }
064     }
065 
066     /* (non-Javadoc)
067      * @see gate.swing.AbstractTreeTableModel#getChildCount(java.lang.Object)
068      */
069     @Override
070     public int getChildCount(Object parent) {
071       if(parent instanceof File){
072         File parentFile = (File)parent;
073         if(parentFile.isDirectory()){
074           File[] children = parentFile.listFiles();
075           return children == null : children.length;
076         }else{
077           return 0;
078         }
079       }else{
080         throw new RuntimeException("Not a file!");
081       }
082     }
083 
084     @Override
085     public Class getColumnClass(int column) {
086       return String.class;
087     }
088 
089     @Override
090     public int getColumnCount() {
091       return columnNames.length;
092     }
093 
094     @Override
095     public String getColumnName(int column) {
096       return columnNames[column];
097     }
098 
099     @Override
100     public Object getValueAt(Object node, int column) {
101       if(node instanceof File){
102         File nodeFile = (File)node;
103         switch(column) {
104           case NAME_COLUMN: return nodeFile.getName();
105           case SIZE_COLUMN: return nodeFile.length();
106           case DATE_COLUMN: return new Date(nodeFile.lastModified()).toString();
107           defaultreturn "";
108         }
109       }else{
110         throw new RuntimeException("Not a file!");
111       }
112     }
113 
114     @Override
115     public boolean isCellEditable(Object node, int column) {
116       return false;
117     }
118     
119   }
120   
121   public TestJTreeTable(){
122     initGui();
123   }
124   
125   private void initGui(){
126     //Unfortunately this will affect ALL trees
127     //There seems to be no way of setting this up on a tree by tree basis.
128     UIManager.put("Tree.collapsedIcon", MainFrame.getIcon("closed"));
129     UIManager.put("Tree.expandedIcon", MainFrame.getIcon("expanded"));
130     
131     mainFrame = new JFrame(JTreeTable.class.getName());
132     mainFrame.setSize(800600);
133     mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
134     
135     treeTableModel = new FileTTModel(new File("/"));
136     treeTable = new JTreeTable(treeTableModel);
137     
138     DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer();
139     treeRenderer.setOpenIcon(MainFrame.getIcon("open-file"));
140     treeRenderer.setClosedIcon(MainFrame.getIcon("open-file"));
141     treeRenderer.setLeafIcon(MainFrame.getIcon("document"));
142     
143     treeTable.getTree().setCellRenderer(treeRenderer);
144 //    treeTable.getTree().setShowsRootHandles(false);
145 //    treeTable.getTree().setRootVisible(false);
146     //according to the Swing tutorial at 
147     //http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html#display,
148     //this will only work on the Java LookAndFeel. 
149     treeTable.getTree().putClientProperty("JTree.lineStyle""None");
150     mainFrame.getContentPane().add(new JScrollPane(treeTable));
151     
152   }
153   
154   public static void main(String[] args) {
155     TestJTreeTable tester = new TestJTreeTable();
156     tester.mainFrame.setVisible(true);
157   }
158 
159 }