1 4 package org.openedit.links; 5 6 import java.io.Serializable ; 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 12 import com.openedit.page.Page; 13 import com.openedit.util.PathUtilities; 14 15 20 public class LinkTree implements Serializable 21 { 22 private static final long serialVersionUID = 766014286370105378L; 23 protected Link fieldRootLink; 24 protected Link fieldSelectedLink; 25 protected long fieldNextId; 26 protected long fieldLastModified; 27 protected Page fieldPage; 28 protected String fieldId; 29 protected List fieldCrumbs; 30 31 35 public Link getLink(String inId) 36 { 37 if (getRootLink().getId().equals(inId)) 38 { 39 return getRootLink(); 40 } 41 42 return getRootLink().getDecendant(inId); 43 } 44 public List getLinkChildren(String inId) 45 { 46 Link link = getLink(inId); 47 if ( link != null) 48 { 49 return link.getChildren(); 50 } 51 return null; 52 } 53 56 public List renderAsList() 57 { 58 if ( getRootLink() != null) 59 { 60 return getRootLink().list(); 61 } 62 return null; 63 } 64 65 public Link getRootLink() 66 { 67 return fieldRootLink; 68 } 69 public void setRootLink(Link inRootLink) 70 { 71 fieldRootLink = inRootLink; 72 } 73 public Link getSelectedLink() 74 { 75 return findSelectedLink(getRootLink()); 76 } 77 public List findSelectedParents(int inParentLevel) 78 { 79 List parents = new ArrayList (); 81 Link parent = getSelectedLink(); while ( parent != null ) 83 { 84 parents.add(0,parent); 85 parent = parent.getParentLink(); 86 } 87 parents.addAll(getCrumbs()); if ( parents.size() > inParentLevel) 89 { 90 return parents.subList(inParentLevel, parents.size()); 91 } 92 return null; 93 } 94 public Link findSelectedParentLink(int inParentLevel) 95 { 96 List parents = new ArrayList (); 98 Link parent = getSelectedLink(); 99 while ( parent != null ) 100 { 101 parents.add(0,parent); 102 parent = parent.getParentLink(); 103 } 104 if ( parents.size() > inParentLevel) 105 { 106 Link selected = (Link)parents.get(inParentLevel); 107 return selected; 108 } 109 return null; 110 } 111 112 116 private Link findSelectedLink(Link inRootLink) 117 { 118 if ( inRootLink == null) 119 { 120 return getRootLink(); 121 } 122 if ( inRootLink.isSelected()) 123 { 124 return inRootLink; 125 } 126 for (Iterator iter = inRootLink.getChildren().iterator(); iter.hasNext();) 127 { 128 Link element = (Link) iter.next(); 129 Link link = findSelectedLink(element); 130 if ( link != null) 131 { 132 return link; 133 } 134 } 135 return null; 136 } 137 140 public void removeLink(Link inLink) 141 { 142 Link parent = inLink.getParentLink(); 143 if ( parent != null) 144 { 145 parent.removeChild( inLink ); 146 } 147 else 148 { 149 setRootLink(null); 150 } 151 if( inLink.hasChildren()) 152 { 153 for (Iterator iter = new ArrayList (inLink.getChildren()).iterator(); iter.hasNext();) 154 { 155 Link link = (Link) iter.next(); 156 removeLink(link); 157 } 158 } 159 } 160 163 public void moveUp(Link inLink) 164 { 165 Link parent = inLink.getParentLink(); 166 if ( parent != null) 167 { 168 parent.moveUp(inLink); 169 } 170 } 171 174 public void moveDown(Link inLink) 175 { 176 Link parent = inLink.getParentLink(); 177 if ( parent != null) 178 { 179 parent.moveDown(inLink); 180 } 181 } 182 185 public void moveRight(Link inLink) 186 { 187 Link parent = inLink.getParentLink(); 189 if( parent != null) 190 { 191 Link brother = parent.getChildAbove(inLink); 192 if ( brother != null) 193 { 194 parent.removeChild(inLink); 195 brother.addChild(inLink); 196 } 197 } 198 } 199 202 public void moveLeft(Link inLink) 203 { 204 Link parent1 = inLink.getParentLink(); 206 if ( parent1 != null) 207 { 208 Link parent2 = parent1.getParentLink(); 209 if ( parent2 != null) 210 { 211 212 parent1.removeChild(inLink); 213 parent2.addChildNearLocation(inLink, parent1); 214 } 215 } 216 } 217 220 public String nextId() 221 { 222 if( fieldNextId == 0) 223 { 224 fieldNextId = System.currentTimeMillis(); 225 } 226 return String.valueOf(fieldNextId++); 227 } 228 229 public void changeLinkId(Link inLink, String inNewId) 230 { 231 inLink.setId(inNewId); 232 } 233 237 public Link addLink(String inParentId, Link inLink) 238 { 239 if( getRootLink() == null ) 240 { 241 setRootLink( inLink ); 242 return inLink; 243 } 244 245 Link oldLink = getLink(inLink.getId()); 246 if ( oldLink != null && oldLink.getParentLink() != null) 247 { 248 oldLink.getParentLink().removeChild(oldLink); 249 } 250 251 Link parentLink = getLink(inParentId); 253 if ( parentLink == null ) 254 { 255 parentLink = getRootLink(); 256 } 257 parentLink.addChild(inLink); 258 return inLink; 259 } 260 264 public Link findSelectedLinkByUrl(String inPath) 265 { 266 if ( getRootLink() == null) 267 { 268 return null; 269 } 270 else if ( getRootLink().getUrl() != null && getRootLink().getUrl().equals( inPath ) ) 271 { 272 return getRootLink(); 273 } 274 Link selected = getSelectedLink(); 275 if ( selected != null) 276 { 277 while( selected != null) 279 { 280 Link found = findLinkByUrl( inPath, selected ); 283 if ( found != null) 284 { 285 return found; 286 } 287 selected = selected.getParentLink(); 288 } 289 } 290 else 291 { 292 return findLinkByUrl( inPath, getRootLink() ); 293 } 294 return null; 295 } 296 297 public Link findLinkByUrl( String inPath, Link inLink ) 298 { 299 if ( inPath == null) 300 { 301 return null; 302 } 303 if ( inLink == null) 304 { 305 return null; 306 } 307 if ( inLink.getUrl() != null ) 308 { 309 if (PathUtilities.match(inPath, inLink.getUrl())) 310 { 311 return inLink; 312 } 313 } 314 if ( inLink.hasChildren() ) 315 { 316 for (Iterator iter = inLink.getChildren().iterator(); iter.hasNext();) 317 { 318 Link link = (Link) iter.next(); 319 Link found = findLinkByUrl( inPath, link); 320 if ( found != null ) 321 { 322 return found; 323 } 324 } 325 } 326 return null; 327 } 328 329 public String checkUnique( String inOriginal ) 330 { 331 return checkUnique( inOriginal, inOriginal, 0); 332 } 333 338 private String checkUnique( String inOriginal, String inId, int count) 339 { 340 Link id = getLink(inId); 341 if ( id != null) 342 { 343 count ++; 344 return checkUnique(inOriginal,inOriginal + count,count); 345 } 346 if ( count > 0) 347 { 348 return inOriginal + count; 349 } 350 return inOriginal; 351 } 352 355 public void setSelectedLink(String inSelectedLink) 356 { 357 Link link = getLink(inSelectedLink); 358 setSelectedLink( link ); 359 } 360 public void setSelectedLink(Link link) 361 { 362 if ( link == null) 363 { 364 return; 365 } 366 else 367 { 368 clearCrumbs(); clearSelection( getRootLink() ); 370 } 371 if ( link.hasChildren()) 373 { 374 for (Iterator iter = link.getChildren().iterator(); iter.hasNext();) 375 { 376 Link child = (Link) iter.next(); 377 Link hit = findLinkByUrl(link.getUrl(),child); 378 if ( hit != null ) 379 { 380 hit.setSelected(true); 381 } 382 } 383 } 384 link.setSelected(true); 385 } 386 protected void clearSelection(Link inRootLink) 387 { 388 inRootLink.setSelected(false); 389 if ( inRootLink.hasChildren() ) 390 { 391 for (Iterator iter = inRootLink.getChildren().iterator(); iter.hasNext();) 392 { 393 Link link = (Link) iter.next(); 394 clearSelection(link); 395 } 396 } 397 } 398 403 public Link insertLink(String inParentId, Link inLink) 404 { 405 Link oldLink = getLink(inLink.getId()); 407 if ( oldLink != null && oldLink.getParentLink() != null) 408 { 409 oldLink.getParentLink().removeChild(oldLink); 410 } 411 412 if( getRootLink() == null ) 413 { 414 setRootLink( inLink ); 415 return inLink; 416 } 417 418 Link parentLink = getLink(inParentId); 420 if ( parentLink == null ) 421 { 422 parentLink = getRootLink(); 423 } 424 parentLink.insertChild(inLink); 425 return inLink; 426 427 } 428 431 public long getLastModified() 432 { 433 return fieldLastModified; 434 } 435 public void setLastModified(long inLastModified) 436 { 437 fieldLastModified = inLastModified; 438 } 439 public String findRedirect(String inPath) 440 { 441 442 Link link = findLinkByUrl(inPath, getRootLink() ); 443 if ( link != null) 444 { 445 String redirectPath = link.getRedirectPath(); 446 if (redirectPath != null) 447 { 448 int indestpath = redirectPath.indexOf("*"); int inpath = link.getUrl().indexOf("*"); if (indestpath > -1 && inpath > -1 ) 452 { 453 String begin = redirectPath.substring(0,indestpath); 457 String ending = inPath.substring(inpath, inPath.length()); 458 459 redirectPath = begin + ending; 460 } 461 return redirectPath; 462 } 463 } 464 return null; 465 } 466 467 public String getId() 468 { 469 return fieldId; 470 } 471 public void setId(String inId) 472 { 473 fieldId = inId; 474 } 475 public boolean isDraft() { 476 return getPage().isDraft(); 477 } 478 public void clearCrumbs() 479 { 480 getCrumbs().clear(); 482 } 483 public List getCrumbs() 484 { 485 if (fieldCrumbs == null) 486 { 487 fieldCrumbs = new ArrayList (); 488 } 489 return fieldCrumbs; 490 } 491 public void setCrumbs(List inCrumbs) 492 { 493 fieldCrumbs = inCrumbs; 494 } 495 public void addCrumb(String inPath, String inText) 496 { 497 Link link = new Link(); 498 link.setPath(inPath); 499 link.setText(inText); 500 link.setId(inText); 501 link.setSelected(true); 502 getCrumbs().add(link); 503 } 504 public Page getPage() 505 { 506 return fieldPage; 507 } 508 public void setPage(Page inPage) 509 { 510 fieldPage = inPage; 511 } 512 public String getPath() 513 { 514 return getPage().getPath(); 515 } 516 } 517 | Popular Tags |