001/* 002 * Copyright (c) 2009 The openGion Project. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 013 * either express or implied. See the License for the specific language 014 * governing permissions and limitations under the License. 015 */ 016package org.opengion.hayabusa.io; 017 018import javax.swing.tree.TreeModel; 019import javax.swing.tree.DefaultTreeModel; 020import javax.swing.tree.TreeNode; 021import javax.swing.tree.DefaultMutableTreeNode; 022import java.io.File; 023 024/** 025 * 内部に TreeModel を持ったファイル階層表現を表すクラスです。 026 * ルートディレクトリを指定して、それ以下のディレクトリ階層を構築します。 027 * このクラスは,すべてのデータを走査してから、TreeModel を構築しますので、 028 * パフォーマンス的には劣ります。 029 * 030 * @og.group その他出力 031 * 032 * @version 4.0 033 * @author Kazuhiko Hasegawa 034 * @since JDK5.0, 035 */ 036public class FileTreeModel { 037 TreeModel model = null; 038 String dir = null; 039 040 /** 041 * デフォルトコンストラクター 042 * 043 */ 044 public FileTreeModel() { 045 initialise( null ); 046 } 047 048 /** 049 * ルートディレクトリを指定して、TreeModel を構築するコンストラクター 050 * 051 * @param dir ルートディレクトリ文字列 052 */ 053 public FileTreeModel( final String dir ) { 054 initialise( dir ); 055 } 056 057 /** 058 * ルートディレクトリを指定して、TreeModel を構築します。 059 * 060 * @param dir ルートディレクトリ文字列 061 */ 062 public void setDirectory( final String dir ) { 063 initialise( dir ); 064 } 065 066 /** 067 * ルートディレクトリを指定して、TreeModel を構築します。 068 * 069 * @param dir ルートディレクトリ文字列 070 */ 071 private void initialise( final String dir ) { 072 this.dir = dir; 073 if( this.dir == null ) { this.dir = "."; } 074 TreeNode root = makeTree( new File(this.dir) ); 075 model = new DefaultTreeModel( root ); 076 } 077 078 /** 079 * TreeModel を取得します。 080 * コンストラクター または、setDirectory()メソッドによって構築された 081 * ディレクトリ階層を TreeModel にマッピングして返します。 082 * 083 * @return ルートディレクトリ文字列 084 */ 085 public TreeModel getTreeModel() { 086 return model; 087 } 088 089 /** 090 * 内部的に ディレクトリ階層を表現した TreeNode を返します。 091 * 092 * @param file ルートディレクトリのファイルオブジェクト 093 * 094 * @return ディレクトリ階層を表現したTreeNode 095 */ 096 private DefaultMutableTreeNode makeTree( final File file ) { 097 DefaultMutableTreeNode node = new DefaultMutableTreeNode( file.getName() ); 098 if(file.isDirectory()) { 099 String[] list = file.list(); 100 for( int i=0; i<list.length; i++ ) { 101 node.add( makeTree( new File(file, list[i]) ) ); 102 } 103 } 104 return node; 105 } 106 107 /** 108 * Tree の表示用メソッド 109 * 110 * これは、テスト用に使用するための Tree を標準出力に 出力するメソッドです。 111 * 112 * @param root トップレベルのTreeNodeオブジェクト(階層的に印字します。) 113 * @param model TreeNodeを含む TreeModelオブジェクト 114 * @param level 階層レベル。一番トップを 0 レベルとする。 115 */ 116 public void printTree( final TreeNode root,final TreeModel model,final int level ) { 117 int num = model.getChildCount( root ); 118 TreeNode[] nodes = new TreeNode[num]; 119 for( int i=0; i<num; i++ ) { 120 nodes[i] = (TreeNode)model.getChild( root,i ); 121 if( nodes[i].isLeaf() ) { 122 System.out.println( level + ":" + nodes[i].toString() ); 123 } 124 else { 125 System.out.println( level + ":" + nodes[i].toString() ); 126 printTree( nodes[i],model,level+1 ); 127 } 128 } 129 } 130 131 /** 132 * main メソッド 133 * 134 * これは、テスト用に使用するための main メソッドです。 135 * 136 * @param args 起動時の引数 args[0] にルートディレクトリ名 137 */ 138 public static void main( final String[] args ) { 139 FileTreeModel fmodel = new FileTreeModel( args[0] ); 140 TreeModel model = fmodel.getTreeModel(); 141 TreeNode root = (TreeNode)model.getRoot() ; 142 fmodel.printTree( root,model,0 ); 143 } 144}