1 12 13 package org.openedit.links.webtree; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import javax.naming.LinkException ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.openedit.links.Link; 25 26 import com.openedit.util.PathUtilities; 27 import com.openedit.webui.tree.DefaultWebTreeNode; 28 29 30 35 public class LinkNode extends DefaultWebTreeNode implements Comparable  36 { 37 protected Set fieldIgnoreTypes; 39 protected Link fieldLink; 40 41 private static final Log log = LogFactory.getLog(LinkNode.class); 42 49 public LinkNode( Link inLink ) 50 { 51 super( inLink.getId(), inLink.getText() ); 52 fieldLink = inLink; 53 setLeaf(!getLink().hasChildren()); 54 } 55 56 63 public LinkNode getChild(String inName) 64 { 65 for (Iterator iter = getChildren().iterator(); iter.hasNext();) 66 { 67 LinkNode child = (LinkNode) iter.next(); 68 69 if ((child.getName() != null) && child.getName().equals(inName)) 70 { 71 return child; 72 } 73 } 74 75 return null; 76 } 77 78 81 public List getChildren() 82 { 83 if (fieldChildren == null) 84 { 85 fieldChildren = new ArrayList (); 86 reloadChildren(); 87 } 88 89 return fieldChildren; 90 } 91 92 95 public int compareTo(Object o) 96 { 97 if (o == null) 98 { 99 return 1; 100 } 101 102 if (o instanceof LinkNode) 103 { 104 LinkNode node = (LinkNode) o; 105 106 return getLink().getPath().compareTo(node.getLink().getPath()); 107 } 108 else 109 { 110 return 0; 111 } 112 } 113 114 121 public LinkNode findNode(String inPath) 122 { 123 if (!inPath.startsWith("/")) 125 { 126 inPath = "/" + inPath; 127 } 128 129 if (inPath.equals("") || inPath.equals("/")) 130 { 131 return this; 132 } 133 134 int beforeSlashIndex = 0; 135 136 if (inPath.startsWith("/")) 137 { 138 beforeSlashIndex = 1; 139 } 140 141 int nextSlashIndex = inPath.indexOf('/', beforeSlashIndex); 142 143 if (nextSlashIndex < 0) 144 { 145 nextSlashIndex = inPath.length(); 146 } 147 148 String childName = inPath.substring(beforeSlashIndex, nextSlashIndex); 149 150 LinkNode child = getChild(childName); 151 152 if (child == null) 153 { 154 return null; 155 } 156 else 157 { 158 return child.findNode(inPath.substring(nextSlashIndex)); 159 } 160 } 161 162 165 public void reloadChildren() 166 { 167 getChildren().clear(); 168 169 if (getLink().hasChildren()) 170 { 171 List childItems = getLink().getChildren(); 172 for ( Iterator iterator = childItems.iterator(); iterator.hasNext(); ) 173 { 174 Link childItem = (Link) iterator.next(); 175 176 String name = childItem.getPath(); 177 boolean okToAdd = true; 178 179 if( getIgnoreTypes() != null) 180 { 181 for (Iterator iter = getIgnoreTypes().iterator(); iter.hasNext();) 183 { 184 String key = (String ) iter.next(); 185 186 if (PathUtilities.match(name, key)) 187 { 188 okToAdd = false; 189 break; 190 } 191 } 192 } 193 if (okToAdd) 194 { 195 196 LinkNode child = createNode( childItem ); 197 child.setParent(this); 198 199 getChildren().add(child); 200 } 201 } 202 203 } 206 } 207 208 protected void setIgnoreTypes(Set inIgnoreTypes) 209 { 210 fieldIgnoreTypes = inIgnoreTypes; 211 } 212 213 protected Set getIgnoreTypes() 214 { 215 return fieldIgnoreTypes; 216 } 217 218 226 protected LinkNode createNode(Link childItem) 227 { 228 LinkNode node = new LinkNode( childItem); 229 node.setIgnoreTypes(getIgnoreTypes()); 230 return node; 231 } 232 233 public Link getLink() 234 { 235 return fieldLink; 236 } 237 public void setLink( Link Link ) 238 { 239 fieldLink = Link; 240 } 241 public String getURL() 242 { 243 return getLink().getId(); 244 } 245 246 } 247 | Popular Tags |