1 12 package org.eclipse.help.internal.context; 13 14 import org.eclipse.help.ICommandLink; 15 import org.eclipse.help.IContext; 16 import org.eclipse.help.IContext2; 17 import org.eclipse.help.IContext3; 18 import org.eclipse.help.IHelpResource; 19 import org.eclipse.help.ITopic; 20 import org.eclipse.help.internal.CommandLink; 21 import org.eclipse.help.internal.Topic; 22 import org.eclipse.help.internal.UAElement; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 27 public class Context extends UAElement implements IContext3 { 28 29 public static final String ATTRIBUTE_TITLE = "title"; public static final String NAME = "context"; public static final String ELEMENT_DESCRIPTION = "description"; public static final String ATTRIBUTE_ID = "id"; public static final String ATTRIBUTE_PLUGIN_ID = "pluginId"; 35 public Context(Element src) { 36 super(src); 37 } 38 39 public Context(IContext src, String id) { 40 super(NAME); 41 setText(src.getText()); 42 setId(id); 43 if (src instanceof IContext2) { 44 String title = ((IContext2)src).getTitle(); 45 if (title != null) { 46 element.setAttribute(ATTRIBUTE_TITLE, title); 47 } 48 } 49 if (src instanceof IContext3) { 50 ICommandLink[] commands = ((IContext3)src).getRelatedCommands(); 51 for (int i=0;i<commands.length;++i) { 52 appendChild(new CommandLink(commands[i])); 53 } 54 } 55 IHelpResource[] topics = src.getRelatedTopics(); 56 for (int i=0;i<topics.length;++i) { 57 if (topics[i] instanceof ITopic) { 58 appendChild(new Topic((ITopic)topics[i])); 59 } 60 else { 61 Topic topic = new Topic(); 62 topic.setHref(topics[i].getHref()); 63 topic.setLabel(topics[i].getLabel()); 64 appendChild(topic); 65 } 66 } 67 } 68 69 public String getCategory(IHelpResource topic) { 70 return null; 71 } 72 73 public String getId() { 74 return getAttribute(ATTRIBUTE_ID); 75 } 76 77 public ICommandLink[] getRelatedCommands() { 78 return (ICommandLink[])getChildren(ICommandLink.class); 79 } 80 81 public IHelpResource[] getRelatedTopics() { 82 return (IHelpResource[])getChildren(IHelpResource.class); 83 } 84 85 public String getStyledText() { 86 return null; 87 } 88 89 public String getText() { 90 Node node = element.getFirstChild(); 91 while (node != null) { 92 if (node.getNodeType() == Node.ELEMENT_NODE) { 93 if (ELEMENT_DESCRIPTION.equals(node.getNodeName())) { 94 node.normalize(); 95 Node text = node.getFirstChild(); 96 if (text == null) { 97 return new String (); 98 } 99 if (text.getNodeType() == Node.TEXT_NODE) { 100 return text.getNodeValue(); 101 } 102 } 103 } 104 node = node.getNextSibling(); 105 } 106 return new String (); 107 } 108 109 public String getTitle() { 110 String title = element.getAttribute(ATTRIBUTE_TITLE); 111 if (title == null || title.length() == 0) { 112 return null; 113 } 114 return title; 115 } 116 117 public void setId(String id) { 118 setAttribute(ATTRIBUTE_ID, id); 119 } 120 121 public void setText(String text) { 122 Node node = element.getFirstChild(); 123 while (node != null) { 124 if (node.getNodeType() == Node.ELEMENT_NODE) { 125 if (ELEMENT_DESCRIPTION.equals(node.getNodeName())) { 126 element.removeChild(node); 127 break; 128 } 129 } 130 node = node.getNextSibling(); 131 } 132 Document document = element.getOwnerDocument(); 133 Node description = element.appendChild(document.createElement(ELEMENT_DESCRIPTION)); 134 description.appendChild(document.createTextNode(text)); 135 } 136 } 137 | Popular Tags |