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