/** * Copyright (c) 2003 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html * * Contributors: * IBM - Initial API and implementation */ package org.eclipse.webbrowser.internal; import java.net.URL; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.jface.action.IAction; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.core.runtime.*; import org.eclipse.core.resources.*; import org.eclipse.ui.*; import org.eclipse.ui.part.*; import org.eclipse.webbrowser.*; /** * An integrated Web browser, defined as an editor to make * better use of the desktop. */ public class WebBrowserEditor extends EditorPart { public static final String WEB_BROWSER_EDITOR_ID = "org.eclipse.webbrowser"; protected WebBrowser webBrowser; protected String initialURL; protected Image image; protected TextAction cutAction; protected TextAction copyAction; protected TextAction pasteAction; protected IResourceChangeListener resourceListener; /** * WebBrowserEditor constructor comment. */ public WebBrowserEditor() { super(); } /** * Creates the SWT controls for this workbench part. *
* Clients should not call this method (the workbench calls this method at * appropriate times). *
** For implementors this is a multi-step process: *
IActionService.IActionService.ISelectionService
* (optional).
* Subclasses must override this method to implement the open-save-close lifecycle
* for an editor. For greater details, see IEditorPart
*
* Subclasses must override this method to implement the open-save-close lifecycle
* for an editor. For greater details, see IEditorPart
*
* Subclasses may override. For greater details, see IEditorPart
*
* Subclasses of EditorPart must implement this method. Within
* the implementation subclasses should verify that the input type is acceptable
* and then save the site and input. Here is sample code:
*
* if (!(input instanceof IFileEditorInput))
* throw new PartInitException("Invalid Input: Must be IFileEditorInput");
* setSite(site);
* setInput(editorInput);
*
*/
public void init(IEditorSite site, IEditorInput input) {
Trace.trace(Trace.FINEST, "Opening browser: " + input);
if (input instanceof IFileEditorInput) {
IFileEditorInput fei = (IFileEditorInput) input;
IFile file = fei.getFile();
URL url = null;
try {
if (file != null && file.exists())
url = file.getLocation().toFile().toURL();
} catch (Exception e) {
Trace.trace(Trace.SEVERE, "Error getting URL to file");
}
addResourceListener(file);
input = new WebBrowserEditorInput(url, WebBrowserEditorInput.SHOW_ALL | WebBrowserEditorInput.SAVE_URL);
}
if (input instanceof IWebBrowserEditorInput) {
IWebBrowserEditorInput wbei = (IWebBrowserEditorInput) input;
initialURL = null;
if (wbei.getURL() != null)
initialURL = wbei.getURL().toExternalForm();
if (webBrowser != null) {
webBrowser.setURL(initialURL);
site.getWorkbenchWindow().getActivePage().bringToTop(this);
}
setPartName(wbei.getName());
setTitleToolTip(wbei.getToolTipText());
Image oldImage = image;
ImageDescriptor id = wbei.getImageDescriptor();
image = id.createImage();
setTitleImage(image);
if (oldImage != null && !oldImage.isDisposed())
oldImage.dispose();
}
setSite(site);
setInput(input);
}
/* (non-Javadoc)
* Returns whether the contents of this editor have changed since the last save
* operation.
*
* Subclasses must override this method to implement the open-save-close lifecycle
* for an editor. For greater details, see IEditorPart
*
* Subclasses must override this method to implement the open-save-close lifecycle
* for an editor. For greater details, see IEditorPart
*
* Clients should not call this method (the workbench calls this method at * appropriate times). *
*/ public void setFocus() { if (webBrowser != null) { if (webBrowser.combo != null) webBrowser.combo.setFocus(); else webBrowser.browser.setFocus(); webBrowser.updateHistory(); } } /** * Update the actions. */ protected void updateActions() { if (cutAction != null) cutAction.update(); if (copyAction != null) copyAction.update(); if (pasteAction != null) pasteAction.update(); } /** * Close the editor correctly. */ protected void closeEditor() { Display.getDefault().asyncExec(new Runnable() { public void run() { getEditorSite().getPage().closeEditor(WebBrowserEditor.this, false); } }); } /** * Adds a resource change listener to see if the file is deleted. */ protected void addResourceListener(final IResource resource) { if (resource == null) return; resourceListener = new IResourceChangeListener() { public void resourceChanged(IResourceChangeEvent event) { try { event.getDelta().accept(new IResourceDeltaVisitor() { public boolean visit(IResourceDelta delta) { IResource res = delta.getResource(); if (res == null || !res.equals(resource)) return true; if (delta.getKind() != IResourceDelta.REMOVED) return true; Display.getDefault().asyncExec(new Runnable() { public void run() { String title = WebBrowserUIPlugin.getResource("%dialogResourceDeletedTitle"); String message = WebBrowserUIPlugin.getResource("%dialogResourceDeletedMessage", resource.getName()); String[] labels = new String[] {WebBrowserUIPlugin.getResource("%dialogResourceDeletedIgnore"), IDialogConstants.CLOSE_LABEL}; MessageDialog dialog = new MessageDialog(getEditorSite().getShell(), title, null, message, MessageDialog.INFORMATION, labels, 0); if (dialog.open() != 0) closeEditor(); } }); return false; } }); } catch (Exception e) { Trace.trace(Trace.SEVERE, "Error listening for resource deletion", e); } } }; ResourcesPlugin.getWorkspace().addResourceChangeListener(resourceListener); } }