Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6fc0020d0a78b2504bd75b8039ccd9fcca820be1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * Copyright (c) 2018 Christian Pontesegger and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License_Identifier: EPL-2.0
 *
 * Contributors:
 *     Christian Pontesegger - initial API and implementation
 *******************************************************************************/

package org.eclipse.ease.ui.help.hovers;

import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.eclipse.ease.Logger;
import org.eclipse.ease.modules.ModuleDefinition;
import org.eclipse.ease.ui.Activator;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.XMLMemento;

public class ModuleHelp implements IHoverHelp {

	/**
	 * Retrieve help page URL for a given module definition.
	 *
	 * @param definition
	 *            module definition to get location for
	 * @return help URL
	 */
	public static URL getModuleHelpLocation(final ModuleDefinition definition) {
		final String helpLocation = definition.getHelpLocation(null);
		return PlatformUI.getWorkbench().getHelpSystem().resolve(helpLocation, true);
	}

	/**
	 * Retrieve help page for a given module definition.
	 *
	 * @param url
	 *            url to read help from
	 * @return help content (HTML body node)
	 * @throws Exception
	 */
	private static IMemento getHtmlContent(final URL url) throws Exception {
		try {
			final IMemento rootNode = XMLMemento.createReadRoot(new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
			return rootNode.getChild("body");
		} catch (final Exception e) {
			Logger.error(Activator.PLUGIN_ID, "Cannot find the module help content ", e);
			throw e;
		}
	}

	private final IMemento fHelpContent;
	private final URL fHelpLocation;

	/**
	 * Help provider for modules, methods and fields.
	 *
	 * @param helpLocation
	 *            location of help html file.
	 * @throws Exception
	 *             when help content cannot be read
	 */
	public ModuleHelp(URL helpLocation) throws Exception {
		fHelpLocation = helpLocation;
		fHelpContent = getHtmlContent(helpLocation);
	}

	@Override
	public String getName() {
		for (final IMemento node : fHelpContent.getChildren()) {
			if ("module".equals(node.getString("class")))
				return node.getString("title");
		}

		return "";
	}

	@Override
	public String getDescription() {
		for (final IMemento node : fHelpContent.getChildren()) {
			if ("module".equals(node.getString("class"))) {
				for (final IMemento contentNode : node.getChildren()) {
					if ("description".equals(contentNode.getString("class"))) {
						IHoverHelp.updateRelativeLinks(contentNode, fHelpLocation);

						return IHoverHelp.getNodeContent(contentNode);
					}
				}
			}
		}

		return "";
	}

	public String getDeprecationMessage() {
		for (final IMemento node : fHelpContent.getChildren()) {
			if ("module".equals(node.getString("class"))) {
				for (final IMemento contentNode : node.getChildren()) {
					if ("deprecated".equals(contentNode.getString("class"))) {
						IHoverHelp.updateRelativeLinks(contentNode, fHelpLocation);

						String content = IHoverHelp.getNodeContent(contentNode);
						if (content.contains("<span class=\"warning\"></span>"))
							content = content.substring(content.indexOf("<span class=\"warning\"></span>") + "<span class=\"warning\"></span>".length());

						return content;
					}
				}
			}
		}

		return "";
	}

	@Override
	public String getHoverContent() {

		final StringBuffer help = new StringBuffer();
		help.append("<h5>"); //$NON-NLS-1$
		help.append(IHoverHelp.getImageAndLabel(HelpHoverImageProvider.getImageLocation("icons/eobj16/module.png"), getName()));
		help.append("</h5>"); //$NON-NLS-1$

		final String deprecationMessage = getDeprecationMessage();
		if (!deprecationMessage.isEmpty()) {
			help.append("<p>"); //$NON-NLS-1$
			help.append(deprecationMessage);
			help.append("</p>"); //$NON-NLS-1$
		}

		final String description = getDescription();
		if (!description.isEmpty()) {
			help.append("<p>"); //$NON-NLS-1$
			help.append(description);
			help.append("</p>"); //$NON-NLS-1$
		}

		return help.toString();
	}

	public IHoverHelp getConstantHelp(final Field field) {
		return new ConstantHelp(fHelpLocation, fHelpContent, field);
	}

	public MethodHelp getMethodHelp(final Method method) throws Exception {
		return new MethodHelp(fHelpLocation, fHelpContent, method);
	}
}

Back to the top