blob: 7e822817a44ed80b4bd65a659ef08257dc64a753 (
plain)
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
|
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.ide.browsers;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.GeneralSettings;
import com.intellij.ide.browsers.impl.WebBrowserServiceImpl;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.LightVirtualFile;
import com.intellij.xml.XmlBundle;
import com.intellij.xml.util.HtmlUtil;
import java.awt.event.InputEvent;
public class OpenFileInDefaultBrowserAction extends DumbAwareAction {
private static final Logger LOG = Logger.getInstance(OpenFileInDefaultBrowserAction.class);
@Override
public void update(AnActionEvent e) {
final DataContext dataContext = e.getDataContext();
final PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
final Presentation presentation = e.getPresentation();
if (file == null || file.getVirtualFile() == null) {
presentation.setVisible(false);
presentation.setEnabled(false);
return;
}
Pair<WebBrowserUrlProvider, Url> browserUrlProvider = WebBrowserServiceImpl.getProvider(file);
final boolean isHtmlFile = HtmlUtil.isHtmlFile(file);
if (browserUrlProvider == null) {
if (file.getVirtualFile() instanceof LightVirtualFile) {
presentation.setVisible(false);
presentation.setEnabled(false);
return;
}
else {
presentation.setEnabled(isHtmlFile);
}
}
else {
presentation.setEnabled(true);
}
presentation.setVisible(true);
String text = getTemplatePresentation().getText();
String description = getTemplatePresentation().getDescription();
if (browserUrlProvider != null) {
final String customText = browserUrlProvider.first.getOpenInBrowserActionText(file);
if (customText != null) {
text = customText;
}
final String customDescription = browserUrlProvider.first.getOpenInBrowserActionDescription(file);
if (customDescription != null) {
description = customDescription;
}
if (isHtmlFile) {
description += " (hold Shift to open URL of local file)";
}
}
presentation.setText(text);
presentation.setDescription(description);
GeneralSettings settings = GeneralSettings.getInstance();
if (!settings.isUseDefaultBrowser()) {
BrowsersConfiguration.BrowserFamily family = BrowsersConfiguration.getInstance().findFamilyByPath(settings.getBrowserPath());
if (family != null) {
presentation.setIcon(family.getIcon());
}
}
if (ActionPlaces.isPopupPlace(e.getPlace())) {
presentation.setVisible(presentation.isEnabled());
}
}
@Override
public void actionPerformed(AnActionEvent e) {
DataContext dataContext = e.getDataContext();
PsiFile psiFile = CommonDataKeys.PSI_FILE.getData(dataContext);
LOG.assertTrue(psiFile != null);
InputEvent event = e.getInputEvent();
doOpen(psiFile, event != null && event.isShiftDown());
}
static void doOpen(PsiFile psiFile, boolean preferLocalUrl) {
try {
Url url = WebBrowserService.getInstance().getUrlToOpen(psiFile, preferLocalUrl);
if (url != null) {
ApplicationManager.getApplication().saveAll();
BrowserUtil.launchBrowser(url.toExternalForm());
}
}
catch (WebBrowserUrlProvider.BrowserException e1) {
Messages.showErrorDialog(e1.getMessage(), XmlBundle.message("browser.error"));
}
catch (Exception e1) {
LOG.error(e1);
}
}
}
|