Made all bundles require a Java 1.4 EE, and backported some classescode to Java 1.4
[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               if (fInstance.getURL() != null && fInstance.getURL().length() > 0) {
94                 fUrl = fInstance.getURL();
95                 fInstance.setURL(fUrl);
96             } else {
97                 fUrl = url;
98                 fInstance.setURL(url);
99             }
100                         // try {
101                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
102                         // public void run(IProgressMonitor monitor) throws CoreException {
103                         // instance.setURL(url);
104                         // }
105                         // }, null);
106                         // } catch (CoreException e) {
107                         // // TO DO Auto-generated catch block
108                         // e.printStackTrace();
109                         // }
110                 }
111         }
112
113         public void refresh() {
114                 if (fInstance != null) {
115                         fInstance.refresh();
116                         // try {
117                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
118                         // public void run(IProgressMonitor monitor) throws CoreException {
119                         // instance.refresh();
120                         // }
121                         // }, null);
122                         // } catch (CoreException e) {
123                         // // TO DO Auto-generated catch block
124                         // e.printStackTrace();
125                         // }
126                 }
127         }
128
129         
130         
131         public void refresh(String url) {
132                 if (fInstance != null && url != null) {
133                         if (fUrl == null) {
134                                 setUrl(url);
135                         } else {
136                                 Browser browser = fInstance.getBrowser();
137                                 if (browser != null) {
138                                         String browserUrl = browser.getUrl();
139                                         try {
140                                                 browserUrl = URLDecoder.decode(browserUrl, J5CharsetEmulator.defaultCharset().name());
141                                         } catch (UnsupportedEncodingException e) {
142                                                 // e.printStackTrace();
143                                         }
144                                         if (!url.equals(browserUrl)) {
145                                                 setUrl(url);
146                                         }
147                                 }
148                         }
149                 }
150         }
151
152         public void addProgressListener(ProgressListener listener) {
153                 if (fInstance != null) {
154                         fInstance.addProgressListener(listener);
155                 }
156         }
157
158         public void addStatusTextListener(StatusTextListener listener) {
159                 if (fInstance != null) {
160                         fInstance.addStatusTextListener(listener);
161                 }
162         }
163
164         public void addTitleListener(TitleListener listener) {
165                 if (fInstance != null) {
166                         fInstance.addTitleListener(listener);
167                 }
168         }
169
170         public boolean show(ShowInContext context) {
171                 if (context instanceof ShowInContextBrowser) {
172                         ShowInContextBrowser contextBrowser = (ShowInContextBrowser) context;
173                         String localhostURL = contextBrowser.getLocalhostUrl();
174                         if (localhostURL != null) {
175                                 setUrl(localhostURL);
176                                 return true;
177                         }
178                 }
179                 // *WARNING*
180                 // This causes unexpected behaviour such as downloading (save file).
181                 // It depends on mime-types setting and native browser, it isn't under control of eclipse.
182                 // (IE shows script as plain text since .php is unknown type by default.
183                 //  Mozilla downloads script file since .php is defined in mimeTypes.rdf as such.)
184                 //
185                 //if (context.getInput() instanceof IFile) {
186                 //      IFile file = (IFile) context.getInput();
187                 //      String localhostURL;
188                 //      localhostURL = "file:///" + file.getLocation().toString();
189                 //      setUrl(localhostURL);
190                 //      return true;
191                 //}
192                 return false;
193         }
194 }