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