KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > browser > WebBrowserEditorInput


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.browser;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.ui.IEditorInput;
19 import org.eclipse.ui.IElementFactory;
20 import org.eclipse.ui.IMemento;
21 import org.eclipse.ui.IPersistableElement;
22 import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
23
24 /**
25  * The editor input for the integrated web browser.
26  */

27 public class WebBrowserEditorInput implements IEditorInput,
28         IPersistableElement, IElementFactory {
29     // --- constants to pass into constructor ---
30

31     // if used, the toolbar will be available
32
// public static final int SHOW_TOOLBAR = 1 << 1;
33

34     // public static final int SHOW_GLOBAL_TOOLBAR = 1 << 2;
35

36     // if used, the status bar will be available
37
// public static final int SHOW_STATUSBAR = 1 << 2;
38

39     // if used, the original URL will be saved and
40
// the page can reopen to the same URL after
41
// shutting down
42
// public static final int SAVE_URL = 1 << 5;
43

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
46
// restarting Eclipse
47
// public static final int TRANSIENT = 1 << 6;
48

49     private static final String JavaDoc ELEMENT_FACTORY_ID = "org.eclipse.ui.browser.elementFactory"; //$NON-NLS-1$
50

51     private static final String JavaDoc MEMENTO_URL = "url"; //$NON-NLS-1$
52

53     private static final String JavaDoc MEMENTO_STYLE = "style"; //$NON-NLS-1$
54

55     private static final String JavaDoc MEMENTO_ID = "id"; //$NON-NLS-1$
56

57     private static final String JavaDoc MEMENTO_NAME = "name"; //$NON-NLS-1$
58

59     private static final String JavaDoc MEMENTO_TOOLTIP = "tooltip"; //$NON-NLS-1$
60

61     private URL JavaDoc url;
62
63     private int style;
64
65     private String JavaDoc id = null;
66
67     private String JavaDoc name;
68
69     private String JavaDoc tooltip;
70
71     /**
72      * ExternalBrowserInstance editor input for the homepage.
73      */

74     public WebBrowserEditorInput() {
75         this(null);
76     }
77
78     /**
79      * WebBrowserEditorInput constructor comment.
80      */

81     public WebBrowserEditorInput(URL JavaDoc url) {
82         this(url, 0);
83     }
84
85     /**
86      * WebBrowserEditorInput constructor comment.
87      */

88     public WebBrowserEditorInput(URL JavaDoc url, int style) {
89         super();
90         this.url = url;
91         this.style = style;
92     }
93
94     /**
95      * WebBrowserEditorInput constructor comment.
96      */

97     public WebBrowserEditorInput(URL JavaDoc url, int style, String JavaDoc browserId) {
98         super();
99         this.url = url;
100         this.style = style;
101         this.id = browserId;
102     }
103
104     /**
105      * WebBrowserEditorInput constructor comment.
106      */

107     public WebBrowserEditorInput(URL JavaDoc url, boolean b) {
108         this(url);
109     }
110
111     public void setName(String JavaDoc n) {
112         name = n;
113     }
114
115     public void setToolTipText(String JavaDoc t) {
116         tooltip = t;
117     }
118
119     /**
120      * Returns true if this page can reuse the browser that the given input is
121      * being displayed in, or false if it should open up in a new page.
122      *
123      * @param input
124      * org.eclipse.ui.internal.browser.IWebBrowserEditorInput
125      * @return boolean
126      */

127     public boolean canReplaceInput(WebBrowserEditorInput input) {
128         Trace.trace(Trace.FINEST, "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 JavaDoc 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 JavaDoc integer = memento.getInteger(MEMENTO_STYLE);
155         if (integer != null) {
156             style = integer.intValue();
157         }
158
159         URL JavaDoc url = null;
160         String JavaDoc str = memento.getString(MEMENTO_URL);
161         if (str != null) {
162             try {
163                 url = new URL JavaDoc(str);
164             }
165             catch (MalformedURLException JavaDoc e) {
166                 String JavaDoc msg = "Malformed URL while initializing browser editor"; //$NON-NLS-1$
167
WebBrowserUIPlugin.logError(msg, e);
168             }
169         }
170
171         String JavaDoc id = memento.getString(MEMENTO_ID);
172         String JavaDoc name = memento.getString(MEMENTO_NAME);
173         String JavaDoc tooltip = memento.getString(MEMENTO_TOOLTIP);
174         
175         WebBrowserEditorInput input = new WebBrowserEditorInput(url, 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 JavaDoc 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 JavaDoc getAdapter(Class JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 && url != null) {
344             memento.putString(MEMENTO_URL, url.toExternalForm());
345         }
346         if (id != null) {
347             memento.putString(MEMENTO_ID, id);
348         }
349         if (name != null) {
350             memento.putString(MEMENTO_NAME, name);
351         }
352         if (tooltip != null) {
353             memento.putString(MEMENTO_TOOLTIP, tooltip);
354         }
355     }
356
357     /**
358      * Converts this object to a string.
359      *
360      * @return java.lang.String
361      */

362     public String JavaDoc toString() {
363         return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
364
}
365 }
366
Popular Tags