KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > swing > resourcetree > TreeNode


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (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 http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.swing.resourcetree;
20
21 import java.util.*;
22 import java.util.Iterator JavaDoc;
23
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.JTree JavaDoc;
26 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
27 import javax.swing.tree.DefaultTreeModel JavaDoc;
28 import javax.swing.tree.TreePath JavaDoc;
29
30 import org.openharmonise.vfs.*;
31 import org.openharmonise.vfs.event.*;
32
33
34 /**
35  * A node in a resource tree.
36  *
37  * @author Matthew Large
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class TreeNode extends DefaultMutableTreeNode JavaDoc implements VirtualFileListener {
42
43     /**
44      * Full path to resource.
45      */

46     private String JavaDoc m_sFullPath = null;
47     
48     /**
49      * Virtual file system for resource.
50      */

51     private AbstractVirtualFileSystem m_vfs = null;
52     
53     /**
54      * Display name of resource.
55      */

56     private String JavaDoc m_sDisplayName = "";
57     
58     /**
59      * Icon of resource.
60      */

61     private Icon JavaDoc m_iDisplayIcon = null;
62     
63     /**
64      * Expanded icon for collection resources.
65      */

66     private Icon JavaDoc m_iDisplayIconExpanded = null;
67     
68     /**
69      * true if this is a leaf node.
70      */

71     private boolean m_bIsLeaf=false;
72     
73     /**
74      * true if child nodes have been populated.
75      */

76     private boolean m_bChildrenPopulated = false;
77     
78     private AbstractResourceFilter m_resourceFilter = null;
79     
80     /**
81      * Tree
82      */

83     private JTree JavaDoc m_tree = null;
84
85     /**
86      * Constructs a new tree node.
87      *
88      * @param vfs Virtual file system for resource
89      * @param sFullPath Full path for resource
90      * @param tree Tree
91      */

92     public TreeNode(AbstractVirtualFileSystem vfs, String JavaDoc sFullPath, JTree JavaDoc tree, AbstractResourceFilter resourceFilter) {
93         super(sFullPath);
94         m_sFullPath = sFullPath;
95         m_vfs = vfs;
96         this.m_tree = tree;
97         VirtualFileSystemView vfsView = vfs.getVirtualFileSystemView();
98         VirtualFile vfFile = vfs.getVirtualFile(sFullPath).getResource();
99         
100         vfFile.addVirtualFileListener(this);
101         
102         this.m_sDisplayName = vfsView.getDisplayName(vfFile);
103         this.m_iDisplayIcon = vfsView.getIcon(vfFile);
104         this.m_iDisplayIconExpanded = vfsView.getIcon(vfFile, true);
105         this.m_bIsLeaf = !vfFile.isDirectory();
106         
107         this.m_resourceFilter = resourceFilter;
108     }
109
110     /**
111      * Returns the icon for the resource.
112      *
113      * @param bExpanded true if the expanded icon is to be returned for collection resoruces
114      * @return Icon
115      */

116     public Icon JavaDoc getDisplayIcon(boolean bExpanded) {
117         if(bExpanded) {
118             return this.m_iDisplayIconExpanded;
119         } else {
120             return this.m_iDisplayIcon;
121         }
122     }
123     
124     /**
125      * Returns the display name for the resource.
126      *
127      * @return Display name
128      */

129     public String JavaDoc getDisplayName() {
130         return this.m_sDisplayName;
131     }
132     
133     /* (non-Javadoc)
134      * @see javax.swing.tree.TreeNode#isLeaf()
135      */

136     public boolean isLeaf() {
137         return this.m_bIsLeaf;
138     }
139     
140     /**
141      * Checks if leaf node children will be displayed.
142      *
143      * @return true if leaf node children will be displayed
144      */

145     public boolean isShowChildren() {
146         return !this.m_resourceFilter.isShowCollectionsOnly();
147     }
148     
149     /**
150      * Checks if only published children will be displayed.
151      *
152      * @return true if only published children will be displayed
153      */

154     public boolean isShowApprovedOnly() {
155         return this.m_resourceFilter.isShowLiveResourcesOnly();
156     }
157     
158     /**
159      * Populates this nodes child nodes.
160      *
161      * @param bShowLeafNodes true if leaf node children are to be displayed
162      * @param bShowApprovedOnly true if only published children are to be displayed
163      */

164     protected void populateChildren() {
165         if(!this.m_bIsLeaf && !m_bChildrenPopulated) {
166             Iterator JavaDoc itor = this.m_vfs.getVirtualFile(this.m_sFullPath).getResource().getChildren().iterator();
167             int nCount = 0;
168             while(itor.hasNext()) {
169                 String JavaDoc sPath = (String JavaDoc)itor.next();
170                 VirtualFile vfChild =this.m_vfs.getVirtualFile(sPath).getResource();
171                 if(sPath!=null && (vfChild.isDirectory() || !this.m_resourceFilter.isShowCollectionsOnly()) && (!this.m_resourceFilter.isShowLiveResourcesOnly() || vfChild.getState()==VirtualFile.STATE_LIVE) && this.m_resourceFilter.checkResource(vfChild)) {
172                     this.add( new TreeNode(this.m_vfs, sPath, this.m_tree, this.m_resourceFilter) );
173                     nCount++;
174                 }
175             }
176             m_bChildrenPopulated = true;
177         }
178     }
179     
180     public boolean isChildrenPopulated() {
181         return this.m_bChildrenPopulated;
182     }
183     
184     /**
185      * Refreshes the children of this node.
186      *
187      */

188     private void refreshChildren() {
189         TreePath JavaDoc treePath = this.m_tree.getSelectionPath();
190         if(treePath!=null) {
191             Object JavaDoc treeComp = treePath.getLastPathComponent();
192         }
193         this.m_bChildrenPopulated=false;
194         this.removeAllChildren();
195         this.populateChildren();
196         DefaultTreeModel JavaDoc model = (DefaultTreeModel JavaDoc) this.m_tree.getModel();
197         model.reload(this);
198         VirtualFile vfFile = this.m_vfs.getVirtualFile(this.m_sFullPath).getResource();
199         vfFile.addVirtualFileListener(this);
200         this.m_tree.revalidate();
201         if(treePath!=null) {
202             this.m_tree.expandPath(treePath);
203         }
204     }
205     
206     /**
207      * Returns the full path of the resouce.
208      *
209      * @return Full path
210      */

211     public String JavaDoc getFilePath() {
212         return this.m_sFullPath;
213     }
214     
215     /**
216      * Returns the virtual file system for the resource.
217      *
218      * @return Virtual file system
219      */

220     public AbstractVirtualFileSystem getVFS() {
221         return this.m_vfs;
222     }
223
224     /**
225      * @param userObject
226      */

227     private TreeNode(Object JavaDoc userObject) {
228         super(userObject);
229     }
230
231     /**
232      * @param userObject
233      * @param allowsChildren
234      */

235     private TreeNode(Object JavaDoc userObject, boolean allowsChildren) {
236         super(userObject, allowsChildren);
237     }
238
239     /* (non-Javadoc)
240      * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
241      */

242     public void virtualFileChanged(VirtualFileEvent vfe) {
243         if(vfe.getEventType()==VirtualFileEvent.FILE_MEMBERS_CHANGED) {
244             this.refreshChildren();
245         }
246     }
247
248     /* (non-Javadoc)
249      * @see javax.swing.tree.TreeNode#children()
250      */

251     public Enumeration children() {
252         populateChildren();
253         return super.children();
254     }
255
256 }
257
Popular Tags