Organized imports
[phpeclipse.git] / net.sourceforge.phpeclipse.webbrowser / src / net / sourceforge / phpeclipse / webbrowser / WebBrowserEditorInput.java
1 /**
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
7  *
8  * Contributors:
9  *    IBM - Initial API and implementation
10  */
11 package net.sourceforge.phpeclipse.webbrowser;
12
13 import java.net.URL;
14
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;
19
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;
25 /**
26  * The editor input for the integrated web browser.
27  */
28 public class WebBrowserEditorInput implements IWebBrowserEditorInput, 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         // if used, the status bar will be available
35         public static final int SHOW_STATUSBAR = 1 << 2;
36
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;
40
41         // if used, the original URL will be saved and
42         // the page can reopen to the same URL after
43         // shutting down
44         public static final int SAVE_URL = 1 << 5;
45         
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
48         // restarting Eclipse
49         public static final int TRANSIENT = 1 << 6;
50
51         public static final int SHOW_ALL = SHOW_TOOLBAR | SHOW_STATUSBAR;
52
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";
57
58         private URL url;
59         private int style;
60         private String id = null;
61
62         /**
63          * WebBrowser editor input for the homepage.
64          */
65         public WebBrowserEditorInput() {
66                 this(null);
67         }
68
69         /**
70          * WebBrowserEditorInput constructor comment.
71          */
72         public WebBrowserEditorInput(URL url) {
73                 this(url, SHOW_ALL | SAVE_URL);
74         }
75
76         /**
77          * WebBrowserEditorInput constructor comment.
78          */
79         public WebBrowserEditorInput(URL url, int style) {
80                 super();
81                 this.url = url;
82                 this.style = style;
83         }
84
85         /**
86          * WebBrowserEditorInput constructor comment.
87          */
88         public WebBrowserEditorInput(URL url, int style, String browserId) {
89                 super();
90                 this.url = url;
91                 this.style = style;
92                 this.id = browserId;
93         }
94
95         /**
96          * WebBrowserEditorInput constructor comment.
97          */
98         public WebBrowserEditorInput(URL url, boolean b) {
99                 this(url);
100         }
101         
102         /**
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.
106          *
107          * @param input net.sourceforge.phpeclipse.webbrowser.IWebBrowserEditorInput
108          * @return boolean
109          */
110         public boolean canReplaceInput(IWebBrowserEditorInput input) {
111                 Trace.trace(Trace.FINEST, "canReplaceInput " + this + " " + input);
112                 if ((style & FORCE_NEW_PAGE) != 0)
113                         return false;
114                 else if (input.isToolbarVisible() != isToolbarVisible())
115                         return false;
116                 else if (input.isStatusbarVisible() != isStatusbarVisible())
117                         return false;
118                 else if (id != null) {
119                         if (!(input instanceof WebBrowserEditorInput))
120                                 return false;
121                         String bid = ((WebBrowserEditorInput) input).getBrowserId();
122                         return id.equals(bid);
123                 } else
124                         return false;
125         }
126         
127         /**
128          * Creates an <code>IElement</code> from the state captured within 
129          * an <code>IMemento</code>.
130          *
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
133          */
134         public IAdaptable createElement(IMemento memento) {
135                 URL url2 = null;
136                 try {
137                         url2 = new URL(WebBrowserPreference.getHomePageURL());
138                 } catch (Exception e) {
139                         // could not determine the URL
140                 }
141
142                 int newStyle = SHOW_TOOLBAR | SHOW_STATUSBAR;
143                 try {
144                         newStyle = memento.getInteger(MEMENTO_STYLE).intValue();
145         
146                         if ((newStyle & SAVE_URL) != 0)
147                                 url = new URL(memento.getString(MEMENTO_URL));
148                 } catch (Exception e) {
149                         // could not determine the style
150                 }
151                 
152                 String id2 = null;
153                 try {
154                         id2 = memento.getString(MEMENTO_ID);
155                         if (id2 != null && id2.length() < 1)
156                                 id2 = null;
157                 } catch (Exception e) { }
158                 
159                 return new WebBrowserEditorInput(url2, newStyle, id2);
160         }
161         
162         /**
163          * Indicates whether some other object is "equal to" this one.
164          * In this case it means that the underlying IFolders are equal.
165          */
166         public boolean equals(Object obj) {
167                 if (this == obj)
168                         return true;
169                 if (!(obj instanceof WebBrowserEditorInput))
170                         return false;
171                 WebBrowserEditorInput other = (WebBrowserEditorInput) obj;
172         
173                 if (url != null && !url.equals(obj))
174                         return false;
175         
176                 return canReplaceInput(other);
177         }
178         
179         /**
180          * Returns whether the editor input exists.  
181          * <p>
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.
186          *
187          * @return <code>true</code> if the editor input exists; <code>false</code>
188          *              otherwise
189          */
190         public boolean exists() {
191                 if ((style & TRANSIENT) != 0)
192                         return false;
193                 else
194                         return true;
195         }
196         
197         /**
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.
201          *
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
206          */
207         public Object getAdapter(Class adapter) {
208                 return null;
209         }
210         
211         /**
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.
215          * 
216          * @return the element factory ID
217          */
218         public String getFactoryId() {
219                 return ELEMENT_FACTORY_ID;
220         }
221         
222         public ImageDescriptor getImageDescriptor() {
223                 return ImageResource.getImageDescriptor(ImageResource.IMG_INTERNAL_BROWSER);
224         }
225         
226         /**
227          * Returns the name of this editor input for display purposes.
228          * <p>
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>.
232          *
233          * @return the file name string
234          */
235         public String getName() {
236                 return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
237         }
238         
239         /*
240          * Returns an object that can be used to save the state of this editor input.
241          *
242          * @return the persistable element, or <code>null</code> if this editor input
243          *   cannot be persisted
244          */
245         public IPersistableElement getPersistable() {
246                 if ((style & TRANSIENT) != 0)
247                         return null;
248                 else
249                         return this;
250         }
251         
252         public String getToolTipText() {
253                 if (url != null)
254                         return url.toExternalForm();
255                 else
256                         return WebBrowserUIPlugin.getResource("%viewWebBrowserTitle");
257         }
258         
259         /**
260          * Returns the url.
261          *
262          * @return java.net.URL
263          */
264         public URL getURL() {
265                 return url;
266         }
267         
268         /**
269          * Returns the browser id. Browsers with a set id will always & only be
270          * replaced by browsers with the same id.
271          * 
272          * @return String
273          */
274         public String getBrowserId() {
275                 return id;
276         }
277         
278         /**
279          * Returns true if the status bar should be shown.
280          *
281          * @return boolean
282          */
283         public boolean isStatusbarVisible() {
284                 return (style & SHOW_STATUSBAR) != 0;
285         }
286         
287         /**
288          * Returns true if the toolbar should be shown.
289          *
290          * @return boolean
291          */
292         public boolean isToolbarVisible() {
293                 return (style & SHOW_TOOLBAR) != 0;
294         }
295         
296         /**
297          * Saves the state of an element within a memento.
298          *
299          * @param memento the storage area for element state
300          */
301         public void saveState(IMemento memento) {
302                 if ((style & SAVE_URL) != 0 && url != null)
303                         memento.putString(MEMENTO_URL, url.toExternalForm());
304
305                 memento.putInteger(MEMENTO_STYLE, style);
306                 
307                 if (id != null)
308                         memento.putString(MEMENTO_ID, id);
309         }
310
311         /**
312          * Converts this object to a string.
313          *
314          * @return java.lang.String
315          */
316         public String toString() {
317                 return "WebBrowserEditorInput[" + url + " " + style + " " + id + "]";
318         }
319 }