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