root / tmp / org.txm.libs.javafx / swt / JFXBrowserViewer.java @ 3166
Historique | Voir | Annoter | Télécharger (25,42 ko)
1 |
package snippet; |
---|---|
2 |
|
3 |
import java.beans.PropertyChangeEvent; |
4 |
import java.beans.PropertyChangeListener; |
5 |
import java.io.File; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
|
9 |
import org.eclipse.core.runtime.IProgressMonitor; |
10 |
import org.eclipse.jface.action.IStatusLineManager; |
11 |
import org.eclipse.swt.SWT; |
12 |
import org.eclipse.swt.SWTError; |
13 |
import org.eclipse.swt.SWTException; |
14 |
import org.eclipse.swt.browser.Browser; |
15 |
import org.eclipse.swt.browser.LocationAdapter; |
16 |
import org.eclipse.swt.browser.LocationEvent; |
17 |
import org.eclipse.swt.browser.LocationListener; |
18 |
import org.eclipse.swt.browser.ProgressEvent; |
19 |
import org.eclipse.swt.browser.ProgressListener; |
20 |
import org.eclipse.swt.browser.VisibilityWindowAdapter; |
21 |
import org.eclipse.swt.browser.WindowEvent; |
22 |
import org.eclipse.swt.dnd.Clipboard; |
23 |
import org.eclipse.swt.events.MouseAdapter; |
24 |
import org.eclipse.swt.events.MouseEvent; |
25 |
import org.eclipse.swt.events.SelectionListener; |
26 |
import org.eclipse.swt.layout.FillLayout; |
27 |
import org.eclipse.swt.layout.GridData; |
28 |
import org.eclipse.swt.layout.GridLayout; |
29 |
import org.eclipse.swt.widgets.Combo; |
30 |
import org.eclipse.swt.widgets.Composite; |
31 |
import org.eclipse.swt.widgets.Display; |
32 |
import org.eclipse.swt.widgets.Shell; |
33 |
import org.eclipse.swt.widgets.ToolBar; |
34 |
import org.eclipse.swt.widgets.ToolItem; |
35 |
import org.eclipse.ui.PlatformUI; |
36 |
import org.eclipse.ui.internal.browser.BusyIndicator; |
37 |
import org.eclipse.ui.internal.browser.ContextIds; |
38 |
import org.eclipse.ui.internal.browser.IBrowserViewerContainer; |
39 |
import org.eclipse.ui.internal.browser.ImageResource; |
40 |
import org.eclipse.ui.internal.browser.ToolbarLayout; |
41 |
import org.eclipse.ui.internal.browser.Trace; |
42 |
import org.eclipse.ui.internal.browser.WebBrowserPreference; |
43 |
import org.eclipse.ui.internal.browser.WebBrowserUtil; |
44 |
import org.txm.rcp.commands.OpenLocalizedWebPage; |
45 |
import org.txm.rcp.swt.JFXBrowser; |
46 |
|
47 |
/**
|
48 |
* A Web browser widget. It extends the Eclipse SWT Browser widget by adding an
|
49 |
* optional toolbar complete with a URL combo box, history, back & forward, and
|
50 |
* refresh buttons.
|
51 |
* <p>
|
52 |
* Use the style bits to choose which toolbars are available within the browser
|
53 |
* composite. You can access the embedded SWT Browser directly using the
|
54 |
* getBrowser() method.
|
55 |
* </p>
|
56 |
* <p>
|
57 |
* Additional capabilities are available when used as the internal Web browser,
|
58 |
* including status text and progress on the Eclipse window's status line, or
|
59 |
* moving the toolbar capabilities up into the main toolbar.
|
60 |
* </p>
|
61 |
* <dl>
|
62 |
* <dt><b>Styles:</b></dt>
|
63 |
* <dd>LOCATION_BAR, BUTTON_BAR</dd>
|
64 |
* <dt><b>Events:</b></dt>
|
65 |
* <dd>None</dd>
|
66 |
* </dl>
|
67 |
*
|
68 |
* @since 1.0
|
69 |
*/
|
70 |
public class JFXBrowserViewer extends Composite { |
71 |
|
72 |
/**
|
73 |
* Style parameter (value 1) indicating that the URL and Go button will be
|
74 |
* on the local toolbar.
|
75 |
*/
|
76 |
public static final int LOCATION_BAR = 1 << 1; |
77 |
|
78 |
/**
|
79 |
* Style parameter (value 2) indicating that the toolbar will be available
|
80 |
* on the web browser. This style parameter cannot be used without the
|
81 |
* LOCATION_BAR style.
|
82 |
*/
|
83 |
public static final int BUTTON_BAR = 1 << 2; |
84 |
|
85 |
protected static final String PROPERTY_TITLE = "title"; //$NON-NLS-1$ |
86 |
|
87 |
private static final int MAX_HISTORY = 50; |
88 |
|
89 |
public Clipboard clipboard; |
90 |
|
91 |
public Combo combo;
|
92 |
|
93 |
protected boolean showToolbar; |
94 |
|
95 |
protected boolean showURLbar; |
96 |
|
97 |
protected ToolItem back;
|
98 |
|
99 |
protected ToolItem forward;
|
100 |
|
101 |
protected BusyIndicator busy;
|
102 |
|
103 |
protected boolean loading; |
104 |
|
105 |
protected static java.util.List<String> history; |
106 |
|
107 |
protected JFXBrowser browser;
|
108 |
|
109 |
protected JFXBrowserText text;
|
110 |
|
111 |
protected boolean newWindow; |
112 |
|
113 |
protected IBrowserViewerContainer container;
|
114 |
|
115 |
protected String title; |
116 |
|
117 |
protected int progressWorked = 0; |
118 |
|
119 |
protected List<PropertyChangeListener> propertyListeners; |
120 |
|
121 |
/**
|
122 |
* Under development - do not use
|
123 |
*/
|
124 |
public static interface ILocationListener { |
125 |
|
126 |
public void locationChanged(String url); |
127 |
|
128 |
public void historyChanged(String[] history2); |
129 |
} |
130 |
|
131 |
public ILocationListener locationListener;
|
132 |
|
133 |
/**
|
134 |
* Under development - do not use
|
135 |
*/
|
136 |
public static interface IBackNextListener { |
137 |
|
138 |
public void updateBackNextBusy(); |
139 |
} |
140 |
|
141 |
public IBackNextListener backNextListener;
|
142 |
|
143 |
/**
|
144 |
* Creates a new Web browser given its parent and a style value describing
|
145 |
* its behavior and appearance.
|
146 |
* <p>
|
147 |
* The style value is either one of the style constants defined in the class
|
148 |
* header or class <code>SWT</code> which is applicable to instances of
|
149 |
* this class, or must be built by <em>bitwise OR</em>'ing together (that
|
150 |
* is, using the <code>int</code> "|" operator) two or more of those
|
151 |
* <code>SWT</code> style constants. The class description lists the style
|
152 |
* constants that are applicable to the class. Style bits are also inherited
|
153 |
* from superclasses.
|
154 |
* </p>
|
155 |
*
|
156 |
* @param parent
|
157 |
* a composite control which will be the parent of the new
|
158 |
* instance (cannot be null)
|
159 |
* @param style
|
160 |
* the style of control to construct
|
161 |
*/
|
162 |
public JFXBrowserViewer(Composite parent, int style) { |
163 |
super(parent, SWT.NONE);
|
164 |
|
165 |
if ((style & LOCATION_BAR) != 0) |
166 |
showURLbar = true;
|
167 |
|
168 |
if ((style & BUTTON_BAR) != 0) |
169 |
showToolbar = true;
|
170 |
|
171 |
GridLayout layout = new GridLayout(); |
172 |
layout.marginHeight = 0;
|
173 |
layout.marginWidth = 0;
|
174 |
layout.horizontalSpacing = 0;
|
175 |
layout.verticalSpacing = 0;
|
176 |
layout.numColumns = 1;
|
177 |
setLayout(layout); |
178 |
setLayoutData(new GridData(GridData.FILL_BOTH));
|
179 |
clipboard = new Clipboard(parent.getDisplay()); |
180 |
|
181 |
if (showToolbar || showURLbar) {
|
182 |
Composite toolbarComp = new Composite(this, SWT.NONE); |
183 |
toolbarComp.setLayout(new ToolbarLayout());
|
184 |
toolbarComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL));
|
185 |
|
186 |
if (showToolbar)
|
187 |
createToolbar(toolbarComp); |
188 |
|
189 |
if (showURLbar)
|
190 |
createLocationBar(toolbarComp); |
191 |
|
192 |
if (showToolbar | showURLbar) {
|
193 |
busy = new BusyIndicator(toolbarComp, SWT.NONE);
|
194 |
busy.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
|
195 |
busy.addMouseListener(new MouseAdapter() { |
196 |
|
197 |
@Override
|
198 |
public void mouseDown(MouseEvent e) { |
199 |
setURL(OpenLocalizedWebPage.URL_TEXTOMETRIE); |
200 |
} |
201 |
}); |
202 |
} |
203 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, ContextIds.WEB_BROWSER);
|
204 |
} |
205 |
|
206 |
// create a new SWT Web browser widget, checking once again to make sure
|
207 |
// we can use it in this environment
|
208 |
// if (WebBrowserUtil.canUseInternalWebBrowser())
|
209 |
try {
|
210 |
this.browser = new JFXBrowser(this, SWT.NONE); |
211 |
} |
212 |
catch (SWTError e) {
|
213 |
if (e.code != SWT.ERROR_NO_HANDLES) {
|
214 |
WebBrowserUtil.openError(org.eclipse.ui.internal.browser.Messages.errorCouldNotLaunchInternalWebBrowser); |
215 |
return;
|
216 |
} |
217 |
text = new JFXBrowserText(this, this, e); |
218 |
} |
219 |
|
220 |
if (showURLbar)
|
221 |
updateHistory(); |
222 |
if (showToolbar)
|
223 |
updateBackNextBusy(); |
224 |
|
225 |
if (browser != null) { |
226 |
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
|
227 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(browser, |
228 |
ContextIds.WEB_BROWSER); |
229 |
} |
230 |
else
|
231 |
text.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
|
232 |
|
233 |
addBrowserListeners(); |
234 |
// listen();
|
235 |
} |
236 |
|
237 |
/**
|
238 |
* Returns the underlying SWT browser widget.
|
239 |
*
|
240 |
* @return the underlying browser
|
241 |
*/
|
242 |
public JFXBrowser getBrowser() {
|
243 |
return browser;
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Navigate to the home URL.
|
248 |
*/
|
249 |
public void home() { |
250 |
System.out.println("JFXBrowserViewer.home()"); |
251 |
// browser.getWebView().setH.set.setText(""); //$NON-NLS-1$
|
252 |
} |
253 |
|
254 |
/**
|
255 |
* Loads a URL.
|
256 |
*
|
257 |
* @param url
|
258 |
* the URL to be loaded
|
259 |
* @return true if the operation was successful and false otherwise.
|
260 |
* @exception IllegalArgumentException
|
261 |
* <ul>
|
262 |
* <li>ERROR_NULL_ARGUMENT - if the url is null</li>
|
263 |
* </ul>
|
264 |
* @exception SWTException
|
265 |
* <ul>
|
266 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the
|
267 |
* wrong thread</li>
|
268 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been
|
269 |
* disposed</li>
|
270 |
* </ul>
|
271 |
* @see #getURL()
|
272 |
*/
|
273 |
public void setURL(String url) { |
274 |
setURL(url, true);
|
275 |
} |
276 |
|
277 |
protected void updateBackNextBusy() { |
278 |
if (!back.isDisposed()) {
|
279 |
back.setEnabled(isBackEnabled()); |
280 |
} |
281 |
if (!forward.isDisposed()) {
|
282 |
forward.setEnabled(isForwardEnabled()); |
283 |
} |
284 |
if (!busy.isDisposed()) {
|
285 |
busy.setBusy(loading); |
286 |
} |
287 |
|
288 |
if (backNextListener != null) |
289 |
backNextListener.updateBackNextBusy(); |
290 |
} |
291 |
|
292 |
protected void updateLocation() { |
293 |
if (locationListener != null) |
294 |
locationListener.historyChanged(null);
|
295 |
|
296 |
if (locationListener != null) |
297 |
locationListener.locationChanged(null);
|
298 |
} |
299 |
|
300 |
/**
|
301 |
*
|
302 |
*/
|
303 |
private void addBrowserListeners() { |
304 |
if (browser == null) return; |
305 |
// respond to ExternalBrowserInstance StatusTextEvents events by
|
306 |
// updating the status line
|
307 |
browser.addStatusTextListener(event -> { |
308 |
// System.out.println("status: " + event.text); //$NON-NLS-1$
|
309 |
if (container != null) { |
310 |
IStatusLineManager status = container.getActionBars().getStatusLineManager(); |
311 |
status.setMessage(event.text); |
312 |
} |
313 |
}); |
314 |
|
315 |
// Add listener for new window creation so that we can instead of
|
316 |
// opening a separate
|
317 |
// new window in which the session is lost, we can instead open a new
|
318 |
// window in a new
|
319 |
// shell within the browser area thereby maintaining the session.
|
320 |
browser.addOpenWindowListener(event -> { |
321 |
Shell shell2 = new Shell(getShell(), SWT.SHELL_TRIM);
|
322 |
shell2.setLayout(new FillLayout());
|
323 |
shell2.setText(org.eclipse.ui.internal.browser.Messages.viewWebBrowserTitle); |
324 |
shell2.setImage(getShell().getImage()); |
325 |
if (event.location != null) |
326 |
shell2.setLocation(event.location); |
327 |
if (event.size != null) |
328 |
shell2.setSize(event.size); |
329 |
int style = 0; |
330 |
if (showURLbar)
|
331 |
style += LOCATION_BAR; |
332 |
if (showToolbar)
|
333 |
style += BUTTON_BAR; |
334 |
JFXBrowserViewer browser2 = new JFXBrowserViewer(shell2, style);
|
335 |
browser2.newWindow = true;
|
336 |
event.browser = null; // TODO should be browser2 |
337 |
}); |
338 |
|
339 |
browser.addVisibilityWindowListener(new VisibilityWindowAdapter() {
|
340 |
|
341 |
@Override
|
342 |
public void show(WindowEvent e) { |
343 |
Browser browser2 = (Browser) e.widget; |
344 |
if (browser2.getParent().getParent() instanceof Shell) { |
345 |
Shell shell = (Shell) browser2.getParent().getParent(); |
346 |
if (e.location != null) |
347 |
shell.setLocation(e.location); |
348 |
if (e.size != null) |
349 |
shell.setSize(shell.computeSize(e.size.x, e.size.y)); |
350 |
shell.open(); |
351 |
} |
352 |
} |
353 |
}); |
354 |
|
355 |
browser.addCloseWindowListener(event -> { |
356 |
// if shell is not null, it must be a secondary popup window,
|
357 |
// else its an editor window
|
358 |
if (newWindow)
|
359 |
getShell().dispose(); |
360 |
else
|
361 |
container.close(); |
362 |
}); |
363 |
|
364 |
browser.addProgressListener(new ProgressListener() {
|
365 |
|
366 |
@Override
|
367 |
public void changed(ProgressEvent event) { |
368 |
// System.out.println("progress: " + event.current + ", " + event.total); //$NON-NLS-1$ //$NON-NLS-2$
|
369 |
if (event.total == 0) |
370 |
return;
|
371 |
|
372 |
boolean done = (event.current == event.total);
|
373 |
|
374 |
int percentProgress = event.current * 100 / event.total; |
375 |
if (container != null) { |
376 |
IProgressMonitor monitor = container.getActionBars() |
377 |
.getStatusLineManager().getProgressMonitor(); |
378 |
if (done) {
|
379 |
monitor.done(); |
380 |
progressWorked = 0;
|
381 |
} |
382 |
else if (progressWorked == 0) { |
383 |
monitor.beginTask("", event.total); //$NON-NLS-1$ |
384 |
progressWorked = percentProgress; |
385 |
} |
386 |
else {
|
387 |
monitor.worked(event.current - progressWorked); |
388 |
progressWorked = event.current; |
389 |
} |
390 |
} |
391 |
|
392 |
if (showToolbar) {
|
393 |
if (!busy.isBusy() && !done)
|
394 |
loading = true;
|
395 |
else if (busy.isBusy() && done) // once the progress hits |
396 |
// 100 percent, done, set
|
397 |
// busy to false
|
398 |
loading = false;
|
399 |
|
400 |
// System.out.println("loading: " + loading); //$NON-NLS-1$
|
401 |
updateBackNextBusy(); |
402 |
updateHistory(); |
403 |
} |
404 |
} |
405 |
|
406 |
@Override
|
407 |
public void completed(ProgressEvent event) { |
408 |
if (container != null) { |
409 |
IProgressMonitor monitor = container.getActionBars() |
410 |
.getStatusLineManager().getProgressMonitor(); |
411 |
monitor.done(); |
412 |
} |
413 |
if (showToolbar) {
|
414 |
loading = false;
|
415 |
updateBackNextBusy(); |
416 |
updateHistory(); |
417 |
} |
418 |
} |
419 |
}); |
420 |
|
421 |
if (showURLbar) {
|
422 |
browser.addLocationListener(new LocationAdapter() {
|
423 |
|
424 |
@Override
|
425 |
public void changed(LocationEvent event) { |
426 |
if (!event.top)
|
427 |
return;
|
428 |
if (combo != null) { |
429 |
if (!"about:blank".equals(event.location)) { //$NON-NLS-1$ |
430 |
combo.setText(event.location); |
431 |
addToHistory(event.location); |
432 |
updateHistory(); |
433 |
}// else
|
434 |
// combo.setText(""); //$NON-NLS-1$
|
435 |
} |
436 |
} |
437 |
}); |
438 |
} |
439 |
|
440 |
browser.addTitleListener(event -> { |
441 |
String oldTitle = title;
|
442 |
title = event.title; |
443 |
firePropertyChangeEvent(PROPERTY_TITLE, oldTitle, title); |
444 |
}); |
445 |
} |
446 |
|
447 |
/**
|
448 |
* Add a property change listener to this instance.
|
449 |
*
|
450 |
* @param listener java.beans.PropertyChangeListener
|
451 |
*/
|
452 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
453 |
if (propertyListeners == null) |
454 |
propertyListeners = new ArrayList<>(); |
455 |
propertyListeners.add(listener); |
456 |
} |
457 |
|
458 |
/**
|
459 |
* Remove a property change listener from this instance.
|
460 |
*
|
461 |
* @param listener java.beans.PropertyChangeListener
|
462 |
*/
|
463 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
464 |
if (propertyListeners != null) |
465 |
propertyListeners.remove(listener); |
466 |
} |
467 |
|
468 |
/**
|
469 |
* Fire a property change event.
|
470 |
*/
|
471 |
protected void firePropertyChangeEvent(String propertyName, Object oldValue, Object newValue) { |
472 |
if (propertyListeners == null) |
473 |
return;
|
474 |
|
475 |
PropertyChangeEvent event = new PropertyChangeEvent(this, propertyName, oldValue, newValue); |
476 |
// Trace.trace("Firing: " + event + " " + oldValue);
|
477 |
try {
|
478 |
int size = propertyListeners.size();
|
479 |
PropertyChangeListener[] pcl = new PropertyChangeListener[size]; |
480 |
propertyListeners.toArray(pcl); |
481 |
|
482 |
for (int i = 0; i < size; i++) |
483 |
try {
|
484 |
pcl[i].propertyChange(event); |
485 |
} |
486 |
catch (Exception e) { |
487 |
// ignore
|
488 |
} |
489 |
} |
490 |
catch (Exception e) { |
491 |
// ignore
|
492 |
} |
493 |
} |
494 |
|
495 |
/**
|
496 |
* Navigate to the next session history item. Convenience method that calls
|
497 |
* the underlying SWT browser.
|
498 |
*
|
499 |
* @return <code>true</code> if the operation was successful and
|
500 |
* <code>false</code> otherwise
|
501 |
* @exception SWTException
|
502 |
* <ul>
|
503 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the
|
504 |
* wrong thread</li>
|
505 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been
|
506 |
* disposed</li>
|
507 |
* </ul>
|
508 |
* @see #back
|
509 |
*/
|
510 |
public boolean forward() { |
511 |
if (browser == null) |
512 |
return false; |
513 |
return browser.forward();
|
514 |
} |
515 |
|
516 |
/**
|
517 |
* Navigate to the previous session history item. Convenience method that
|
518 |
* calls the underlying SWT browser.
|
519 |
*
|
520 |
* @return <code>true</code> if the operation was successful and
|
521 |
* <code>false</code> otherwise
|
522 |
* @exception SWTException
|
523 |
* <ul>
|
524 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the
|
525 |
* wrong thread</li>
|
526 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been
|
527 |
* disposed</li>
|
528 |
* </ul>
|
529 |
* @see #forward
|
530 |
*/
|
531 |
public boolean back() { |
532 |
if (browser == null) |
533 |
return false; |
534 |
return browser.back();
|
535 |
} |
536 |
|
537 |
/**
|
538 |
* Returns <code>true</code> if the receiver can navigate to the previous
|
539 |
* session history item, and <code>false</code> otherwise. Convenience
|
540 |
* method that calls the underlying SWT browser.
|
541 |
*
|
542 |
* @return the receiver's back command enabled state
|
543 |
* @exception SWTException
|
544 |
* <ul>
|
545 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
|
546 |
* disposed</li>
|
547 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
|
548 |
* thread that created the receiver</li>
|
549 |
* </ul>
|
550 |
* @see #back
|
551 |
*/
|
552 |
public boolean isBackEnabled() { |
553 |
if (browser == null) |
554 |
return false; |
555 |
return browser.isBackEnabled();
|
556 |
} |
557 |
|
558 |
/**
|
559 |
* Returns <code>true</code> if the receiver can navigate to the next
|
560 |
* session history item, and <code>false</code> otherwise. Convenience
|
561 |
* method that calls the underlying SWT browser.
|
562 |
*
|
563 |
* @return the receiver's forward command enabled state
|
564 |
* @exception SWTException
|
565 |
* <ul>
|
566 |
* <li>ERROR_WIDGET_DISPOSED - if the receiver has been
|
567 |
* disposed</li>
|
568 |
* <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
|
569 |
* thread that created the receiver</li>
|
570 |
* </ul>
|
571 |
* @see #forward
|
572 |
*/
|
573 |
public boolean isForwardEnabled() { |
574 |
if (browser == null) |
575 |
return false; |
576 |
return browser.isForwardEnabled();
|
577 |
} |
578 |
|
579 |
/**
|
580 |
* Stop any loading and rendering activity. Convenience method that calls
|
581 |
* the underlying SWT browser.
|
582 |
*
|
583 |
* @exception SWTException
|
584 |
* <ul>
|
585 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the
|
586 |
* wrong thread</li>
|
587 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been
|
588 |
* disposed</li>
|
589 |
* </ul>
|
590 |
*/
|
591 |
public void stop() { |
592 |
if (browser != null) |
593 |
browser.stop(); |
594 |
} |
595 |
|
596 |
/**
|
597 |
*
|
598 |
*/
|
599 |
private boolean navigate(String url) { |
600 |
Trace.trace(Trace.FINER, "Navigate: " + url); //$NON-NLS-1$ |
601 |
if (url != null && url.equals(getURL())) { |
602 |
refresh(); |
603 |
return true; |
604 |
} |
605 |
if (browser != null) |
606 |
return browser.setUrl(url, null, new String[] { "Cache-Control: no-cache" }); //$NON-NLS-1$ |
607 |
return text.setUrl(url);
|
608 |
} |
609 |
|
610 |
/**
|
611 |
* Refresh the current page. Convenience method that calls the underlying
|
612 |
* SWT browser.
|
613 |
*
|
614 |
* @exception SWTException
|
615 |
* <ul>
|
616 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the
|
617 |
* wrong thread</li>
|
618 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been
|
619 |
* disposed</li>
|
620 |
* </ul>
|
621 |
*/
|
622 |
public void refresh() { |
623 |
if (browser != null) |
624 |
browser.refresh(); |
625 |
else
|
626 |
text.refresh(); |
627 |
try {
|
628 |
Thread.sleep(50); |
629 |
} |
630 |
catch (Exception e) { |
631 |
// ignore
|
632 |
} |
633 |
} |
634 |
|
635 |
private void setURL(String url, boolean browse) { |
636 |
Trace.trace(Trace.FINEST, "setURL: " + url + " " + browse); //$NON-NLS-1$ //$NON-NLS-2$ |
637 |
if (url == null) { |
638 |
home(); |
639 |
return;
|
640 |
} |
641 |
|
642 |
if (browse)
|
643 |
navigate(url); |
644 |
|
645 |
addToHistory(url); |
646 |
updateHistory(); |
647 |
} |
648 |
|
649 |
protected void addToHistory(String url) { |
650 |
if (history == null) |
651 |
history = WebBrowserPreference.getInternalWebBrowserHistory(); |
652 |
int found = -1; |
653 |
int size = history.size();
|
654 |
for (int i = 0; i < size; i++) { |
655 |
String s = history.get(i);
|
656 |
if (s.equals(url)) {
|
657 |
found = i; |
658 |
break;
|
659 |
} |
660 |
} |
661 |
|
662 |
if (found == -1) { |
663 |
if (size >= MAX_HISTORY)
|
664 |
history.remove(size - 1);
|
665 |
history.add(0, url);
|
666 |
WebBrowserPreference.setInternalWebBrowserHistory(history); |
667 |
} |
668 |
else if (found != 0) { |
669 |
history.remove(found); |
670 |
history.add(0, url);
|
671 |
WebBrowserPreference.setInternalWebBrowserHistory(history); |
672 |
} |
673 |
} |
674 |
|
675 |
/**
|
676 |
*
|
677 |
*/
|
678 |
@Override
|
679 |
public void dispose() { |
680 |
super.dispose();
|
681 |
|
682 |
showToolbar = false;
|
683 |
|
684 |
if (busy != null) |
685 |
busy.dispose(); |
686 |
busy = null;
|
687 |
|
688 |
browser = null;
|
689 |
text = null;
|
690 |
if (clipboard != null) |
691 |
clipboard.dispose(); |
692 |
clipboard = null;
|
693 |
|
694 |
removeSynchronizationListener(); |
695 |
} |
696 |
|
697 |
private ToolBar createLocationBar(Composite parent) { |
698 |
combo = new Combo(parent, SWT.DROP_DOWN);
|
699 |
|
700 |
updateHistory(); |
701 |
|
702 |
combo.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> { |
703 |
try {
|
704 |
if (combo.getSelectionIndex() != -1 && !combo.getListVisible()) { |
705 |
setURL(combo.getItem(combo.getSelectionIndex())); |
706 |
} |
707 |
} |
708 |
catch (Exception e1) { |
709 |
// ignore
|
710 |
} |
711 |
})); |
712 |
combo.addListener(SWT.DefaultSelection, e -> setURL(combo.getText())); |
713 |
|
714 |
ToolBar toolbar = new ToolBar(parent, SWT.FLAT);
|
715 |
|
716 |
ToolItem go = new ToolItem(toolbar, SWT.NONE);
|
717 |
go.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_GO)); |
718 |
go.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_GO)); |
719 |
go.setDisabledImage(ImageResource |
720 |
.getImage(ImageResource.IMG_DLCL_NAV_GO)); |
721 |
go.setToolTipText(org.eclipse.ui.internal.browser.Messages.actionWebBrowserGo); |
722 |
go.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> setURL(combo.getText()))); |
723 |
|
724 |
return toolbar;
|
725 |
} |
726 |
|
727 |
private ToolBar createToolbar(Composite parent) { |
728 |
ToolBar toolbar = new ToolBar(parent, SWT.FLAT);
|
729 |
|
730 |
// create back and forward actions
|
731 |
back = new ToolItem(toolbar, SWT.NONE);
|
732 |
back.setImage(ImageResource |
733 |
.getImage(ImageResource.IMG_ELCL_NAV_BACKWARD)); |
734 |
back.setHotImage(ImageResource |
735 |
.getImage(ImageResource.IMG_CLCL_NAV_BACKWARD)); |
736 |
back.setDisabledImage(ImageResource |
737 |
.getImage(ImageResource.IMG_DLCL_NAV_BACKWARD)); |
738 |
back.setToolTipText(org.eclipse.ui.internal.browser.Messages.actionWebBrowserBack); |
739 |
back.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> back())); |
740 |
|
741 |
forward = new ToolItem(toolbar, SWT.NONE);
|
742 |
forward.setImage(ImageResource |
743 |
.getImage(ImageResource.IMG_ELCL_NAV_FORWARD)); |
744 |
forward.setHotImage(ImageResource |
745 |
.getImage(ImageResource.IMG_CLCL_NAV_FORWARD)); |
746 |
forward.setDisabledImage(ImageResource |
747 |
.getImage(ImageResource.IMG_DLCL_NAV_FORWARD)); |
748 |
forward.setToolTipText(org.eclipse.ui.internal.browser.Messages.actionWebBrowserForward); |
749 |
forward.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> forward())); |
750 |
|
751 |
// create refresh, stop, and print actions
|
752 |
ToolItem stop = new ToolItem(toolbar, SWT.NONE);
|
753 |
stop.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_STOP)); |
754 |
stop.setHotImage(ImageResource |
755 |
.getImage(ImageResource.IMG_CLCL_NAV_STOP)); |
756 |
stop.setDisabledImage(ImageResource |
757 |
.getImage(ImageResource.IMG_DLCL_NAV_STOP)); |
758 |
stop.setToolTipText(org.eclipse.ui.internal.browser.Messages.actionWebBrowserStop); |
759 |
stop.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> stop())); |
760 |
|
761 |
ToolItem refresh = new ToolItem(toolbar, SWT.NONE);
|
762 |
refresh.setImage(ImageResource |
763 |
.getImage(ImageResource.IMG_ELCL_NAV_REFRESH)); |
764 |
refresh.setHotImage(ImageResource |
765 |
.getImage(ImageResource.IMG_CLCL_NAV_REFRESH)); |
766 |
refresh.setDisabledImage(ImageResource |
767 |
.getImage(ImageResource.IMG_DLCL_NAV_REFRESH)); |
768 |
refresh.setToolTipText(org.eclipse.ui.internal.browser.Messages.actionWebBrowserRefresh); |
769 |
refresh.addSelectionListener(SelectionListener.widgetSelectedAdapter(e -> refresh())); |
770 |
|
771 |
return toolbar;
|
772 |
} |
773 |
|
774 |
/**
|
775 |
* Returns the current URL. Convenience method that calls the underlying SWT
|
776 |
* browser.
|
777 |
*
|
778 |
* @return the current URL or an empty <code>String</code> if there is no
|
779 |
* current URL
|
780 |
* @exception SWTException
|
781 |
* <ul>
|
782 |
* <li>ERROR_THREAD_INVALID_ACCESS when called from the
|
783 |
* wrong thread</li>
|
784 |
* <li>ERROR_WIDGET_DISPOSED when the widget has been
|
785 |
* disposed</li>
|
786 |
* </ul>
|
787 |
* @see #setURL(String)
|
788 |
*/
|
789 |
public String getURL() { |
790 |
if (browser != null) |
791 |
return browser.getUrl();
|
792 |
return text.getUrl();
|
793 |
} |
794 |
|
795 |
@Override
|
796 |
public boolean setFocus() { |
797 |
if (browser != null) { |
798 |
browser.setFocus(); |
799 |
updateHistory(); |
800 |
return true; |
801 |
} |
802 |
return super.setFocus(); |
803 |
} |
804 |
|
805 |
/**
|
806 |
* Update the history list to the global/shared copy.
|
807 |
*/
|
808 |
protected void updateHistory() { |
809 |
if (combo == null || combo.isDisposed()) |
810 |
return;
|
811 |
|
812 |
String temp = combo.getText();
|
813 |
if (history == null) |
814 |
history = WebBrowserPreference.getInternalWebBrowserHistory(); |
815 |
|
816 |
String[] historyList = new String[history.size()]; |
817 |
history.toArray(historyList); |
818 |
combo.setItems(historyList); |
819 |
|
820 |
combo.setText(temp); |
821 |
} |
822 |
|
823 |
public IBrowserViewerContainer getContainer() {
|
824 |
return container;
|
825 |
} |
826 |
|
827 |
public void setContainer(IBrowserViewerContainer container) { |
828 |
if (container == null && this.container != null) { |
829 |
IStatusLineManager manager = this.container.getActionBars().getStatusLineManager();
|
830 |
if (manager != null) |
831 |
manager.getProgressMonitor().done(); |
832 |
} |
833 |
this.container = container;
|
834 |
} |
835 |
|
836 |
protected File file; |
837 |
|
838 |
protected long timestamp; |
839 |
|
840 |
protected Thread fileListenerThread; |
841 |
|
842 |
protected LocationListener locationListener2;
|
843 |
|
844 |
protected Object syncObject = new Object(); |
845 |
|
846 |
protected void addSynchronizationListener() { |
847 |
if (fileListenerThread != null) |
848 |
return;
|
849 |
|
850 |
fileListenerThread = new Thread("Browser file synchronization") { //$NON-NLS-1$ |
851 |
|
852 |
@Override
|
853 |
public void run() { |
854 |
while (fileListenerThread != null) { |
855 |
try {
|
856 |
Thread.sleep(2000); |
857 |
} |
858 |
catch (Exception e) { |
859 |
// ignore
|
860 |
} |
861 |
synchronized (syncObject) {
|
862 |
if (file != null && file.lastModified() != timestamp) { |
863 |
timestamp = file.lastModified(); |
864 |
Display.getDefault().syncExec(() -> refresh()); |
865 |
} |
866 |
} |
867 |
} |
868 |
} |
869 |
}; |
870 |
fileListenerThread.setDaemon(true);
|
871 |
fileListenerThread.setPriority(Thread.MIN_PRIORITY);
|
872 |
|
873 |
locationListener2 = new LocationAdapter() {
|
874 |
|
875 |
@Override
|
876 |
public void changed(LocationEvent event) { |
877 |
File temp = getFile(event.location);
|
878 |
if (temp != null && temp.exists()) { |
879 |
synchronized (syncObject) {
|
880 |
file = temp; |
881 |
timestamp = file.lastModified(); |
882 |
} |
883 |
} |
884 |
else
|
885 |
file = null;
|
886 |
} |
887 |
}; |
888 |
browser.addLocationListener(locationListener2); |
889 |
|
890 |
File temp = getFile(browser.getUrl());
|
891 |
if (temp != null && temp.exists()) { |
892 |
file = temp; |
893 |
timestamp = file.lastModified(); |
894 |
} |
895 |
fileListenerThread.start(); |
896 |
} |
897 |
|
898 |
protected static File getFile(String location) { |
899 |
if (location == null) |
900 |
return null; |
901 |
if (location.startsWith("file:/")) //$NON-NLS-1$ |
902 |
location = location.substring(6);
|
903 |
|
904 |
return new File(location); |
905 |
} |
906 |
|
907 |
protected void removeSynchronizationListener() { |
908 |
if (fileListenerThread == null) |
909 |
return;
|
910 |
|
911 |
fileListenerThread = null;
|
912 |
browser.removeLocationListener(locationListener2); |
913 |
locationListener2 = null;
|
914 |
} |
915 |
} |