root / tmp / org.txm.libs.javafx / swt / TXMJFXBrowserEditor.java @ 3166
Historique | Voir | Annoter | Télécharger (10,72 ko)
1 |
/*******************************************************************************
|
---|---|
2 |
* Copyright (c) 2003, 2016 IBM Corporation and others.
|
3 |
* All rights reserved. This program and the accompanying materials
|
4 |
* are made available under the terms of the Eclipse Public License v1.0
|
5 |
* which accompanies this distribution, and is available at
|
6 |
* http://www.eclipse.org/legal/epl-v10.html
|
7 |
*
|
8 |
* Contributors:
|
9 |
* IBM Corporation - Initial API and implementation
|
10 |
*
|
11 |
*
|
12 |
*******************************************************************************/
|
13 |
package snippet; |
14 |
|
15 |
import java.beans.PropertyChangeListener; |
16 |
import java.net.MalformedURLException; |
17 |
import java.net.URL; |
18 |
|
19 |
import org.eclipse.core.runtime.Adapters; |
20 |
import org.eclipse.core.runtime.IPath; |
21 |
import org.eclipse.core.runtime.IProgressMonitor; |
22 |
import org.eclipse.jface.action.IAction; |
23 |
import org.eclipse.jface.resource.ImageDescriptor; |
24 |
import org.eclipse.osgi.util.NLS; |
25 |
import org.eclipse.swt.graphics.Image; |
26 |
import org.eclipse.swt.widgets.Composite; |
27 |
import org.eclipse.swt.widgets.Display; |
28 |
import org.eclipse.ui.IActionBars; |
29 |
import org.eclipse.ui.IEditorDescriptor; |
30 |
import org.eclipse.ui.IEditorInput; |
31 |
import org.eclipse.ui.IEditorPart; |
32 |
import org.eclipse.ui.IEditorReference; |
33 |
import org.eclipse.ui.IEditorRegistry; |
34 |
import org.eclipse.ui.IEditorSite; |
35 |
import org.eclipse.ui.IPathEditorInput; |
36 |
import org.eclipse.ui.IWorkbenchPage; |
37 |
import org.eclipse.ui.IWorkbenchWindow; |
38 |
import org.eclipse.ui.PartInitException; |
39 |
import org.eclipse.ui.PlatformUI; |
40 |
import org.eclipse.ui.browser.IWorkbenchBrowserSupport; |
41 |
import org.eclipse.ui.internal.browser.BrowserViewer; |
42 |
import org.eclipse.ui.internal.browser.IBrowserViewerContainer; |
43 |
import org.eclipse.ui.internal.browser.ImageResource; |
44 |
import org.eclipse.ui.internal.browser.TextAction; |
45 |
import org.eclipse.ui.internal.browser.Trace; |
46 |
import org.eclipse.ui.internal.browser.WebBrowserEditorInput; |
47 |
import org.eclipse.ui.internal.browser.WebBrowserUIPlugin; |
48 |
import org.eclipse.ui.part.EditorPart; |
49 |
|
50 |
/**
|
51 |
* An integrated JFX Web browser (webkit), defined as an editor to make better use of the desktop.
|
52 |
*
|
53 |
* TODO the JFXBrowser has to implements lots of event trigger to be fully functionnal
|
54 |
*/
|
55 |
public class TXMJFXBrowserEditor extends EditorPart implements IBrowserViewerContainer { |
56 |
|
57 |
protected static final String PROPERTY_TITLE = "title"; //$NON-NLS-1$ |
58 |
|
59 |
public static final String WEB_BROWSER_EDITOR_ID = "org.txm.rcp.editors.TXMJFXBrowserEditor"; //$NON-NLS-1$ |
60 |
|
61 |
protected JFXBrowserViewer webBrowser;
|
62 |
|
63 |
protected String initialURL; |
64 |
|
65 |
protected Image image; |
66 |
|
67 |
protected TextAction cutAction; |
68 |
|
69 |
protected TextAction copyAction; |
70 |
|
71 |
protected TextAction pasteAction; |
72 |
|
73 |
private boolean disposed; |
74 |
|
75 |
private boolean lockName; |
76 |
|
77 |
/**
|
78 |
* WebBrowserEditor constructor comment.
|
79 |
*/
|
80 |
public TXMJFXBrowserEditor() {
|
81 |
super();
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void createPartControl(Composite parent) { |
86 |
WebBrowserEditorInput input = getWebBrowserEditorInput(); |
87 |
|
88 |
int style = 0; |
89 |
if (input == null || input.isLocationBarLocal()) { |
90 |
style += BrowserViewer.LOCATION_BAR; |
91 |
} |
92 |
if (input == null || input.isToolbarLocal()) { |
93 |
style += BrowserViewer.BUTTON_BAR; |
94 |
} |
95 |
webBrowser = new JFXBrowserViewer(parent, style);
|
96 |
|
97 |
webBrowser.setURL(initialURL); |
98 |
webBrowser.setContainer(this);
|
99 |
|
100 |
if (input == null || input.isLocationBarLocal()) { |
101 |
// cutAction = new TextAction(webBrowser, TextAction.CUT);
|
102 |
// copyAction = new TextAction(webBrowser, TextAction.COPY);
|
103 |
// pasteAction = new TextAction(webBrowser, TextAction.PASTE);
|
104 |
} |
105 |
|
106 |
if (!lockName) {
|
107 |
PropertyChangeListener propertyChangeListener = event -> {
|
108 |
if (PROPERTY_TITLE.equals(event.getPropertyName())) {
|
109 |
setPartName((String) event.getNewValue());
|
110 |
} |
111 |
}; |
112 |
webBrowser.addPropertyChangeListener(propertyChangeListener); |
113 |
} |
114 |
} |
115 |
|
116 |
@Override
|
117 |
public void setPartName(String partName) { |
118 |
super.setPartName(partName);
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
public void setTitleImage(Image titleImage) { |
123 |
super.setTitleImage(titleImage);
|
124 |
} |
125 |
|
126 |
@Override
|
127 |
public void dispose() { |
128 |
if (image != null && !image.isDisposed()) |
129 |
image.dispose(); |
130 |
image = null;
|
131 |
|
132 |
super.dispose();
|
133 |
// mark this instance as disposed to avoid stale references
|
134 |
disposed = true;
|
135 |
} |
136 |
|
137 |
public boolean isDisposed() { |
138 |
return disposed;
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
public void doSave(IProgressMonitor monitor) { |
143 |
// do nothing
|
144 |
} |
145 |
|
146 |
@Override
|
147 |
public void doSaveAs() { |
148 |
// do nothing
|
149 |
} |
150 |
|
151 |
/**
|
152 |
* Returns the copy action.
|
153 |
*
|
154 |
* @return org.eclipse.jface.action.IAction
|
155 |
*/
|
156 |
public IAction getCopyAction() {
|
157 |
return copyAction;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* Returns the cut action.
|
162 |
*
|
163 |
* @return org.eclipse.jface.action.IAction
|
164 |
*/
|
165 |
public IAction getCutAction() {
|
166 |
return cutAction;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Returns the web editor input, if available. If the input was of
|
171 |
* another type, <code>null</code> is returned.
|
172 |
*
|
173 |
* @return org.eclipse.ui.internal.browser.IWebBrowserEditorInput
|
174 |
*/
|
175 |
protected WebBrowserEditorInput getWebBrowserEditorInput() {
|
176 |
IEditorInput input = getEditorInput(); |
177 |
if (input instanceof WebBrowserEditorInput) |
178 |
return (WebBrowserEditorInput) input;
|
179 |
return null; |
180 |
} |
181 |
|
182 |
/**
|
183 |
* Returns the paste action.
|
184 |
*
|
185 |
* @return org.eclipse.jface.action.IAction
|
186 |
*/
|
187 |
public IAction getPasteAction() {
|
188 |
return pasteAction;
|
189 |
} |
190 |
|
191 |
@Override
|
192 |
public void init(IEditorSite site, IEditorInput input) throws PartInitException { |
193 |
Trace.trace(Trace.FINEST, "Opening browser: " + input); //$NON-NLS-1$ |
194 |
if (input instanceof IPathEditorInput) { |
195 |
IPathEditorInput pei = (IPathEditorInput) input; |
196 |
final IPath path = pei.getPath();
|
197 |
URL url = null; |
198 |
try {
|
199 |
if (path != null) { |
200 |
setPartName(path.lastSegment()); |
201 |
url = path.toFile().toURI().toURL(); |
202 |
} |
203 |
if (url != null) |
204 |
initialURL = url.toExternalForm(); |
205 |
} |
206 |
catch (Exception e) { |
207 |
Trace.trace(Trace.SEVERE, "Error getting URL to file"); //$NON-NLS-1$ |
208 |
} |
209 |
if (webBrowser != null) { |
210 |
if (initialURL != null) |
211 |
webBrowser.setURL(initialURL); |
212 |
site.getWorkbenchWindow().getActivePage().activate(this);
|
213 |
} |
214 |
|
215 |
if (url != null) |
216 |
setTitleToolTip(url.getFile()); |
217 |
|
218 |
Image oldImage = image;
|
219 |
ImageDescriptor id = ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER); |
220 |
image = id.createImage(); |
221 |
|
222 |
setTitleImage(image); |
223 |
if (oldImage != null && !oldImage.isDisposed()) |
224 |
oldImage.dispose(); |
225 |
// addResourceListener(file);
|
226 |
} |
227 |
else if (input instanceof WebBrowserEditorInput) { |
228 |
WebBrowserEditorInput wbei = (WebBrowserEditorInput) input; |
229 |
initialURL = null;
|
230 |
if (wbei.getURL() != null) |
231 |
initialURL = wbei.getURL().toExternalForm(); |
232 |
if (webBrowser != null) { |
233 |
webBrowser.setURL(initialURL); |
234 |
site.getWorkbenchWindow().getActivePage().activate(this);
|
235 |
} |
236 |
|
237 |
setPartName(wbei.getName()); |
238 |
setTitleToolTip(wbei.getToolTipText()); |
239 |
lockName = false;
|
240 |
|
241 |
Image oldImage = image;
|
242 |
ImageDescriptor id = wbei.getImageDescriptor(); |
243 |
image = id.createImage(); |
244 |
|
245 |
setTitleImage(image); |
246 |
if (oldImage != null && !oldImage.isDisposed()) |
247 |
oldImage.dispose(); |
248 |
} |
249 |
else {
|
250 |
IPathEditorInput pinput = Adapters.adapt(input, IPathEditorInput.class); |
251 |
if (pinput != null) { |
252 |
init(site, pinput); |
253 |
} |
254 |
else {
|
255 |
throw new PartInitException(NLS.bind(org.eclipse.ui.internal.browser.Messages.errorInvalidEditorInput, input.getName())); |
256 |
} |
257 |
} |
258 |
|
259 |
setSite(site); |
260 |
setInput(input); |
261 |
} |
262 |
|
263 |
@Override
|
264 |
public boolean isDirty() { |
265 |
return false; |
266 |
} |
267 |
|
268 |
@Override
|
269 |
public boolean isSaveAsAllowed() { |
270 |
return false; |
271 |
} |
272 |
|
273 |
/**
|
274 |
* Open the input in the internal Web browser.
|
275 |
*/
|
276 |
public static void open(WebBrowserEditorInput input) { |
277 |
IWorkbenchWindow workbenchWindow = WebBrowserUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow(); |
278 |
IWorkbenchPage page = workbenchWindow.getActivePage(); |
279 |
|
280 |
try {
|
281 |
IEditorReference[] editors = page.getEditorReferences();
|
282 |
int size = editors.length;
|
283 |
for (int i = 0; i < size; i++) { |
284 |
if (WEB_BROWSER_EDITOR_ID.equals(editors[i].getId())) {
|
285 |
IEditorPart editor = editors[i].getEditor(true);
|
286 |
if (editor != null && editor instanceof TXMJFXBrowserEditor) { |
287 |
TXMJFXBrowserEditor webEditor = (TXMJFXBrowserEditor) editor; |
288 |
WebBrowserEditorInput input2 = webEditor.getWebBrowserEditorInput(); |
289 |
if (input2 == null || input.canReplaceInput(input2)) { |
290 |
editor.init(editor.getEditorSite(), input); |
291 |
return;
|
292 |
} |
293 |
} |
294 |
} |
295 |
} |
296 |
|
297 |
page.openEditor(input, TXMJFXBrowserEditor.WEB_BROWSER_EDITOR_ID); |
298 |
} |
299 |
catch (Exception e) { |
300 |
Trace.trace(Trace.SEVERE, "Error opening Web browser", e); //$NON-NLS-1$ |
301 |
} |
302 |
} |
303 |
|
304 |
/*
|
305 |
* Asks this part to take focus within the workbench.
|
306 |
*/
|
307 |
@Override
|
308 |
public void setFocus() { |
309 |
if (webBrowser != null) |
310 |
webBrowser.setFocus(); |
311 |
} |
312 |
|
313 |
/**
|
314 |
* Close the editor correctly.
|
315 |
*/
|
316 |
@Override
|
317 |
public boolean close() { |
318 |
final boolean[] result = new boolean[1]; |
319 |
Display.getDefault() |
320 |
.asyncExec(() -> result[0] = getEditorSite().getPage().closeEditor(TXMJFXBrowserEditor.this, false)); |
321 |
return result[0]; |
322 |
} |
323 |
|
324 |
@Override
|
325 |
public IActionBars getActionBars() {
|
326 |
return getEditorSite().getActionBars();
|
327 |
} |
328 |
|
329 |
@Override
|
330 |
public void openInExternalBrowser(String url) { |
331 |
final IEditorInput input = getEditorInput();
|
332 |
final String id = getEditorSite().getId(); |
333 |
Runnable runnable = () -> doOpenExternalEditor(id, input);
|
334 |
Display display = getSite().getShell().getDisplay(); |
335 |
close(); |
336 |
display.asyncExec(runnable); |
337 |
} |
338 |
|
339 |
protected void doOpenExternalEditor(String id, IEditorInput input) { |
340 |
IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); |
341 |
String name = input.getName();
|
342 |
IEditorDescriptor[] editors = registry.getEditors(name);
|
343 |
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
344 |
|
345 |
String editorId = null; |
346 |
for (IEditorDescriptor editor : editors) {
|
347 |
if (editor.getId().equals(id))
|
348 |
continue;
|
349 |
editorId = editor.getId(); |
350 |
break;
|
351 |
} |
352 |
|
353 |
IEditorDescriptor ddesc = registry.getDefaultEditor(name); |
354 |
if (ddesc != null && ddesc.getId().equals(id)) { |
355 |
int dot = name.lastIndexOf('.'); |
356 |
String ext = name;
|
357 |
if (dot != -1) |
358 |
ext = "*." + name.substring(dot + 1); //$NON-NLS-1$ |
359 |
registry.setDefaultEditor(ext, null);
|
360 |
} |
361 |
|
362 |
if (editorId == null) { |
363 |
// no editor
|
364 |
// next check with the OS for an external editor
|
365 |
if (registry.isSystemExternalEditorAvailable(name))
|
366 |
editorId = IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID; |
367 |
} |
368 |
|
369 |
if (editorId != null) { |
370 |
try {
|
371 |
page.openEditor(input, editorId); |
372 |
return;
|
373 |
} |
374 |
catch (PartInitException e) {
|
375 |
// ignore
|
376 |
} |
377 |
} |
378 |
|
379 |
// no registered editor - open using browser support
|
380 |
try {
|
381 |
URL theURL = new URL(webBrowser.getURL()); |
382 |
IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport(); |
383 |
support.getExternalBrowser().openURL(theURL); |
384 |
} |
385 |
catch (MalformedURLException e) { |
386 |
// TODO handle this
|
387 |
} |
388 |
catch (PartInitException e) {
|
389 |
// TODO handle this
|
390 |
} |
391 |
} |
392 |
} |