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 */ 016 package org.opengion.hayabusa.io; 017 018 import javax.swing.tree.TreeModel; 019 import javax.swing.tree.DefaultTreeModel; 020 import javax.swing.tree.TreeNode; 021 import javax.swing.tree.DefaultMutableTreeNode; 022 import 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 */ 036 public 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 }