52f6ab463571abb9208be21581da49fcd8090194
[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 net.sourceforge.phpeclipse.webbrowser.internal.WebBrowser;
14 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUtil;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.swt.browser.Browser;
18 import org.eclipse.swt.browser.CloseWindowListener;
19 import org.eclipse.swt.browser.ProgressListener;
20 import org.eclipse.swt.browser.StatusTextListener;
21 import org.eclipse.swt.browser.TitleListener;
22 import org.eclipse.swt.browser.WindowEvent;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.ui.part.IShowInTarget;
25 import org.eclipse.ui.part.ShowInContext;
26 import org.eclipse.ui.part.ViewPart;
27
28 /**
29  * <code>BrowserView</code> is a simple demonstration of the SWT Browser
30  * widget. It consists of a workbench view and tab folder where each tab in the
31  * folder allows the user to interact with a control.
32  * 
33  * @see ViewPart
34  */
35 public class BrowserView extends ViewPart implements IShowInTarget {
36         public final static String ID_BROWSER = "net.sourceforge.phpeclipse.webbrowser.views";
37
38         WebBrowser fInstance = null;
39
40         String fUrl = null;
41
42         /**
43          * Create the example
44          * 
45          * @see ViewPart#createPartControl
46          */
47         public void createPartControl(Composite frame) {
48                 try {
49                         if (WebBrowserUtil.isInternalBrowserOperational()) {
50                                 fInstance = new WebBrowser(frame, true, true);
51                                 // #1365431 (toshihiro) start
52                                 fInstance.getBrowser().addCloseWindowListener(
53                                                 new CloseWindowListener() {
54                                                         public void close(WindowEvent event) {
55                                                                 getViewSite().getPage().hideView(
56                                                                                 BrowserView.this);
57                                                         }
58                                                 });
59                                 // #1365431 (toshihiro) end
60                         }
61                 } catch (Exception e) {
62                         fInstance = null;
63                 }
64         }
65
66         /**
67          * Called when we must grab focus.
68          * 
69          * @see org.eclipse.ui.part.ViewPart#setFocus
70          */
71         public void setFocus() {
72                 if (fInstance != null) {
73                         fInstance.setFocus();
74                 }
75         }
76
77         /**
78          * Called when the View is to be disposed
79          */
80         public void dispose() {
81                 if (fInstance != null) {
82                         fInstance.dispose();
83                         fInstance = null;
84                 }
85                 super.dispose();
86         }
87
88         public void setUrl(final String url) {
89                 if (fInstance != null) {
90                         fUrl = url;
91                         fInstance.setURL(url);
92                         // try {
93                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
94                         // public void run(IProgressMonitor monitor) throws CoreException {
95                         // instance.setURL(url);
96                         // }
97                         // }, null);
98                         // } catch (CoreException e) {
99                         // // TO DO Auto-generated catch block
100                         // e.printStackTrace();
101                         // }
102                 }
103         }
104
105         public void refresh() {
106                 if (fInstance != null) {
107                         fInstance.refresh();
108                         // try {
109                         // ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() {
110                         // public void run(IProgressMonitor monitor) throws CoreException {
111                         // instance.refresh();
112                         // }
113                         // }, null);
114                         // } catch (CoreException e) {
115                         // // TO DO Auto-generated catch block
116                         // e.printStackTrace();
117                         // }
118                 }
119         }
120
121         public void refresh(String url) {
122                 if (fInstance != null && url != null) {
123                         if (fUrl == null) {
124                                 setUrl(url);
125                         } else {
126                                 Browser browser = fInstance.getBrowser();
127                                 if (browser != null && !browser.getUrl().equals(url)) {
128                                         setUrl(url);
129                                 }
130                         }
131                 }
132         }
133
134         public void addProgressListener(ProgressListener listener) {
135                 if (fInstance != null) {
136                         fInstance.addProgressListener(listener);
137                 }
138         }
139
140         public void addStatusTextListener(StatusTextListener listener) {
141                 if (fInstance != null) {
142                         fInstance.addStatusTextListener(listener);
143                 }
144         }
145
146         public void addTitleListener(TitleListener listener) {
147                 if (fInstance != null) {
148                         fInstance.addTitleListener(listener);
149                 }
150         }
151
152         public boolean show(ShowInContext context) {
153                 if (context instanceof ShowInContextBrowser) {
154                         ShowInContextBrowser contextBrowser = (ShowInContextBrowser) context;
155                         String localhostURL = contextBrowser.getLocalhostUrl();
156                         if (localhostURL != null) {
157                                 setUrl(localhostURL);
158                                 return true;
159                         }
160                 }
161                 if (context.getInput() instanceof IFile) {
162                         IFile file = (IFile) context.getInput();
163                         String localhostURL;
164                         localhostURL = "file:///" + file.getLocation().toString();
165                         setUrl(localhostURL);
166                         return true;
167                 }
168                 return false;
169         }
170 }