reverted test
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / views / BrowserView.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpeclipse.webbrowser.views;
12
13 import java.io.UnsupportedEncodingException;
14 import java.net.URLDecoder;
15 import java.nio.charset.Charset;
16
17 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowser;
18 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
19
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;
30
31 /**
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.
35  * 
36  * @see ViewPart
37  */
38 public class BrowserView extends ViewPart implements IShowInTarget {
39         public final static String ID_BROWSER = "net.sourceforge.phpeclipse.webbrowser.views";
40
41         WebBrowser fInstance = null;
42
43         String fUrl = null;
44
45         /**
46          * Create the example
47          * 
48          * @see ViewPart#createPartControl
49          */
50         public void createPartControl(Composite frame) {
51                 try {
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(
59                                                                                 BrowserView.this);
60                                                         }
61                                                 });
62                                 // #1365431 (toshihiro) end
63                         }
64                 } catch (Exception e) {
65                         fInstance = null;
66                 }
67         }
68
69         /**
70          * Called when we must grab focus.
71          * 
72          * @see org.eclipse.ui.part.ViewPart#setFocus
73          */
74         public void setFocus() {
75                 if (fInstance != null) {
76                         fInstance.setFocus();
77                 }
78         }
79
80         /**
81          * Called when the View is to be disposed
82          */
83         public void dispose() {
84                 if (fInstance != null) {
85                         fInstance.dispose();
86                         fInstance = null;
87                 }
88                 super.dispose();
89         }
90
91         public void setUrl(final String url) {
92                 if (fInstance != null) {
93                         fUrl = url;
94                         fInstance.setURL(url);
95                         // try {
96                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
97                         // public void run(IProgressMonitor monitor) throws CoreException {
98                         // instance.setURL(url);
99                         // }
100                         // }, null);
101                         // } catch (CoreException e) {
102                         // // TO DO Auto-generated catch block
103                         // e.printStackTrace();
104                         // }
105                 }
106         }
107
108         public void refresh() {
109                 if (fInstance != null) {
110                         fInstance.refresh();
111                         // try {
112                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
113                         // public void run(IProgressMonitor monitor) throws CoreException {
114                         // instance.refresh();
115                         // }
116                         // }, null);
117                         // } catch (CoreException e) {
118                         // // TO DO Auto-generated catch block
119                         // e.printStackTrace();
120                         // }
121                 }
122         }
123
124         public void refresh(String url) {
125                 if (fInstance != null && url != null) {
126                         if (fUrl == null) {
127                                 setUrl(url);
128                         } else {
129                                 Browser browser = fInstance.getBrowser();
130                                 if (browser != null) {
131                                         String browserUrl = browser.getUrl();
132                                         try {
133                                                 browserUrl = URLDecoder.decode(browserUrl, Charset.defaultCharset().name());
134                                         } catch (UnsupportedEncodingException e) {
135                                                 // e.printStackTrace();
136                                         }
137                                         if (!url.equals(browserUrl)) {
138                                                 setUrl(url);
139                                         }
140                                 }
141                         }
142                 }
143         }
144
145         public void addProgressListener(ProgressListener listener) {
146                 if (fInstance != null) {
147                         fInstance.addProgressListener(listener);
148                 }
149         }
150
151         public void addStatusTextListener(StatusTextListener listener) {
152                 if (fInstance != null) {
153                         fInstance.addStatusTextListener(listener);
154                 }
155         }
156
157         public void addTitleListener(TitleListener listener) {
158                 if (fInstance != null) {
159                         fInstance.addTitleListener(listener);
160                 }
161         }
162
163         public boolean show(ShowInContext context) {
164                 if (context instanceof ShowInContextBrowser) {
165                         ShowInContextBrowser contextBrowser = (ShowInContextBrowser) context;
166                         String localhostURL = contextBrowser.getLocalhostUrl();
167                         if (localhostURL != null) {
168                                 setUrl(localhostURL);
169                                 return true;
170                         }
171                 }
172                 // *WARNING*
173                 // This causes unexpected behaviour such as downloading (save file).
174                 // It depends on mime-types setting and native browser, it isn't under control of eclipse.
175                 // (IE shows script as plain text since .php is unknown type by default.
176                 //  Mozilla downloads script file since .php is defined in mimeTypes.rdf as such.)
177                 //
178                 //if (context.getInput() instanceof IFile) {
179                 //      IFile file = (IFile) context.getInput();
180                 //      String localhostURL;
181                 //      localhostURL = "file:///" + file.getLocation().toString();
182                 //      setUrl(localhostURL);
183                 //      return true;
184                 //}
185                 return false;
186         }
187 }