]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.swt.minidesktop/src/org/argeo/minidesktop/MiniBrowser.java
Work on typing
[lgpl/argeo-commons.git] / rcp / org.argeo.swt.minidesktop / src / org / argeo / minidesktop / MiniBrowser.java
1 package org.argeo.minidesktop;
2
3 import java.util.Arrays;
4 import java.util.List;
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.KeyAdapter;
11 import org.eclipse.swt.events.KeyEvent;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.graphics.Point;
15 import org.eclipse.swt.graphics.Rectangle;
16 import org.eclipse.swt.layout.FillLayout;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Display;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.swt.widgets.Text;
24
25 /** A very minimalistic web browser based on {@link Browser}. */
26 public class MiniBrowser {
27 private static Point defaultShellSize = new Point(800, 480);
28
29 private Browser browser;
30 private Text addressT;
31
32 private final boolean fullscreen;
33 private final boolean appMode;
34
35 public MiniBrowser(Composite composite, String url, boolean fullscreen, boolean appMode) {
36 this.fullscreen = fullscreen;
37 this.appMode = appMode;
38 createUi(composite);
39 setUrl(url);
40 }
41
42 public Control createUi(Composite parent) {
43 parent.setLayout(noSpaceGridLayout(new GridLayout()));
44 if (!isAppMode()) {
45 Control toolBar = createToolBar(parent);
46 toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
47 }
48 Control body = createBody(parent);
49 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
50 return body;
51 }
52
53 protected Control createToolBar(Composite parent) {
54 Composite toolBar = new Composite(parent, SWT.NONE);
55 toolBar.setLayout(new FillLayout());
56 addressT = new Text(toolBar, SWT.SINGLE);
57 addressT.addSelectionListener(new SelectionAdapter() {
58
59 @Override
60 public void widgetDefaultSelected(SelectionEvent e) {
61 setUrl(addressT.getText().trim());
62 }
63 });
64 return toolBar;
65 }
66
67 protected Control createBody(Composite parent) {
68 browser = new Browser(parent, SWT.NONE);
69 if (isFullScreen())
70 browser.addKeyListener(new KeyAdapter() {
71 @Override
72 public void keyPressed(KeyEvent e) {
73 if (e.keyCode == 0x77 && e.stateMask == 0x40000) {// Ctrl+W
74 browser.getShell().dispose();
75 }
76 }
77 });
78 browser.addLocationListener(new LocationAdapter() {
79 @Override
80 public void changed(LocationEvent event) {
81 System.out.println(event);
82 if (addressT != null)
83 addressT.setText(event.location);
84 }
85
86 });
87 browser.addTitleListener(e -> titleChanged(e.title));
88 browser.addOpenWindowListener((e) -> {
89 e.browser = openNewBrowserWindow();
90 });
91 return browser;
92 }
93
94 protected Browser openNewBrowserWindow() {
95
96 if (isFullScreen()) {
97 // TODO manage multiple tabs?
98 return browser;
99 } else {
100 Shell newShell = new Shell(browser.getDisplay(), SWT.SHELL_TRIM);
101 MiniBrowser newMiniBrowser = new MiniBrowser(newShell, null, false, isAppMode());
102 newShell.setSize(defaultShellSize);
103 newShell.open();
104 return newMiniBrowser.browser;
105 }
106 }
107
108 protected boolean isFullScreen() {
109 return fullscreen;
110 }
111
112 void setUrl(String url) {
113 if (browser != null && url != null && !url.equals(browser.getUrl()))
114 browser.setUrl(url.toString());
115 }
116
117 /** Called when URL changed; to be overridden, does nothing by default. */
118 protected void urlChanged(String url) {
119 }
120
121 /** Called when title changed; to be overridden, does nothing by default. */
122 protected void titleChanged(String title) {
123 }
124
125 protected Browser getBrowser() {
126 return browser;
127 }
128
129 protected boolean isAppMode() {
130 return appMode;
131 }
132
133 private static GridLayout noSpaceGridLayout(GridLayout layout) {
134 layout.horizontalSpacing = 0;
135 layout.verticalSpacing = 0;
136 layout.marginWidth = 0;
137 layout.marginHeight = 0;
138 return layout;
139 }
140
141 public static void main(String[] args) {
142 List<String> options = Arrays.asList(args);
143 if (options.contains("--help")) {
144 System.out.println("Usage: java " + MiniBrowser.class.getName().replace('.', '/') + " [OPTION] [URL]");
145 System.out.println("A minimalistic web browser Eclipse SWT Browser integration.");
146 System.out.println(" --fullscreen : take control of the whole screen (default is to run in a window)");
147 System.out.println(" --app : open without an address bar and a toolbar");
148 System.out.println(" --help : print this help and exit");
149 System.exit(1);
150 }
151 boolean fullscreen = options.contains("--fullscreen");
152 boolean appMode = options.contains("--app");
153 String url = "https://start.duckduckgo.com/";
154 if (options.size() > 0) {
155 String last = options.get(options.size() - 1);
156 if (!last.startsWith("--"))
157 url = last.trim();
158 }
159
160 Display display = Display.getCurrent() == null ? new Display() : Display.getCurrent();
161 Shell shell;
162 if (fullscreen) {
163 shell = new Shell(display, SWT.NO_TRIM);
164 shell.setFullScreen(true);
165 Rectangle bounds = display.getBounds();
166 shell.setSize(bounds.width, bounds.height);
167 } else {
168 shell = new Shell(display, SWT.SHELL_TRIM);
169 shell.setSize(defaultShellSize);
170 }
171
172 new MiniBrowser(shell, url, fullscreen, appMode) {
173
174 @Override
175 protected void titleChanged(String title) {
176 shell.setText(title);
177 }
178 };
179 shell.open();
180
181 while (!shell.isDisposed()) {
182 if (!display.readAndDispatch())
183 display.sleep();
184 }
185 }
186
187 }