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.reference.SoftReference;
31 import com.intellij.util.PlatformUtils;
32 import org.jetbrains.annotations.NonNls;
33 import org.jetbrains.annotations.Nullable;
35 import javax.help.BadIDException;
36 import javax.help.HelpSet;
38 import java.lang.ref.WeakReference;
41 public class HelpManagerImpl extends HelpManager {
42 private static final Logger LOG = Logger.getInstance("#com.intellij.help.impl.HelpManagerImpl");
44 @NonNls private static final String HELP_HS = "Help.hs";
46 private WeakReference<IdeaHelpBroker> myBrokerReference = null;
48 public void invokeHelp(@Nullable String id) {
49 UsageTrigger.trigger("ide.help." + id);
51 if (MacHelpUtil.isApplicable() && MacHelpUtil.invokeHelp(id)) {
55 IdeaHelpBroker broker = SoftReference.dereference(myBrokerReference);
57 HelpSet set = createHelpSet();
59 broker = new IdeaHelpBroker(set);
60 myBrokerReference = new WeakReference<>(broker);
65 ApplicationInfoEx info = ApplicationInfoEx.getInstanceEx();
66 String minorVersion = info.getMinorVersion();
67 int dot = minorVersion.indexOf('.');
69 minorVersion = minorVersion.substring(0, dot);
71 String productVersion = info.getMajorVersion() + "." + minorVersion;
72 String productCode = info.getPackageCode();
74 String url = info.getWebHelpUrl() + "/" + productVersion + "/?" + id;
76 if (PlatformUtils.isJetBrainsProduct()) {
77 url += "&utm_source=from_product&utm_medium=help_link&utm_campaign=" + productCode + "&utm_content=" + productVersion;
80 BrowserUtil.browse(url);
84 Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
85 broker.setActivationWindow(activeWindow);
89 broker.setCurrentID(id);
91 catch (BadIDException e) {
92 Messages.showErrorDialog(IdeBundle.message("help.topic.not.found.error", id), CommonBundle.getErrorTitle());
96 broker.setDisplayed(true);
100 private static HelpSet createHelpSet() {
101 String urlToHelp = ApplicationInfo.getInstance().getHelpURL() + "/" + HELP_HS;
102 HelpSet mainHelpSet = loadHelpSet(urlToHelp);
103 if (mainHelpSet == null) return null;
105 // merge plugins help sets
106 IdeaPluginDescriptor[] pluginDescriptors = PluginManagerCore.getPlugins();
107 for (IdeaPluginDescriptor pluginDescriptor : pluginDescriptors) {
108 HelpSetPath[] sets = pluginDescriptor.getHelpSets();
109 for (HelpSetPath hsPath : sets) {
110 String url = "jar:file:///" + pluginDescriptor.getPath().getAbsolutePath() + "/help/" + hsPath.getFile() + "!";
111 if (!hsPath.getPath().startsWith("/")) {
114 url += hsPath.getPath();
115 HelpSet pluginHelpSet = loadHelpSet(url);
116 if (pluginHelpSet != null) {
117 mainHelpSet.add(pluginHelpSet);
126 private static HelpSet loadHelpSet(final String url) {
128 return new HelpSet(null, new URL(url));
130 catch (Exception e) {
131 LOG.info("Failed to load help set from '" + url + "'", e);