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 org.eclipse.webbrowser;
14 import org.eclipse.jface.resource.ImageDescriptor;
15 import org.eclipse.ui.*;
16 import org.eclipse.webbrowser.internal.ImageResource;
17 import org.eclipse.webbrowser.internal.Trace;
18 import org.eclipse.webbrowser.internal.WebBrowserPreference;
19 import org.eclipse.webbrowser.internal.WebBrowserUIPlugin;
20 import org.eclipse.core.runtime.IAdaptable;
22 * The editor input for the integrated web browser.
24 public class WebBrowserEditorInput implements IWebBrowserEditorInput, IPersistableElement, IElementFactory {
25 // --- constants to pass into constructor ---
27 // if used, the toolbar will be available
28 public static final int SHOW_TOOLBAR = 1 << 1;
30 // if used, the status bar will be available
31 public static final int SHOW_STATUSBAR = 1 << 2;
33 // if used, this input will always force a new page
34 // and will never reuse an open Web browser
35 public static final int FORCE_NEW_PAGE = 1 << 3;
37 // if used, the original URL will be saved and
38 // the page can reopen to the same URL after
40 public static final int SAVE_URL = 1 << 5;
42 // if used, the browser will be transient and will not appear
43 // in the most recently used file list, nor will it reopen after
45 public static final int TRANSIENT = 1 << 6;
47 public static final int SHOW_ALL = SHOW_TOOLBAR | SHOW_STATUSBAR;
49 private static final String ELEMENT_FACTORY_ID = "org.eclipse.webbrowser.elementFactory";
50 private static final String MEMENTO_URL = "url";
51 private static final String MEMENTO_STYLE = "style";
52 private static final String MEMENTO_ID = "id";
56 private String id = null;
59 * WebBrowser editor input for the homepage.
61 public WebBrowserEditorInput() {
66 * WebBrowserEditorInput constructor comment.
68 public WebBrowserEditorInput(URL url) {
69 this(url, SHOW_ALL | SAVE_URL);
73 * WebBrowserEditorInput constructor comment.
75 public WebBrowserEditorInput(URL url, int style) {
82 * WebBrowserEditorInput constructor comment.
84 public WebBrowserEditorInput(URL url, int style, String browserId) {
92 * WebBrowserEditorInput constructor comment.
94 public WebBrowserEditorInput(URL url, boolean b) {
99 * Returns true if this page can reuse the browser that the
100 * given input is being displayed in, or false if it should
101 * open up in a new page.
103 * @param input org.eclipse.webbrowser.IWebBrowserEditorInput
106 public boolean canReplaceInput(IWebBrowserEditorInput input) {
107 Trace.trace(Trace.FINEST, "canReplaceInput " + this + " " + input);
108 if ((style & FORCE_NEW_PAGE) != 0)
110 else if (input.isToolbarVisible() != isToolbarVisible())
112 else if (input.isStatusbarVisible() != isStatusbarVisible())
114 else if (id != null) {
115 if (!(input instanceof WebBrowserEditorInput))
117 String bid = ((WebBrowserEditorInput) input).getBrowserId();
118 return id.equals(bid);
124 * Creates an <code>IElement</code> from the state captured within
125 * an <code>IMemento</code>.
127 * @param memento a memento containing the state for an element
128 * @return an element, or <code>null</code> if the element could not be created
130 public IAdaptable createElement(IMemento memento) {
133 url2 = new URL(WebBrowserPreference.getHomePageURL());
134 } catch (Exception e) {
135 // could not determine the URL
138 int newStyle = SHOW_TOOLBAR | SHOW_STATUSBAR;
140 newStyle = memento.getInteger(MEMENTO_STYLE).intValue();
142 if ((newStyle & SAVE_URL) != 0)
143 url = new URL(memento.getString(MEMENTO_URL));
144 } catch (Exception e) {
145 // could not determine the style
150 id2 = memento.getString(MEMENTO_ID);
151 if (id2 != null && id2.length() < 1)
153 } catch (Exception e) { }
155 return new WebBrowserEditorInput(url2, newStyle, id2);
159 * Indicates whether some other object is "equal to" this one.
160 * In this case it means that the underlying IFolders are equal.
162 public boolean equals(Object obj) {
165 if (!(obj instanceof WebBrowserEditorInput))
167 WebBrowserEditorInput other = (WebBrowserEditorInput) obj;
169 if (url != null && !url.equals(obj))
172 return canReplaceInput(other);
176 * Returns whether the editor input exists.
178 * This method is primarily used to determine if an editor input should
179 * appear in the "File Most Recently Used" menu. An editor input will appear
180 * in the list until the return value of <code>exists</code> becomes
181 * <code>false</code> or it drops off the bottom of the list.
183 * @return <code>true</code> if the editor input exists; <code>false</code>
186 public boolean exists() {
187 if ((style & TRANSIENT) != 0)
194 * Returns an object which is an instance of the given class
195 * associated with this object. Returns <code>null</code> if
196 * no such object can be found.
198 * @param adapter the adapter class to look up
199 * @return a object castable to the given class,
200 * or <code>null</code> if this object does not
201 * have an adapter for the given class
203 public Object getAdapter(Class adapter) {
208 * Returns the ID of an element factory which can be used to recreate
209 * this object. An element factory extension with this ID must exist
210 * within the workbench registry.
212 * @return the element factory ID
214 public String getFactoryId() {
215 return ELEMENT_FACTORY_ID;
218 public ImageDescriptor getImageDescriptor() {
219 return ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
223 * Returns the name of this editor input for display purposes.
225 * For instance, if the fully qualified input name is
226 * <code>"a\b\MyFile.gif"</code>, the return value would be just
227 * <code>"MyFile.gif"</code>.
229 * @return the file name string
231 public String getName() {
232 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
236 * Returns an object that can be used to save the state of this editor input.
238 * @return the persistable element, or <code>null</code> if this editor input
239 * cannot be persisted
241 public IPersistableElement getPersistable() {
242 if ((style & TRANSIENT) != 0)
248 public String getToolTipText() {
250 return url.toExternalForm();
252 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
258 * @return java.net.URL
260 public URL getURL() {
265 * Returns the browser id. Browsers with a set id will always & only be
266 * replaced by browsers with the same id.
270 public String getBrowserId() {
275 * Returns true if the status bar should be shown.
279 public boolean isStatusbarVisible() {
280 return (style & SHOW_STATUSBAR) != 0;
284 * Returns true if the toolbar should be shown.
288 public boolean isToolbarVisible() {
289 return (style & SHOW_TOOLBAR) != 0;
293 * Saves the state of an element within a memento.
295 * @param memento the storage area for element state
297 public void saveState(IMemento memento) {
298 if ((style & SAVE_URL) != 0 && url != null)
299 memento.putString(MEMENTO_URL, url.toExternalForm());
301 memento.putInteger(MEMENTO_STYLE, style);
304 memento.putString(MEMENTO_ID, id);
308 * Converts this object to a string.
310 * @return java.lang.String
312 public String toString() {
313 return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]";