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;
26 * The editor input for the integrated web browser.
28 public class WebBrowserEditorInput implements IWebBrowserEditorInput, IPersistableElement, IElementFactory {
29 // --- constants to pass into constructor ---
31 // if used, the toolbar will be available
32 public static final int SHOW_TOOLBAR = 1 << 1;
34 // if used, the status bar will be available
35 public static final int SHOW_STATUSBAR = 1 << 2;
37 // if used, this input will always force a new page
38 // and will never reuse an open Web browser
39 public static final int FORCE_NEW_PAGE = 1 << 3;
41 // if used, the original URL will be saved and
42 // the page can reopen to the same URL after
44 public static final int SAVE_URL = 1 << 5;
46 // if used, the browser will be transient and will not appear
47 // in the most recently used file list, nor will it reopen after
49 public static final int TRANSIENT = 1 << 6;
51 public static final int SHOW_ALL = SHOW_TOOLBAR | SHOW_STATUSBAR;
53 private static final String ELEMENT_FACTORY_ID = "net.sourceforge.phpeclipse.webbrowser.elementFactory";
54 private static final String MEMENTO_URL = "url";
55 private static final String MEMENTO_STYLE = "style";
56 private static final String MEMENTO_ID = "id";
60 private String id = null;
63 * WebBrowser editor input for the homepage.
65 public WebBrowserEditorInput() {
70 * WebBrowserEditorInput constructor comment.
72 public WebBrowserEditorInput(URL url) {
73 this(url, SHOW_ALL | SAVE_URL);
77 * WebBrowserEditorInput constructor comment.
79 public WebBrowserEditorInput(URL url, int style) {
86 * WebBrowserEditorInput constructor comment.
88 public WebBrowserEditorInput(URL url, int style, String browserId) {
96 * WebBrowserEditorInput constructor comment.
98 public WebBrowserEditorInput(URL url, boolean b) {
103 * Returns true if this page can reuse the browser that the
104 * given input is being displayed in, or false if it should
105 * open up in a new page.
107 * @param input net.sourceforge.phpeclipse.webbrowser.IWebBrowserEditorInput
110 public boolean canReplaceInput(IWebBrowserEditorInput input) {
111 Trace.trace(Trace.FINEST, "canReplaceInput " + this + " " + input);
112 if ((style & FORCE_NEW_PAGE) != 0)
114 else if (input.isToolbarVisible() != isToolbarVisible())
116 else if (input.isStatusbarVisible() != isStatusbarVisible())
118 else if (id != null) {
119 if (!(input instanceof WebBrowserEditorInput))
121 String bid = ((WebBrowserEditorInput) input).getBrowserId();
122 return id.equals(bid);
128 * Creates an <code>IElement</code> from the state captured within
129 * an <code>IMemento</code>.
131 * @param memento a memento containing the state for an element
132 * @return an element, or <code>null</code> if the element could not be created
134 public IAdaptable createElement(IMemento memento) {
137 url2 = new URL(WebBrowserPreference.getHomePageURL());
138 } catch (Exception e) {
139 // could not determine the URL
142 int newStyle = SHOW_TOOLBAR | SHOW_STATUSBAR;
144 newStyle = memento.getInteger(MEMENTO_STYLE).intValue();
146 if ((newStyle & SAVE_URL) != 0)
147 url = new URL(memento.getString(MEMENTO_URL));
148 } catch (Exception e) {
149 // could not determine the style
154 id2 = memento.getString(MEMENTO_ID);
155 if (id2 != null && id2.length() < 1)
157 } catch (Exception e) { }
159 return new WebBrowserEditorInput(url2, newStyle, id2);
163 * Indicates whether some other object is "equal to" this one.
164 * In this case it means that the underlying IFolders are equal.
166 public boolean equals(Object obj) {
169 if (!(obj instanceof WebBrowserEditorInput))
171 WebBrowserEditorInput other = (WebBrowserEditorInput) obj;
173 if (url != null && !url.equals(obj))
176 return canReplaceInput(other);
180 * Returns whether the editor input exists.
182 * This method is primarily used to determine if an editor input should
183 * appear in the "File Most Recently Used" menu. An editor input will appear
184 * in the list until the return value of <code>exists</code> becomes
185 * <code>false</code> or it drops off the bottom of the list.
187 * @return <code>true</code> if the editor input exists; <code>false</code>
190 public boolean exists() {
191 if ((style & TRANSIENT) != 0)
198 * Returns an object which is an instance of the given class
199 * associated with this object. Returns <code>null</code> if
200 * no such object can be found.
202 * @param adapter the adapter class to look up
203 * @return a object castable to the given class,
204 * or <code>null</code> if this object does not
205 * have an adapter for the given class
207 public Object getAdapter(Class adapter) {
212 * Returns the ID of an element factory which can be used to recreate
213 * this object. An element factory extension with this ID must exist
214 * within the workbench registry.
216 * @return the element factory ID
218 public String getFactoryId() {
219 return ELEMENT_FACTORY_ID;
222 public ImageDescriptor getImageDescriptor() {
223 return ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
227 * Returns the name of this editor input for display purposes.
229 * For instance, if the fully qualified input name is
230 * <code>"a\b\MyFile.gif"</code>, the return value would be just
231 * <code>"MyFile.gif"</code>.
233 * @return the file name string
235 public String getName() {
236 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
240 * Returns an object that can be used to save the state of this editor input.
242 * @return the persistable element, or <code>null</code> if this editor input
243 * cannot be persisted
245 public IPersistableElement getPersistable() {
246 if ((style & TRANSIENT) != 0)
252 public String getToolTipText() {
254 return url.toExternalForm();
256 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
262 * @return java.net.URL
264 public URL getURL() {
269 * Returns the browser id. Browsers with a set id will always & only be
270 * replaced by browsers with the same id.
274 public String getBrowserId() {
279 * Returns true if the status bar should be shown.
283 public boolean isStatusbarVisible() {
284 return (style & SHOW_STATUSBAR) != 0;
288 * Returns true if the toolbar should be shown.
292 public boolean isToolbarVisible() {
293 return (style & SHOW_TOOLBAR) != 0;
297 * Saves the state of an element within a memento.
299 * @param memento the storage area for element state
301 public void saveState(IMemento memento) {
302 if ((style & SAVE_URL) != 0 && url != null)
303 memento.putString(MEMENTO_URL, url.toExternalForm());
305 memento.putInteger(MEMENTO_STYLE, style);
308 memento.putString(MEMENTO_ID, id);
312 * Converts this object to a string.
314 * @return java.lang.String
316 public String toString() {
317 return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]";