java中嵌入浏览器代码
1 import org.eclipse.swt.SWT;
2 import org.eclipse.swt.browser.Browser;
3 import org.eclipse.swt.browser.CloseWindowListener;
4 import org.eclipse.swt.browser.LocationEvent;
5 import org.eclipse.swt.browser.LocationListener;
6 import org.eclipse.swt.browser.OpenWindowListener;
7 import org.eclipse.swt.browser.ProgressEvent;
8 import org.eclipse.swt.browser.ProgressListener;
9 import org.eclipse.swt.browser.StatusTextEvent;
10 import org.eclipse.swt.browser.StatusTextListener;
11 import org.eclipse.swt.browser.WindowEvent;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.layout.FormAttachment;
15 import org.eclipse.swt.layout.FormData;
16 import org.eclipse.swt.layout.FormLayout;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25
26 /**
27 * This class implements a web browser
28 */
29 public class TestBrowser {
30 // The "at rest" text of the throbber
31
32 private Label throbber;
33
34 private Button button;
35
36 private Combo url;
37
38 private Button button_4;
39
40 private Button button_3;
41
42 private Browser browser;
43
44 private Browser browser2;
45
46 private Label status;
47
48 private Button button_2;
49
50 private Button button_1;
51
52 private static final String AT_REST = "Ready";
53
54 private String[] urlList = new String[256];
55
56 int urlListItemCount = 0;
57
58 /**
59 * Runs the application
60 *
61 * @param location
62 * the initial location to display
63 */
64 public void run(String location) {
65 Display display = new Display();
66 Shell shell = new Shell(display);
67 shell.setText("Advanced Browser");
68 createContents(shell, location);
69 shell.open();
70 while (!shell.isDisposed()) {
71 if (!display.readAndDispatch()) {
72 display.sleep();
73 }
74 }
75 display.dispose();
76 }
77
78 /**
79 * Creates the main window's contents
80 *
81 * @param shell
82 * the main window
83 * @param location
84 * the initial location
85 */
86 public void createContents(Shell shell, String location) {
87 shell.setLayout(new FormLayout());
88
89 // Create the composite to hold the buttons and text field
90
91 Composite controls = new Composite(shell, SWT.NONE);
92 FormData data = new FormData();
93 data.top = new FormAttachment(0, 0);
94 data.left = new FormAttachment(0, 0);
95 data.right = new FormAttachment(100, 0);
96 controls.setLayoutData(data);
97
98 // Create the status bar
99
100 status = new Label(shell, SWT.NONE);
101 data = new FormData();
102 data.left = new FormAttachment(0, 0);
103 data.right = new FormAttachment(100, 0);
104 data.bottom = new FormAttachment(100, 0);
105 status.setLayoutData(data);
106
107 // Create the web browser
108
109 browser = new Browser(shell, SWT.BORDER);
110 browser.addOpenWindowListener(new OpenWindowListener() {
111 public void open(WindowEvent arg0) {
112 String urlStr = arg0.browser.getUrl();
113 int flag = 1;
114 browser.setVisible(false);
115 browser2.setVisible(true);
116 arg0.browser = browser2;
117 for (int i = 0; i < urlListItemCount; i++) {
118 if (urlList[i].equals(urlStr)) {
119 flag = 0;
120 }
121 }
122 if (flag == 1) {
123 urlList[urlListItemCount] = urlStr;
124 url.add(urlStr);
125 url.setText(urlStr);
126 urlListItemCount++;
127 }
128 }
129 });
130 data = new FormData();
131 data.top = new FormAttachment(controls);
132 data.bottom = new FormAttachment(status);
133 data.left = new FormAttachment(0, 0);
134 data.right = new FormAttachment(100, 0);
135 browser.setLayoutData(data);
136
137 // Create the web browser
138
139 browser2 = new Browser(shell, SWT.BORDER);
140 browser2.addOpenWindowListener(new OpenWindowListener() {
141 public void open(WindowEvent arg0) {
142 String urlStr = arg0.browser.getUrl();
143 int flag = 1;
144 browser2.setVisible(false);
145 browser.setVisible(true);
146 arg0.browser = browser;
147 for (int i = 0; i < urlListItemCount; i++) {
148 if (urlList[i].equals(urlStr)) {
149 flag = 0;
150 }
151 }
152 if (flag == 1) {
153 urlList[urlListItemCount] = urlStr;
154 url.add(urlStr);
155 url.setText(urlStr);
156 urlListItemCount++;
157 }
158 }
159 });
160 data = new FormData();
161 data.top = new FormAttachment(controls);
162 data.bottom = new FormAttachment(status);
163 data.left = new FormAttachment(0, 0);
164 data.right = new FormAttachment(100, 0);
165 browser2.setLayoutData(data);
166
167 // Create the controls and wire them to the browser
168
169 controls.setLayout(new GridLayout(7, false));
170
171 // Create the back button
172
173 button_1 = new Button(controls, SWT.PUSH);
174 button_1.setText("Back");
175 button_1.addSelectionListener(new SelectionAdapter() {
176 public void widgetSelected(SelectionEvent event) {
177 browser.back();
178 }
179 });
180
181 // Create the forward button
182
183 button_2 = new Button(controls, SWT.PUSH);
184 button_2.setText("Forward");
185 button_2.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent event) {
187 browser.forward();
188 }
189 });
190
191 // Create the refresh button
192
193 button_3 = new Button(controls, SWT.PUSH);
194 button_3.setText("Refresh");
195 button_3.addSelectionListener(new SelectionAdapter() {
196 public void widgetSelected(SelectionEvent event) {
197 browser.refresh();
198 }
199 });
200
201 // Create the stop button
202
203 button_4 = new Button(controls, SWT.PUSH);
204 button_4.setText("Stop");
205 button_4.addSelectionListener(new SelectionAdapter() {
206 public void widgetSelected(SelectionEvent event) {
207 browser.stop();
208 }
209 });
210
211 // Create the address entry field and set focus to it
212
213 url = new Combo(controls, SWT.ARROW_DOWN);
214 url.setLayoutData(new GridData(296, SWT.DEFAULT));
215 url.setSize(600, url.getSize().y);
216 url.setFocus();
217
218 // Create the go button
219
220 button = new Button(controls, SWT.PUSH);
221 button.setText("Go");
222 button.addSelectionListener(new SelectionAdapter() {
223 public void widgetSelected(SelectionEvent event) {
224 String urlStr = url.getText();
225 int flag = 1;
226 browser.setUrl(urlStr);
227 for (int i = 0; i < urlListItemCount; i++) {
228 if (urlList[i].equals(urlStr)) {
229 flag = 0;
230 }
231 }
232 if (flag == 1) {
233 urlList[urlListItemCount] = urlStr;
234 url.add(urlStr);
235 urlListItemCount++;
236 }
237 }
238 });
239
240 // Create the animated "throbber"
241
242 throbber = new Label(controls, SWT.NONE);
243 throbber.setText(AT_REST);
244
245 // Allow users to hit enter to go to the typed URL
246
247 shell.setDefaultButton(button);
248
249 // Add event handlers
250
251 browser.addCloseWindowListener(new AdvancedCloseWindowListener());
252 browser.addLocationListener(new AdvancedLocationListener(url));
253 browser.addProgressListener(new AdvancedProgressListener(throbber));
254 browser.addStatusTextListener(new AdvancedStatusTextListener(status));
255
256 // Go to the initial URL
257
258 if (location != null) {
259 browser.setUrl(location);
260 }
261 }
262
263 /**
264 * This class implements a CloseWindowListener for TestBrowser
265 */
266 class AdvancedCloseWindowListener implements CloseWindowListener {
267 /**
268 * Called when the parent window should be closed
269 */
270 public void close(WindowEvent event) {
271 // Close the parent window
272
273 ((Browser) event.widget).getShell().close();
274 }
275 }
276
277 /**
278 * This class implements a LocationListener for TestBrowser
279 */
280 class AdvancedLocationListener implements LocationListener {
281 // The address text box to update
282
283 private Combo location;
284
285 /**
286 * Constructs an AdvancedLocationListener
287 *
288 * @param text
289 * the address text box to update
290 */
291 public AdvancedLocationListener(Combo text) {
292 // Store the address box for updates
293
294 location = text;
295 }
296
297 /**
298 * Called before the location changes
299 *
300 * @param event
301 * the event
302 */
303 public void changing(LocationEvent event) {
304 // Show the location that's loading
305
306 location.setText("Loading " + event.location + "...");
307 }
308
309 /**
310 * Called after the location changes
311 *
312 * @param event
313 * the event
314 */
315 public void changed(LocationEvent event) {
316 // Show the loaded location
317
318 location.setText(event.location);
319 }
320 }
321
322 /**
323 * This class implements a ProgressListener for TestBrowser
324 */
325 class AdvancedProgressListener implements ProgressListener {
326 // The label on which to report progress
327
328 private Label progress;
329
330 /**
331 * Constructs an AdvancedProgressListener
332 *
333 * @param label
334 * the label on which to report progress
335 */
336 public AdvancedProgressListener(Label label) {
337 // Store the label on which to report updates
338
339 progress = label;
340 }
341
342 /**
343 * Called when progress is made
344 *
345 * @param event
346 * the event
347 */
348 public void changed(ProgressEvent event) {
349 // Avoid divide-by-zero
350
351 if (event.total != 0) {
352 // Calculate a percentage and display it
353
354 int percent = (int) (event.current / event.total);
355 progress.setText(percent + "%");
356 } else {
357 // Since we can't calculate a percent, show confusion :-)
358
359 progress.setText("???");
360 }
361 }
362
363 /**
364 * Called when load is complete
365 *
366 * @param event
367 * the event
368 */
369 public void completed(ProgressEvent event) {
370 // Reset to the "at rest" message
371
372 progress.setText(AT_REST);
373 }
374 }
375
376 /**
377 * This class implements a StatusTextListener for TestBrowser
378 */
379 class AdvancedStatusTextListener implements StatusTextListener {
380 // The label on which to report status
381
382 private Label status;
383
384 /**
385 * Constructs an AdvancedStatusTextListener
386 *
387 * @param label
388 * the label on which to report status
389 */
390 public AdvancedStatusTextListener(Label label) {
391 // Store the label on which to report status
392
393 status = label;
394 }
395
396 /**
397 * Called when the status changes
398 *
399 * @param event
400 * the event
401 */
402 public void changed(StatusTextEvent event) {
403 // Report the status
404
405 status.setText(event.text);
406 }
407 }
408
409 /**
410 * The application entry point
411 *
412 * @param args
413 * the command line arguments
414 */
415 public static void main(String[] args) {
416 new TestBrowser().run(args.length == 0 ? null : args[0]);
417 }
418 }
419
2 import org.eclipse.swt.browser.Browser;
3 import org.eclipse.swt.browser.CloseWindowListener;
4 import org.eclipse.swt.browser.LocationEvent;
5 import org.eclipse.swt.browser.LocationListener;
6 import org.eclipse.swt.browser.OpenWindowListener;
7 import org.eclipse.swt.browser.ProgressEvent;
8 import org.eclipse.swt.browser.ProgressListener;
9 import org.eclipse.swt.browser.StatusTextEvent;
10 import org.eclipse.swt.browser.StatusTextListener;
11 import org.eclipse.swt.browser.WindowEvent;
12 import org.eclipse.swt.events.SelectionAdapter;
13 import org.eclipse.swt.events.SelectionEvent;
14 import org.eclipse.swt.layout.FormAttachment;
15 import org.eclipse.swt.layout.FormData;
16 import org.eclipse.swt.layout.FormLayout;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Button;
20 import org.eclipse.swt.widgets.Combo;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25
26 /**
27 * This class implements a web browser
28 */
29 public class TestBrowser {
30 // The "at rest" text of the throbber
31
32 private Label throbber;
33
34 private Button button;
35
36 private Combo url;
37
38 private Button button_4;
39
40 private Button button_3;
41
42 private Browser browser;
43
44 private Browser browser2;
45
46 private Label status;
47
48 private Button button_2;
49
50 private Button button_1;
51
52 private static final String AT_REST = "Ready";
53
54 private String[] urlList = new String[256];
55
56 int urlListItemCount = 0;
57
58 /**
59 * Runs the application
60 *
61 * @param location
62 * the initial location to display
63 */
64 public void run(String location) {
65 Display display = new Display();
66 Shell shell = new Shell(display);
67 shell.setText("Advanced Browser");
68 createContents(shell, location);
69 shell.open();
70 while (!shell.isDisposed()) {
71 if (!display.readAndDispatch()) {
72 display.sleep();
73 }
74 }
75 display.dispose();
76 }
77
78 /**
79 * Creates the main window's contents
80 *
81 * @param shell
82 * the main window
83 * @param location
84 * the initial location
85 */
86 public void createContents(Shell shell, String location) {
87 shell.setLayout(new FormLayout());
88
89 // Create the composite to hold the buttons and text field
90
91 Composite controls = new Composite(shell, SWT.NONE);
92 FormData data = new FormData();
93 data.top = new FormAttachment(0, 0);
94 data.left = new FormAttachment(0, 0);
95 data.right = new FormAttachment(100, 0);
96 controls.setLayoutData(data);
97
98 // Create the status bar
99
100 status = new Label(shell, SWT.NONE);
101 data = new FormData();
102 data.left = new FormAttachment(0, 0);
103 data.right = new FormAttachment(100, 0);
104 data.bottom = new FormAttachment(100, 0);
105 status.setLayoutData(data);
106
107 // Create the web browser
108
109 browser = new Browser(shell, SWT.BORDER);
110 browser.addOpenWindowListener(new OpenWindowListener() {
111 public void open(WindowEvent arg0) {
112 String urlStr = arg0.browser.getUrl();
113 int flag = 1;
114 browser.setVisible(false);
115 browser2.setVisible(true);
116 arg0.browser = browser2;
117 for (int i = 0; i < urlListItemCount; i++) {
118 if (urlList[i].equals(urlStr)) {
119 flag = 0;
120 }
121 }
122 if (flag == 1) {
123 urlList[urlListItemCount] = urlStr;
124 url.add(urlStr);
125 url.setText(urlStr);
126 urlListItemCount++;
127 }
128 }
129 });
130 data = new FormData();
131 data.top = new FormAttachment(controls);
132 data.bottom = new FormAttachment(status);
133 data.left = new FormAttachment(0, 0);
134 data.right = new FormAttachment(100, 0);
135 browser.setLayoutData(data);
136
137 // Create the web browser
138
139 browser2 = new Browser(shell, SWT.BORDER);
140 browser2.addOpenWindowListener(new OpenWindowListener() {
141 public void open(WindowEvent arg0) {
142 String urlStr = arg0.browser.getUrl();
143 int flag = 1;
144 browser2.setVisible(false);
145 browser.setVisible(true);
146 arg0.browser = browser;
147 for (int i = 0; i < urlListItemCount; i++) {
148 if (urlList[i].equals(urlStr)) {
149 flag = 0;
150 }
151 }
152 if (flag == 1) {
153 urlList[urlListItemCount] = urlStr;
154 url.add(urlStr);
155 url.setText(urlStr);
156 urlListItemCount++;
157 }
158 }
159 });
160 data = new FormData();
161 data.top = new FormAttachment(controls);
162 data.bottom = new FormAttachment(status);
163 data.left = new FormAttachment(0, 0);
164 data.right = new FormAttachment(100, 0);
165 browser2.setLayoutData(data);
166
167 // Create the controls and wire them to the browser
168
169 controls.setLayout(new GridLayout(7, false));
170
171 // Create the back button
172
173 button_1 = new Button(controls, SWT.PUSH);
174 button_1.setText("Back");
175 button_1.addSelectionListener(new SelectionAdapter() {
176 public void widgetSelected(SelectionEvent event) {
177 browser.back();
178 }
179 });
180
181 // Create the forward button
182
183 button_2 = new Button(controls, SWT.PUSH);
184 button_2.setText("Forward");
185 button_2.addSelectionListener(new SelectionAdapter() {
186 public void widgetSelected(SelectionEvent event) {
187 browser.forward();
188 }
189 });
190
191 // Create the refresh button
192
193 button_3 = new Button(controls, SWT.PUSH);
194 button_3.setText("Refresh");
195 button_3.addSelectionListener(new SelectionAdapter() {
196 public void widgetSelected(SelectionEvent event) {
197 browser.refresh();
198 }
199 });
200
201 // Create the stop button
202
203 button_4 = new Button(controls, SWT.PUSH);
204 button_4.setText("Stop");
205 button_4.addSelectionListener(new SelectionAdapter() {
206 public void widgetSelected(SelectionEvent event) {
207 browser.stop();
208 }
209 });
210
211 // Create the address entry field and set focus to it
212
213 url = new Combo(controls, SWT.ARROW_DOWN);
214 url.setLayoutData(new GridData(296, SWT.DEFAULT));
215 url.setSize(600, url.getSize().y);
216 url.setFocus();
217
218 // Create the go button
219
220 button = new Button(controls, SWT.PUSH);
221 button.setText("Go");
222 button.addSelectionListener(new SelectionAdapter() {
223 public void widgetSelected(SelectionEvent event) {
224 String urlStr = url.getText();
225 int flag = 1;
226 browser.setUrl(urlStr);
227 for (int i = 0; i < urlListItemCount; i++) {
228 if (urlList[i].equals(urlStr)) {
229 flag = 0;
230 }
231 }
232 if (flag == 1) {
233 urlList[urlListItemCount] = urlStr;
234 url.add(urlStr);
235 urlListItemCount++;
236 }
237 }
238 });
239
240 // Create the animated "throbber"
241
242 throbber = new Label(controls, SWT.NONE);
243 throbber.setText(AT_REST);
244
245 // Allow users to hit enter to go to the typed URL
246
247 shell.setDefaultButton(button);
248
249 // Add event handlers
250
251 browser.addCloseWindowListener(new AdvancedCloseWindowListener());
252 browser.addLocationListener(new AdvancedLocationListener(url));
253 browser.addProgressListener(new AdvancedProgressListener(throbber));
254 browser.addStatusTextListener(new AdvancedStatusTextListener(status));
255
256 // Go to the initial URL
257
258 if (location != null) {
259 browser.setUrl(location);
260 }
261 }
262
263 /**
264 * This class implements a CloseWindowListener for TestBrowser
265 */
266 class AdvancedCloseWindowListener implements CloseWindowListener {
267 /**
268 * Called when the parent window should be closed
269 */
270 public void close(WindowEvent event) {
271 // Close the parent window
272
273 ((Browser) event.widget).getShell().close();
274 }
275 }
276
277 /**
278 * This class implements a LocationListener for TestBrowser
279 */
280 class AdvancedLocationListener implements LocationListener {
281 // The address text box to update
282
283 private Combo location;
284
285 /**
286 * Constructs an AdvancedLocationListener
287 *
288 * @param text
289 * the address text box to update
290 */
291 public AdvancedLocationListener(Combo text) {
292 // Store the address box for updates
293
294 location = text;
295 }
296
297 /**
298 * Called before the location changes
299 *
300 * @param event
301 * the event
302 */
303 public void changing(LocationEvent event) {
304 // Show the location that's loading
305
306 location.setText("Loading " + event.location + "...");
307 }
308
309 /**
310 * Called after the location changes
311 *
312 * @param event
313 * the event
314 */
315 public void changed(LocationEvent event) {
316 // Show the loaded location
317
318 location.setText(event.location);
319 }
320 }
321
322 /**
323 * This class implements a ProgressListener for TestBrowser
324 */
325 class AdvancedProgressListener implements ProgressListener {
326 // The label on which to report progress
327
328 private Label progress;
329
330 /**
331 * Constructs an AdvancedProgressListener
332 *
333 * @param label
334 * the label on which to report progress
335 */
336 public AdvancedProgressListener(Label label) {
337 // Store the label on which to report updates
338
339 progress = label;
340 }
341
342 /**
343 * Called when progress is made
344 *
345 * @param event
346 * the event
347 */
348 public void changed(ProgressEvent event) {
349 // Avoid divide-by-zero
350
351 if (event.total != 0) {
352 // Calculate a percentage and display it
353
354 int percent = (int) (event.current / event.total);
355 progress.setText(percent + "%");
356 } else {
357 // Since we can't calculate a percent, show confusion :-)
358
359 progress.setText("???");
360 }
361 }
362
363 /**
364 * Called when load is complete
365 *
366 * @param event
367 * the event
368 */
369 public void completed(ProgressEvent event) {
370 // Reset to the "at rest" message
371
372 progress.setText(AT_REST);
373 }
374 }
375
376 /**
377 * This class implements a StatusTextListener for TestBrowser
378 */
379 class AdvancedStatusTextListener implements StatusTextListener {
380 // The label on which to report status
381
382 private Label status;
383
384 /**
385 * Constructs an AdvancedStatusTextListener
386 *
387 * @param label
388 * the label on which to report status
389 */
390 public AdvancedStatusTextListener(Label label) {
391 // Store the label on which to report status
392
393 status = label;
394 }
395
396 /**
397 * Called when the status changes
398 *
399 * @param event
400 * the event
401 */
402 public void changed(StatusTextEvent event) {
403 // Report the status
404
405 status.setText(event.text);
406 }
407 }
408
409 /**
410 * The application entry point
411 *
412 * @param args
413 * the command line arguments
414 */
415 public static void main(String[] args) {
416 new TestBrowser().run(args.length == 0 ? null : args[0]);
417 }
418 }
419