]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.eclipse.ui.rcp/src/org/argeo/swt/desktop/MiniBrowser.java
The Equinox Jetty bundle should not be explicitly started anymore.
[lgpl/argeo-commons.git] / rcp / org.argeo.eclipse.ui.rcp / src / org / argeo / swt / desktop / MiniBrowser.java
1 package org.argeo.swt.desktop;
2
3 import java.util.Observable;
4 import java.util.function.BiFunction;
5
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;
21
22 /** A minimalistic web browser based on {@link Browser}. */
23 public class MiniBrowser implements BiFunction<Composite, MiniBrowser.Context, Control> {
24 @Override
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));
31 return body;
32 }
33
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() {
39
40 @Override
41 public void widgetDefaultSelected(SelectionEvent e) {
42 String url = addressT.getText().trim();
43 context.setUrl(url);
44 }
45 });
46 context.addObserver((o, v) -> addressT.setText(((Context) o).getUrl().toString()));
47 return toolBar;
48 }
49
50 public Control createBody(Composite parent, MiniBrowser.Context context) {
51 Browser browser = new Browser(parent, SWT.WEBKIT);
52 browser.addLocationListener(new LocationAdapter() {
53 @Override
54 public void changing(LocationEvent event) {
55 if (!context.getUrl().equals(event.location))
56 context.setUrl(event.location);
57 }
58 });
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());
64 });
65 return browser;
66 }
67
68 /** The observable context of this web browser. */
69 public static class Context extends Observable {
70 private String url;
71 private String title = "";
72
73 public void setUrl(String url) {
74 this.url = url;
75 System.out.println(url);
76 setChanged();
77 notifyObservers(url);
78 }
79
80 public String getUrl() {
81 return url;
82 }
83
84 public String getTitle() {
85 return title;
86 }
87
88 public void setTitle(String title) {
89 this.title = title;
90 setChanged();
91 notifyObservers(title);
92 }
93
94 }
95
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);
99
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";
105 context.setUrl(url);
106
107 shell.open();
108 shell.setSize(new Point(800, 480));
109 while (!shell.isDisposed()) {
110 if (!display.readAndDispatch())
111 display.sleep();
112 }
113 }
114
115 }