KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
9 import java.util.List JavaDoc;
10
11
12 /**
13  * @author cburkey
14  *
15  */

16 public class Link implements Serializable JavaDoc
17 {
18     /**
19      *
20      */

21     private static final long serialVersionUID = 6696306680268063277L;
22     protected String JavaDoc fieldPath;
23     protected String JavaDoc fieldText;
24     protected String JavaDoc fieldId;
25     protected boolean fieldSelected;
26     
27     protected String JavaDoc[] fieldParsedId;
28     protected Link fieldParentLink;
29     protected List JavaDoc fieldChildren;
30     protected String JavaDoc fieldUserData;
31     protected String JavaDoc fieldRedirectPath;
32     protected String JavaDoc fieldAccessKey;
33     protected boolean fieldAutoLoadChildren;
34     
35     public String JavaDoc getText()
36     {
37         return fieldText;
38     }
39     public void setText(String JavaDoc inText)
40     {
41         fieldText = inText;
42     }
43     
44     /** I could not decide what to call this TODO: Remove URL */
45     public String JavaDoc getPath()
46     {
47         return fieldPath;
48     }
49     public String JavaDoc getHref()
50     {
51         return getPath();
52     }
53     public String JavaDoc getUrl()
54     {
55         return getPath();
56     }
57     public void setPath(String JavaDoc inString)
58     {
59         fieldPath = inString;
60     }
61
62     public String JavaDoc getId()
63     {
64         return fieldId;
65     }
66     public void setId(String JavaDoc inId)
67     {
68         fieldId = inId;
69         fieldParsedId = null;
70     }
71     public int getDepth()
72     {
73         int i = 0;
74         Link parent = getParentLink();
75         while(parent != null)
76         {
77             i++;
78             parent = parent.getParentLink();
79         }
80         return i;
81     }
82     public String JavaDoc[] getParsedId()
83     {
84         if ( fieldParsedId == null)
85         {
86             List JavaDoc ids = new ArrayList JavaDoc();
87             ids.add(0,getId());
88             Link parent = getParentLink();
89             while( parent != null)
90             {
91                 ids.add(0,parent.getId());
92                 parent = getParentLink();
93             }
94             fieldParsedId = (String JavaDoc[])ids.toArray(new String JavaDoc[ids.size()]);
95         }
96         return fieldParsedId;
97     }
98     public boolean hasChildren()
99     {
100         return fieldChildren != null && fieldChildren.size() > 0;
101     }
102     /**
103      * Divides the children into rows
104      * @param inRowCount
105      * @return
106      */

107     public List JavaDoc getChildrenInRows(int inColCount)
108     {
109         double rowscount = (double)getChildren().size() / (double)inColCount;
110         
111         List JavaDoc rows = new ArrayList JavaDoc();
112         for (int i = 0; i < rowscount; i++)
113         {
114             int start = i*inColCount;
115             int end = i*inColCount + inColCount;
116             //start = Math.min
117
List JavaDoc sublist = getChildren().subList(start,Math.min( getChildren().size(),end ));
118             rows.add(sublist);
119         }
120         return rows;
121     }
122     public List JavaDoc getChildren()
123     {
124         if ( fieldChildren == null)
125         {
126             fieldChildren = new ArrayList JavaDoc();
127         }
128         return fieldChildren;
129     }
130     public void setChildren(List JavaDoc inChildren)
131     {
132         fieldChildren = inChildren;
133     }
134     public Link getParentLink()
135     {
136         return fieldParentLink;
137     }
138     public void setParentLink(Link inParentLink)
139     {
140         fieldParentLink = inParentLink;
141     }
142     /**
143      * @param inLink
144      */

145     public void addChild(Link inLink)
146     {
147         inLink.setParentLink(this);
148         getChildren().add(inLink);
149     }
150     public void insertChild(Link inLink)
151     {
152         inLink.setParentLink(this);
153         getChildren().add(0,inLink);
154     }/**
155      * @param inLink
156      */

157     public void removeChild(Link inLink)
158     {
159         getChildren().remove(inLink);
160         inLink.setParentLink(null);
161     }
162     /**
163      * @param inLink
164      */

165     public void moveUp(Link inLink)
166     {
167         int index = getChildren().indexOf(inLink);
168         if ( index != -1 && index != 0)
169         {
170             getChildren().remove(index);
171             index--;
172             getChildren().add(index,inLink);
173         }
174     }
175     /**
176      * @param inLink
177      */

178     public void moveDown(Link inLink)
179     {
180         int index = getChildren().indexOf(inLink);
181         if ( index != -1 && index != getChildren().size()-1)
182         {
183             getChildren().remove(index);
184             index++;
185             getChildren().add(index,inLink);
186         }
187     }
188     /**
189      * @param inLink
190      * @return
191      */

192     public Link getChildAbove(Link inLink)
193     {
194         int count = getChildren().indexOf(inLink);
195         if ( count != -1 && count != 0)
196         {
197             count--;
198             Link brother = (Link)getChildren().get(count);
199             return brother;
200         }
201         return null;
202     }
203     public Link getChildBelow(Link inLink)
204     {
205         int count = getChildren().indexOf(inLink);
206         if ( count != -1 && count != getChildren().size() -1)
207         {
208             count++;
209             Link brother = (Link)getChildren().get(count);
210             return brother;
211         }
212         return null;
213     }
214     
215     public Link getDecendant(String JavaDoc inId)
216     {
217         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
218         {
219             Link child = (Link) iter.next();
220             if (child.getId().equals(inId))
221             {
222                 return child;
223             }
224             else
225             {
226                 Link decendant = child.getDecendant(inId);
227                 if (decendant != null)
228                 {
229                     return decendant;
230                 }
231             }
232         }
233         return null;
234     }
235     
236     
237     /**
238      * @param inLink
239      * @param inParent1
240      */

241     public void addChildNearLocation(Link inLink, Link inParent1)
242     {
243         int count = getChildren().indexOf(inParent1);
244         if ( count != -1 )
245         {
246             getChildren().add(count+1,inLink);
247         }
248         else
249         {
250             getChildren().add(inLink );
251         }
252         inLink.setParentLink(this);
253     }
254     /**
255      * This is a flat list of links. Useful for generating menus or trees in velocity
256      * @return
257      */

258     public List JavaDoc list()
259     {
260         ArrayList JavaDoc fieldAllLinks = new ArrayList JavaDoc();
261         addLinksToList(this,fieldAllLinks);
262         return fieldAllLinks;
263     }
264     /**
265      * @param inRootLink
266      * @param inAllLinks
267      */

268     protected void addLinksToList(Link inRootLink, List JavaDoc inAllLinks)
269     {
270         inAllLinks.add(inRootLink);
271         if ( inRootLink.hasChildren() )
272         {
273             for (Iterator JavaDoc iter = inRootLink.getChildren().iterator(); iter.hasNext();)
274             {
275                 Link element = (Link) iter.next();
276                 addLinksToList(element, inAllLinks);
277             }
278         }
279     }
280
281     public String JavaDoc getUserData()
282     {
283         return fieldUserData;
284     }
285     public void setUserData(String JavaDoc inUserData)
286     {
287         fieldUserData = inUserData;
288     }
289     public boolean isSelected()
290     {
291         return fieldSelected;
292     }
293     public void setSelected(boolean inSelected)
294     {
295         fieldSelected = inSelected;
296     }
297     /**
298      * @return
299      */

300     public boolean isChildSelected()
301     {
302         if ( isSelected())
303         {
304             return true;
305         }
306         if ( hasChildren() )
307         {
308             for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
309             {
310                 Link child = (Link) iter.next();
311                 if ( child.isChildSelected())
312                 {
313                     return true;
314                 }
315             }
316         }
317         return false;
318     }
319     /**
320      * @param inUrl
321      * @return
322      */

323     public boolean isChild(Link inUrl)
324     {
325         for (Iterator JavaDoc iter = getChildren().iterator(); iter.hasNext();)
326         {
327             Link element = (Link) iter.next();
328             if ( element == inUrl)
329             {
330                 return true;
331             }
332             if( element.isChild(inUrl) )
333             {
334                 return true;
335             }
336         }
337         return false;
338     }
339     /**
340      * @return Returns the redirectPath.
341      */

342     public String JavaDoc getRedirectPath()
343     {
344         return fieldRedirectPath;
345     }
346     /**
347      * @param inRedirectPath The redirectPath to set.
348      */

349     public void setRedirectPath(String JavaDoc inRedirectPath)
350     {
351         fieldRedirectPath = inRedirectPath;
352     }
353     public String JavaDoc getAccessKey()
354     {
355         return fieldAccessKey;
356     }
357     public void setAccessKey(String JavaDoc inAccessKey)
358     {
359         fieldAccessKey = inAccessKey;
360     }
361     public boolean isAutoLoadChildren()
362     {
363         return fieldAutoLoadChildren;
364     }
365     public void setAutoLoadChildren(boolean inAutoLoadChildren)
366     {
367         fieldAutoLoadChildren = inAutoLoadChildren;
368     }
369     public String JavaDoc toString()
370     {
371         return getHref();
372     }
373 }
374
Popular Tags