001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.swing.resourcetree;
020:
021: import java.util.*;
022: import java.util.Iterator;
023:
024: import javax.swing.Icon;
025: import javax.swing.JTree;
026: import javax.swing.tree.DefaultMutableTreeNode;
027: import javax.swing.tree.DefaultTreeModel;
028: import javax.swing.tree.TreePath;
029:
030: import org.openharmonise.vfs.*;
031: import org.openharmonise.vfs.event.*;
032:
033: /**
034: * A node in a resource tree.
035: *
036: * @author Matthew Large
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class TreeNode extends DefaultMutableTreeNode implements
041: VirtualFileListener {
042:
043: /**
044: * Full path to resource.
045: */
046: private String m_sFullPath = null;
047:
048: /**
049: * Virtual file system for resource.
050: */
051: private AbstractVirtualFileSystem m_vfs = null;
052:
053: /**
054: * Display name of resource.
055: */
056: private String m_sDisplayName = "";
057:
058: /**
059: * Icon of resource.
060: */
061: private Icon m_iDisplayIcon = null;
062:
063: /**
064: * Expanded icon for collection resources.
065: */
066: private Icon m_iDisplayIconExpanded = null;
067:
068: /**
069: * true if this is a leaf node.
070: */
071: private boolean m_bIsLeaf = false;
072:
073: /**
074: * true if child nodes have been populated.
075: */
076: private boolean m_bChildrenPopulated = false;
077:
078: private AbstractResourceFilter m_resourceFilter = null;
079:
080: /**
081: * Tree
082: */
083: private JTree m_tree = null;
084:
085: /**
086: * Constructs a new tree node.
087: *
088: * @param vfs Virtual file system for resource
089: * @param sFullPath Full path for resource
090: * @param tree Tree
091: */
092: public TreeNode(AbstractVirtualFileSystem vfs, String sFullPath,
093: JTree tree, AbstractResourceFilter resourceFilter) {
094: super (sFullPath);
095: m_sFullPath = sFullPath;
096: m_vfs = vfs;
097: this .m_tree = tree;
098: VirtualFileSystemView vfsView = vfs.getVirtualFileSystemView();
099: VirtualFile vfFile = vfs.getVirtualFile(sFullPath)
100: .getResource();
101:
102: vfFile.addVirtualFileListener(this );
103:
104: this .m_sDisplayName = vfsView.getDisplayName(vfFile);
105: this .m_iDisplayIcon = vfsView.getIcon(vfFile);
106: this .m_iDisplayIconExpanded = vfsView.getIcon(vfFile, true);
107: this .m_bIsLeaf = !vfFile.isDirectory();
108:
109: this .m_resourceFilter = resourceFilter;
110: }
111:
112: /**
113: * Returns the icon for the resource.
114: *
115: * @param bExpanded true if the expanded icon is to be returned for collection resoruces
116: * @return Icon
117: */
118: public Icon getDisplayIcon(boolean bExpanded) {
119: if (bExpanded) {
120: return this .m_iDisplayIconExpanded;
121: } else {
122: return this .m_iDisplayIcon;
123: }
124: }
125:
126: /**
127: * Returns the display name for the resource.
128: *
129: * @return Display name
130: */
131: public String getDisplayName() {
132: return this .m_sDisplayName;
133: }
134:
135: /* (non-Javadoc)
136: * @see javax.swing.tree.TreeNode#isLeaf()
137: */
138: public boolean isLeaf() {
139: return this .m_bIsLeaf;
140: }
141:
142: /**
143: * Checks if leaf node children will be displayed.
144: *
145: * @return true if leaf node children will be displayed
146: */
147: public boolean isShowChildren() {
148: return !this .m_resourceFilter.isShowCollectionsOnly();
149: }
150:
151: /**
152: * Checks if only published children will be displayed.
153: *
154: * @return true if only published children will be displayed
155: */
156: public boolean isShowApprovedOnly() {
157: return this .m_resourceFilter.isShowLiveResourcesOnly();
158: }
159:
160: /**
161: * Populates this nodes child nodes.
162: *
163: * @param bShowLeafNodes true if leaf node children are to be displayed
164: * @param bShowApprovedOnly true if only published children are to be displayed
165: */
166: protected void populateChildren() {
167: if (!this .m_bIsLeaf && !m_bChildrenPopulated) {
168: Iterator itor = this .m_vfs.getVirtualFile(this .m_sFullPath)
169: .getResource().getChildren().iterator();
170: int nCount = 0;
171: while (itor.hasNext()) {
172: String sPath = (String) itor.next();
173: VirtualFile vfChild = this .m_vfs.getVirtualFile(sPath)
174: .getResource();
175: if (sPath != null
176: && (vfChild.isDirectory() || !this .m_resourceFilter
177: .isShowCollectionsOnly())
178: && (!this .m_resourceFilter
179: .isShowLiveResourcesOnly() || vfChild
180: .getState() == VirtualFile.STATE_LIVE)
181: && this .m_resourceFilter.checkResource(vfChild)) {
182: this .add(new TreeNode(this .m_vfs, sPath,
183: this .m_tree, this .m_resourceFilter));
184: nCount++;
185: }
186: }
187: m_bChildrenPopulated = true;
188: }
189: }
190:
191: public boolean isChildrenPopulated() {
192: return this .m_bChildrenPopulated;
193: }
194:
195: /**
196: * Refreshes the children of this node.
197: *
198: */
199: private void refreshChildren() {
200: TreePath treePath = this .m_tree.getSelectionPath();
201: if (treePath != null) {
202: Object treeComp = treePath.getLastPathComponent();
203: }
204: this .m_bChildrenPopulated = false;
205: this .removeAllChildren();
206: this .populateChildren();
207: DefaultTreeModel model = (DefaultTreeModel) this .m_tree
208: .getModel();
209: model.reload(this );
210: VirtualFile vfFile = this .m_vfs
211: .getVirtualFile(this .m_sFullPath).getResource();
212: vfFile.addVirtualFileListener(this );
213: this .m_tree.revalidate();
214: if (treePath != null) {
215: this .m_tree.expandPath(treePath);
216: }
217: }
218:
219: /**
220: * Returns the full path of the resouce.
221: *
222: * @return Full path
223: */
224: public String getFilePath() {
225: return this .m_sFullPath;
226: }
227:
228: /**
229: * Returns the virtual file system for the resource.
230: *
231: * @return Virtual file system
232: */
233: public AbstractVirtualFileSystem getVFS() {
234: return this .m_vfs;
235: }
236:
237: /**
238: * @param userObject
239: */
240: private TreeNode(Object userObject) {
241: super (userObject);
242: }
243:
244: /**
245: * @param userObject
246: * @param allowsChildren
247: */
248: private TreeNode(Object userObject, boolean allowsChildren) {
249: super (userObject, allowsChildren);
250: }
251:
252: /* (non-Javadoc)
253: * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
254: */
255: public void virtualFileChanged(VirtualFileEvent vfe) {
256: if (vfe.getEventType() == VirtualFileEvent.FILE_MEMBERS_CHANGED) {
257: this .refreshChildren();
258: }
259: }
260:
261: /* (non-Javadoc)
262: * @see javax.swing.tree.TreeNode#children()
263: */
264: public Enumeration children() {
265: populateChildren();
266: return super.children();
267: }
268:
269: }
|