]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.swt.minidesktop/src/org/argeo/minidesktop/MiniBrowser.java
Improve mini desktop clean up
[lgpl/argeo-commons.git] / swt / 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 if (addressT != null)
82 addressT.setText(event.location);
83 }
84 });
85
86 MiniDesktopSpecific.getMiniDesktopSpecific().addBrowserTitleListener(this, browser);
87 MiniDesktopSpecific.getMiniDesktopSpecific().addBrowserOpenWindowListener(this, browser);
88 return browser;
89 }
90
91 public Browser openNewBrowserWindow() {
92 if (isFullScreen()) {
93 // TODO manage multiple tabs?
94 return browser;
95 } else {
96 Shell newShell = new Shell(browser.getDisplay(), SWT.SHELL_TRIM);
97 MiniBrowser newMiniBrowser = new MiniBrowser(newShell, null, false, isAppMode());
98 newShell.setSize(defaultShellSize);
99 newShell.open();
100 return newMiniBrowser.browser;
101 }
102 }
103
104 protected boolean isFullScreen() {
105 return fullscreen;
106 }
107
108 void setUrl(String url) {
109 if (browser != null && url != null && !url.equals(browser.getUrl()))
110 browser.setUrl(url.toString());
111 }
112
113 /** Called when URL changed; to be overridden, does nothing by default. */
114 protected void urlChanged(String url) {
115 }
116
117 /** Called when title changed; to be overridden, does nothing by default. */
118 public void titleChanged(String title) {
119 }
120
121 protected Browser getBrowser() {
122 return browser;
123 }
124
125 protected boolean isAppMode() {
126 return appMode;
127 }
128
129 private static GridLayout noSpaceGridLayout(GridLayout layout) {
130 layout.horizontalSpacing = 0;
131 layout.verticalSpacing = 0;
132 layout.marginWidth = 0;
133 layout.marginHeight = 0;
134 return layout;
135 }
136
137 public static void main(String[] args) {
138 List<String> options = Arrays.asList(args);
139 if (options.contains("--help")) {
140 System.out.println("Usage: java " + MiniBrowser.class.getName().replace('.', '/') + " [OPTION] [URL]");
141 System.out.println("A minimalistic web browser Eclipse SWT Browser integration.");
142 System.out.println(" --fullscreen : take control of the whole screen (default is to run in a window)");
143 System.out.println(" --app : open without an address bar and a toolbar");
144 System.out.println(" --help : print this help and exit");
145 System.exit(1);
146 }
147 boolean fullscreen = options.contains("--fullscreen");
148 boolean appMode = options.contains("--app");
149 String url = "https://start.duckduckgo.com/";
150 if (options.size() > 0) {
151 String last = options.get(options.size() - 1);
152 if (!last.startsWith("--"))
153 url = last.trim();
154 }
155
156 Display display = Display.getCurrent() == null ? new Display() : Display.getCurrent();
157 Shell shell;
158 if (fullscreen) {
159 shell = new Shell(display, SWT.NO_TRIM);
160 shell.setFullScreen(true);
161 Rectangle bounds = display.getBounds();
162 shell.setSize(bounds.width, bounds.height);
163 } else {
164 shell = new Shell(display, SWT.SHELL_TRIM);
165 shell.setSize(defaultShellSize);
166 }
167
168 new MiniBrowser(shell, url, fullscreen, appMode) {
169
170 @Override
171 public void titleChanged(String title) {
172 shell.setText(title);
173 }
174 };
175 shell.open();
176
177 while (!shell.isDisposed()) {
178 if (!display.readAndDispatch())
179 display.sleep();
180 }
181 }
182
183 }