KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.ui.browser.BrowserFactory;
18 import org.eclipse.ui.browser.IWebBrowser;
19 /**
20  * @since 1.0
21  */

22 public class BrowserExt implements IBrowserExt {
23     private static final String JavaDoc ATTR_FACTORY_CLASS = "factoryclass"; //$NON-NLS-1$
24

25     private IConfigurationElement element;
26
27     private BrowserFactory delegate;
28
29     /**
30      * BrowserExt constructor comment.
31      */

32     public BrowserExt(IConfigurationElement element) {
33         super();
34         this.element = element;
35     }
36
37     /**
38      * Returns the id of this browser.
39      *
40      * @return java.lang.String
41      */

42     public String JavaDoc getId() {
43         return element.getAttribute("id"); //$NON-NLS-1$
44
}
45
46     public String JavaDoc getName() {
47         String JavaDoc label = element.getAttribute("name"); //$NON-NLS-1$
48
if (label == null)
49             return "n/a"; //$NON-NLS-1$
50
return label;
51     }
52
53     public String JavaDoc getParameters() {
54         return element.getAttribute("parameters"); //$NON-NLS-1$
55
}
56
57     public String JavaDoc getExecutable() {
58         return element.getAttribute("executable"); //$NON-NLS-1$
59
}
60
61     public String JavaDoc getOS() {
62         String JavaDoc os = element.getAttribute("os"); //$NON-NLS-1$
63
if (os == null)
64             os = ""; //$NON-NLS-1$
65
return os;
66     }
67
68     public String JavaDoc[] getDefaultLocations() {
69         List JavaDoc list = new ArrayList JavaDoc();
70         IConfigurationElement[] children = element.getChildren("location"); //$NON-NLS-1$
71
if (children != null) {
72             int size = children.length;
73             for (int i = 0; i < size; i++) {
74                 list.add(children[i].getValue());
75             }
76         }
77
78         String JavaDoc[] s = new String JavaDoc[list.size()];
79         list.toArray(s);
80         return s;
81     }
82
83     protected BrowserFactory getDelegate() {
84         if (delegate == null) {
85             if (element.getAttribute(ATTR_FACTORY_CLASS) == null
86                     || element.getAttribute(ATTR_FACTORY_CLASS).length() == 0)
87                 return null;
88
89             try {
90                 delegate = (BrowserFactory) element
91                         .createExecutableExtension(ATTR_FACTORY_CLASS);
92             } catch (Exception JavaDoc e) {
93                 Trace
94                         .trace(
95                                 Trace.SEVERE,
96                                 "Could not create delegate" + toString() + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
97
}
98         }
99         return delegate;
100     }
101
102     /**
103      * Checks whether the factory can work on the user system.
104      *
105      * @return false if the factory cannot work on this system; for example the
106      * required native browser required by browser adapters that it
107      * creates is not installed
108      */

109     public boolean isAvailable() {
110         if (delegate == null && (element.getAttribute(ATTR_FACTORY_CLASS) == null
111                 || element.getAttribute(ATTR_FACTORY_CLASS).length() == 0))
112             return true;
113         
114         try {
115             return getDelegate().isAvailable();
116         } catch (Exception JavaDoc e) {
117             Trace
118                     .trace(
119                             Trace.SEVERE,
120                             "Error calling delegate " + toString() + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
121
return false;
122         }
123     }
124
125     /**
126      * Obtains a new instance of a web browser.
127      *
128      * @return instance of IBrowser
129      */

130     public IWebBrowser createBrowser(String JavaDoc id, String JavaDoc location,
131             String JavaDoc parameters) {
132         try {
133             return getDelegate().createBrowser(id, location, parameters);
134         } catch (Exception JavaDoc e) {
135             Trace
136                     .trace(
137                             Trace.SEVERE,
138                             "Error calling delegate " + toString() + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
139
return null;
140         }
141     }
142
143     /**
144      * Return a string representation of this object.
145      *
146      * @return java.lang.String
147      */

148     public String JavaDoc toString() {
149         String JavaDoc s = "BrowserExt: " + getId() + ", " + getName() + ", " + getOS() //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
150
+ ", " + getExecutable() + ", " + getParameters() + ", "; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
151
String JavaDoc[] locations = getDefaultLocations();
152         if (locations != null) {
153             int size = locations.length;
154             for (int i = 0; i < size; i++) {
155                 s += locations[i] + ";"; //$NON-NLS-1$
156
}
157         }
158         return s;
159     }
160 }
161
Popular Tags