2 * Copyright 2000-2016 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.help.impl;
18 import com.intellij.CommonBundle;
19 import com.intellij.ide.BrowserUtil;
20 import com.intellij.ide.IdeBundle;
21 import com.intellij.ide.plugins.HelpSetPath;
22 import com.intellij.ide.plugins.IdeaPluginDescriptor;
23 import com.intellij.ide.plugins.PluginManagerCore;
24 import com.intellij.internal.statistic.UsageTrigger;
25 import com.intellij.openapi.application.ApplicationInfo;
26 import com.intellij.openapi.application.ex.ApplicationInfoEx;
27 import com.intellij.openapi.diagnostic.Logger;
28 import com.intellij.openapi.help.HelpManager;
29 import com.intellij.openapi.ui.Messages;
30 import com.intellij.openapi.util.text.StringUtil;
31 import com.intellij.reference.SoftReference;
32 import com.intellij.util.PlatformUtils;
33 import org.jetbrains.annotations.NonNls;
34 import org.jetbrains.annotations.Nullable;
36 import javax.help.BadIDException;
37 import javax.help.HelpSet;
39 import java.lang.ref.WeakReference;
42 public class HelpManagerImpl extends HelpManager {
43 private static final Logger LOG = Logger.getInstance("#com.intellij.help.impl.HelpManagerImpl");
45 @NonNls private static final String HELP_HS = "Help.hs";
47 private WeakReference<IdeaHelpBroker> myBrokerReference = null;
49 public void invokeHelp(@Nullable String id) {
50 UsageTrigger.trigger("ide.help." + id);
52 if (MacHelpUtil.isApplicable() && MacHelpUtil.invokeHelp(id)) {
56 IdeaHelpBroker broker = SoftReference.dereference(myBrokerReference);
58 HelpSet set = createHelpSet();
60 broker = new IdeaHelpBroker(set);
61 myBrokerReference = new WeakReference<>(broker);
66 ApplicationInfoEx info = ApplicationInfoEx.getInstanceEx();
67 String minorVersion = info.getMinorVersion();
68 int dot = minorVersion.indexOf('.');
70 minorVersion = minorVersion.substring(0, dot);
72 String productVersion = info.getMajorVersion() + "." + minorVersion;
74 String url = info.getWebHelpUrl() + "/" + productVersion + "/?" + id;
76 if (PlatformUtils.isJetBrainsProduct()) {
77 String productCode = info.getBuild().getProductCode();
78 if(!StringUtil.isEmpty(productCode)) {
79 url += "&utm_source=from_product&utm_medium=help_link&utm_campaign=" + productCode + "&utm_content=" + productVersion;
83 BrowserUtil.browse(url);
87 Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
88 broker.setActivationWindow(activeWindow);
92 broker.setCurrentID(id);
94 catch (BadIDException e) {
95 Messages.showErrorDialog(IdeBundle.message("help.topic.not.found.error", id), CommonBundle.getErrorTitle());
99 broker.setDisplayed(true);
103 private static HelpSet createHelpSet() {
104 String urlToHelp = ApplicationInfo.getInstance().getHelpURL() + "/" + HELP_HS;
105 HelpSet mainHelpSet = loadHelpSet(urlToHelp);
106 if (mainHelpSet == null) return null;
108 // merge plugins help sets
109 IdeaPluginDescriptor[] pluginDescriptors = PluginManagerCore.getPlugins();
110 for (IdeaPluginDescriptor pluginDescriptor : pluginDescriptors) {
111 HelpSetPath[] sets = pluginDescriptor.getHelpSets();
112 for (HelpSetPath hsPath : sets) {
113 String url = "jar:file:///" + pluginDescriptor.getPath().getAbsolutePath() + "/help/" + hsPath.getFile() + "!";
114 if (!hsPath.getPath().startsWith("/")) {
117 url += hsPath.getPath();
118 HelpSet pluginHelpSet = loadHelpSet(url);
119 if (pluginHelpSet != null) {
120 mainHelpSet.add(pluginHelpSet);
129 private static HelpSet loadHelpSet(final String url) {
131 return new HelpSet(null, new URL(url));
133 catch (Exception e) {
134 LOG.info("Failed to load help set from '" + url + "'", e);