]> git.argeo.org Git - gpl/argeo-slc.git/blob - MiniBrowser.java
d0ac4ee368facf84d1080c88ca675813d5ab8cc4
[gpl/argeo-slc.git] / MiniBrowser.java
1 package org.argeo.swt.desktop;
2
3 import org.eclipse.swt.SWT;
4 import org.eclipse.swt.browser.Browser;
5 import org.eclipse.swt.browser.LocationAdapter;
6 import org.eclipse.swt.browser.LocationEvent;
7 import org.eclipse.swt.events.SelectionAdapter;
8 import org.eclipse.swt.events.SelectionEvent;
9 import org.eclipse.swt.graphics.Point;
10 import org.eclipse.swt.layout.FillLayout;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Display;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.swt.widgets.Text;
18
19 /** A minimalistic web browser based on {@link Browser}. */
20 public class MiniBrowser {
21 private Browser browser;
22 private Text addressT;
23
24 public MiniBrowser(Composite composite, String url) {
25 createUi(composite);
26 setUrl(url);
27 }
28
29 public Control createUi(Composite parent) {
30 parent.setLayout(noSpaceGridLayout(new GridLayout()));
31 Control toolBar = createToolBar(parent);
32 toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
33 Control body = createBody(parent);
34 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
35 return body;
36 }
37
38 public Control createToolBar(Composite parent) {
39 Composite toolBar = new Composite(parent, SWT.NONE);
40 toolBar.setLayout(new FillLayout());
41 addressT = new Text(toolBar, SWT.SINGLE);
42 addressT.addSelectionListener(new SelectionAdapter() {
43
44 @Override
45 public void widgetDefaultSelected(SelectionEvent e) {
46 setUrl(addressT.getText().trim());
47 }
48 });
49 return toolBar;
50 }
51
52 public Control createBody(Composite parent) {
53 browser = new Browser(parent, SWT.NONE);
54 browser.addLocationListener(new LocationAdapter() {
55 @Override
56 public void changed(LocationEvent event) {
57 addressT.setText(event.location);
58 }
59
60 });
61 browser.addTitleListener(e -> titleChanged(e.title));
62 return browser;
63 }
64
65 void setUrl(String url) {
66 if (browser != null && url != null && !url.equals(browser.getUrl()))
67 browser.setUrl(url.toString());
68 }
69
70 /** Called when URL changed; to be overridden, does nothing by default. */
71 protected void urlChanged(String url) {
72 }
73
74 /** Called when title changed; to be overridden, does nothing by default. */
75 protected void titleChanged(String title) {
76 }
77
78 private static GridLayout noSpaceGridLayout(GridLayout layout) {
79 layout.horizontalSpacing = 0;
80 layout.verticalSpacing = 0;
81 layout.marginWidth = 0;
82 layout.marginHeight = 0;
83 return layout;
84 }
85
86 public static void main(String[] args) {
87 Display display = Display.getCurrent() == null ? new Display() : Display.getCurrent();
88 Shell shell = new Shell(display, SWT.SHELL_TRIM);
89
90 String url = args.length > 0 ? args[0] : "https://duckduckgo.com/";
91 new MiniBrowser(shell, url) {
92
93 @Override
94 protected void titleChanged(String title) {
95 shell.setText(title);
96 }
97 };
98 shell.open();
99 shell.setSize(new Point(800, 480));
100
101 while (!shell.isDisposed()) {
102 if (!display.readAndDispatch())
103 display.sleep();
104 }
105 }
106
107 }