2 * Copyright (c) 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
9 * IBM - Initial API and implementation
11 package net.sourceforge.phpeclipse.webbrowser.internal;
15 import net.sourceforge.phpeclipse.webbrowser.IWebBrowserEditorInput;
16 import net.sourceforge.phpeclipse.webbrowser.WebBrowserEditorInput;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IMarker;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.resources.IResourceChangeEvent;
22 import org.eclipse.core.resources.IResourceChangeListener;
23 import org.eclipse.core.resources.IResourceDelta;
24 import org.eclipse.core.resources.IResourceDeltaVisitor;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jface.action.IAction;
28 import org.eclipse.jface.dialogs.IDialogConstants;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.resource.ImageDescriptor;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.IEditorReference;
37 import org.eclipse.ui.IEditorSite;
38 import org.eclipse.ui.IFileEditorInput;
39 import org.eclipse.ui.IWorkbenchPage;
40 import org.eclipse.ui.IWorkbenchWindow;
41 import org.eclipse.ui.part.EditorPart;
44 * An integrated Web browser, defined as an editor to make better use of the
47 public class WebBrowserEditor extends EditorPart {
48 public static final String WEB_BROWSER_EDITOR_ID = "net.sourceforge.phpeclipse.webbrowser";
50 protected WebBrowser webBrowser;
52 protected String initialURL;
54 protected Image image;
56 protected TextAction cutAction;
58 protected TextAction copyAction;
60 protected TextAction pasteAction;
62 protected IResourceChangeListener resourceListener;
65 * WebBrowserEditor constructor comment.
67 public WebBrowserEditor() {
72 * Creates the SWT controls for this workbench part.
74 * Clients should not call this method (the workbench calls this method at
78 * For implementors this is a multi-step process:
80 * <li>Create one or more controls within the parent.</li>
81 * <li>Set the parent layout as needed.</li>
82 * <li>Register any global actions with the <code>IActionService</code>.</li>
83 * <li>Register any popup menus with the <code>IActionService</code>.</li>
84 * <li>Register a selection provider with the
85 * <code>ISelectionService</code> (optional). </li>
92 public void createPartControl(Composite parent) {
93 IWebBrowserEditorInput input = getWebBrowserEditorInput();
95 if (input == null || input.isToolbarVisible() == false)
96 webBrowser = new WebBrowser(parent, false, input
97 .isStatusbarVisible());
99 webBrowser = new WebBrowser(parent, true, input
100 .isStatusbarVisible());
101 cutAction = new TextAction(webBrowser, TextAction.CUT);
102 copyAction = new TextAction(webBrowser, TextAction.COPY);
103 pasteAction = new TextAction(webBrowser, TextAction.PASTE);
106 webBrowser.setURL(initialURL);
107 webBrowser.editor = this;
110 public void dispose() {
111 if (image != null && !image.isDisposed())
115 if (resourceListener != null)
116 ResourcesPlugin.getWorkspace().removeResourceChangeListener(
121 * (non-Javadoc) Saves the contents of this editor. <p> Subclasses must
122 * override this method to implement the open-save-close lifecycle for an
123 * editor. For greater details, see <code>IEditorPart</code> </p>
127 public void doSave(IProgressMonitor monitor) {
131 * (non-Javadoc) Saves the contents of this editor to another object. <p>
132 * Subclasses must override this method to implement the open-save-close
133 * lifecycle for an editor. For greater details, see <code>IEditorPart</code>
138 public void doSaveAs() {
142 * Returns the copy action.
144 * @return org.eclipse.jface.action.IAction
146 public IAction getCopyAction() {
151 * Returns the cut action.
153 * @return org.eclipse.jface.action.IAction
155 public IAction getCutAction() {
160 * Returns the paste action.
162 * @return org.eclipse.jface.action.IAction
164 public IAction getPasteAction() {
169 * Returns the web editor input, if available.
171 * @return net.sourceforge.phpeclipse.webbrowser.IWebBrowserEditorInput
173 protected IWebBrowserEditorInput getWebBrowserEditorInput() {
174 IEditorInput input = getEditorInput();
175 if (input instanceof IWebBrowserEditorInput)
176 return (IWebBrowserEditorInput) input;
181 * (non-Javadoc) Sets the cursor and selection state for this editor to the
182 * passage defined by the given marker. <p> Subclasses may override. For
183 * greater details, see <code>IEditorPart</code> </p>
187 public void gotoMarker(IMarker marker) {
191 * (non-Javadoc) Initializes the editor part with a site and input. <p>
192 * Subclasses of <code>EditorPart</code> must implement this method.
193 * Within the implementation subclasses should verify that the input type is
194 * acceptable and then save the site and input. Here is sample code: </p>
195 * <pre> if (!(input instanceof IFileEditorInput)) throw new
196 * PartInitException("Invalid Input: Must be IFileEditorInput");
197 * setSite(site); setInput(editorInput); </pre>
199 public void init(IEditorSite site, IEditorInput input) {
200 Trace.trace(Trace.FINEST, "Opening browser: " + input);
201 if (input instanceof IFileEditorInput) {
202 IFileEditorInput fei = (IFileEditorInput) input;
203 IFile file = fei.getFile();
206 if (file != null && file.exists())
207 url = file.getLocation().toFile().toURL();
208 } catch (Exception e) {
209 Trace.trace(Trace.SEVERE, "Error getting URL to file");
211 addResourceListener(file);
212 input = new WebBrowserEditorInput(url,
213 WebBrowserEditorInput.SHOW_ALL
214 | WebBrowserEditorInput.SAVE_URL);
216 if (input instanceof IWebBrowserEditorInput) {
217 IWebBrowserEditorInput wbei = (IWebBrowserEditorInput) input;
219 if (wbei.getURL() != null)
220 initialURL = wbei.getURL().toExternalForm();
221 if (webBrowser != null) {
222 webBrowser.setURL(initialURL);
223 site.getWorkbenchWindow().getActivePage().bringToTop(this);
226 setPartName(wbei.getName());
227 setTitleToolTip(wbei.getToolTipText());
229 Image oldImage = image;
230 ImageDescriptor id = wbei.getImageDescriptor();
231 image = id.createImage();
233 setTitleImage(image);
234 if (oldImage != null && !oldImage.isDisposed())
242 * (non-Javadoc) Returns whether the contents of this editor have changed
243 * since the last save operation. <p> Subclasses must override this method
244 * to implement the open-save-close lifecycle for an editor. For greater
245 * details, see <code>IEditorPart</code> </p>
249 public boolean isDirty() {
254 * (non-Javadoc) Returns whether the "save as" operation is supported by
255 * this editor. <p> Subclasses must override this method to implement the
256 * open-save-close lifecycle for an editor. For greater details, see <code>IEditorPart</code>
261 public boolean isSaveAsAllowed() {
266 * Returns true if this editor has a toolbar.
270 public boolean isToolbarVisible() {
271 IWebBrowserEditorInput input = getWebBrowserEditorInput();
272 if (input == null || input.isToolbarVisible())
279 * Open the input in the internal Web browser.
281 public static void open(IWebBrowserEditorInput input) {
282 IWorkbenchWindow workbenchWindow = WebBrowserUIPlugin.getInstance()
283 .getWorkbench().getActiveWorkbenchWindow();
284 IWorkbenchPage page = workbenchWindow.getActivePage();
287 IEditorReference[] editors = page.getEditorReferences();
288 int size = editors.length;
289 for (int i = 0; i < size; i++) {
290 if (WEB_BROWSER_EDITOR_ID.equals(editors[i].getId())) {
291 IEditorPart editor = editors[i].getEditor(true);
292 if (editor != null && editor instanceof WebBrowserEditor) {
293 WebBrowserEditor webEditor = (WebBrowserEditor) editor;
294 if (input.canReplaceInput(webEditor
295 .getWebBrowserEditorInput())) {
296 editor.init(editor.getEditorSite(), input);
303 page.openEditor(input, WebBrowserEditor.WEB_BROWSER_EDITOR_ID);
304 } catch (Exception e) {
305 Trace.trace(Trace.SEVERE, "Error opening Web browser", e);
310 * Asks this part to take focus within the workbench.
312 * Clients should not call this method (the workbench calls this method at
313 * appropriate times).
316 public void setFocus() {
317 if (webBrowser != null) {
318 if (webBrowser.combo != null)
319 webBrowser.combo.setFocus();
321 webBrowser.browser.setFocus();
322 webBrowser.updateHistory();
327 * Update the actions.
329 protected void updateActions() {
330 if (cutAction != null)
332 if (copyAction != null)
334 if (pasteAction != null)
335 pasteAction.update();
339 * Close the editor correctly.
341 protected void closeEditor() {
342 Display.getDefault().asyncExec(new Runnable() {
344 getEditorSite().getPage().closeEditor(WebBrowserEditor.this,
351 * Adds a resource change listener to see if the file is deleted.
353 protected void addResourceListener(final IResource resource) {
354 if (resource == null)
357 resourceListener = new IResourceChangeListener() {
358 public void resourceChanged(IResourceChangeEvent event) {
360 event.getDelta().accept(new IResourceDeltaVisitor() {
361 public boolean visit(IResourceDelta delta) {
362 IResource res = delta.getResource();
364 if (res == null || !res.equals(resource))
367 if (delta.getKind() != IResourceDelta.REMOVED)
370 Display.getDefault().asyncExec(new Runnable() {
372 String title = WebBrowserUIPlugin
373 .getResource("%dialogResourceDeletedTitle");
374 String message = WebBrowserUIPlugin
376 "%dialogResourceDeletedMessage",
378 String[] labels = new String[] {
380 .getResource("%dialogResourceDeletedIgnore"),
381 IDialogConstants.CLOSE_LABEL };
382 MessageDialog dialog = new MessageDialog(
383 getEditorSite().getShell(), title,
385 MessageDialog.INFORMATION, labels,
388 if (dialog.open() != 0)
395 } catch (Exception e) {
396 Trace.trace(Trace.SEVERE,
397 "Error listening for resource deletion", e);
401 ResourcesPlugin.getWorkspace().addResourceChangeListener(