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.ArrayList;
023:
024: import javax.swing.tree.*;
025: import javax.swing.tree.DefaultTreeModel;
026: import javax.swing.tree.TreeNode;
027:
028: /**
029: * Data model for resource trees.
030: *
031: * @author Matthew Large
032: * @version $Revision: 1.1 $
033: *
034: */
035: public class TreeModel extends DefaultTreeModel {
036:
037: /**
038: * Constructs a new tree model.
039: *
040: * @param root Root node
041: */
042: public TreeModel(TreeNode root) {
043: super (root);
044: }
045:
046: /**
047: * Constructs a new tree model.
048: *
049: * @param root Root node
050: * @param asksAllowsChildren false if any node can have children, true if each node is asked to see if it can have children
051: */
052: public TreeModel(TreeNode root, boolean asksAllowsChildren) {
053: super (root, asksAllowsChildren);
054: }
055:
056: /* (non-Javadoc)
057: * @see javax.swing.tree.DefaultTreeModel#fireTreeNodesChanged(java.lang.Object, java.lang.Object[], int[], java.lang.Object[])
058: */
059: protected void fireTreeNodesChanged(Object arg0, Object[] arg1,
060: int[] arg2, Object[] arg3) {
061: super .fireTreeNodesChanged(arg0, arg1, arg2, arg3);
062: }
063:
064: /* (non-Javadoc)
065: * @see javax.swing.tree.DefaultTreeModel#fireTreeNodesInserted(java.lang.Object, java.lang.Object[], int[], java.lang.Object[])
066: */
067: protected void fireTreeNodesInserted(Object arg0, Object[] arg1,
068: int[] arg2, Object[] arg3) {
069: super .fireTreeNodesInserted(arg0, arg1, arg2, arg3);
070: }
071:
072: /* (non-Javadoc)
073: * @see javax.swing.tree.DefaultTreeModel#fireTreeNodesRemoved(java.lang.Object, java.lang.Object[], int[], java.lang.Object[])
074: */
075: protected void fireTreeNodesRemoved(Object arg0, Object[] arg1,
076: int[] arg2, Object[] arg3) {
077: super .fireTreeNodesRemoved(arg0, arg1, arg2, arg3);
078: }
079:
080: /* (non-Javadoc)
081: * @see javax.swing.tree.DefaultTreeModel#fireTreeStructureChanged(java.lang.Object, java.lang.Object[], int[], java.lang.Object[])
082: */
083: public void fireTreeStructureChanged(Object arg0, Object[] arg1,
084: int[] arg2, Object[] arg3) {
085: super .fireTreeStructureChanged(arg0, arg1, arg2, arg3);
086: }
087:
088: /**
089: * Returns the tree path for a given full path.
090: *
091: * @param sPath Full path
092: * @return Tree path
093: */
094: public TreePath getTreePath(String sPath) {
095: TreePath tPath = null;
096: ArrayList nodes = new ArrayList();
097:
098: DefaultMutableTreeNode root = (DefaultMutableTreeNode) this
099: .getRoot();
100:
101: nodes.add(root);
102:
103: DefaultMutableTreeNode currNode = null;
104:
105: Enumeration children = root.children();
106:
107: while (children != null && children.hasMoreElements()) {
108: org.openharmonise.him.swing.resourcetree.TreeNode child = (org.openharmonise.him.swing.resourcetree.TreeNode) children
109: .nextElement();
110:
111: String sTempPath = child.getFilePath();
112:
113: if (sPath.startsWith(sTempPath)) {
114: nodes.add(child);
115:
116: if (child.isLeaf() == false) {
117: children = child.children();
118: } else {
119: children = null;
120: }
121:
122: }
123: }
124:
125: DefaultMutableTreeNode[] pathArray = (DefaultMutableTreeNode[]) nodes
126: .toArray(new DefaultMutableTreeNode[nodes.size()]);
127:
128: tPath = new TreePath(pathArray);
129:
130: return tPath;
131: }
132:
133: }
|