1 11 package org.eclipse.help.internal.toc; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 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 ; 23 24 public class Toc extends UAElement implements IToc { 25 26 public static final String NAME = "toc"; public static final String ATTRIBUTE_LABEL = "label"; public static final String ATTRIBUTE_HREF = "href"; public static final String ATTRIBUTE_TOPIC = "topic"; public static final String ATTRIBUTE_LINK_TO = "link_to"; public static final String ATTRIBUTE_ID = "id"; 33 private ITocContribution contribution; 34 private ITopic topic; 35 private Map 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 55 private Map createHref2TopicMap() { 56 Map map = new HashMap (); 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 71 private void createHref2TopicMapAux(Map 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 getHref() { 84 return getAttribute(ATTRIBUTE_HREF); 85 } 86 87 90 public Map getHref2TopicMap() { 91 if (href2TopicMap == null) { 92 href2TopicMap = createHref2TopicMap(); 93 } 94 return href2TopicMap; 95 } 96 97 public String getLabel() { 98 return getAttribute(ATTRIBUTE_LABEL); 99 } 100 101 public String getLinkTo() { 102 return getAttribute(ATTRIBUTE_LINK_TO); 103 } 104 105 public String getTopic() { 106 return getAttribute(ATTRIBUTE_TOPIC); 107 } 108 109 public ITopic getTopic(String href) { 110 if (href == null) { 111 if (topic == null) { 112 topic = new ITopic() { 113 public String getHref() { 114 return getTopic(); 115 } 116 public String 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 label) { 142 setAttribute(ATTRIBUTE_LABEL, label); 143 } 144 145 public void setLinkTo(String linkTo) { 146 setAttribute(ATTRIBUTE_LINK_TO, linkTo); 147 } 148 149 public void setTopic(String href) { 150 setAttribute(ATTRIBUTE_TOPIC, href); 151 } 152 153 public void setHref(String 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 |