60510164b2a18317341664b9e1e52408eded40e9
[idea/community.git] / platform / platform-impl / src / com / intellij / help / impl / HelpManagerImpl.java
1 /*
2  * Copyright 2000-2016 JetBrains s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.intellij.help.impl;
17
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;
34
35 import javax.help.BadIDException;
36 import javax.help.HelpSet;
37 import java.awt.*;
38 import java.lang.ref.WeakReference;
39 import java.net.URL;
40
41 public class HelpManagerImpl extends HelpManager {
42   private static final Logger LOG = Logger.getInstance("#com.intellij.help.impl.HelpManagerImpl");
43
44   @NonNls private static final String HELP_HS = "Help.hs";
45
46   private WeakReference<IdeaHelpBroker> myBrokerReference = null;
47
48   public void invokeHelp(@Nullable String id) {
49     UsageTrigger.trigger("ide.help." + id);
50
51     if (MacHelpUtil.isApplicable() && MacHelpUtil.invokeHelp(id)) {
52       return;
53     }
54
55     IdeaHelpBroker broker = SoftReference.dereference(myBrokerReference);
56     if (broker == null) {
57       HelpSet set = createHelpSet();
58       if (set != null) {
59         broker = new IdeaHelpBroker(set);
60         myBrokerReference = new WeakReference<>(broker);
61       }
62     }
63
64     if (broker == null) {
65       ApplicationInfoEx info = ApplicationInfoEx.getInstanceEx();
66       String minorVersion = info.getMinorVersion();
67       int dot = minorVersion.indexOf('.');
68       if (dot != -1) {
69         minorVersion = minorVersion.substring(0, dot);
70       }
71       String productVersion = info.getMajorVersion() + "." + minorVersion;
72       String productCode = info.getPackageCode();
73
74       String url = info.getWebHelpUrl() + "/" + productVersion + "/?" + id;
75       
76       if (PlatformUtils.isJetBrainsProduct()) {
77         url += "&utm_source=from_product&utm_medium=help_link&utm_campaign=" + productCode + "&utm_content=" + productVersion;
78       }
79
80       BrowserUtil.browse(url);
81       return;
82     }
83
84     Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
85     broker.setActivationWindow(activeWindow);
86
87     if (id != null) {
88       try {
89         broker.setCurrentID(id);
90       }
91       catch (BadIDException e) {
92         Messages.showErrorDialog(IdeBundle.message("help.topic.not.found.error", id), CommonBundle.getErrorTitle());
93         return;
94       }
95     }
96     broker.setDisplayed(true);
97   }
98
99   @Nullable
100   private static HelpSet createHelpSet() {
101     String urlToHelp = ApplicationInfo.getInstance().getHelpURL() + "/" + HELP_HS;
102     HelpSet mainHelpSet = loadHelpSet(urlToHelp);
103     if (mainHelpSet == null) return null;
104
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("/")) {
112           url += "/";
113         }
114         url += hsPath.getPath();
115         HelpSet pluginHelpSet = loadHelpSet(url);
116         if (pluginHelpSet != null) {
117           mainHelpSet.add(pluginHelpSet);
118         }
119       }
120     }
121
122     return mainHelpSet;
123   }
124
125   @Nullable
126   private static HelpSet loadHelpSet(final String url) {
127     try {
128       return new HelpSet(null, new URL(url));
129     }
130     catch (Exception e) {
131       LOG.info("Failed to load help set from '" + url + "'", e);
132       return null;
133     }
134   }
135 }