Added: small label to make an effect of RemoteSourcePath clear
[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.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;
31
32 /**
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.
36  * 
37  * @see ViewPart
38  */
39 public class BrowserView extends ViewPart implements IShowInTarget {
40         public final static String ID_BROWSER = "net.sourceforge.phpeclipse.webbrowser.views";
41
42         WebBrowser fInstance = null;
43
44         String fUrl = null;
45
46         /**
47          * Create the example
48          * 
49          * @see ViewPart#createPartControl
50          */
51         public void createPartControl(Composite frame) {
52                 try {
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(
60                                                                                 BrowserView.this);
61                                                         }
62                                                 });
63                                 // #1365431 (toshihiro) end
64                         }
65                 } catch (Exception e) {
66                         fInstance = null;
67                 }
68         }
69
70         /**
71          * Called when we must grab focus.
72          * 
73          * @see org.eclipse.ui.part.ViewPart#setFocus
74          */
75         public void setFocus() {
76                 if (fInstance != null) {
77                         fInstance.setFocus();
78                 }
79         }
80
81         /**
82          * Called when the View is to be disposed
83          */
84         public void dispose() {
85                 if (fInstance != null) {
86                         fInstance.dispose();
87                         fInstance = null;
88                 }
89                 super.dispose();
90         }
91
92         public void setUrl(final String url) {
93                 if (fInstance != null) {
94                         fUrl = url;
95                         fInstance.setURL(url);
96                         // try {
97                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
98                         // public void run(IProgressMonitor monitor) throws CoreException {
99                         // instance.setURL(url);
100                         // }
101                         // }, null);
102                         // } catch (CoreException e) {
103                         // // TO DO Auto-generated catch block
104                         // e.printStackTrace();
105                         // }
106                 }
107         }
108
109         public void refresh() {
110                 if (fInstance != null) {
111                         fInstance.refresh();
112                         // try {
113                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
114                         // public void run(IProgressMonitor monitor) throws CoreException {
115                         // instance.refresh();
116                         // }
117                         // }, null);
118                         // } catch (CoreException e) {
119                         // // TO DO Auto-generated catch block
120                         // e.printStackTrace();
121                         // }
122                 }
123         }
124
125         public void refresh(String url) {
126                 if (fInstance != null && url != null) {
127                         if (fUrl == null) {
128                                 setUrl(url);
129                         } else {
130                                 Browser browser = fInstance.getBrowser();
131                                 if (browser != null) {
132                                         String browserUrl = browser.getUrl();
133                                         try {
134                                                 browserUrl = URLDecoder.decode(browserUrl, Charset.defaultCharset().name());
135                                         } catch (UnsupportedEncodingException e) {
136                                                 // e.printStackTrace();
137                                         }
138                                         if (!url.equals(browserUrl)) {
139                                                 setUrl(url);
140                                         }
141                                 }
142                         }
143                 }
144         }
145
146         public void addProgressListener(ProgressListener listener) {
147                 if (fInstance != null) {
148                         fInstance.addProgressListener(listener);
149                 }
150         }
151
152         public void addStatusTextListener(StatusTextListener listener) {
153                 if (fInstance != null) {
154                         fInstance.addStatusTextListener(listener);
155                 }
156         }
157
158         public void addTitleListener(TitleListener listener) {
159                 if (fInstance != null) {
160                         fInstance.addTitleListener(listener);
161                 }
162         }
163
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);
170                                 return true;
171                         }
172                 }
173                 if (context.getInput() instanceof IFile) {
174                         IFile file = (IFile) context.getInput();
175                         String localhostURL;
176                         localhostURL = "file:///" + file.getLocation().toString();
177                         setUrl(localhostURL);
178                         return true;
179                 }
180                 return false;
181         }
182 }