Merging code from trunk that fixes #774, this work was done by incastrix
[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
16 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowser;
17 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
18
19 import org.eclipse.swt.browser.Browser;
20 import org.eclipse.swt.browser.CloseWindowListener;
21 import org.eclipse.swt.browser.ProgressListener;
22 import org.eclipse.swt.browser.StatusTextListener;
23 import org.eclipse.swt.browser.TitleListener;
24 import org.eclipse.swt.browser.WindowEvent;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.ui.part.IShowInTarget;
27 import org.eclipse.ui.part.ShowInContext;
28 import org.eclipse.ui.part.ViewPart;
29
30 /**
31  * <code>BrowserView</code> is a simple demonstration of the SWT Browser
32  * widget. It consists of a workbench view and tab folder where each tab in the
33  * folder allows the user to interact with a control.
34  * 
35  * @see ViewPart
36  */
37 public class BrowserView extends ViewPart implements IShowInTarget {
38         public final static String ID_BROWSER = "net.sourceforge.phpeclipse.webbrowser.views";
39
40         WebBrowser fInstance = null;
41
42         String fUrl = null;
43
44         /**
45          * Create the example
46          * 
47          * @see ViewPart#createPartControl
48          */
49         public void createPartControl(Composite frame) {
50                 try {
51                         if (WebBrowserUtil.isInternalBrowserOperational()) {
52                                 fInstance = new WebBrowser(frame, true, true);
53                                 // #1365431 (toshihiro) start
54                                 fInstance.getBrowser().addCloseWindowListener(
55                                                 new CloseWindowListener() {
56                                                         public void close(WindowEvent event) {
57                                                                 getViewSite().getPage().hideView(
58                                                                                 BrowserView.this);
59                                                         }
60                                                 });
61                                 // #1365431 (toshihiro) end
62                         }
63                 } catch (Exception e) {
64                         fInstance = null;
65                 }
66         }
67
68         /**
69          * Called when we must grab focus.
70          * 
71          * @see org.eclipse.ui.part.ViewPart#setFocus
72          */
73         public void setFocus() {
74                 if (fInstance != null) {
75                         fInstance.setFocus();
76                 }
77         }
78
79         /**
80          * Called when the View is to be disposed
81          */
82         public void dispose() {
83                 if (fInstance != null) {
84                         fInstance.dispose();
85                         fInstance = null;
86                 }
87                 super.dispose();
88         }
89
90         public String getUrl() {
91                 if (fInstance != null) {
92                         return fInstance.getURL();
93                 } else {
94                         return null;
95                 }
96         }
97
98         public void setUrl(final String url) {
99                 if (fInstance != null) {
100                                 fUrl = url;
101                                 fInstance.setURL(url);
102                         // try {
103                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
104                         // public void run(IProgressMonitor monitor) throws CoreException {
105                         // instance.setURL(url);
106                         // }
107                         // }, null);
108                         // } catch (CoreException e) {
109                         // // TO DO Auto-generated catch block
110                         // e.printStackTrace();
111                         // }
112                 }
113         }
114
115         public void refresh() {
116                 if (fInstance != null) {
117                         fInstance.refresh();
118                         // try {
119                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
120                         // public void run(IProgressMonitor monitor) throws CoreException {
121                         // instance.refresh();
122                         // }
123                         // }, null);
124                         // } catch (CoreException e) {
125                         // // TO DO Auto-generated catch block
126                         // e.printStackTrace();
127                         // }
128                 }
129         }
130
131         
132         
133         public void refresh(String url) {
134                 if (fInstance != null && url != null) {
135                         if (fUrl == null) {
136                                 setUrl(url);
137                         } else {
138                                 Browser browser = fInstance.getBrowser();
139                                 if (browser != null) {
140                                         String browserUrl = browser.getUrl();
141                                         try {
142                                                 browserUrl = URLDecoder.decode(browserUrl, J5CharsetEmulator.defaultCharset().name());
143                                         } catch (UnsupportedEncodingException e) {
144                                                 // e.printStackTrace();
145                                         }
146                                         if (!url.equals(browserUrl)) {
147                                                 setUrl(url);
148                                         }
149                                 }
150                         }
151                 }
152         }
153
154         public void addProgressListener(ProgressListener listener) {
155                 if (fInstance != null) {
156                         fInstance.addProgressListener(listener);
157                 }
158         }
159
160         public void addStatusTextListener(StatusTextListener listener) {
161                 if (fInstance != null) {
162                         fInstance.addStatusTextListener(listener);
163                 }
164         }
165
166         public void addTitleListener(TitleListener listener) {
167                 if (fInstance != null) {
168                         fInstance.addTitleListener(listener);
169                 }
170         }
171
172         public boolean show(ShowInContext context) {
173                 if (context instanceof ShowInContextBrowser) {
174                         ShowInContextBrowser contextBrowser = (ShowInContextBrowser) context;
175                         String localhostURL = contextBrowser.getLocalhostUrl();
176                         if (localhostURL != null) {
177                                 setUrl(localhostURL);
178                                 return true;
179                         }
180                 }
181                 // *WARNING*
182                 // This causes unexpected behaviour such as downloading (save file).
183                 // It depends on mime-types setting and native browser, it isn't under control of eclipse.
184                 // (IE shows script as plain text since .php is unknown type by default.
185                 //  Mozilla downloads script file since .php is defined in mimeTypes.rdf as such.)
186                 //
187                 //if (context.getInput() instanceof IFile) {
188                 //      IFile file = (IFile) context.getInput();
189                 //      String localhostURL;
190                 //      localhostURL = "file:///" + file.getLocation().toString();
191                 //      setUrl(localhostURL);
192                 //      return true;
193                 //}
194                 return false;
195         }
196 }