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