KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > context > Context


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Phil Loats (IBM Corp.) - fix to use only foundation APIs
11  *******************************************************************************/

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 JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26
27 public class Context extends UAElement implements IContext3 {
28
29     public static final String JavaDoc ATTRIBUTE_TITLE = "title"; //$NON-NLS-1$
30
public static final String JavaDoc NAME = "context"; //$NON-NLS-1$
31
public static final String JavaDoc ELEMENT_DESCRIPTION = "description"; //$NON-NLS-1$
32
public static final String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
33
public static final String JavaDoc ATTRIBUTE_PLUGIN_ID = "pluginId"; //$NON-NLS-1$
34

35     public Context(Element src) {
36         super(src);
37     }
38     
39     public Context(IContext src, String JavaDoc id) {
40         super(NAME);
41         setText(src.getText());
42         setId(id);
43         if (src instanceof IContext2) {
44             String JavaDoc 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 JavaDoc getCategory(IHelpResource topic) {
70         return null;
71     }
72     
73     public String JavaDoc 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 JavaDoc getStyledText() {
86         return null;
87     }
88     
89     public String JavaDoc getText() {
90         Node JavaDoc 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 JavaDoc text = node.getFirstChild();
96                     if (text == null) {
97                         return new String JavaDoc();
98                     }
99                     if (text.getNodeType() == Node.TEXT_NODE) {
100                         return text.getNodeValue();
101                     }
102                 }
103             }
104             node = node.getNextSibling();
105         }
106         return new String JavaDoc();
107     }
108     
109     public String JavaDoc getTitle() {
110         String JavaDoc 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 JavaDoc id) {
118         setAttribute(ATTRIBUTE_ID, id);
119     }
120
121     public void setText(String JavaDoc text) {
122         Node JavaDoc 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 JavaDoc document = element.getOwnerDocument();
133         Node JavaDoc description = element.appendChild(document.createElement(ELEMENT_DESCRIPTION));
134         description.appendChild(document.createTextNode(text));
135     }
136 }
137
Popular Tags