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.jface.resource.ImageDescriptor;
21 import org.eclipse.ui.*;
22 import org.eclipse.core.runtime.IAdaptable;
24 * The editor input for the integrated web browser.
26 public class WebBrowserEditorInput implements IWebBrowserEditorInput, IPersistableElement, IElementFactory {
27 // --- constants to pass into constructor ---
29 // if used, the toolbar will be available
30 public static final int SHOW_TOOLBAR = 1 << 1;
32 // if used, the status bar will be available
33 public static final int SHOW_STATUSBAR = 1 << 2;
35 // if used, this input will always force a new page
36 // and will never reuse an open Web browser
37 public static final int FORCE_NEW_PAGE = 1 << 3;
39 // if used, the original URL will be saved and
40 // the page can reopen to the same URL after
42 public static final int SAVE_URL = 1 << 5;
44 // if used, the browser will be transient and will not appear
45 // in the most recently used file list, nor will it reopen after
47 public static final int TRANSIENT = 1 << 6;
49 public static final int SHOW_ALL = SHOW_TOOLBAR | SHOW_STATUSBAR;
51 private static final String ELEMENT_FACTORY_ID = "net.sourceforge.phpeclipse.webbrowser.elementFactory";
52 private static final String MEMENTO_URL = "url";
53 private static final String MEMENTO_STYLE = "style";
54 private static final String MEMENTO_ID = "id";
58 private String id = null;
61 * WebBrowser editor input for the homepage.
63 public WebBrowserEditorInput() {
68 * WebBrowserEditorInput constructor comment.
70 public WebBrowserEditorInput(URL url) {
71 this(url, SHOW_ALL | SAVE_URL);
75 * WebBrowserEditorInput constructor comment.
77 public WebBrowserEditorInput(URL url, int style) {
84 * WebBrowserEditorInput constructor comment.
86 public WebBrowserEditorInput(URL url, int style, String browserId) {
94 * WebBrowserEditorInput constructor comment.
96 public WebBrowserEditorInput(URL url, boolean b) {
101 * Returns true if this page can reuse the browser that the
102 * given input is being displayed in, or false if it should
103 * open up in a new page.
105 * @param input net.sourceforge.phpeclipse.webbrowser.IWebBrowserEditorInput
108 public boolean canReplaceInput(IWebBrowserEditorInput input) {
109 Trace.trace(Trace.FINEST, "canReplaceInput " + this + " " + input);
110 if ((style & FORCE_NEW_PAGE) != 0)
112 else if (input.isToolbarVisible() != isToolbarVisible())
114 else if (input.isStatusbarVisible() != isStatusbarVisible())
116 else if (id != null) {
117 if (!(input instanceof WebBrowserEditorInput))
119 String bid = ((WebBrowserEditorInput) input).getBrowserId();
120 return id.equals(bid);
126 * Creates an <code>IElement</code> from the state captured within
127 * an <code>IMemento</code>.
129 * @param memento a memento containing the state for an element
130 * @return an element, or <code>null</code> if the element could not be created
132 public IAdaptable createElement(IMemento memento) {
135 url2 = new URL(WebBrowserPreference.getHomePageURL());
136 } catch (Exception e) {
137 // could not determine the URL
140 int newStyle = SHOW_TOOLBAR | SHOW_STATUSBAR;
142 newStyle = memento.getInteger(MEMENTO_STYLE).intValue();
144 if ((newStyle & SAVE_URL) != 0)
145 url = new URL(memento.getString(MEMENTO_URL));
146 } catch (Exception e) {
147 // could not determine the style
152 id2 = memento.getString(MEMENTO_ID);
153 if (id2 != null && id2.length() < 1)
155 } catch (Exception e) { }
157 return new WebBrowserEditorInput(url2, newStyle, id2);
161 * Indicates whether some other object is "equal to" this one.
162 * In this case it means that the underlying IFolders are equal.
164 public boolean equals(Object obj) {
167 if (!(obj instanceof WebBrowserEditorInput))
169 WebBrowserEditorInput other = (WebBrowserEditorInput) obj;
171 if (url != null && !url.equals(obj))
174 return canReplaceInput(other);
178 * Returns whether the editor input exists.
180 * This method is primarily used to determine if an editor input should
181 * appear in the "File Most Recently Used" menu. An editor input will appear
182 * in the list until the return value of <code>exists</code> becomes
183 * <code>false</code> or it drops off the bottom of the list.
185 * @return <code>true</code> if the editor input exists; <code>false</code>
188 public boolean exists() {
189 if ((style & TRANSIENT) != 0)
196 * Returns an object which is an instance of the given class
197 * associated with this object. Returns <code>null</code> if
198 * no such object can be found.
200 * @param adapter the adapter class to look up
201 * @return a object castable to the given class,
202 * or <code>null</code> if this object does not
203 * have an adapter for the given class
205 public Object getAdapter(Class adapter) {
210 * Returns the ID of an element factory which can be used to recreate
211 * this object. An element factory extension with this ID must exist
212 * within the workbench registry.
214 * @return the element factory ID
216 public String getFactoryId() {
217 return ELEMENT_FACTORY_ID;
220 public ImageDescriptor getImageDescriptor() {
221 return ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
225 * Returns the name of this editor input for display purposes.
227 * For instance, if the fully qualified input name is
228 * <code>"a\b\MyFile.gif"</code>, the return value would be just
229 * <code>"MyFile.gif"</code>.
231 * @return the file name string
233 public String getName() {
234 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
238 * Returns an object that can be used to save the state of this editor input.
240 * @return the persistable element, or <code>null</code> if this editor input
241 * cannot be persisted
243 public IPersistableElement getPersistable() {
244 if ((style & TRANSIENT) != 0)
250 public String getToolTipText() {
252 return url.toExternalForm();
254 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
260 * @return java.net.URL
262 public URL getURL() {
267 * Returns the browser id. Browsers with a set id will always & only be
268 * replaced by browsers with the same id.
272 public String getBrowserId() {
277 * Returns true if the status bar should be shown.
281 public boolean isStatusbarVisible() {
282 return (style & SHOW_STATUSBAR) != 0;
286 * Returns true if the toolbar should be shown.
290 public boolean isToolbarVisible() {
291 return (style & SHOW_TOOLBAR) != 0;
295 * Saves the state of an element within a memento.
297 * @param memento the storage area for element state
299 public void saveState(IMemento memento) {
300 if ((style & SAVE_URL) != 0 && url != null)
301 memento.putString(MEMENTO_URL, url.toExternalForm());
303 memento.putInteger(MEMENTO_STYLE, style);
306 memento.putString(MEMENTO_ID, id);
310 * Converts this object to a string.
312 * @return java.lang.String
314 public String toString() {
315 return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]";