2 * Copyright (c) 2003 IBM Corporation and others. All rights reserved. � This
3 * program and the accompanying materials are made available under the terms of
4 * the Common Public License v1.0 which accompanies this distribution, and is
5 * available at http://www.eclipse.org/legal/cpl-v10.html �* Contributors: IBM -
6 * Initial API and implementation
9 // TODO 1. Handle the sizing of a popup running in shelled out secondary window.
10 // TODO 2. Support printing: waiting on eclipse bug 47937/44823.
11 package net.sourceforge.phpeclipse.webbrowser.internal;
13 import java.util.Iterator;
15 import net.sourceforge.phpeclipse.webbrowser.IURLMap;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.browser.Browser;
19 import org.eclipse.swt.browser.CloseWindowListener;
20 import org.eclipse.swt.browser.LocationEvent;
21 import org.eclipse.swt.browser.LocationListener;
22 import org.eclipse.swt.browser.OpenWindowListener;
23 import org.eclipse.swt.browser.ProgressEvent;
24 import org.eclipse.swt.browser.ProgressListener;
25 import org.eclipse.swt.browser.StatusTextEvent;
26 import org.eclipse.swt.browser.StatusTextListener;
27 import org.eclipse.swt.browser.TitleEvent;
28 import org.eclipse.swt.browser.TitleListener;
29 import org.eclipse.swt.browser.WindowEvent;
30 import org.eclipse.swt.dnd.Clipboard;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.swt.graphics.Rectangle;
35 import org.eclipse.swt.layout.FillLayout;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Combo;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Listener;
44 import org.eclipse.swt.widgets.Menu;
45 import org.eclipse.swt.widgets.MenuItem;
46 import org.eclipse.swt.widgets.ProgressBar;
47 import org.eclipse.swt.widgets.Shell;
48 import org.eclipse.swt.widgets.ToolBar;
49 import org.eclipse.swt.widgets.ToolItem;
50 import org.eclipse.ui.PlatformUI;
52 public class WebBrowser extends Composite {
53 protected Composite toolbarComp;
55 protected Composite statusComp;
57 protected Combo combo;
59 protected Clipboard clipboard;
61 protected boolean showToolbar;
63 protected ToolItem back;
65 protected ToolItem forward;
67 protected ToolItem stop;
69 protected ToolItem favorites;
71 protected ToolItem refresh;
73 protected BusyIndicator busy;
75 protected boolean showStatusbar;
77 protected ProgressBar progress;
79 protected Label status;
81 private static int MAX_HISTORY = 50;
83 protected static java.util.List history;
85 protected Browser browser;
87 protected Shell shell;
89 protected WebBrowserEditor editor;
91 protected String title;
93 public WebBrowser(Composite parent, final boolean showToolbar,
94 final boolean showStatusbar) {
95 super(parent, SWT.NONE);
97 this.showToolbar = showToolbar;
98 this.showStatusbar = showStatusbar;
100 GridLayout layout = new GridLayout();
101 layout.marginHeight = 3;
102 layout.marginWidth = 3;
103 layout.horizontalSpacing = 3;
104 layout.verticalSpacing = 3;
105 layout.numColumns = 1;
107 setLayoutData(new GridData(GridData.FILL_BOTH));
108 clipboard = new Clipboard(parent.getDisplay());
109 PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
110 ContextIds.WEB_BROWSER);
113 toolbarComp = new Composite(this, SWT.NONE);
114 GridLayout outerLayout = new GridLayout();
115 outerLayout.numColumns = 2;
116 outerLayout.marginWidth = 0;
117 outerLayout.marginHeight = 0;
118 toolbarComp.setLayout(outerLayout);
119 toolbarComp.setLayoutData(new GridData(
120 GridData.VERTICAL_ALIGN_BEGINNING
121 | GridData.FILL_HORIZONTAL));
123 // create the top line, with a combo box for history and a "go"
125 Composite top = new Composite(toolbarComp, SWT.NONE);
126 GridLayout topLayout = new GridLayout();
127 topLayout.numColumns = 2;
128 topLayout.marginWidth = 0;
129 topLayout.marginHeight = 0;
130 top.setLayout(topLayout);
131 top.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER
132 | GridData.FILL_HORIZONTAL));
134 combo = new Combo(top, SWT.DROP_DOWN);
138 combo.addSelectionListener(new SelectionAdapter() {
139 public void widgetSelected(SelectionEvent we) {
141 if (combo.getSelectionIndex() != -1)
142 setURL(combo.getItem(combo.getSelectionIndex()));
143 } catch (Exception e) {
147 combo.addListener(SWT.DefaultSelection, new Listener() {
148 public void handleEvent(Event e) {
149 setURL(combo.getText());
152 combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
153 PlatformUI.getWorkbench().getHelpSystem().setHelp(combo,
154 ContextIds.WEB_BROWSER_URL);
156 ToolBar toolbar = new ToolBar(top, SWT.FLAT);
157 fillToolBar(toolbar);
159 new ToolItem(toolbar, SWT.SEPARATOR);
161 busy = new BusyIndicator(toolbarComp, SWT.NONE);
162 busy.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
165 // create a new SWT Web browser widget, checking once again to make sure
166 // we can use it in this environment
167 // if (WebBrowserUtil.canUseInternalWebBrowser() &
168 // WebBrowserUtil.isInternalBrowserOperational())
169 if (WebBrowserUtil.isInternalBrowserOperational())
170 this.browser = new Browser(this, SWT.NONE);
172 WebBrowserUtil.openError(WebBrowserUIPlugin
173 .getResource("%errorCouldNotLaunchInternalWebBrowser"));
178 back.setEnabled(browser.isBackEnabled());
179 forward.setEnabled(browser.isForwardEnabled());
182 PlatformUI.getWorkbench().getHelpSystem().setHelp(browser,
183 ContextIds.WEB_BROWSER_WEB);
184 GridData data = new GridData();
185 data.horizontalAlignment = GridData.FILL;
186 data.verticalAlignment = GridData.FILL;
187 data.horizontalSpan = 3;
188 data.grabExcessHorizontalSpace = true;
189 data.grabExcessVerticalSpace = true;
190 browser.setLayoutData(data);
193 createStatusArea(this);
195 addBrowserListeners();
201 protected void addBrowserListeners() {
203 // respond to Browser StatusTextEvents events by updating the status
205 browser.addStatusTextListener(new StatusTextListener() {
206 public void changed(StatusTextEvent event) {
207 status.setText(event.text);
213 * Add listener for new window creation so that we can instead of
214 * opening a separate new window in which the session is lost, we can
215 * instead open a new window in a new shell within the browser area
216 * thereby maintaining the session.
218 browser.addOpenWindowListener(new OpenWindowListener() {
219 public void open(WindowEvent event) {
220 Shell shell2 = new Shell(getDisplay());
221 shell2.setLayout(new FillLayout());
222 shell2.setText(WebBrowserUIPlugin
223 .getResource("%viewWebBrowserTitle"));
224 shell2.setImage(getShell().getImage());
225 WebBrowser browser2 = new WebBrowser(shell2, showToolbar,
227 browser2.shell = shell2;
228 event.browser = browser2.browser;
233 browser.addCloseWindowListener(new CloseWindowListener() {
234 public void close(WindowEvent event) {
235 // if shell is not null, it must be a secondary popup window,
236 // else its an editor window
240 editor.closeEditor();
244 browser.addProgressListener(new ProgressListener() {
245 public void changed(ProgressEvent event) {
246 if (event.total == 0)
249 boolean done = (event.current == event.total);
251 int percentProgress = event.current * 100 / event.total;
254 progress.setSelection(0);
256 progress.setSelection(percentProgress);
261 && (percentProgress > 0 && percentProgress < 100)) {
264 // Once the progress hits 100 percent, done, set busy to
266 else if (busy.isBusy() && done) {
272 public void completed(ProgressEvent event) {
274 progress.setSelection(0);
277 back.setEnabled(browser.isBackEnabled());
278 forward.setEnabled(browser.isForwardEnabled());
284 browser.addLocationListener(new LocationListener() {
285 public void changed(LocationEvent event) {
289 combo.setText(event.location);
290 addToHistory(event.location);
296 public void changing(LocationEvent event) {
301 browser.addTitleListener(new TitleListener() {
302 public void changed(TitleEvent event) {
309 * Return the underlying browser control.
311 * @return org.eclipse.swt.browser.Browser
313 public Browser getBrowser() {
320 protected void forward() {
327 protected void back() {
334 protected void stop() {
341 protected void navigate(String url) {
342 Trace.trace(Trace.FINER, "Navigate: " + url);
343 if (url != null && url.equals(getURL())) {
351 * Refresh the currently viewed page.
353 public void refresh() {
357 protected void setURL(String url, boolean browse) {
358 Trace.trace(Trace.FINEST, "setURL: " + url + " " + browse);
364 if (url.endsWith(WebBrowserPreference.getHomePageURL().substring(9)))
368 Iterator iterator = WebBrowserUtil.getURLMaps().iterator();
369 String newURL = null;
370 while (iterator.hasNext() && newURL == null) {
372 IURLMap map = (IURLMap) iterator.next();
373 newURL = map.getMappedURL(url);
374 } catch (Exception e) {
387 protected void addToHistory(String url) {
389 history = WebBrowserPreference.getInternalWebBrowserHistory();
391 int size = history.size();
392 for (int i = 0; i < size; i++) {
393 String s = (String) history.get(i);
401 if (size >= MAX_HISTORY)
402 history.remove(size - 1);
404 WebBrowserPreference.setInternalWebBrowserHistory(history);
405 } else if (found != 0) {
406 history.remove(found);
408 WebBrowserPreference.setInternalWebBrowserHistory(history);
412 public void setURL(String url) {
417 * Creates the Web browser status area.
419 private void createStatusArea(Composite parent) {
420 Composite composite = new Composite(parent, SWT.NONE);
421 GridLayout layout = new GridLayout();
422 layout.numColumns = 2;
423 layout.marginHeight = 0;
424 layout.marginWidth = 0;
425 layout.horizontalSpacing = 4;
426 layout.verticalSpacing = 0;
427 composite.setLayout(layout);
428 composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
430 // Add a label for displaying status messages as they are received from
432 status = new Label(composite, SWT.SINGLE | SWT.READ_ONLY);
433 GridData gridData = new GridData(GridData.FILL_HORIZONTAL
434 | GridData.VERTICAL_ALIGN_FILL);
435 gridData.horizontalIndent = 2;
436 status.setLayoutData(gridData);
438 // Add a progress bar to display downloading progress information
439 progress = new ProgressBar(composite, SWT.BORDER);
440 gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
441 | GridData.VERTICAL_ALIGN_FILL);
442 gridData.widthHint = 100;
443 gridData.heightHint = 10;
444 progress.setLayoutData(gridData);
450 public void dispose() {
453 showStatusbar = false;
464 * Populate the toolbar.
467 * org.eclipse.swt.widgets.ToolBar
469 private void fillToolBar(final ToolBar toolbar) {
470 ToolItem go = new ToolItem(toolbar, SWT.NONE);
471 go.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_GO));
472 go.setHotImage(ImageResource.getImage(ImageResource.IMG_CLCL_NAV_GO));
473 go.setDisabledImage(ImageResource
474 .getImage(ImageResource.IMG_DLCL_NAV_GO));
476 .setToolTipText(WebBrowserUIPlugin
477 .getResource("%actionWebBrowserGo"));
478 go.addSelectionListener(new SelectionAdapter() {
479 public void widgetSelected(SelectionEvent event) {
480 setURL(combo.getText());
484 new ToolItem(toolbar, SWT.SEPARATOR);
486 favorites = new ToolItem(toolbar, SWT.DROP_DOWN);
487 favorites.setImage(ImageResource
488 .getImage(ImageResource.IMG_ELCL_NAV_FAVORITES));
489 favorites.setHotImage(ImageResource
490 .getImage(ImageResource.IMG_CLCL_NAV_FAVORITES));
491 favorites.setDisabledImage(ImageResource
492 .getImage(ImageResource.IMG_DLCL_NAV_FAVORITES));
493 favorites.setToolTipText(WebBrowserUIPlugin
494 .getResource("%actionWebBrowserFavorites"));
496 favorites.addSelectionListener(new SelectionAdapter() {
497 public void widgetSelected(SelectionEvent event) {
498 if (event.detail == SWT.ARROW) {
499 Rectangle r = favorites.getBounds();
500 showFavorites(toolbar, toolbar.toDisplay(r.x, r.y
507 // create back and forward actions
508 back = new ToolItem(toolbar, SWT.NONE);
509 back.setImage(ImageResource
510 .getImage(ImageResource.IMG_ELCL_NAV_BACKWARD));
511 back.setHotImage(ImageResource
512 .getImage(ImageResource.IMG_CLCL_NAV_BACKWARD));
513 back.setDisabledImage(ImageResource
514 .getImage(ImageResource.IMG_DLCL_NAV_BACKWARD));
515 back.setToolTipText(WebBrowserUIPlugin
516 .getResource("%actionWebBrowserBack"));
517 back.addSelectionListener(new SelectionAdapter() {
518 public void widgetSelected(SelectionEvent event) {
523 forward = new ToolItem(toolbar, SWT.NONE);
524 forward.setImage(ImageResource
525 .getImage(ImageResource.IMG_ELCL_NAV_FORWARD));
526 forward.setHotImage(ImageResource
527 .getImage(ImageResource.IMG_CLCL_NAV_FORWARD));
528 forward.setDisabledImage(ImageResource
529 .getImage(ImageResource.IMG_DLCL_NAV_FORWARD));
530 forward.setToolTipText(WebBrowserUIPlugin
531 .getResource("%actionWebBrowserForward"));
532 forward.addSelectionListener(new SelectionAdapter() {
533 public void widgetSelected(SelectionEvent event) {
538 // create refresh, stop, and print actions
539 stop = new ToolItem(toolbar, SWT.NONE);
540 stop.setImage(ImageResource.getImage(ImageResource.IMG_ELCL_NAV_STOP));
541 stop.setHotImage(ImageResource
542 .getImage(ImageResource.IMG_CLCL_NAV_STOP));
543 stop.setDisabledImage(ImageResource
544 .getImage(ImageResource.IMG_DLCL_NAV_STOP));
545 stop.setToolTipText(WebBrowserUIPlugin
546 .getResource("%actionWebBrowserStop"));
547 stop.addSelectionListener(new SelectionAdapter() {
548 public void widgetSelected(SelectionEvent event) {
553 refresh = new ToolItem(toolbar, SWT.NONE);
554 refresh.setImage(ImageResource
555 .getImage(ImageResource.IMG_ELCL_NAV_REFRESH));
556 refresh.setHotImage(ImageResource
557 .getImage(ImageResource.IMG_CLCL_NAV_REFRESH));
558 refresh.setDisabledImage(ImageResource
559 .getImage(ImageResource.IMG_DLCL_NAV_REFRESH));
560 refresh.setToolTipText(WebBrowserUIPlugin
561 .getResource("%actionWebBrowserRefresh"));
562 refresh.addSelectionListener(new SelectionAdapter() {
563 public void widgetSelected(SelectionEvent event) {
569 protected void addFavorite() {
570 java.util.List list = WebBrowserPreference
571 .getInternalWebBrowserFavorites();
572 Favorite f = new Favorite(title, browser.getUrl());
573 if (!list.contains(f)) {
575 WebBrowserPreference.setInternalWebBrowserFavorites(list);
579 protected void showFavorites(Control parent, Point p) {
580 Menu perspectiveBarMenu = null;
581 if (perspectiveBarMenu == null) {
582 Menu menu = new Menu(parent);
585 Iterator iterator = WebBrowserUtil.getLockedFavorites().iterator();
586 if (iterator.hasNext()) {
587 while (iterator.hasNext()) {
588 final Favorite f = (Favorite) iterator.next();
589 MenuItem item = new MenuItem(menu, SWT.NONE);
590 item.setText(f.getName());
591 item.setImage(ImageResource
592 .getImage(ImageResource.IMG_FAVORITE));
593 item.addSelectionListener(new SelectionAdapter() {
594 public void widgetSelected(SelectionEvent event) {
600 new MenuItem(menu, SWT.SEPARATOR);
603 iterator = WebBrowserPreference.getInternalWebBrowserFavorites()
605 if (!iterator.hasNext()) {
606 MenuItem item = new MenuItem(menu, SWT.NONE);
607 item.setText(WebBrowserUIPlugin
608 .getResource("%actionWebBrowserNoFavorites"));
610 while (iterator.hasNext()) {
611 final Favorite f = (Favorite) iterator.next();
612 MenuItem item = new MenuItem(menu, SWT.NONE);
613 item.setText(f.getName());
614 item.setImage(ImageResource
615 .getImage(ImageResource.IMG_FAVORITE));
616 item.addSelectionListener(new SelectionAdapter() {
617 public void widgetSelected(SelectionEvent event) {
623 new MenuItem(menu, SWT.SEPARATOR);
625 MenuItem item = new MenuItem(menu, SWT.NONE);
626 item.setText(WebBrowserUIPlugin
627 .getResource("%actionWebBrowserOrganizeFavorites"));
628 item.addSelectionListener(new SelectionAdapter() {
629 public void widgetSelected(SelectionEvent event) {
630 OrganizeFavoritesDialog dialog = new OrganizeFavoritesDialog(
636 perspectiveBarMenu = menu;
639 if (perspectiveBarMenu != null) {
640 perspectiveBarMenu.setLocation(p.x, p.y);
641 perspectiveBarMenu.setVisible(true);
646 navigate(WebBrowserPreference.getHomePageURL());
650 * Returns true if the homepage is currently being displayed.
654 protected boolean isHome() {
655 return getURL() != null
656 && getURL().endsWith(
657 WebBrowserPreference.getHomePageURL().substring(9));
660 protected String getURL() {
661 return browser.getUrl();
665 * Update the history list to the global copy.
667 protected void updateHistory() {
671 String temp = combo.getText();
673 history = WebBrowserPreference.getInternalWebBrowserHistory();
675 String[] historyList = new String[history.size()];
676 history.toArray(historyList);
677 combo.setItems(historyList);
682 public void addProgressListener(ProgressListener listener) {
683 browser.addProgressListener(listener);
686 public void addStatusTextListener(StatusTextListener listener) {
687 browser.addStatusTextListener(listener);
690 public void addTitleListener(TitleListener listener) {
691 browser.addTitleListener(listener);