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    }