KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > WebBrowserEditor


1 /*******************************************************************************
2  * Copyright (c) 203, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import java.beans.PropertyChangeEvent JavaDoc;
14 import java.beans.PropertyChangeListener JavaDoc;
15 import java.net.MalformedURLException JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.ui.IActionBars;
27 import org.eclipse.ui.IEditorDescriptor;
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IEditorReference;
31 import org.eclipse.ui.IEditorRegistry;
32 import org.eclipse.ui.IEditorSite;
33 import org.eclipse.ui.IPathEditorInput;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
39 import org.eclipse.ui.part.EditorPart;
40 /**
41  * An integrated Web browser, defined as an editor to make
42  * better use of the desktop.
43  */

44 public class WebBrowserEditor extends EditorPart implements IBrowserViewerContainer {
45     public static final String JavaDoc WEB_BROWSER_EDITOR_ID = "org.eclipse.ui.browser.editor"; //$NON-NLS-1$
46

47     protected BrowserViewer webBrowser;
48     protected String JavaDoc initialURL;
49     protected Image image;
50
51     protected TextAction cutAction;
52     protected TextAction copyAction;
53     protected TextAction pasteAction;
54     
55     private boolean disposed;
56     private boolean lockName;
57
58     /**
59      * WebBrowserEditor constructor comment.
60      */

61     public WebBrowserEditor() {
62         super();
63     }
64     
65     /*
66      * Creates the SWT controls for this workbench part.
67      */

68     public void createPartControl(Composite parent) {
69         WebBrowserEditorInput input = getWebBrowserEditorInput();
70         
71         int style = 0;
72         if (input == null || input.isLocationBarLocal()) {
73             style += BrowserViewer.LOCATION_BAR;
74         }
75         if (input == null || input.isToolbarLocal()) {
76             style += BrowserViewer.BUTTON_BAR;
77         }
78         webBrowser = new BrowserViewer(parent, style);
79         
80         webBrowser.setURL(initialURL);
81         webBrowser.setContainer(this);
82         
83         if (input == null || input.isLocationBarLocal()) {
84             cutAction = new TextAction(webBrowser, TextAction.CUT);
85             copyAction = new TextAction(webBrowser, TextAction.COPY);
86             pasteAction = new TextAction(webBrowser, TextAction.PASTE);
87         }
88         
89         if (!lockName) {
90             PropertyChangeListener JavaDoc propertyChangeListener = new PropertyChangeListener JavaDoc() {
91                 public void propertyChange(PropertyChangeEvent JavaDoc event) {
92                     if (BrowserViewer.PROPERTY_TITLE.equals(event.getPropertyName())) {
93                         setPartName((String JavaDoc) event.getNewValue());
94                     }
95                 }
96             };
97             webBrowser.addPropertyChangeListener(propertyChangeListener);
98         }
99     }
100     
101     public void dispose() {
102         if (image != null && !image.isDisposed())
103             image.dispose();
104         image = null;
105
106         super.dispose();
107         // mark this instance as disposed to avoid stale references
108
disposed = true;
109     }
110     
111     public boolean isDisposed() {
112         return disposed;
113     }
114     
115     /* (non-Javadoc)
116      * Saves the contents of this editor.
117      */

118     public void doSave(IProgressMonitor monitor) {
119         // do nothing
120
}
121
122     /* (non-Javadoc)
123      * Saves the contents of this editor to another object.
124      */

125     public void doSaveAs() {
126         // do nothing
127
}
128     
129     /**
130      * Returns the copy action.
131      *
132      * @return org.eclipse.jface.action.IAction
133      */

134     public IAction getCopyAction() {
135         return copyAction;
136     }
137     
138     /**
139      * Returns the cut action.
140      *
141      * @return org.eclipse.jface.action.IAction
142      */

143     public IAction getCutAction() {
144         return cutAction;
145     }
146     
147     /**
148      * Returns the web editor input, if available. If the input was of
149      * another type, <code>null</code> is returned.
150      *
151      * @return org.eclipse.ui.internal.browser.IWebBrowserEditorInput
152      */

153     protected WebBrowserEditorInput getWebBrowserEditorInput() {
154         IEditorInput input = getEditorInput();
155         if (input instanceof WebBrowserEditorInput)
156             return (WebBrowserEditorInput) input;
157         return null;
158     }
159
160     /**
161      * Returns the paste action.
162      *
163      * @return org.eclipse.jface.action.IAction
164      */

165     public IAction getPasteAction() {
166         return pasteAction;
167     }
168
169     /* (non-Javadoc)
170      * Initializes the editor part with a site and input.
171      */

172     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
173         Trace.trace(Trace.FINEST, "Opening browser: " + input); //$NON-NLS-1$
174
if (input instanceof IPathEditorInput) {
175             IPathEditorInput pei = (IPathEditorInput) input;
176             IPath path = pei.getPath();
177             URL JavaDoc url = null;
178             try {
179                 if (path != null)
180                     url = path.toFile().toURL();
181                 initialURL = url.toExternalForm();
182             } catch (Exception JavaDoc e) {
183                 Trace.trace(Trace.SEVERE, "Error getting URL to file"); //$NON-NLS-1$
184
}
185             if (webBrowser != null) {
186                 if (initialURL != null)
187                     webBrowser.setURL(initialURL);
188                 site.getWorkbenchWindow().getActivePage().activate(this);
189             }
190             
191             setPartName(path.lastSegment());
192             if (url != null)
193                 setTitleToolTip(url.getFile());
194
195             Image oldImage = image;
196             ImageDescriptor id = ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
197             image = id.createImage();
198
199             setTitleImage(image);
200             if (oldImage != null && !oldImage.isDisposed())
201                 oldImage.dispose();
202             //addResourceListener(file);
203
} else if (input instanceof WebBrowserEditorInput) {
204             WebBrowserEditorInput wbei = (WebBrowserEditorInput) input;
205             initialURL = null;
206             if (wbei.getURL() != null)
207                 initialURL = wbei.getURL().toExternalForm();
208             if (webBrowser != null) {
209                 webBrowser.setURL(initialURL);
210                 site.getWorkbenchWindow().getActivePage().activate(this);
211             }
212     
213             setPartName(wbei.getName());
214             setTitleToolTip(wbei.getToolTipText());
215             lockName = wbei.isNameLocked();
216
217             Image oldImage = image;
218             ImageDescriptor id = wbei.getImageDescriptor();
219             image = id.createImage();
220
221             setTitleImage(image);
222             if (oldImage != null && !oldImage.isDisposed())
223                 oldImage.dispose();
224         } else
225             throw new PartInitException(NLS.bind(Messages.errorInvalidEditorInput, input.getName()));
226         
227         setSite(site);
228         setInput(input);
229     }
230     
231     /* (non-Javadoc)
232      * Returns whether the contents of this editor have changed since the last save
233      * operation.
234      */

