KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > toc > Topic


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.toc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.ListIterator JavaDoc;
17
18 import org.eclipse.help.IToc;
19 import org.eclipse.help.ITopic;
20 import org.eclipse.help.internal.model.ITopicElement;
21 import org.xml.sax.Attributes JavaDoc;
22
23 /**
24  * Topic. Visible navigation element. Labeled, contains linik to a document. Can
25  * also act as a container for other documents.
26  */

27 public class Topic extends TocNode implements ITopic, ITopicElement {
28     private String JavaDoc href;
29     private String JavaDoc label;
30     private ITopic[] topicArray;
31     /**
32      * Constructor.
33      */

34     protected Topic(TocFile tocFile, Attributes JavaDoc attrs) {
35         if (attrs == null)
36             return;
37         href = attrs.getValue("href"); //$NON-NLS-1$
38
if (href != null && href.length() > 0)
39             href = HrefUtil.normalizeHref(tocFile.getPluginID(), href);
40         label = attrs.getValue("label"); //$NON-NLS-1$
41
if (label == null) {
42             throw new RuntimeException JavaDoc("topic label==null"); //$NON-NLS-1$
43
}
44         tocFile.getToc().registerTopic(this);
45         addFilters(attrs);
46     }
47     /**
48      * Implements abstract method.
49      */

50     public final void build(TocBuilder builder) {
51         builder.buildTopic(this);
52     }
53     public String JavaDoc getHref() {
54         return href;
55     }
56     public String JavaDoc getLabel() {
57         return label;
58     }
59     /**
60      * This public method is to be used after the build of TOCs is finished.
61      * With assumption that TOC model is not modifiable after the build, this
62      * method caches subtopics in an array and releases objects used only during
63      * build.
64      *
65      * @return ITopic list
66      */

67     public ITopic[] getSubtopics() {
68         if (topicArray == null) {
69             List JavaDoc topics = getChildTopics();
70             // create and cache array of children (Topics only)
71
topicArray = new ITopic[topics.size()];
72             topics.toArray(topicArray);
73         }
74         return topicArray;
75     }
76
77     void setLabel(String JavaDoc label) {
78         this.label = label;
79     }
80
81     void setHref(String JavaDoc href) {
82         this.href = href;
83     }
84     /**
85      * Obtains shortest path leading to this topic in a given TOC
86      *
87      * @param toc
88      * @return ITopic[] or null, path excludes TOC and includes this topic
89      */

90     public ITopic[] getPathInToc(IToc toc) {
91         List JavaDoc /* of TocNode */ancestors = getTopicPathInToc(toc, this);
92         if (ancestors == null) {
93             return null;
94         }
95         return (ITopic[]) ancestors.toArray(new ITopic[ancestors.size()]);
96     }
97
98     /**
99      * Obtains List of ancestors (TocNodes) leading to specific topic or null
100      *
101      * @param toc
102      * @param topic
103      * @return List with TocElements: topic1, topic2, topic
104      */

105     static List JavaDoc getTopicPathInToc(IToc toc, Topic topic) {
106         List JavaDoc topicParents = new ArrayList JavaDoc(topic.getParents());
107         for (ListIterator JavaDoc it = topicParents.listIterator(); it.hasNext();) {
108             TocNode tocNode = (TocNode) it.next();
109             if (!(tocNode instanceof Topic)) {
110                 // Check if any parent is the needed TOC
111
if (tocNode == toc) {
112                     // success, found the correct TOC
113
List JavaDoc ancestors = new ArrayList JavaDoc();
114                     ancestors.add(topic);
115                     return ancestors;
116                 }
117                 // substitute real topics for toc, link, and anchor parent
118
// nodes, because we are looking for the shortest path
119
List JavaDoc grandParents = tocNode.getParents();
120                 it.remove();
121                 for (Iterator JavaDoc it2 = grandParents.iterator(); it2.hasNext();) {
122                     it.add(it2.next());
123                     it.previous();
124                 }
125             }
126         }
127
128         for (Iterator JavaDoc it = topicParents.iterator(); it.hasNext();) {
129             // delegate to ancestors first
130
List JavaDoc a = getTopicPathInToc(toc, (Topic) it.next());
131             if (a != null) {
132                 // then add this topic to the path
133
a.add(topic);
134                 return a;
135             }
136         }
137
138         return null;
139     }
140 }
141
Popular Tags