KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > workingset > AdaptableTopic


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.workingset;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.help.*;
17 import org.eclipse.help.internal.util.*;
18 import org.w3c.dom.*;
19
20 /**
21  * Makes help resources adaptable and persistable
22  */

23 public class AdaptableTopic extends AdaptableHelpResource {
24     /**
25      * Map of all topics with this topic as ancestor
26      */

27     private Map JavaDoc topicMap;
28
29     /**
30      * This constructor will be called when wrapping help resources.
31      */

32     AdaptableTopic(ITopic element) {
33         super(element);
34     }
35
36     /**
37      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
38      */

39     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
40         if (adapter == ITopic.class)
41             return element;
42         return super.getAdapter(adapter);
43     }
44
45     public AdaptableHelpResource[] getChildren() {
46         ITopic[] topics = this.getSubtopics();
47         AdaptableHelpResource[] adaptableTopic = new AdaptableTopic[topics.length];
48         for (int i = 0; i < topics.length; i++) {
49             adaptableTopic[i] = new AdaptableTopic(topics[i]);
50         }
51         return adaptableTopic;
52     }
53
54     /**
55      * @see org.eclipse.help.ITopic#getSubtopics()
56      */

57     public ITopic[] getSubtopics() {
58         return ((ITopic) element).getSubtopics();
59     }
60
61     /**
62      * Returns a topic with the specified href. <br>
63      * It is possible that multiple tocs have the same href, in which case there
64      * is no guarantee which one is returned.
65      *
66      * @param href
67      * The topic's href value.
68      */

69     public ITopic getTopic(String JavaDoc href) {
70         if (href == null)
71             return null;
72
73         if (topicMap == null) {
74             // traverse TOC and fill in the topicMap
75
topicMap = new HashMap JavaDoc();
76             topicMap.put(getHref(), element);
77             FastStack stack = new FastStack();
78             ITopic[] topics = getSubtopics();
79             for (int i = 0; i < topics.length; i++)
80                 stack.push(topics[i]);
81             while (!stack.isEmpty()) {
82                 ITopic topic = (ITopic) stack.pop();
83                 if (topic != null) {
84                     String JavaDoc topicHref = topic.getHref();
85                     if (topicHref != null) {
86                         topicMap.put(topicHref, topic);
87                     }
88                     ITopic[] subtopics = topic.getSubtopics();
89                     for (int i = 0; i < subtopics.length; i++)
90                         stack.push(subtopics[i]);
91                 }
92             }
93         }
94         return (ITopic) topicMap.get(href);
95     }
96
97     public void saveState(Element element) {
98         AdaptableToc toc = (AdaptableToc) getParent();
99         toc.saveState(element);
100         AdaptableHelpResource[] topics = toc.getChildren();
101         for (int i = 0; i < topics.length; i++)
102             if (topics[i] == this)
103                 element.setAttribute("topic", String.valueOf(i)); //$NON-NLS-1$
104
}
105 }
106
Popular Tags