1 package org
.argeo
.minidesktop
;
3 import java
.util
.Arrays
;
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
;
25 /** A very minimalistic web browser based on {@link Browser}. */
26 public class MiniBrowser
{
27 private static Point defaultShellSize
= new Point(800, 480);
29 private Browser browser
;
30 private Text addressT
;
32 private final boolean fullscreen
;
33 private final boolean appMode
;
35 public MiniBrowser(Composite composite
, String url
, boolean fullscreen
, boolean appMode
) {
36 this.fullscreen
= fullscreen
;
37 this.appMode
= appMode
;
42 public Control
createUi(Composite parent
) {
43 parent
.setLayout(noSpaceGridLayout(new GridLayout()));
45 Control toolBar
= createToolBar(parent
);
46 toolBar
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
48 Control body
= createBody(parent
);
49 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
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() {
60 public void widgetDefaultSelected(SelectionEvent e
) {
61 setUrl(addressT
.getText().trim());
67 protected Control
createBody(Composite parent
) {
68 browser
= new Browser(parent
, SWT
.NONE
);
70 browser
.addKeyListener(new KeyAdapter() {
72 public void keyPressed(KeyEvent e
) {
73 if (e
.keyCode
== 0x77 && e
.stateMask
== 0x40000) {// Ctrl+W
74 browser
.getShell().dispose();
78 browser
.addLocationListener(new LocationAdapter() {
80 public void changed(LocationEvent event
) {
82 addressT
.setText(event
.location
);
86 MiniDesktopSpecific
.getMiniDesktopSpecific().addBrowserTitleListener(this, browser
);
87 MiniDesktopSpecific
.getMiniDesktopSpecific().addBrowserOpenWindowListener(this, browser
);
91 public Browser
openNewBrowserWindow() {
93 // TODO manage multiple tabs?
96 Shell newShell
= new Shell(browser
.getDisplay(), SWT
.SHELL_TRIM
);
97 MiniBrowser newMiniBrowser
= new MiniBrowser(newShell
, null, false, isAppMode());
98 newShell
.setSize(defaultShellSize
);
100 return newMiniBrowser
.browser
;
104 protected boolean isFullScreen() {
108 void setUrl(String url
) {
109 if (browser
!= null && url
!= null && !url
.equals(browser
.getUrl()))
110 browser
.setUrl(url
.toString());
113 /** Called when URL changed; to be overridden, does nothing by default. */
114 protected void urlChanged(String url
) {
117 /** Called when title changed; to be overridden, does nothing by default. */
118 public void titleChanged(String title
) {
121 protected Browser
getBrowser() {
125 protected boolean isAppMode() {
129 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
130 layout
.horizontalSpacing
= 0;
131 layout
.verticalSpacing
= 0;
132 layout
.marginWidth
= 0;
133 layout
.marginHeight
= 0;
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");
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("--"))
156 Display display
= Display
.getCurrent() == null ?
new Display() : Display
.getCurrent();
159 shell
= new Shell(display
, SWT
.NO_TRIM
);
160 shell
.setFullScreen(true);
161 Rectangle bounds
= display
.getBounds();
162 shell
.setSize(bounds
.width
, bounds
.height
);
164 shell
= new Shell(display
, SWT
.SHELL_TRIM
);
165 shell
.setSize(defaultShellSize
);
168 new MiniBrowser(shell
, url
, fullscreen
, appMode
) {
171 public void titleChanged(String title
) {
172 shell
.setText(title
);
177 while (!shell
.isDisposed()) {
178 if (!display
.readAndDispatch())