]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.eclipse.ui.rcp/src/org/argeo/swt/desktop/MiniBrowser.java
1 package org
.argeo
.swt
.desktop
;
3 import java
.util
.Observable
;
4 import java
.util
.function
.BiFunction
;
6 import org
.eclipse
.swt
.SWT
;
7 import org
.eclipse
.swt
.browser
.Browser
;
8 import org
.eclipse
.swt
.browser
.LocationAdapter
;
9 import org
.eclipse
.swt
.browser
.LocationEvent
;
10 import org
.eclipse
.swt
.events
.SelectionAdapter
;
11 import org
.eclipse
.swt
.events
.SelectionEvent
;
12 import org
.eclipse
.swt
.graphics
.Point
;
13 import org
.eclipse
.swt
.layout
.FillLayout
;
14 import org
.eclipse
.swt
.layout
.GridData
;
15 import org
.eclipse
.swt
.layout
.GridLayout
;
16 import org
.eclipse
.swt
.widgets
.Composite
;
17 import org
.eclipse
.swt
.widgets
.Control
;
18 import org
.eclipse
.swt
.widgets
.Display
;
19 import org
.eclipse
.swt
.widgets
.Shell
;
20 import org
.eclipse
.swt
.widgets
.Text
;
22 /** A minimalistic web browser based on {@link Browser}. */
23 public class MiniBrowser
implements BiFunction
<Composite
, MiniBrowser
.Context
, Control
> {
25 public Control
apply(Composite parent
, MiniBrowser
.Context context
) {
26 parent
.setLayout(new GridLayout());
27 Control toolBar
= createToolBar(parent
, context
);
28 toolBar
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
29 Control body
= createBody(parent
, context
);
30 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
34 public Control
createToolBar(Composite parent
, MiniBrowser
.Context context
) {
35 Composite toolBar
= new Composite(parent
, SWT
.NONE
);
36 toolBar
.setLayout(new FillLayout());
37 Text addressT
= new Text(toolBar
, SWT
.SINGLE
| SWT
.BORDER
);
38 addressT
.addSelectionListener(new SelectionAdapter() {
41 public void widgetDefaultSelected(SelectionEvent e
) {
42 String url
= addressT
.getText().trim();
46 context
.addObserver((o
, v
) -> addressT
.setText(((Context
) o
).getUrl().toString()));
50 public Control
createBody(Composite parent
, MiniBrowser
.Context context
) {
51 Browser browser
= new Browser(parent
, SWT
.WEBKIT
);
52 browser
.addLocationListener(new LocationAdapter() {
54 public void changing(LocationEvent event
) {
55 if (!context
.getUrl().equals(event
.location
))
56 context
.setUrl(event
.location
);
59 browser
.addTitleListener(e
-> context
.setTitle(e
.title
));
60 context
.addObserver((o
, v
) -> {
61 String url
= ((Context
) o
).getUrl();
62 if (!url
.equals(browser
.getUrl()))
63 browser
.setUrl(url
.toString());
68 /** The observable context of this web browser. */
69 public static class Context
extends Observable
{
71 private String title
= "";
73 public void setUrl(String url
) {
75 System
.out
.println(url
);
80 public String
getUrl() {
84 public String
getTitle() {
88 public void setTitle(String title
) {
91 notifyObservers(title
);
96 public static void main(String
[] args
) {
97 Display display
= Display
.getCurrent() == null ?
new Display() : Display
.getCurrent();
98 Shell shell
= new Shell(display
, SWT
.SHELL_TRIM
);
100 MiniBrowser miniBrowser
= new MiniBrowser();
101 MiniBrowser
.Context context
= new MiniBrowser
.Context();
102 miniBrowser
.apply(shell
, context
);
103 context
.addObserver((o
, v
) -> shell
.setText(((Context
) o
).getTitle()));
104 String url
= args
.length
> 0 ? args
[0] : "http://www.argeo.org";
108 shell
.setSize(new Point(800, 480));
109 while (!shell
.isDisposed()) {
110 if (!display
.readAndDispatch())