001: /*
002: * Created on Dec 22, 2004
003: */
004: package org.openedit.links;
005:
006: import java.io.Serializable;
007: import java.util.ArrayList;
008: import java.util.Collections;
009: import java.util.Comparator;
010: import java.util.Iterator;
011: import java.util.List;
012:
013: import com.openedit.WebPageRequest;
014: import com.openedit.util.PathUtilities;
015: import com.openedit.web.Crumb;
016:
017: /**
018: * @author cburkey
019: *
020: */
021: public class Link implements Serializable {
022:
023: private static final long serialVersionUID = 6696306680268063277L;
024:
025: protected String fieldPath;
026: protected String fieldText;
027: protected String fieldId;
028: protected boolean fieldSelected;
029: protected Link fieldParentLink;
030: protected List fieldChildren;
031: protected String fieldUserData;
032: protected String fieldRedirectPath;
033: protected String fieldAccessKey;
034: protected boolean fieldAutoLoadChildren;
035: protected String fieldPrefix;
036: protected String fieldPostfix;
037:
038: public String getPrefix() {
039: return fieldPrefix;
040: }
041:
042: public void setPrefix(String fieldPrefix) {
043: this .fieldPrefix = fieldPrefix;
044: }
045:
046: public String getPostfix() {
047: return fieldPostfix;
048: }
049:
050: public void setPostfix(String fieldPostfix) {
051: this .fieldPostfix = fieldPostfix;
052: }
053:
054: public String getText() {
055: return fieldText;
056: }
057:
058: public void setText(String inText) {
059: fieldText = inText;
060: }
061:
062: /** I could not decide what to call this TODO: Remove URL */
063: public String getPath() {
064: return fieldPath;
065: }
066:
067: public String getHref() {
068: return getPath();
069: }
070:
071: public String getUrl() {
072: return getPath();
073: }
074:
075: public String getPageName() {
076: return PathUtilities.extractPageName(getPath());
077: }
078:
079: public void setPath(String inString) {
080: fieldPath = inString;
081: }
082:
083: public String getId() {
084: return fieldId;
085: }
086:
087: public void setId(String inId) {
088: fieldId = inId;
089: }
090:
091: public int getDepth() {
092: int i = 0;
093: Link parent = getParentLink();
094: while (parent != null) {
095: i++;
096: parent = parent.getParentLink();
097: }
098: return i;
099: }
100:
101: public boolean hasChildren() {
102: return fieldChildren != null && fieldChildren.size() > 0;
103: }
104:
105: /**
106: * Divides the children into rows
107: *
108: * @param inRowCount
109: * @return
110: */
111: public List getChildrenInRows(int inColCount) {
112: double rowscount = (double) getChildren().size()
113: / (double) inColCount;
114:
115: List rows = new ArrayList();
116: for (int i = 0; i < rowscount; i++) {
117: int start = i * inColCount;
118: int end = i * inColCount + inColCount;
119: // start = Math.min
120: List sublist = getChildren().subList(start,
121: Math.min(getChildren().size(), end));
122: rows.add(sublist);
123: }
124: return rows;
125: }
126:
127: public List getChildren() {
128: if (fieldChildren == null) {
129: fieldChildren = new ArrayList();
130: }
131: return fieldChildren;
132: }
133:
134: public void setChildren(List inChildren) {
135: fieldChildren = inChildren;
136: }
137:
138: public Link getParentLink() {
139: return fieldParentLink;
140: }
141:
142: public void setParentLink(Link inParentLink) {
143: fieldParentLink = inParentLink;
144: }
145:
146: /**
147: * @param inLink
148: */
149: public void addChild(Link inLink) {
150: inLink.setParentLink(this );
151: getChildren().add(inLink);
152: }
153:
154: public void insertChild(Link inLink) {
155: inLink.setParentLink(this );
156: getChildren().add(0, inLink);
157: }
158:
159: /**
160: * @param inLink
161: */
162: public void removeChild(Link inLink) {
163: getChildren().remove(inLink);
164: inLink.setParentLink(null);
165: }
166:
167: /**
168: * @param inLink
169: */
170: public void moveUp(Link inLink) {
171: int index = getChildren().indexOf(inLink);
172: if (index != -1 && index != 0) {
173: getChildren().remove(index);
174: index--;
175: getChildren().add(index, inLink);
176: }
177: }
178:
179: /**
180: * @param inLink
181: */
182: public void moveDown(Link inLink) {
183: int index = getChildren().indexOf(inLink);
184: if (index != -1 && index != getChildren().size() - 1) {
185: getChildren().remove(index);
186: index++;
187: getChildren().add(index, inLink);
188: }
189: }
190:
191: /**
192: * @param inLink
193: * @return
194: */
195: public Link getChildAbove(Link inLink) {
196: int count = getChildren().indexOf(inLink);
197: if (count != -1 && count != 0) {
198: count--;
199: Link brother = (Link) getChildren().get(count);
200: return brother;
201: }
202: return null;
203: }
204:
205: public Link getChildBelow(Link inLink) {
206: int count = getChildren().indexOf(inLink);
207: if (count != -1 && count != getChildren().size() - 1) {
208: count++;
209: Link brother = (Link) getChildren().get(count);
210: return brother;
211: }
212: return null;
213: }
214:
215: public Link getDecendant(String inId) {
216: for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
217: Link child = (Link) iter.next();
218: if (child.getId().equals(inId)) {
219: return child;
220: } else {
221: Link decendant = child.getDecendant(inId);
222: if (decendant != null) {
223: return decendant;
224: }
225: }
226: }
227: return null;
228: }
229:
230: /**
231: * @param inLink
232: * @param inParent1
233: */
234: public void addChildNearLocation(Link inLink, Link inParent1) {
235: int count = getChildren().indexOf(inParent1);
236: if (count != -1) {
237: getChildren().add(count + 1, inLink);
238: } else {
239: getChildren().add(inLink);
240: }
241: inLink.setParentLink(this );
242: }
243:
244: /**
245: * This is a flat list of links. Useful for generating menus or trees in
246: * velocity
247: *
248: * @return
249: */
250: public List list() {
251: ArrayList fieldAllLinks = new ArrayList();
252: addLinksToList(this , fieldAllLinks);
253: return fieldAllLinks;
254: }
255:
256: /**
257: * @param inRootLink
258: * @param inAllLinks
259: */
260: protected void addLinksToList(Link inRootLink, List inAllLinks) {
261: inAllLinks.add(inRootLink);
262: if (inRootLink.hasChildren()) {
263: for (Iterator iter = inRootLink.getChildren().iterator(); iter
264: .hasNext();) {
265: Link element = (Link) iter.next();
266: addLinksToList(element, inAllLinks);
267: }
268: }
269: }
270:
271: public String getUserData() {
272: return fieldUserData;
273: }
274:
275: public void setUserData(String inUserData) {
276: fieldUserData = inUserData;
277: }
278:
279: public boolean isSelected() {
280: return fieldSelected;
281: }
282:
283: public void setSelected(boolean inSelected) {
284: fieldSelected = inSelected;
285: }
286:
287: /**
288: * @return
289: */
290: public boolean isChildSelected() {
291: if (isSelected()) {
292: return true;
293: }
294: if (hasChildren()) {
295: for (Iterator iter = getChildren().iterator(); iter
296: .hasNext();) {
297: Link child = (Link) iter.next();
298: if (child.isChildSelected()) {
299: return true;
300: }
301: }
302: }
303: return false;
304: }
305:
306: /**
307: * @param inUrl
308: * @return
309: */
310: public boolean isChild(Link inUrl) {
311: for (Iterator iter = getChildren().iterator(); iter.hasNext();) {
312: Link element = (Link) iter.next();
313: if (element == inUrl) {
314: return true;
315: }
316: if (element.isChild(inUrl)) {
317: return true;
318: }
319: }
320: return false;
321: }
322:
323: /**
324: * @return Returns the redirectPath.
325: */
326: public String getRedirectPath() {
327: return fieldRedirectPath;
328: }
329:
330: /**
331: * @param inRedirectPath
332: * The redirectPath to set.
333: */
334: public void setRedirectPath(String inRedirectPath) {
335: fieldRedirectPath = inRedirectPath;
336: }
337:
338: public String getAccessKey() {
339: return fieldAccessKey;
340: }
341:
342: public void setAccessKey(String inAccessKey) {
343: fieldAccessKey = inAccessKey;
344: }
345:
346: public boolean isAutoLoadChildren() {
347: return fieldAutoLoadChildren;
348: }
349:
350: public void setAutoLoadChildren(boolean inAutoLoadChildren) {
351: fieldAutoLoadChildren = inAutoLoadChildren;
352: }
353:
354: public String toString() {
355: return getHref();
356: }
357:
358: public void sortChildren() {
359: Collections.sort(getChildren(), new Comparator() {
360: public int compare(Object arg0, Object arg1) {
361: Link link1 = (Link) arg0;
362: Link link2 = (Link) arg1;
363: if (link1.getText() != null && link2.getText() != null) {
364: return link1.getText().compareTo(link2.getText());
365: }
366: return 0;
367: }
368: });
369:
370: }
371:
372: public String getDirectory() {
373: String path = PathUtilities.extractDirectoryPath(getPath());
374: // urlpath is the address the link came in on
375: return path;
376: }
377:
378: public List getParents() {
379: List parents = new ArrayList();
380: Link parent = this ;
381: while (parent != null) {
382: parents.add(0, parent);
383: parent = parent.getParentLink();
384: }
385: return parents;
386: }
387:
388: }
|