KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.expressions.IEvaluationContext;
17 import org.eclipse.help.IToc;
18 import org.eclipse.help.ITocContribution;
19 import org.eclipse.help.ITopic;
20 import org.eclipse.help.IUAElement;
21 import org.eclipse.help.internal.UAElement;
22 import org.w3c.dom.Element JavaDoc;
23
24 public class Toc extends UAElement implements IToc {
25
26     public static final String JavaDoc NAME = "toc"; //$NON-NLS-1$
27
public static final String JavaDoc ATTRIBUTE_LABEL = "label"; //$NON-NLS-1$
28
public static final String JavaDoc ATTRIBUTE_HREF = "href"; //$NON-NLS-1$
29
public static final String JavaDoc ATTRIBUTE_TOPIC = "topic"; //$NON-NLS-1$
30
public static final String JavaDoc ATTRIBUTE_LINK_TO = "link_to"; //$NON-NLS-1$
31
public static final String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
32

33     private ITocContribution contribution;
34     private ITopic topic;
35     private Map JavaDoc href2TopicMap;
36
37     public Toc(IToc src) {
38         super(NAME, src);
39         setHref(src.getHref());
40         setLabel(src.getLabel());
41         ITopic topic = src.getTopic(null);
42         if (topic != null) {
43             setTopic(topic.getHref());
44         }
45         appendChildren(src.getChildren());
46     }
47     
48     public Toc(Element src) {
49         super(src);
50     }
51     
52     /*
53      * Creates a mapping of all topic hrefs to ITopics.
54      */

55     private Map JavaDoc createHref2TopicMap() {
56         Map JavaDoc map = new HashMap JavaDoc();
57         if (topic != null) {
58             map.put(topic.getHref(), topic);
59         }
60         ITopic[] topics = getTopics();
61         for (int i=0;i<topics.length;++i) {
62             createHref2TopicMapAux(map, topics[i]);
63         }
64         return map;
65     }
66
67     /*
68      * Creates a mapping of all topic hrefs to ITopics under the given
69      * ITopic and stores in the given Map.
70      */

71     private void createHref2TopicMapAux(Map JavaDoc map, ITopic topic) {
72         map.put(topic.getHref(), topic);
73         ITopic[] subtopics = topic.getSubtopics();
74         if (subtopics != null) {
75             for (int i=0;i<subtopics.length;++i) {
76                 if (subtopics[i] != null) {
77                     createHref2TopicMapAux(map, subtopics[i]);
78                 }
79             }
80         }
81     }
82
83     public String JavaDoc getHref() {
84         return getAttribute(ATTRIBUTE_HREF);
85     }
86
87     /*
88      * Returns a mapping of all topic hrefs to ITopics.
89      */

90     public Map JavaDoc getHref2TopicMap() {
91         if (href2TopicMap == null) {
92             href2TopicMap = createHref2TopicMap();
93         }
94         return href2TopicMap;
95     }
96     
97     public String JavaDoc getLabel() {
98         return getAttribute(ATTRIBUTE_LABEL);
99     }
100     
101     public String JavaDoc getLinkTo() {
102         return getAttribute(ATTRIBUTE_LINK_TO);
103     }
104     
105     public String JavaDoc getTopic() {
106         return getAttribute(ATTRIBUTE_TOPIC);
107     }
108     
109     public ITopic getTopic(String JavaDoc href) {
110         if (href == null) {
111             if (topic == null) {
112                 topic = new ITopic() {
113                     public String JavaDoc getHref() {
114                         return getTopic();
115                     }
116                     public String JavaDoc getLabel() {
117                         return Toc.this.getLabel();
118                     }
119                     public ITopic[] getSubtopics() {
120                         return getTopics();
121                     }
122                     public boolean isEnabled(IEvaluationContext context) {
123                         return isEnabled(context);
124                     }
125                     public IUAElement[] getChildren() {
126                         return getChildren();
127                     }
128                 };
129             }
130             return topic;
131         }
132         else {
133             return (ITopic)getHref2TopicMap().get(href);
134         }
135     }
136
137     public ITopic[] getTopics() {
138         return (ITopic[])getChildren(ITopic.class);
139     }
140     
141     public void setLabel(String JavaDoc label) {
142         setAttribute(ATTRIBUTE_LABEL, label);
143     }
144
145     public void setLinkTo(String JavaDoc linkTo) {
146         setAttribute(ATTRIBUTE_LINK_TO, linkTo);
147     }
148
149     public void setTopic(String JavaDoc href) {
150         setAttribute(ATTRIBUTE_TOPIC, href);
151     }
152     
153     public void setHref(String JavaDoc href) {
154         setAttribute(ATTRIBUTE_HREF, href);
155     }
156     
157     public ITocContribution getTocContribution() {
158         return contribution;
159     }
160     
161     public void setTocContribution(ITocContribution contribution) {
162         this.contribution = contribution;
163     }
164 }
165
Popular Tags