1 /*******************************************************************************
2 * Copyright (c) 2000, 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 Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpeclipse.webbrowser.views;
13 import java.io.UnsupportedEncodingException;
14 import java.net.URLDecoder;
15 import java.nio.charset.Charset;
17 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowser;
18 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.swt.browser.Browser;
22 import org.eclipse.swt.browser.CloseWindowListener;
23 import org.eclipse.swt.browser.ProgressListener;
24 import org.eclipse.swt.browser.StatusTextListener;
25 import org.eclipse.swt.browser.TitleListener;
26 import org.eclipse.swt.browser.WindowEvent;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.ui.part.IShowInTarget;
29 import org.eclipse.ui.part.ShowInContext;
30 import org.eclipse.ui.part.ViewPart;
33 * <code>BrowserView</code> is a simple demonstration of the SWT Browser
34 * widget. It consists of a workbench view and tab folder where each tab in the
35 * folder allows the user to interact with a control.
39 public class BrowserView extends ViewPart implements IShowInTarget {
40 public final static String ID_BROWSER = "net.sourceforge.phpeclipse.webbrowser.views";
42 WebBrowser fInstance = null;
49 * @see ViewPart#createPartControl
51 public void createPartControl(Composite frame) {
53 if (WebBrowserUtil.isInternalBrowserOperational()) {
54 fInstance = new WebBrowser(frame, true, true);
55 // #1365431 (toshihiro) start
56 fInstance.getBrowser().addCloseWindowListener(
57 new CloseWindowListener() {
58 public void close(WindowEvent event) {
59 getViewSite().getPage().hideView(
63 // #1365431 (toshihiro) end
65 } catch (Exception e) {
71 * Called when we must grab focus.
73 * @see org.eclipse.ui.part.ViewPart#setFocus
75 public void setFocus() {
76 if (fInstance != null) {
82 * Called when the View is to be disposed
84 public void dispose() {
85 if (fInstance != null) {
92 public void setUrl(final String url) {
93 if (fInstance != null) {
95 fInstance.setURL(url);
97 // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
98 // public void run(IProgressMonitor monitor) throws CoreException {
99 // instance.setURL(url);
102 // } catch (CoreException e) {
103 // // TO DO Auto-generated catch block
104 // e.printStackTrace();
109 public void refresh() {
110 if (fInstance != null) {
113 // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
114 // public void run(IProgressMonitor monitor) throws CoreException {
115 // instance.refresh();
118 // } catch (CoreException e) {
119 // // TO DO Auto-generated catch block
120 // e.printStackTrace();
125 public void refresh(String url) {
126 if (fInstance != null && url != null) {
130 Browser browser = fInstance.getBrowser();
131 if (browser != null) {
132 String browserUrl = browser.getUrl();
134 browserUrl = URLDecoder.decode(browserUrl, Charset.defaultCharset().name());
135 } catch (UnsupportedEncodingException e) {
136 // e.printStackTrace();
138 if (!url.equals(browserUrl)) {
146 public void addProgressListener(ProgressListener listener) {
147 if (fInstance != null) {
148 fInstance.addProgressListener(listener);
152 public void addStatusTextListener(StatusTextListener listener) {
153 if (fInstance != null) {
154 fInstance.addStatusTextListener(listener);
158 public void addTitleListener(TitleListener listener) {
159 if (fInstance != null) {
160 fInstance.addTitleListener(listener);
164 public boolean show(ShowInContext context) {
165 if (context instanceof ShowInContextBrowser) {
166 ShowInContextBrowser contextBrowser = (ShowInContextBrowser) context;
167 String localhostURL = contextBrowser.getLocalhostUrl();
168 if (localhostURL != null) {
169 setUrl(localhostURL);
173 if (context.getInput() instanceof IFile) {
174 IFile file = (IFile) context.getInput();
176 localhostURL = "file:///" + file.getLocation().toString();
177 setUrl(localhostURL);