-
Notifications
You must be signed in to change notification settings - Fork 821
/
WebBrowser.java
293 lines (265 loc) · 12.4 KB
/
WebBrowser.java
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.netbeans.modules.web.browser.api;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.net.URL;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;
import javax.swing.ImageIcon;
import javax.swing.event.ChangeListener;
import org.netbeans.api.annotations.common.StaticResource;
import org.netbeans.api.project.Project;
import org.netbeans.core.IDESettings;
import org.netbeans.modules.extbrowser.ExtWebBrowser;
import org.netbeans.modules.web.browser.spi.BrowserURLMapperImplementation;
import org.netbeans.modules.web.common.api.WebUtils;
import org.openide.awt.HtmlBrowser;
import org.openide.filesystems.FileObject;
import org.openide.util.ChangeSupport;
import org.openide.util.ImageUtilities;
import org.openide.util.NbPreferences;
import org.openide.util.RequestProcessor;
/**
* Single browser registered in the IDE.
*/
public final class WebBrowser {
private WebBrowserFactoryDescriptor factoryDesc;
private Preferences prefs = null;
private ChangeSupport changeSupport = new ChangeSupport(this);
private static RequestProcessor RP = new RequestProcessor();
WebBrowser(WebBrowserFactoryDescriptor factoryDesc) {
this.factoryDesc = factoryDesc;
}
private WebBrowser() {
refreshDelegate();
addListener();
}
private void refreshDelegate() {
WebBrowser ideBrowser = WebBrowsers.getInstance().getPreferred();
this.factoryDesc = new WebBrowserFactoryDescriptor(
ideBrowser.getFactoryDesc(), WebBrowsers.DEFAULT, Bundle.WebBrowsers_idebrowser());
changeSupport.fireChange();
}
private void addListener() {
assert prefs == null;
prefs = NbPreferences.forModule(IDESettings.class);
prefs.addPreferenceChangeListener(new PreferenceChangeListener() {
@Override
public void preferenceChange(PreferenceChangeEvent evt) {
if (IDESettings.PROP_WWWBROWSER.equals(evt.getKey())) {
RP.post(new Runnable() {
@Override
public void run() {
refreshDelegate();
}
});
}
}
});
if (factoryDesc.getFactory() instanceof ExtWebBrowser) {
ExtWebBrowser fa = (ExtWebBrowser)factoryDesc.getFactory();
fa.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals(ExtWebBrowser.PROP_PRIVATE_BROWSER_FAMILY)) {
RP.post(new Runnable() {
@Override
public void run() {
refreshDelegate();
}
});
}
}
});
}
}
static WebBrowser createIDEGlobalDelegate() {
return new WebBrowser();
}
/**
* Unique ID of browser. Useful for example to store per project reference to
* user's browser choice.
*/
public String getId() {
return factoryDesc.getId();
}
public boolean hasNetBeansIntegration() {
return factoryDesc.hasNetBeansIntegration();
}
/**
* Name eg. FireFox, WebView, ...
*
* @return
*/
public String getName() {
return factoryDesc.getName();
}
public Image getIconImage(boolean small) {
Image im = factoryDesc.getIconImage(small);
if (im == null) {
ImageIcon icon = ImageUtilities.loadImageIcon( getIconFile(getBrowserFamily(), small), true );
im = ImageUtilities.icon2Image( icon );
}
if (hasNetBeansIntegration() && factoryDesc.getBrowserFamily() != BrowserFamilyId.JAVAFX_WEBVIEW) {
// im = ImageUtilities.mergeImages(
// im,
// ImageUtilities.loadImage("org/netbeans/modules/web/browser/ui/resources/nb-badge.png"),
// 12, 12);
}
return im;
}
public BrowserFamilyId getBrowserFamily() {
return factoryDesc.getBrowserFamily();
}
/**
* Is IDE embedded browser or external browser.
*/
public boolean isEmbedded() {
return getBrowserFamily() == BrowserFamilyId.JAVAFX_WEBVIEW;
}
private static Map<Project, BrowserURLMapperImplementation.BrowserURLMapper> browserMappings =
new WeakHashMap<>();
/**
* Let browser implementation convert given URL into a browser specific URL
* before opening it in browser. For example Android device Chrome browser
* converts localhost URL into IP address so that Android device can access
* the locahost.
* @return converted or original URL
*/
public URL toBrowserURL(Project p, FileObject projectFile, URL serverURL) {
BrowserURLMapperImplementation impl = factoryDesc.getBrowserURLMapper();
if (impl != null) {
BrowserURLMapperImplementation.BrowserURLMapper m = impl.toBrowser(p, projectFile, serverURL);
if (m != null) {
browserMappings.put(p, m);
String url = WebUtils.urlToString(serverURL);
if (url.startsWith(m.getServerURLRoot())) {
url = m.getBrowserURLRoot() + url.substring(m.getServerURLRoot().length());
return WebUtils.stringToUrl(url);
}
} else {
browserMappings.remove(p);
}
}
return serverURL;
}
/**
* Let browser implementation convert given browser URL into an URL which
* can be translated back to project's source file. This is counter part for
* {@link #toBrowserURL} method which translates browser specific URL back
* to a URL which can be translated into project's source file.
* @return converted or original URL
*/
public URL fromBrowserURL(Project p, URL serverURL) {
BrowserURLMapperImplementation.BrowserURLMapper m = browserMappings.get(p);
if (m != null) {
String url = WebUtils.urlToString(serverURL);
if (url.startsWith(m.getBrowserURLRoot())) {
url = m.getServerURLRoot()+ url.substring(m.getBrowserURLRoot().length());
}
return WebUtils.stringToUrl(url);
}
return serverURL;
}
/**
* This methods creates new browser "pane", that is tab in external browser
* or TopComponent for embedded browser. Through this method clients have control
* how many browser panes are opened. In case of embedded browser it is
* straightforward - each call of this method will result into a new TopComponent.
* In case of external browser situation depends on availability of NetBeans
* browser plugins. If browser plugins are available then the same behaviour as
* in the case of embedded browser is possible and user can via this method
* create multiple tabs in external browser or keep single tab and open all
* URLs in the single tab.
*/
public WebBrowserPane createNewBrowserPane() {
return createNewBrowserPane(true);
}
public WebBrowserPane createNewBrowserPane(WebBrowserFeatures features) {
return createNewBrowserPane(features, true);
}
/**
* The only difference from createNewBrowserPane() is that automatic TopComponent
* creation in case of embedded browser can be prevented by setting
* wrapEmbeddedBrowserInTopComponent to false. Doing that means that client
* of WebBrowserPane must call WebBrowserPane.getComponent method and
* take care about showing browser component in IDE. This is useful for example
* in case when HTML file editor has multiview and one of its tabs is "Preview"
* showing rendered view of the HTML document.
*/
public WebBrowserPane createNewBrowserPane(boolean wrapEmbeddedBrowserInTopComponent) {
return createNewBrowserPane(new WebBrowserFeatures(), wrapEmbeddedBrowserInTopComponent);
}
public WebBrowserPane createNewBrowserPane(WebBrowserFeatures features, boolean wrapEmbeddedBrowserInTopComponent) {
return new WebBrowserPane(features, factoryDesc, wrapEmbeddedBrowserInTopComponent);
}
/**
* Retrieve HTMLBrowser factory wrapped in this instance.
* @return HtmlBrowser factory.
*/
public HtmlBrowser.Factory getHtmlBrowserFactory() {
return factoryDesc.getFactory();
}
WebBrowserFactoryDescriptor getFactoryDesc() {
return factoryDesc;
}
private static final @StaticResource String CHROME_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_chrome_16x.png"; // NOI18N
private static final @StaticResource String CHROME_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_chrome_24x.png"; // NOI18N
private static final @StaticResource String FIREFOX_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_firefox_16x.png"; // NOI18N
private static final @StaticResource String FIREFOX_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_firefox_24x.png"; // NOI18N
private static final @StaticResource String CHROMIUM_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_chromium_16x.png"; // NOI18N
private static final @StaticResource String CHROMIUM_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_chromium_24x.png"; // NOI18N
private static final @StaticResource String IE_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_ie_16x.png"; // NOI18N
private static final @StaticResource String IE_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_ie_24x.png"; // NOI18N
private static final @StaticResource String EDGE_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_edge_16x.png"; // NOI18N
private static final @StaticResource String EDGE_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_edge_24x.png"; // NOI18N
private static final @StaticResource String SAFARI_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_safari_16x.png"; // NOI18N
private static final @StaticResource String SAFARI_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_safari_24x.png"; // NOI18N
private static final @StaticResource String GENERIC_SMALL = "org/netbeans/modules/web/browser/ui/resources/browser_generic_16x.png"; // NOI18N
private static final @StaticResource String GENERIC_LARGE = "org/netbeans/modules/web/browser/ui/resources/browser_generic_24x.png"; // NOI18N
private static String getIconFile(BrowserFamilyId browserFamily, boolean small) {
switch (browserFamily) {
case CHROME:
return small ? CHROME_SMALL : CHROME_LARGE;
case FIREFOX:
return small ? FIREFOX_SMALL : FIREFOX_LARGE;
case CHROMIUM:
return small ? CHROMIUM_SMALL : CHROMIUM_LARGE;
case IE:
return small ? IE_SMALL : IE_LARGE;
case SAFARI:
return small ? SAFARI_SMALL : SAFARI_LARGE;
case EDGE:
return small ? EDGE_SMALL : EDGE_LARGE;
default:
return small ? GENERIC_SMALL : GENERIC_LARGE;
}
}
public void addChangeListener(ChangeListener l) {
changeSupport.addChangeListener(l);
}
public void removeChangeListener(ChangeListener l) {
changeSupport.removeChangeListener(l);
}
}