2 * Copyright (c) 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM - Initial API and implementation
12 //TODO 1. Handle the sizing of a popup running in shelled out secondary window.
13 //TODO 2. Support printing: waiting on eclipse bug 47937/44823.
15 package org.eclipse.webbrowser.internal;
18 import org.eclipse.webbrowser.IURLMap;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.swt.layout.FillLayout;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.dnd.Clipboard;
25 import org.eclipse.swt.events.*;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.ui.help.WorkbenchHelp;
29 import org.eclipse.swt.browser.*;
31 public class WebBrowser extends Composite {
32 protected Composite toolbarComp;
33 protected Composite statusComp;
34 protected Combo combo;
35 protected Clipboard clipboard;
36 protected boolean showToolbar;
37 protected ToolItem back;
38 protected ToolItem forward;
39 protected ToolItem stop;
40 protected ToolItem favorites;
41 protected ToolItem refresh;
42 protected BusyIndicator busy;
43 protected boolean showStatusbar;
44 protected ProgressBar progress;
45 protected Label status;
46 private static int MAX_HISTORY = 50;
47 protected static java.util.List history;
48 protected Browser browser;
49 protected Shell shell;
50 protected WebBrowserEditor editor;
51 protected String title;
53 public WebBrowser(Composite parent, final boolean showToolbar, final boolean showStatusbar) {
54 super(parent, SWT.NONE);
56 this.showToolbar = showToolbar;
57 this.showStatusbar = showStatusbar;
59 GridLayout layout = new GridLayout();
60 layout.marginHeight = 3;
61 layout.marginWidth = 3;
62 layout.horizontalSpacing = 3;
63 layout.verticalSpacing = 3;
64 layout.numColumns = 1;
66 setLayoutData(new GridData(GridData.FILL_BOTH));
67 clipboard = new Clipboard(parent.getDisplay());
68 WorkbenchHelp.setHelp(this, ContextIds.WEB_BROWSER);
71 toolbarComp = new Composite(this, SWT.NONE);
72 GridLayout outerLayout = new GridLayout();
73 outerLayout.numColumns = 2;
74 outerLayout.marginWidth = 0;
75 outerLayout.marginHeight = 0;
76 toolbarComp.setLayout(outerLayout);
77 toolbarComp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.FILL_HORIZONTAL));
79 // create the top line, with a combo box for history and a "go" button
80 Composite top = new Composite(toolbarComp, SWT.NONE);
81 GridLayout topLayout = new GridLayout();
82 topLayout.numColumns = 2;
83 topLayout.marginWidth = 0;
84 topLayout.marginHeight = 0;
85 top.setLayout(topLayout);
86 top.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.FILL_HORIZONTAL));
88 combo = new Combo(top, SWT.DROP_DOWN);
92 combo.addSelectionListener(new SelectionAdapter() {
93 public void widgetSelected(SelectionEvent we) {
95 if (combo.getSelectionIndex() != -1)
96 setURL(combo.getItem(combo.getSelectionIndex()));
97 } catch (Exception e) { }
100 combo.addListener(SWT.DefaultSelection, new Listener() {
101 public void handleEvent(Event e) {
102 setURL(combo.getText());
105 combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
106 WorkbenchHelp.setHelp(combo, ContextIds.WEB_BROWSER_URL);
108 ToolBar toolbar = new ToolBar(top, SWT.FLAT);
109 fillToolBar(toolbar);
111 new ToolItem(toolbar, SWT.SEPARATOR);
113 busy = new BusyIndicator(toolbarComp, SWT.NONE);
114 busy.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
117 // create a new SWT Web browser widget, checking once again to make sure we can use it in this environment
118 if (WebBrowserUtil.canUseInternalWebBrowser() & WebBrowserUtil.isInternalBrowserOperational())
119 this.browser = new Browser(this, SWT.NONE);
121 WebBrowserUtil.openError(WebBrowserUIPlugin.getResource("%errorCouldNotLaunchInternalWebBrowser"));
126 back.setEnabled(browser.isBackEnabled());
127 forward.setEnabled(browser.isForwardEnabled());
130 WorkbenchHelp.setHelp(browser, ContextIds.WEB_BROWSER_WEB);
131 GridData data = new GridData();
132 data.horizontalAlignment = GridData.FILL;
133 data.verticalAlignment = GridData.FILL;
134 data.horizontalSpan = 3;
135 data.grabExcessHorizontalSpace = true;
136 data.grabExcessVerticalSpace = true;
137 browser.setLayoutData(data);
140 createStatusArea(this);
142 addBrowserListeners();
147 protected void addBrowserListeners() {
149 // respond to Browser StatusTextEvents events by updating the status Text label
150 browser.addStatusTextListener(new StatusTextListener() {
151 public void changed(StatusTextEvent event) {
152 status.setText(event.text);
157 /** Add listener for new window creation so that we can instead of opening a separate
158 * new window in which the session is lost, we can instead open a new window in a new
159 * shell within the browser area thereby maintaining the session.
161 browser.addOpenWindowListener(new OpenWindowListener() {
162 public void open(WindowEvent event) {
163 Shell shell2 = new Shell(getDisplay());
164 shell2.setLayout(new FillLayout());
165 shell2.setText(WebBrowserUIPlugin.getResource("%viewWebBrowserTitle"));
166 shell2.setImage(getShell().getImage());
167 WebBrowser browser2 = new WebBrowser(shell2, showToolbar, showStatusbar);
168 browser2.shell = shell2;
169 event.browser = browser2.browser;
174 browser.addCloseWindowListener(new CloseWindowListener(){
175 public void close(WindowEvent event) {
176 // if shell is not null, it must be a secondary popup window, else its an editor window
180 editor.closeEditor();
184 browser.addProgressListener(new ProgressListener() {
185 public void changed(ProgressEvent event) {
186 if (event.total == 0)
189 boolean done = (event.current == event.total);
191 int percentProgress = event.current * 100 / event.total;
194 progress.setSelection(0);
196 progress.setSelection(percentProgress);
200 if (!busy.isBusy() && (percentProgress > 0 && percentProgress < 100)) {
203 // Once the progress hits 100 percent, done, set busy to false
204 else if (busy.isBusy() && done) {
210 public void completed(ProgressEvent event) {
212 progress.setSelection(0);
215 back.setEnabled(browser.isBackEnabled());
216 forward.setEnabled(browser.isForwardEnabled());
222 browser.addLocationListener(new LocationListener() {
223 public void changed(LocationEvent event) {
227 combo.setText(event.location);
228 addToHistory(event.location);
234 public void changing(LocationEvent event) { }
238 browser.addTitleListener(new TitleListener() {
239 public void changed(TitleEvent event) {
246 * Return the underlying browser control.
248 * @return org.eclipse.swt.browser.Browser
250 public Browser getBrowser() {
257 protected void forward() {
264 protected void back() {
271 protected void stop() {
278 protected void navigate(String url) {
279 Trace.trace(Trace.FINER, "Navigate: " + url);
280 if (url != null && url.equals(getURL())) {
288 * Refresh the currently viewed page.
290 public void refresh() {
294 protected void setURL(String url, boolean browse) {
295 Trace.trace(Trace.FINEST, "setURL: " + url + " " + browse);
301 if (url.endsWith(WebBrowserPreference.getHomePageURL().substring(9)))
305 Iterator iterator = WebBrowserUtil.getURLMaps().iterator();
306 String newURL = null;
307 while (iterator.hasNext() && newURL == null) {
309 IURLMap map = (IURLMap) iterator.next();
310 newURL = map.getMappedURL(url);
311 } catch (Exception e) { }
323 protected void addToHistory(String url) {
325 history = WebBrowserPreference.getInternalWebBrowserHistory();
327 int size = history.size();
328 for (int i = 0; i < size; i++){
329 String s = (String) history.get(i);
337 if (size >= MAX_HISTORY)
338 history.remove(size - 1);
340 WebBrowserPreference.setInternalWebBrowserHistory(history);
341 } else if (found != 0) {
342 history.remove(found);
344 WebBrowserPreference.setInternalWebBrowserHistory(history);
348 public void setURL(String url) {
353 * Creates the Web browser status area.
355 private void createStatusArea(Composite parent) {
356 Composite composite = new Composite(parent, SWT.NONE);
357 GridLayout layout = new GridLayout();
358 layout.numColumns = 2;
359 layout.marginHeight = 0;
360 layout.marginWidth = 0;
361 layout.horizontalSpacing = 4;
362 layout.verticalSpacing = 0;
363 composite.setLayout(layout);
364 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
366 // Add a label for displaying status messages as they are received from the control
367 status = new Label(composite, SWT.SINGLE | SWT.READ_ONLY);
368 GridData gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);
369 gridData.horizontalIndent = 2;
370 status.setLayoutData(gridData);
372 // Add a progress bar to display downloading progress information
373 progress = new ProgressBar(composite, SWT.BORDER);
374 gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.VERTICAL_ALIGN_FILL);
375 gridData.widthHint = 100;
376 gridData.heightHint = 10;
377 progress.setLayoutData(gridData);
383 public void dispose() {
386 showStatusbar = false;
397 * Populate the toolbar.
398 * @param toolbar org.eclipse.swt.widgets.ToolBar
400 private void fillToolBar(final ToolBar toolbar) {
401 ToolItem go = new ToolItem(toolbar, SWT.NONE);
402 go.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_GO));
403 go.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_GO));
404 go.setDisabledImage(ImageResource.getImage(ImageResource.IMG_DLCL_NAV_GO));
405 go.setToolTipText(WebBrowserUIPlugin.getResource("%actionWebBrowserGo"));
406 go.addSelectionListener(new SelectionAdapter() {
407 public void widgetSelected(SelectionEvent event) {
408 setURL(combo.getText());
412 new ToolItem(toolbar, SWT.SEPARATOR);
414 favorites = new ToolItem(toolbar, SWT.DROP_DOWN);
415 favorites.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_FAVORITES));
416 favorites.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_FAVORITES));
417 favorites.setDisabledImage(ImageResource.getImage(ImageResource.IMG_DLCL_NAV_FAVORITES));
418 favorites.setToolTipText(WebBrowserUIPlugin.getResource("%actionWebBrowserFavorites"));
420 favorites.addSelectionListener(new SelectionAdapter() {
421 public void widgetSelected(SelectionEvent event) {
422 if (event.detail == SWT.ARROW) {
423 Rectangle r = favorites.getBounds();
424 showFavorites(toolbar, toolbar.toDisplay(r.x, r.y + r.height));
430 // create back and forward actions
431 back = new ToolItem(toolbar, SWT.NONE);
432 back.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_BACKWARD));
433 back.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_BACKWARD));
434 back.setDisabledImage(ImageResource.getImage(ImageResource.IMG_DLCL_NAV_BACKWARD));
435 back.setToolTipText(WebBrowserUIPlugin.getResource("%actionWebBrowserBack"));
436 back.addSelectionListener(new SelectionAdapter() {
437 public void widgetSelected(SelectionEvent event) {
442 forward = new ToolItem(toolbar, SWT.NONE);
443 forward.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_FORWARD));
444 forward.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_FORWARD));
445 forward.setDisabledImage(ImageResource.getImage(ImageResource.IMG_DLCL_NAV_FORWARD));
446 forward.setToolTipText(WebBrowserUIPlugin.getResource("%actionWebBrowserForward"));
447 forward.addSelectionListener(new SelectionAdapter() {
448 public void widgetSelected(SelectionEvent event) {
453 // create refresh, stop, and print actions
454 stop = new ToolItem(toolbar, SWT.NONE);
455 stop.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_STOP));
456 stop.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_STOP));
457 stop.setDisabledImage(ImageResource.getImage(ImageResource.IMG_DLCL_NAV_STOP));
458 stop.setToolTipText(WebBrowserUIPlugin.getResource("%actionWebBrowserStop"));
459 stop.addSelectionListener(new SelectionAdapter() {
460 public void widgetSelected(SelectionEvent event) {
465 refresh = new ToolItem(toolbar, SWT.NONE);
466 refresh.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_REFRESH));
467 refresh.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_REFRESH));
468 refresh.setDisabledImage(ImageResource.getImage(ImageResource.IMG_DLCL_NAV_REFRESH));
469 refresh.setToolTipText(WebBrowserUIPlugin.getResource("%actionWebBrowserRefresh"));
470 refresh.addSelectionListener(new SelectionAdapter() {
471 public void widgetSelected(SelectionEvent event) {
477 protected void addFavorite() {
478 java.util.List list = WebBrowserPreference.getInternalWebBrowserFavorites();
479 Favorite f = new Favorite(title, browser.getUrl());
480 if (!list.contains(f)) {
482 WebBrowserPreference.setInternalWebBrowserFavorites(list);
486 protected void showFavorites(Control parent, Point p) {
487 Menu perspectiveBarMenu = null;
488 if (perspectiveBarMenu == null) {
489 Menu menu = new Menu(parent);
492 Iterator iterator = WebBrowserUtil.getLockedFavorites().iterator();
493 if (iterator.hasNext()) {
494 while (iterator.hasNext()) {
495 final Favorite f = (Favorite) iterator.next();
496 MenuItem item = new MenuItem(menu, SWT.NONE);
497 item.setText(f.getName());
498 item.setImage(ImageResource.getImage(ImageResource.IMG_FAVORITE));
499 item.addSelectionListener(new SelectionAdapter() {
500 public void widgetSelected(SelectionEvent event) {
506 new MenuItem(menu, SWT.SEPARATOR);
509 iterator = WebBrowserPreference.getInternalWebBrowserFavorites().iterator();
510 if (!iterator.hasNext()) {
511 MenuItem item = new MenuItem(menu, SWT.NONE);
512 item.setText(WebBrowserUIPlugin.getResource("%actionWebBrowserNoFavorites"));
514 while (iterator.hasNext()) {
515 final Favorite f = (Favorite) iterator.next();
516 MenuItem item = new MenuItem(menu, SWT.NONE);
517 item.setText(f.getName());
518 item.setImage(ImageResource.getImage(ImageResource.IMG_FAVORITE));
519 item.addSelectionListener(new SelectionAdapter() {
520 public void widgetSelected(SelectionEvent event) {
526 new MenuItem(menu, SWT.SEPARATOR);
528 MenuItem item = new MenuItem(menu, SWT.NONE);
529 item.setText(WebBrowserUIPlugin.getResource("%actionWebBrowserOrganizeFavorites"));
530 item.addSelectionListener(new SelectionAdapter() {
531 public void widgetSelected(SelectionEvent event) {
532 OrganizeFavoritesDialog dialog = new OrganizeFavoritesDialog(shell);
537 perspectiveBarMenu = menu;
540 if (perspectiveBarMenu != null) {
541 perspectiveBarMenu.setLocation(p.x, p.y);
542 perspectiveBarMenu.setVisible(true);
547 navigate(WebBrowserPreference.getHomePageURL());
551 * Returns true if the homepage is currently being displayed.
554 protected boolean isHome() {
555 return getURL() != null && getURL().endsWith(WebBrowserPreference.getHomePageURL().substring(9));
558 protected String getURL() {
559 return browser.getUrl();
563 * Update the history list to the global copy.
565 protected void updateHistory() {
569 String temp = combo.getText();
571 history = WebBrowserPreference.getInternalWebBrowserHistory();
573 String[] historyList = new String[history.size()];
574 history.toArray(historyList);
575 combo.setItems(historyList);