001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - Initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.browser;
011:
012: import java.net.MalformedURLException;
013: import java.net.URL;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.jface.resource.ImageDescriptor;
017: import org.eclipse.ui.IEditorInput;
018: import org.eclipse.ui.IElementFactory;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.IPersistableElement;
021: import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
022:
023: /**
024: * The editor input for the integrated web browser.
025: */
026: public class WebBrowserEditorInput implements IEditorInput,
027: IPersistableElement, IElementFactory {
028: // --- constants to pass into constructor ---
029:
030: // if used, the toolbar will be available
031: // public static final int SHOW_TOOLBAR = 1 << 1;
032:
033: // public static final int SHOW_GLOBAL_TOOLBAR = 1 << 2;
034:
035: // if used, the status bar will be available
036: // public static final int SHOW_STATUSBAR = 1 << 2;
037:
038: // if used, the original URL will be saved and
039: // the page can reopen to the same URL after
040: // shutting down
041: // public static final int SAVE_URL = 1 << 5;
042:
043: // if used, the browser will be transient and will not appear
044: // in the most recently used file list, nor will it reopen after
045: // restarting Eclipse
046: // public static final int TRANSIENT = 1 << 6;
047:
048: private static final String ELEMENT_FACTORY_ID = "org.eclipse.ui.browser.elementFactory"; //$NON-NLS-1$
049:
050: private static final String MEMENTO_URL = "url"; //$NON-NLS-1$
051:
052: private static final String MEMENTO_STYLE = "style"; //$NON-NLS-1$
053:
054: private static final String MEMENTO_ID = "id"; //$NON-NLS-1$
055:
056: private static final String MEMENTO_NAME = "name"; //$NON-NLS-1$
057:
058: private static final String MEMENTO_TOOLTIP = "tooltip"; //$NON-NLS-1$
059:
060: private URL url;
061:
062: private int style;
063:
064: private String id = null;
065:
066: private String name;
067:
068: private String tooltip;
069:
070: /**
071: * ExternalBrowserInstance editor input for the homepage.
072: */
073: public WebBrowserEditorInput() {
074: this (null);
075: }
076:
077: /**
078: * WebBrowserEditorInput constructor comment.
079: */
080: public WebBrowserEditorInput(URL url) {
081: this (url, 0);
082: }
083:
084: /**
085: * WebBrowserEditorInput constructor comment.
086: */
087: public WebBrowserEditorInput(URL url, int style) {
088: super ();
089: this .url = url;
090: this .style = style;
091: }
092:
093: /**
094: * WebBrowserEditorInput constructor comment.
095: */
096: public WebBrowserEditorInput(URL url, int style, String browserId) {
097: super ();
098: this .url = url;
099: this .style = style;
100: this .id = browserId;
101: }
102:
103: /**
104: * WebBrowserEditorInput constructor comment.
105: */
106: public WebBrowserEditorInput(URL url, boolean b) {
107: this (url);
108: }
109:
110: public void setName(String n) {
111: name = n;
112: }
113:
114: public void setToolTipText(String t) {
115: tooltip = t;
116: }
117:
118: /**
119: * Returns true if this page can reuse the browser that the given input is
120: * being displayed in, or false if it should open up in a new page.
121: *
122: * @param input
123: * org.eclipse.ui.internal.browser.IWebBrowserEditorInput
124: * @return boolean
125: */
126: public boolean canReplaceInput(WebBrowserEditorInput input) {
127: Trace.trace(Trace.FINEST,
128: "canReplaceInput " + this + " " + input); //$NON-NLS-1$ //$NON-NLS-2$
129: // if ((style & FORCE_NEW_PAGE) != 0)
130: // return false;
131: // if (input.isToolbarVisible() != isToolbarVisible())
132: // return false;
133: // else
134: if (input.isStatusbarVisible() != isStatusbarVisible())
135: return false;
136: else if (id != null) {
137: String bid = input.getBrowserId();
138: return id.equals(bid);
139: } else
140: return false;
141: }
142:
143: /**
144: * Creates an <code>IElement</code> from the state captured within an
145: * <code>IMemento</code>.
146: *
147: * @param memento
148: * a memento containing the state for an element
149: * @return an element, or <code>null</code> if the element could not be
150: * created
151: */
152: public IAdaptable createElement(IMemento memento) {
153: int style = 0;
154: Integer integer = memento.getInteger(MEMENTO_STYLE);
155: if (integer != null) {
156: style = integer.intValue();
157: }
158:
159: URL url = null;
160: String str = memento.getString(MEMENTO_URL);
161: if (str != null) {
162: try {
163: url = new URL(str);
164: } catch (MalformedURLException e) {
165: String msg = "Malformed URL while initializing browser editor"; //$NON-NLS-1$
166: WebBrowserUIPlugin.logError(msg, e);
167: }
168: }
169:
170: String id = memento.getString(MEMENTO_ID);
171: String name = memento.getString(MEMENTO_NAME);
172: String tooltip = memento.getString(MEMENTO_TOOLTIP);
173:
174: WebBrowserEditorInput input = new WebBrowserEditorInput(url,
175: style, id);
176: input.setName(name);
177: input.setToolTipText(tooltip);
178: return input;
179: }
180:
181: /**
182: * Indicates whether some other object is "equal to" this one. In this case
183: * it means that the underlying IFolders are equal.
184: */
185: public boolean equals(Object obj) {
186: if (this == obj)
187: return true;
188: if (!(obj instanceof WebBrowserEditorInput))
189: return false;
190: WebBrowserEditorInput other = (WebBrowserEditorInput) obj;
191:
192: if (url != null && !url.equals(obj))
193: return false;
194:
195: return canReplaceInput(other);
196: }
197:
198: /*
199: * Returns whether the editor input exists.
200: */
201: public boolean exists() {
202: if ((style & IWorkbenchBrowserSupport.PERSISTENT) != 0)
203: return false;
204:
205: return true;
206: }
207:
208: /**
209: * Returns an object which is an instance of the given class associated with
210: * this object. Returns <code>null</code> if no such object can be found.
211: *
212: * @param adapter
213: * the adapter class to look up
214: * @return a object castable to the given class, or <code>null</code> if
215: * this object does not have an adapter for the given class
216: */
217: public Object getAdapter(Class adapter) {
218: return null;
219: }
220:
221: /**
222: * Returns the ID of an element factory which can be used to recreate this
223: * object. An element factory extension with this ID must exist within the
224: * workbench registry.
225: *
226: * @return the element factory ID
227: */
228: public String getFactoryId() {
229: return ELEMENT_FACTORY_ID;
230: }
231:
232: public ImageDescriptor getImageDescriptor() {
233: return ImageResource
234: .getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
235: }
236:
237: /**
238: * Returns true if the name is locked and cannot be changed.
239: *
240: * @return <code>true</code> if the name of the browser should not change
241: */
242: protected boolean isNameLocked() {
243: return (name != null);
244: }
245:
246: /**
247: * Returns the name of this editor input for display purposes.
248: * <p>
249: * For instance, if the fully qualified input name is
250: * <code>"a\b\MyFile.gif"</code>, the return value would be just
251: * <code>"MyFile.gif"</code>.
252: *
253: * @return the file name string
254: */
255: public String getName() {
256: if (name != null)
257: return name;
258:
259: return Messages.viewWebBrowserTitle;
260: }
261:
262: /*
263: * Returns an object that can be used to save the state of this editor
264: * input. @return the persistable element, or <code>null</code> if this
265: * editor input cannot be persisted
266: */
267: public IPersistableElement getPersistable() {
268: if ((style & IWorkbenchBrowserSupport.PERSISTENT) == 0)
269: return null;
270:
271: return this ;
272: }
273:
274: public String getToolTipText() {
275: if (tooltip != null)
276: return tooltip;
277:
278: if (url != null)
279: return url.toExternalForm();
280:
281: return Messages.viewWebBrowserTitle;
282: }
283:
284: /**
285: * Returns the url.
286: *
287: * @return java.net.URL
288: */
289: public URL getURL() {
290: return url;
291: }
292:
293: /**
294: * Returns the browser id. Browsers with a set id will always & only be
295: * replaced by browsers with the same id.
296: *
297: * @return String
298: */
299: public String getBrowserId() {
300: return id;
301: }
302:
303: /**
304: * Returns true if the status bar should be shown.
305: *
306: * @return boolean
307: */
308: public boolean isStatusbarVisible() {
309: return (style & IWorkbenchBrowserSupport.STATUS) != 0;
310: }
311:
312: /**
313: * Returns true if the toolbar should be shown.
314: *
315: * @return boolean
316: */
317: public boolean isLocationBarLocal() {
318: return (style & BrowserViewer.LOCATION_BAR) != 0;
319: }
320:
321: /*
322: * public boolean isLocationBarGlobal() { return (style &
323: * ExternalBrowserInstance.LOCATION_TOOLBAR) != 0; }
324: */
325:
326: public boolean isToolbarLocal() {
327: return (style & BrowserViewer.BUTTON_BAR) != 0;
328: }
329:
330: /*
331: * public boolean isToolbarGlobal() { return (style &
332: * ExternalBrowserInstance.BUTTON_TOOLBAR) != 0; }
333: */
334:
335: /**
336: * Saves the state of an element within a memento.
337: *
338: * @param memento
339: * the storage area for element state
340: */
341: public void saveState(IMemento memento) {
342: memento.putInteger(MEMENTO_STYLE, style);
343: if ((style & IWorkbenchBrowserSupport.PERSISTENT) != 0
344: && url != null) {
345: memento.putString(MEMENTO_URL, url.toExternalForm());
346: }
347: if (id != null) {
348: memento.putString(MEMENTO_ID, id);
349: }
350: if (name != null) {
351: memento.putString(MEMENTO_NAME, name);
352: }
353: if (tooltip != null) {
354: memento.putString(MEMENTO_TOOLTIP, tooltip);
355: }
356: }
357:
358: /**
359: * Converts this object to a string.
360: *
361: * @return java.lang.String
362: */
363: public String toString() {
364: return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
365: }
366: }
|