]> git.argeo.org Git - lgpl/argeo-commons.git/blob - rcp/org.argeo.cms.desktop/src/org/argeo/cms/desktop/mini/MiniBrowser.java
Minimal desktop utilities
[lgpl/argeo-commons.git] / rcp / org.argeo.cms.desktop / src / org / argeo / cms / desktop / mini / MiniBrowser.java
1 package org.argeo.cms.desktop.mini;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5
6 import org.eclipse.swt.SWT;
7 import org.eclipse.swt.browser.Browser;
8 import org.eclipse.swt.browser.LocationEvent;
9 import org.eclipse.swt.browser.LocationListener;
10 import org.eclipse.swt.browser.TitleListener;
11 import org.eclipse.swt.events.SelectionAdapter;
12 import org.eclipse.swt.events.SelectionEvent;
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.layout.FillLayout;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.swt.widgets.Text;
21
22 public class MiniBrowser {
23 private URL url;
24 private Text addressT;
25 private Browser browser;
26
27 public MiniBrowser(Composite parent, int style) {
28 parent.setLayout(new GridLayout());
29
30 Composite toolBar = new Composite(parent, SWT.NONE);
31 toolBar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
32 toolBar.setLayout(new FillLayout());
33 addressT = new Text(toolBar, SWT.SINGLE | SWT.BORDER);
34 // addressT.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
35 addressT.addSelectionListener(new SelectionAdapter() {
36
37 @Override
38 public void widgetDefaultSelected(SelectionEvent e) {
39 setUrl(addressT.getText().trim());
40 }
41 });
42
43 browser = new Browser(parent, SWT.WEBKIT);
44 browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
45 browser.addLocationListener(new LocationListener() {
46
47 @Override
48 public void changing(LocationEvent event) {
49 }
50
51 @Override
52 public void changed(LocationEvent event) {
53 try {
54 MiniBrowser.this.url = new URL(browser.getUrl());
55 addressT.setText(url.toString());
56 } catch (MalformedURLException e) {
57 addressT.setText(e.getMessage());
58 throw new IllegalArgumentException("Cannot interpet new URL", e);
59
60 }
61 }
62 });
63 }
64
65 public void setUrl(URL url) {
66 this.url = url;
67 if (addressT != null)
68 addressT.setText(url.toString());
69 if (browser != null)
70 browser.setUrl(url.toString());
71 }
72
73 public void setUrl(String url) {
74 try {
75 setUrl(new URL(url));
76 } catch (MalformedURLException e) {
77 // try with http
78 try {
79 setUrl(new URL("http://"+url));
80 return;
81 } catch (MalformedURLException e1) {
82 // nevermind...
83 }
84 throw new IllegalArgumentException("Cannot interpret URL " + url, e);
85 }
86 }
87
88 public void addTitleListener(TitleListener titleListener) {
89 browser.addTitleListener(titleListener);
90 }
91
92 public static void main(String[] args) {
93 Display display = Display.getCurrent() == null ? new Display() : Display.getCurrent();
94 Shell shell = new Shell(display, SWT.SHELL_TRIM);
95
96 MiniBrowser miniBrowser = new MiniBrowser(shell, SWT.NONE);
97 miniBrowser.addTitleListener(e -> shell.setText(e.title));
98 String url = args.length > 0 ? args[0] : "http://www.argeo.org";
99 miniBrowser.setUrl(url);
100
101 shell.open();
102 shell.setSize(new Point(800, 480));
103 while (!shell.isDisposed()) {
104 if (!display.readAndDispatch())
105 display.sleep();
106 }
107 }
108
109 }