]>
git.argeo.org Git - gpl/argeo-slc.git/blob - rcp/org.argeo.eclipse.ui.rcp/src/org/argeo/swt/desktop/MiniBrowser.java
1 package org
.argeo
.swt
.desktop
;
3 import org
.eclipse
.swt
.SWT
;
4 import org
.eclipse
.swt
.browser
.Browser
;
5 import org
.eclipse
.swt
.browser
.LocationAdapter
;
6 import org
.eclipse
.swt
.browser
.LocationEvent
;
7 import org
.eclipse
.swt
.events
.SelectionAdapter
;
8 import org
.eclipse
.swt
.events
.SelectionEvent
;
9 import org
.eclipse
.swt
.graphics
.Point
;
10 import org
.eclipse
.swt
.layout
.FillLayout
;
11 import org
.eclipse
.swt
.layout
.GridData
;
12 import org
.eclipse
.swt
.layout
.GridLayout
;
13 import org
.eclipse
.swt
.widgets
.Composite
;
14 import org
.eclipse
.swt
.widgets
.Control
;
15 import org
.eclipse
.swt
.widgets
.Display
;
16 import org
.eclipse
.swt
.widgets
.Shell
;
17 import org
.eclipse
.swt
.widgets
.Text
;
19 /** A minimalistic web browser based on {@link Browser}. */
20 public class MiniBrowser
{
21 private Browser browser
;
22 private Text addressT
;
24 public MiniBrowser(Composite composite
, String url
) {
29 public Control
createUi(Composite parent
) {
30 parent
.setLayout(noSpaceGridLayout(new GridLayout()));
31 Control toolBar
= createToolBar(parent
);
32 toolBar
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
33 Control body
= createBody(parent
);
34 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
38 public Control
createToolBar(Composite parent
) {
39 Composite toolBar
= new Composite(parent
, SWT
.NONE
);
40 toolBar
.setLayout(new FillLayout());
41 addressT
= new Text(toolBar
, SWT
.SINGLE
);
42 addressT
.addSelectionListener(new SelectionAdapter() {
45 public void widgetDefaultSelected(SelectionEvent e
) {
46 setUrl(addressT
.getText().trim());
52 public Control
createBody(Composite parent
) {
53 browser
= new Browser(parent
, SWT
.NONE
);
54 browser
.addLocationListener(new LocationAdapter() {
56 public void changed(LocationEvent event
) {
57 addressT
.setText(event
.location
);
61 browser
.addTitleListener(e
-> titleChanged(e
.title
));
65 void setUrl(String url
) {
66 if (browser
!= null && url
!= null && !url
.equals(browser
.getUrl()))
67 browser
.setUrl(url
.toString());
70 /** Called when URL changed; to be overridden, does nothing by default. */
71 protected void urlChanged(String url
) {
74 /** Called when title changed; to be overridden, does nothing by default. */
75 protected void titleChanged(String title
) {
78 private static GridLayout
noSpaceGridLayout(GridLayout layout
) {
79 layout
.horizontalSpacing
= 0;
80 layout
.verticalSpacing
= 0;
81 layout
.marginWidth
= 0;
82 layout
.marginHeight
= 0;
86 public static void main(String
[] args
) {
87 Display display
= Display
.getCurrent() == null ?
new Display() : Display
.getCurrent();
88 Shell shell
= new Shell(display
, SWT
.SHELL_TRIM
);
90 String url
= args
.length
> 0 ? args
[0] : "https://duckduckgo.com/";
91 new MiniBrowser(shell
, url
) {
94 protected void titleChanged(String title
) {
99 shell
.setSize(new Point(800, 480));
101 while (!shell
.isDisposed()) {
102 if (!display
.readAndDispatch())