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.swt.browser.Browser;
21 import org.eclipse.swt.browser.CloseWindowListener;
22 import org.eclipse.swt.browser.ProgressListener;
23 import org.eclipse.swt.browser.StatusTextListener;
24 import org.eclipse.swt.browser.TitleListener;
25 import org.eclipse.swt.browser.WindowEvent;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.ui.part.IShowInTarget;
28 import org.eclipse.ui.part.ShowInContext;
29 import org.eclipse.ui.part.ViewPart;
32 * <code>BrowserView</code> is a simple demonstration of the SWT Browser
33 * widget. It consists of a workbench view and tab folder where each tab in the
34 * folder allows the user to interact with a control.
38 public class BrowserView extends ViewPart implements IShowInTarget {
39 public final static String ID_BROWSER = "net.sourceforge.phpeclipse.webbrowser.views";
41 WebBrowser fInstance = null;
48 * @see ViewPart#createPartControl
50 public void createPartControl(Composite frame) {
52 if (WebBrowserUtil.isInternalBrowserOperational()) {
53 fInstance = new WebBrowser(frame, true, true);
54 // #1365431 (toshihiro) start
55 fInstance.getBrowser().addCloseWindowListener(
56 new CloseWindowListener() {
57 public void close(WindowEvent event) {
58 getViewSite().getPage().hideView(
62 // #1365431 (toshihiro) end
64 } catch (Exception e) {
70 * Called when we must grab focus.
72 * @see org.eclipse.ui.part.ViewPart#setFocus
74 public void setFocus() {
75 if (fInstance != null) {
81 * Called when the View is to be disposed
83 public void dispose() {
84 if (fInstance != null) {
91 public void setUrl(final String url) {
92 if (fInstance != null) {
93 if (fInstance.getURL() != null && fInstance.getURL().length() > 0) {
94 fUrl = fInstance.getURL();
95 fInstance.setURL(fUrl);
98 fInstance.setURL(url);
101 // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
102 // public void run(IProgressMonitor monitor) throws CoreException {
103 // instance.setURL(url);
106 // } catch (CoreException e) {
107 // // TO DO Auto-generated catch block
108 // e.printStackTrace();
113 public void refresh() {
114 if (fInstance != null) {
117 // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
118 // public void run(IProgressMonitor monitor) throws CoreException {
119 // instance.refresh();
122 // } catch (CoreException e) {
123 // // TO DO Auto-generated catch block
124 // e.printStackTrace();
129 public void refresh(String url) {
130 if (fInstance != null && url != null) {
134 Browser browser = fInstance.getBrowser();
135 if (browser != null) {
136 String browserUrl = browser.getUrl();
138 browserUrl = URLDecoder.decode(browserUrl, Charset.defaultCharset().name());
139 } catch (UnsupportedEncodingException e) {
140 // e.printStackTrace();
142 if (!url.equals(browserUrl)) {
150 public void addProgressListener(ProgressListener listener) {
151 if (fInstance != null) {
152 fInstance.addProgressListener(listener);
156 public void addStatusTextListener(StatusTextListener listener) {
157 if (fInstance != null) {
158 fInstance.addStatusTextListener(listener);
162 public void addTitleListener(TitleListener listener) {
163 if (fInstance != null) {
164 fInstance.addTitleListener(listener);
168 public boolean show(ShowInContext context) {
169 if (context instanceof ShowInContextBrowser) {
170 ShowInContextBrowser contextBrowser = (ShowInContextBrowser) context;
171 String localhostURL = contextBrowser.getLocalhostUrl();
172 if (localhostURL != null) {
173 setUrl(localhostURL);
178 // This causes unexpected behaviour such as downloading (save file).
179 // It depends on mime-types setting and native browser, it isn't under control of eclipse.
180 // (IE shows script as plain text since .php is unknown type by default.
181 // Mozilla downloads script file since .php is defined in mimeTypes.rdf as such.)
183 //if (context.getInput() instanceof IFile) {
184 // IFile file = (IFile) context.getInput();
185 // String localhostURL;
186 // localhostURL = "file:///" + file.getLocation().toString();
187 // setUrl(localhostURL);