KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > links > LinkTree


1 /*
2  * Created on Dec 22, 2004
3  */

4 package org.openedit.links;
5
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11
12 import com.openedit.page.Page;
13 import com.openedit.util.PathUtilities;
14
15 /**
16  * This keeps track of a list of Link objects: ie. <a HREF="sdsdfdf">sddsfsd</a>
17  * @author cburkey
18  *
19  */

20 public class LinkTree implements Serializable JavaDoc
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 JavaDoc fieldId;
29     protected List JavaDoc fieldCrumbs;
30     
31     /**
32      * @param inString
33      * @return
34      */

35     public Link getLink(String JavaDoc inId)
36     {
37         if (getRootLink().getId().equals(inId))
38         {
39             return getRootLink();
40         }
41
42         return getRootLink().getDecendant(inId);
43     }
44     public List JavaDoc getLinkChildren(String JavaDoc inId)
45     {
46         Link link = getLink(inId);
47         if ( link != null)
48         {
49             return link.getChildren();
50         }
51         return null;
52     }
53     /**
54      * @return
55      */

56     public List JavaDoc 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 JavaDoc findSelectedParents(int inParentLevel)
78     {
79         //get the selected link
80
List JavaDoc parents = new ArrayList JavaDoc();
81         Link parent = getSelectedLink(); //Most specific on bottom
82
while ( parent != null )
83         {
84             parents.add(0,parent);
85             parent = parent.getParentLink();
86         }
87         parents.addAll(getCrumbs()); //Crumbs become more specific
88
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         //get the selected link
97
List JavaDoc parents = new ArrayList JavaDoc();
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     /**
113      * @param inRootLink
114      * @return
115      */

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 JavaDoc 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     /**
138      * @param inLink
139      */

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 JavaDoc iter = new ArrayList JavaDoc(inLink.getChildren()).iterator(); iter.hasNext();)
154             {
155                 Link link = (Link) iter.next();
156                 removeLink(link);
157             }
158         }
159     }
160     /**
161      * @param inLink
162      */

163     public void moveUp(Link inLink)
164     {
165         Link parent = inLink.getParentLink();
166         if ( parent != null)
167         {
168             parent.moveUp(inLink);
169         }
170     }
171     /**
172      * @param inLink
173      */

174     public void moveDown(Link inLink)
175     {
176         Link parent = inLink.getParentLink();
177         if ( parent != null)
178         {
179             parent.moveDown(inLink);
180         }
181     }
182     /**
183      * @param inLink
184      */

185     public void moveRight(Link inLink)
186     {
187         //put it as a child of my upper brother
188
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     /**
200      * @param inLink
201      */

202     public void moveLeft(Link inLink)
203     {
204         //pull it up one
205
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     /**
218      * @return
219      */

220     public String JavaDoc 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 JavaDoc inNewId)
230     {
231         inLink.setId(inNewId);
232     }
233     /**
234      * @param inParentId
235      * @param inLink
236      */

237     public Link addLink(String JavaDoc 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         //find the parent node if none then use the root node
252
Link parentLink = getLink(inParentId);
253         if ( parentLink == null )
254         {
255             parentLink = getRootLink();
256         }
257         parentLink.addChild(inLink);
258         return inLink;
259     }
260     /**
261      * @param inPath
262      * @return
263      */

264     public Link findSelectedLinkByUrl(String JavaDoc 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             //look in all the parents
278
while( selected != null)
279             {
280                 //loop up the tree and check each parent link.
281
//This keeps us near the place we where before
282
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 JavaDoc 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 JavaDoc 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 JavaDoc checkUnique( String JavaDoc inOriginal )
330     {
331         return checkUnique( inOriginal, inOriginal, 0);
332     }
333     /**
334      * @param inLinks
335      * @param inId
336      * @return
337      */

338     private String JavaDoc checkUnique( String JavaDoc inOriginal, String JavaDoc 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     /**
353      * @param inSelectedLink
354      */

355     public void setSelectedLink(String JavaDoc 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(); //We have hit a known place that can use normall crumbs
369
clearSelection( getRootLink() );
370         }
371         //Look for any children that may also have the same url. i.e. about.html could be in the about category
372
if ( link.hasChildren())
373         {
374             for (Iterator JavaDoc 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 JavaDoc iter = inRootLink.getChildren().iterator(); iter.hasNext();)
392             {
393                 Link link = (Link) iter.next();
394                 clearSelection(link);
395             }
396         }
397     }
398     /**
399      * @param inString
400      * @param inLink
401      * @param inI
402      */

403     public Link insertLink(String JavaDoc inParentId, Link inLink)
404     {
405         //duplicate code above
406
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         //find the parent node if none then use the root node
419
Link parentLink = getLink(inParentId);
420         if ( parentLink == null )
421         {
422             parentLink = getRootLink();
423         }
424         parentLink.insertChild(inLink);
425         return inLink;
426
427     }
428     /**
429      * @return
430      */

431     public long getLastModified()
432     {
433         return fieldLastModified;
434     }
435     public void setLastModified(long inLastModified)
436     {
437         fieldLastModified = inLastModified;
438     }
439     public String JavaDoc findRedirect(String JavaDoc inPath)
440     {
441         
442         Link link = findLinkByUrl(inPath, getRootLink() );
443         if ( link != null)
444         {
445             String JavaDoc redirectPath = link.getRedirectPath();
446             if (redirectPath != null)
447             {
448                 //path /abc/345.html -> http://xyz/abc/345.html
449
int indestpath = redirectPath.indexOf("*"); //http://xyz/*
450
int inpath = link.getUrl().indexOf("*"); // /abc/*
451
if (indestpath > -1 && inpath > -1 )
452                 {
453                     //this is a dynamic redirect path
454
//http://xyz/
455
//take off a part of the path before the *?
456
String JavaDoc begin = redirectPath.substring(0,indestpath);
457                     String JavaDoc 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 JavaDoc getId()
468     {
469         return fieldId;
470     }
471     public void setId(String JavaDoc inId)
472     {
473         fieldId = inId;
474     }
475     public boolean isDraft() {
476         return getPage().isDraft();
477     }
478     public void clearCrumbs()
479     {
480         // TODO Auto-generated method stub
481
getCrumbs().clear();
482     }
483     public List JavaDoc getCrumbs()
484     {
485         if (fieldCrumbs == null)
486         {
487             fieldCrumbs = new ArrayList JavaDoc();
488         }
489         return fieldCrumbs;
490     }
491     public void setCrumbs(List JavaDoc inCrumbs)
492     {
493         fieldCrumbs = inCrumbs;
494     }
495     public void addCrumb(String JavaDoc inPath, String JavaDoc 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 JavaDoc getPath()
513     {
514         return getPage().getPath();
515     }
516 }
517
Popular Tags