KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > workbench > tree > examples > fsmodel > FolderObject


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.workbench.tree.examples.fsmodel;
16
17 import java.io.File JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import org.apache.tapestry.contrib.tree.model.ITreeNode;
23
24 public class FolderObject extends SFObject{
25
26     /**
27      * @associates <{File}>
28      * @supplierCardinality 0..*
29      * @link aggregation
30      */

31     private Vector JavaDoc m_vFiles = null;
32
33     /**
34      * @associates <{FolderObject}>
35      * @supplierCardinality 0..*
36      * @link aggregation
37      */

38     private Vector JavaDoc m_vFolders = null;
39     private boolean m_bShared;
40
41     public FolderObject(ITreeNode objParent, File JavaDoc objFile, boolean bInvokeInit) {
42         super(objParent, objFile);
43         if(bInvokeInit)
44             init();
45     }
46
47     public void reload() {
48         m_vFolders = new Vector JavaDoc();
49         m_vFiles = new Vector JavaDoc();
50
51         File JavaDoc[] arrFiles = getFile().listFiles();
52
53         if (arrFiles == null) {
54             return;
55         }
56
57         for (int i=0; i<arrFiles.length; i++) {
58             if (arrFiles[i].isDirectory()) {
59                 m_vFolders.addElement(new FolderObject(this, arrFiles[i], true));
60             } else {
61                 m_vFiles.addElement(new FileObject(this, arrFiles[i]));
62             }
63         }
64     }
65
66     public boolean isShared() {
67         return m_bShared;
68     }
69
70     public Vector JavaDoc getFolders() {
71         if (m_vFolders == null) {
72             reload();
73         }
74         return m_vFolders;
75     }
76
77     public Vector JavaDoc getFiles() {
78         if (m_vFiles == null) {
79             reload();
80         }
81         return m_vFiles;
82     }
83
84     public int getChildNumber(Object JavaDoc objChild) {
85         for(int i = 0; i < m_vFolders.size(); i++) {
86             Object JavaDoc objChildFolder = m_vFolders.elementAt(i);
87             if (objChildFolder.equals(objChild)) {
88                 return i;
89             }
90         }
91         return -1;
92     }
93
94     /**
95      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#containsChild(ITreeNode)
96      */

97     public boolean containsChild(ITreeNode node) {
98         return true;
99     }
100
101     /**
102      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#getAllowsChildren()
103      */

104     public boolean getAllowsChildren() {
105         return true;
106     }
107
108     /**
109      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#getChildCount()
110      */

111     public int getChildCount() {
112         return getFolders().size() + getFiles().size();
113     }
114
115     /**
116      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#getChildren()
117      */

118     public Collection JavaDoc getChildren() {
119         ArrayList JavaDoc arrChildrens = new ArrayList JavaDoc();
120         arrChildrens.addAll(getFolders());
121         arrChildrens.addAll(getFiles());
122         return arrChildrens;
123     }
124
125     /**
126      * @see org.apache.tapestry.contrib.tree.model.ITreeNode#isLeaf()
127      */

128     public boolean isLeaf() {
129         return false;
130     }
131
132     private final static String JavaDoc openImage =
133         "/org/apache/tapestry/workbench/tree/examples/fsmodel/TreeOpen.gif";
134
135     private final static String JavaDoc closedImage =
136         "/org/apache/tapestry/workbench/tree/examples/fsmodel/TreeClosed.gif";
137
138     public AssetsHolder getAssets() {
139         if (m_objAssetsHolder == null) {
140             m_objAssetsHolder = new AssetsHolder(openImage, closedImage);
141         }
142         return m_objAssetsHolder;
143     }
144 }
145
Popular Tags