235     public boolean isDirty() {
236         return false;
237     }
238     
239     /* (non-Javadoc)
240      * Returns whether the "save as" operation is supported by this editor.
241      */

242     public boolean isSaveAsAllowed() {
243         return false;
244     }
245
246     /**
247      * Open the input in the internal Web browser.
248      */

249     public static void open(WebBrowserEditorInput input) {
250         IWorkbenchWindow workbenchWindow = WebBrowserUIPlugin.getInstance().getWorkbench().getActiveWorkbenchWindow();
251         IWorkbenchPage page = workbenchWindow.getActivePage();
252
253         try {
254             IEditorReference[] editors = page.getEditorReferences();
255             int size = editors.length;
256             for (int i = 0; i < size; i++) {
257                 if (WEB_BROWSER_EDITOR_ID.equals(editors[i].getId())) {
258                     IEditorPart editor = editors[i].getEditor(true);
259                     if (editor != null && editor instanceof WebBrowserEditor) {
260                         WebBrowserEditor webEditor = (WebBrowserEditor) editor;
261                         WebBrowserEditorInput input2 = webEditor.getWebBrowserEditorInput();
262                         if (input2 == null || input.canReplaceInput(input2)) {
263                             editor.init(editor.getEditorSite(), input);
264                             return;
265                         }
266                     }
267                 }
268             }
269             
270             page.openEditor(input, WebBrowserEditor.WEB_BROWSER_EDITOR_ID);
271         } catch (Exception JavaDoc e) {
272             Trace.trace(Trace.SEVERE, "Error opening Web browser", e); //$NON-NLS-1$
273
}
274     }
275     
276     /*
277      * Asks this part to take focus within the workbench.
278      */

279     public void setFocus() {
280         if (webBrowser != null)
281             webBrowser.setFocus();
282     }
283
284     /**
285      * Close the editor correctly.
286      */

287     public boolean close() {
288         final boolean [] result = new boolean[1];
289         Display.getDefault().asyncExec(new Runnable JavaDoc() {
290             public void run() {
291                 result[0] = getEditorSite().getPage().closeEditor(WebBrowserEditor.this, false);
292             }
293         });
294         return result[0];
295     }
296     
297     public IActionBars getActionBars() {
298         return getEditorSite().getActionBars();
299     }
300
301     public void openInExternalBrowser(String JavaDoc url) {
302         final IEditorInput input = getEditorInput();
303         final String JavaDoc id = getEditorSite().getId();
304         Runnable JavaDoc runnable = new Runnable JavaDoc() {
305             public void run() {
306                 doOpenExternalEditor(id, input);
307             }
308         };
309         Display display = getSite().getShell().getDisplay();
310         close();
311         display.asyncExec(runnable);
312     }
313     
314     protected void doOpenExternalEditor(String JavaDoc id, IEditorInput input) {
315         IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry();
316         String JavaDoc name = input.getName();
317         IEditorDescriptor [] editors = registry.getEditors(name);
318         IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
319
320         String JavaDoc editorId = null;
321         for (int i = 0; i < editors.length; i++) {
322             IEditorDescriptor editor = editors[i];
323             if (editor.getId().equals(id))
324                 continue;
325             editorId = editor.getId();
326             break;
327         }
328         
329         IEditorDescriptor ddesc = registry.getDefaultEditor(name);
330         if (ddesc!=null && ddesc.getId().equals(id)) {
331             int dot = name.lastIndexOf('.');
332             String JavaDoc ext = name;
333             if (dot!= -1)
334                 ext = "*."+name.substring(dot+1); //$NON-NLS-1$
335
registry.setDefaultEditor(ext, null);
336         }
337  
338          if (editorId==null) {
339             // no editor
340
// next check with the OS for an external editor
341
if (registry.isSystemExternalEditorAvailable(name))
342                 editorId = IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID;
343         }
344
345         if (editorId!=null) {
346             try {
347                 page.openEditor(input, editorId);
348                 return;
349             } catch (PartInitException e) {
350                     // ignore
351
}
352         }
353         
354         // no registered editor - open using browser support
355
try {
356             URL JavaDoc theURL = new URL JavaDoc(webBrowser.getURL());
357             IWorkbenchBrowserSupport support = PlatformUI.getWorkbench().getBrowserSupport();
358             support.getExternalBrowser().openURL(theURL);
359         }
360         catch (MalformedURLException JavaDoc e) {
361             //TODO handle this
362
}
363         catch (PartInitException e) {
364             //TODO handle this
365
}
366     }
367 }
Popular Tags