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;
 
  15 import net.sourceforge.phpeclipse.webbrowser.internal.ImageResource;
 
  16 import net.sourceforge.phpeclipse.webbrowser.internal.Trace;
 
  17 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserPreference;
 
  18 import net.sourceforge.phpeclipse.webbrowser.internal.WebBrowserUIPlugin;
 
  20 import org.eclipse.core.runtime.IAdaptable;
 
  21 import org.eclipse.jface.resource.ImageDescriptor;
 
  22 import org.eclipse.ui.IElementFactory;
 
  23 import org.eclipse.ui.IMemento;
 
  24 import org.eclipse.ui.IPersistableElement;
 
  27  * The editor input for the integrated web browser.
 
  29 public class WebBrowserEditorInput implements IWebBrowserEditorInput,
 
  30                 IPersistableElement, IElementFactory {
 
  31         // --- constants to pass into constructor ---
 
  33         // if used, the toolbar will be available
 
  34         public static final int SHOW_TOOLBAR = 1 << 1;
 
  36         // if used, the status bar will be available
 
  37         public static final int SHOW_STATUSBAR = 1 << 2;
 
  39         // if used, this input will always force a new page
 
  40         // and will never reuse an open Web browser
 
  41         public static final int FORCE_NEW_PAGE = 1 << 3;
 
  43         // if used, the original URL will be saved and
 
  44         // the page can reopen to the same URL after
 
  46         public static final int SAVE_URL = 1 << 5;
 
  48         // if used, the browser will be transient and will not appear
 
  49         // in the most recently used file list, nor will it reopen after
 
  51         public static final int TRANSIENT = 1 << 6;
 
  53         public static final int SHOW_ALL = SHOW_TOOLBAR | SHOW_STATUSBAR;
 
  55         private static final String ELEMENT_FACTORY_ID = "net.sourceforge.phpeclipse.webbrowser.elementFactory";
 
  57         private static final String MEMENTO_URL = "url";
 
  59         private static final String MEMENTO_STYLE = "style";
 
  61         private static final String MEMENTO_ID = "id";
 
  67         private String id = null;
 
  70          * WebBrowser editor input for the homepage.
 
  72         public WebBrowserEditorInput() {
 
  77          * WebBrowserEditorInput constructor comment.
 
  79         public WebBrowserEditorInput(URL url) {
 
  80                 this(url, SHOW_ALL | SAVE_URL);
 
  84          * WebBrowserEditorInput constructor comment.
 
  86         public WebBrowserEditorInput(URL url, int style) {
 
  93          * WebBrowserEditorInput constructor comment.
 
  95         public WebBrowserEditorInput(URL url, int style, String browserId) {
 
 103          * WebBrowserEditorInput constructor comment.
 
 105         public WebBrowserEditorInput(URL url, boolean b) {
 
 110          * Returns true if this page can reuse the browser that the given input is
 
 111          * being displayed in, or false if it should open up in a new page.
 
 114          *            net.sourceforge.phpeclipse.webbrowser.IWebBrowserEditorInput
 
 117         public boolean canReplaceInput(IWebBrowserEditorInput input) {
 
 118                 Trace.trace(Trace.FINEST, "canReplaceInput " + this + " " + input);
 
 119                 if ((style & FORCE_NEW_PAGE) != 0)
 
 121                 else if (input.isToolbarVisible() != isToolbarVisible())
 
 123                 else if (input.isStatusbarVisible() != isStatusbarVisible())
 
 125                 else if (id != null) {
 
 126                         if (!(input instanceof WebBrowserEditorInput))
 
 128                         String bid = ((WebBrowserEditorInput) input).getBrowserId();
 
 129                         return id.equals(bid);
 
 135          * Creates an <code>IElement</code> from the state captured within an
 
 136          * <code>IMemento</code>.
 
 139          *            a memento containing the state for an element
 
 140          * @return an element, or <code>null</code> if the element could not be
 
 143         public IAdaptable createElement(IMemento memento) {
 
 146                         url2 = new URL(WebBrowserPreference.getHomePageURL());
 
 147                 } catch (Exception e) {
 
 148                         // could not determine the URL
 
 151                 int newStyle = SHOW_TOOLBAR | SHOW_STATUSBAR;
 
 153                         newStyle = memento.getInteger(MEMENTO_STYLE).intValue();
 
 155                         if ((newStyle & SAVE_URL) != 0)
 
 156                                 url = new URL(memento.getString(MEMENTO_URL));
 
 157                 } catch (Exception e) {
 
 158                         // could not determine the style
 
 163                         id2 = memento.getString(MEMENTO_ID);
 
 164                         if (id2 != null && id2.length() < 1)
 
 166                 } catch (Exception e) {
 
 169                 return new WebBrowserEditorInput(url2, newStyle, id2);
 
 173          * Indicates whether some other object is "equal to" this one. In this case
 
 174          * it means that the underlying IFolders are equal.
 
 176         public boolean equals(Object obj) {
 
 179                 if (!(obj instanceof WebBrowserEditorInput))
 
 181                 WebBrowserEditorInput other = (WebBrowserEditorInput) obj;
 
 183                 if (url != null && !url.equals(obj))
 
 186                 return canReplaceInput(other);
 
 190          * Returns whether the editor input exists.
 
 192          * This method is primarily used to determine if an editor input should
 
 193          * appear in the "File Most Recently Used" menu. An editor input will appear
 
 194          * in the list until the return value of <code>exists</code> becomes
 
 195          * <code>false</code> or it drops off the bottom of the list.
 
 197          * @return <code>true</code> if the editor input exists;
 
 198          *         <code>false</code> otherwise
 
 200         public boolean exists() {
 
 201                 if ((style & TRANSIENT) != 0)
 
 208          * Returns an object which is an instance of the given class associated with
 
 209          * this object. Returns <code>null</code> if no such object can be found.
 
 212          *            the adapter class to look up
 
 213          * @return a object castable to the given class, or <code>null</code> if
 
 214          *         this object does not have an adapter for the given class
 
 216         public Object getAdapter(Class adapter) {
 
 221          * Returns the ID of an element factory which can be used to recreate this
 
 222          * object. An element factory extension with this ID must exist within the
 
 223          * workbench registry.
 
 225          * @return the element factory ID
 
 227         public String getFactoryId() {
 
 228                 return ELEMENT_FACTORY_ID;
 
 231         public ImageDescriptor getImageDescriptor() {
 
 233                                 .getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
 
 237          * Returns the name of this editor input for display purposes.
 
 239          * For instance, if the fully qualified input name is
 
 240          * <code>"a\b\MyFile.gif"</code>, the return value would be just
 
 241          * <code>"MyFile.gif"</code>.
 
 243          * @return the file name string
 
 245         public String getName() {
 
 246                 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
 
 250          * Returns an object that can be used to save the state of this editor
 
 253          * @return the persistable element, or <code>null</code> if this editor
 
 254          * input cannot be persisted
 
 256         public IPersistableElement getPersistable() {
 
 257                 if ((style & TRANSIENT) != 0)
 
 263         public String getToolTipText() {
 
 265                         return url.toExternalForm();
 
 267                         return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
 
 273          * @return java.net.URL
 
 275         public URL getURL() {
 
 280          * Returns the browser id. Browsers with a set id will always & only be
 
 281          * replaced by browsers with the same id.
 
 285         public String getBrowserId() {
 
 290          * Returns true if the status bar should be shown.
 
 294         public boolean isStatusbarVisible() {
 
 295                 return (style & SHOW_STATUSBAR) != 0;
 
 299          * Returns true if the toolbar should be shown.
 
 303         public boolean isToolbarVisible() {
 
 304                 return (style & SHOW_TOOLBAR) != 0;
 
 308          * Saves the state of an element within a memento.
 
 311          *            the storage area for element state
 
 313         public void saveState(IMemento memento) {
 
 314                 if ((style & SAVE_URL) != 0 && url != null)
 
 315                         memento.putString(MEMENTO_URL, url.toExternalForm());
 
 317                 memento.putInteger(MEMENTO_STYLE, style);
 
 320                         memento.putString(MEMENTO_ID, id);
 
 324          * Converts this object to a string.
 
 326          * @return java.lang.String
 
 328         public String toString() {
 
 329                 return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]";