28
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Ported to the D Programming Language:
|
|
11 * by John Reimer
|
|
12 *******************************************************************************/
|
|
13 module Snippet128;
|
|
14
|
|
15 /*
|
|
16 * Browser example snippet: bring up a browser
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 *
|
|
21 * @since 3.0
|
|
22 */
|
|
23
|
|
24 version(linux)
|
|
25 {
|
|
26 version(build)
|
|
27 {
|
|
28 pragma(link,"stdc++");
|
|
29 pragma(link,"xpcomglue");
|
|
30 }
|
|
31 }
|
|
32
|
|
33 import tango.io.Console;
|
|
34 import java.lang.all;
|
|
35
|
|
36 import org.eclipse.swt.SWT;
|
|
37 import org.eclipse.swt.DWTError;
|
|
38 import org.eclipse.swt.DWTException;
|
|
39
|
|
40 import org.eclipse.swt.browser.Browser;
|
|
41 import org.eclipse.swt.browser.ProgressEvent;
|
|
42 import org.eclipse.swt.browser.ProgressListener;
|
|
43 import org.eclipse.swt.browser.StatusTextEvent;
|
|
44 import org.eclipse.swt.browser.StatusTextListener;
|
|
45 import org.eclipse.swt.browser.LocationEvent;
|
|
46 import org.eclipse.swt.browser.LocationListener;
|
|
47
|
|
48 import org.eclipse.swt.layout.GridLayout;
|
|
49 import org.eclipse.swt.layout.GridData;
|
|
50
|
|
51 import org.eclipse.swt.widgets.Shell;
|
|
52 import org.eclipse.swt.widgets.Display;
|
|
53 import org.eclipse.swt.widgets.ToolBar;
|
|
54 import org.eclipse.swt.widgets.ToolItem;
|
|
55 import org.eclipse.swt.widgets.Label;
|
|
56 import org.eclipse.swt.widgets.Text;
|
|
57 import org.eclipse.swt.widgets.ProgressBar;
|
|
58 import org.eclipse.swt.widgets.Listener;
|
|
59 import org.eclipse.swt.widgets.Event;
|
|
60
|
|
61 public static void main(String [] args) {
|
|
62 Display display = new Display();
|
|
63 final Shell shell = new Shell(display);
|
|
64 GridLayout gridLayout = new GridLayout();
|
|
65 gridLayout.numColumns = 3;
|
|
66 shell.setLayout(gridLayout);
|
|
67 ToolBar toolbar = new ToolBar(shell, SWT.NONE);
|
|
68 ToolItem itemBack = new ToolItem(toolbar, SWT.PUSH);
|
|
69 itemBack.setText("Back");
|
|
70 ToolItem itemForward = new ToolItem(toolbar, SWT.PUSH);
|
|
71 itemForward.setText("Forward");
|
|
72 ToolItem itemStop = new ToolItem(toolbar, SWT.PUSH);
|
|
73 itemStop.setText("Stop");
|
|
74 ToolItem itemRefresh = new ToolItem(toolbar, SWT.PUSH);
|
|
75 itemRefresh.setText("Refresh");
|
|
76 ToolItem itemGo = new ToolItem(toolbar, SWT.PUSH);
|
|
77 itemGo.setText("Go");
|
|
78
|
|
79 GridData data = new GridData();
|
|
80 data.horizontalSpan = 3;
|
|
81 toolbar.setLayoutData(data);
|
|
82
|
|
83 Label labelAddress = new Label(shell, SWT.NONE);
|
|
84 labelAddress.setText("Address");
|
|
85
|
|
86 final Text location = new Text(shell, SWT.BORDER);
|
|
87 data = new GridData();
|
|
88 data.horizontalAlignment = GridData.FILL;
|
|
89 data.horizontalSpan = 2;
|
|
90 data.grabExcessHorizontalSpace = true;
|
|
91 location.setLayoutData(data);
|
|
92
|
|
93 Browser browser;
|
|
94 try {
|
|
95 browser = new Browser(shell, SWT.NONE);
|
|
96 } catch (DWTError e) {
|
|
97 Cout ("Could not instantiate Browser: " ~ e.getMessage());
|
|
98 return;
|
|
99 }
|
|
100 data = new GridData();
|
|
101 data.horizontalAlignment = GridData.FILL;
|
|
102 data.verticalAlignment = GridData.FILL;
|
|
103 data.horizontalSpan = 3;
|
|
104 data.grabExcessHorizontalSpace = true;
|
|
105 data.grabExcessVerticalSpace = true;
|
|
106 browser.setLayoutData(data);
|
|
107
|
|
108 Label status = new Label(shell, SWT.NONE);
|
|
109 data = new GridData(GridData.FILL_HORIZONTAL);
|
|
110 data.horizontalSpan = 2;
|
|
111 status.setLayoutData(data);
|
|
112
|
|
113 ProgressBar progressBar = new ProgressBar(shell, SWT.NONE);
|
|
114 data = new GridData();
|
|
115 data.horizontalAlignment = GridData.END;
|
|
116 progressBar.setLayoutData(data);
|
|
117
|
|
118 /* event handling */
|
|
119 Listener listener = new class() Listener {
|
|
120 public void handleEvent(Event event) {
|
|
121 ToolItem item = cast(ToolItem)event.widget;
|
|
122 String string = item.getText();
|
|
123 if (string.equals("Back")) browser.back();
|
|
124 else if (string.equals("Forward")) browser.forward();
|
|
125 else if (string.equals("Stop")) browser.stop();
|
|
126 else if (string.equals("Refresh")) browser.refresh();
|
|
127 else if (string.equals("Go")) browser.setUrl(location.getText());
|
|
128 }
|
|
129 };
|
|
130 browser.addProgressListener(new class() ProgressListener {
|
|
131 public void changed(ProgressEvent event) {
|
|
132 if (event.total == 0) return;
|
|
133 int ratio = event.current * 100 / event.total;
|
|
134 progressBar.setSelection(ratio);
|
|
135 }
|
|
136 public void completed(ProgressEvent event) {
|
|
137 progressBar.setSelection(0);
|
|
138 }
|
|
139 });
|
|
140 browser.addStatusTextListener(new class() StatusTextListener {
|
|
141 public void changed(StatusTextEvent event) {
|
|
142 status.setText(event.text);
|
|
143 }
|
|
144 });
|
|
145 browser.addLocationListener(new class() LocationListener {
|
|
146 public void changed(LocationEvent event) {
|
|
147 if (event.top) location.setText(event.location);
|
|
148 }
|
|
149 public void changing(LocationEvent event) {
|
|
150 }
|
|
151 });
|
|
152 itemBack.addListener(SWT.Selection, listener);
|
|
153 itemForward.addListener(SWT.Selection, listener);
|
|
154 itemStop.addListener(SWT.Selection, listener);
|
|
155 itemRefresh.addListener(SWT.Selection, listener);
|
|
156 itemGo.addListener(SWT.Selection, listener);
|
|
157 location.addListener(SWT.DefaultSelection, new class() Listener {
|
|
158 public void handleEvent(Event e) {
|
|
159 browser.setUrl(location.getText());
|
|
160 }
|
|
161 });
|
|
162
|
|
163 shell.open();
|
|
164 browser.setUrl("http://eclipse.org");
|
|
165
|
|
166 while (!shell.isDisposed()) {
|
|
167 if (!display.readAndDispatch())
|
|
168 display.sleep();
|
|
169 }
|
|
170 display.dispose();
|
|
171 }
|
|
172
|