KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > splash > SplashHandlerFactory


1 /*******************************************************************************
2  * Copyright (c) 2007 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
12 package org.eclipse.ui.internal.splash;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IExtension;
19 import org.eclipse.core.runtime.IExtensionPoint;
20 import org.eclipse.core.runtime.IProduct;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.SafeRunner;
23 import org.eclipse.jface.util.SafeRunnable;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.WorkbenchPlugin;
26 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
27 import org.eclipse.ui.splash.AbstractSplashHandler;
28
29 /**
30  * Simple non-caching access to the splashHandler extension point.
31  *
32  * @since 3.3
33  */

34 public final class SplashHandlerFactory {
35
36     /**
37      * Find the splash handler for the given product or <code>null</code> if
38      * it cannot be found.
39      *
40      * @param product
41      * the product
42      * @return the splash or <code>null</code>
43      */

44     public static AbstractSplashHandler findSplashHandlerFor(IProduct product) {
45         if (product == null)
46             return null;
47
48         IExtensionPoint point = Platform.getExtensionRegistry()
49                 .getExtensionPoint(PlatformUI.PLUGIN_ID,
50                         IWorkbenchRegistryConstants.PL_SPLASH_HANDLERS);
51
52         if (point == null)
53             return null;
54
55         IExtension[] extensions = point.getExtensions();
56         Map JavaDoc idToSplash = new HashMap JavaDoc(); // String->ConfigurationElement
57
String JavaDoc[] targetId = new String JavaDoc[1];
58         for (int i = 0; i < extensions.length; i++) {
59             IConfigurationElement[] children = extensions[i]
60                     .getConfigurationElements();
61             for (int j = 0; j < children.length; j++) {
62                 AbstractSplashHandler handler = processElement(children[j],
63                         idToSplash, targetId, product);
64                 if (handler != null)
65                     return handler;
66
67             }
68         }
69         return null;
70     }
71
72     /**
73      * Process a given element.
74      *
75      * @param configurationElement
76      * the element to process
77      * @param idToSplash
78      * the map of current splash elements
79      * @param targetId
80      * the target id if known
81      * @param product
82      * the product to search for
83      * @return a splash matching the target id from this element or
84      * <code>null</code>
85      */

86     private static AbstractSplashHandler processElement(
87             IConfigurationElement configurationElement, Map JavaDoc idToSplash,
88             String JavaDoc[] targetId, IProduct product) {
89         String JavaDoc type = configurationElement.getName();
90         if (IWorkbenchRegistryConstants.TAG_SPLASH_HANDLER.equals(type)) {
91             String JavaDoc id = configurationElement
92                     .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
93             if (id == null)
94                 return null;
95
96             // we know the target and this element is it
97
if (targetId[0] != null && id.equals(targetId[0])) {
98                 return create(configurationElement);
99             }
100             // store for later examination
101
idToSplash.put(id, configurationElement);
102
103         } else if (IWorkbenchRegistryConstants.TAG_SPLASH_HANDLER_PRODUCT_BINDING
104                 .equals(type)) {
105             String JavaDoc productId = configurationElement
106                     .getAttribute(IWorkbenchRegistryConstants.ATT_PRODUCTID);
107             if (product.getId().equals(productId) && targetId[0] == null) { // we
108
// found the target ID
109
targetId[0] = configurationElement
110                         .getAttribute(IWorkbenchRegistryConstants.ATT_SPLASH_ID);
111                 // check all currently located splashes
112
IConfigurationElement splashElement = (IConfigurationElement) idToSplash
113                         .get(targetId[0]);
114                 if (splashElement != null)
115                     return create(splashElement);
116             }
117         }
118
119         return null;
120     }
121
122     /**
123      * Create the splash implementation.
124      *
125      * @param splashElement
126      * the element to create from
127      * @return the element or <code>null</code> if it couldn't be created
128      */

129     private static AbstractSplashHandler create(
130             final IConfigurationElement splashElement) {
131         final AbstractSplashHandler[] handler = new AbstractSplashHandler[1];
132         SafeRunner.run(new SafeRunnable() {
133
134             /*
135              * (non-Javadoc)
136              *
137              * @see org.eclipse.core.runtime.ISafeRunnable#run()
138              */

139             public void run() throws Exception JavaDoc {
140                 handler[0] = (AbstractSplashHandler) WorkbenchPlugin
141                         .createExtension(splashElement,
142                                 IWorkbenchRegistryConstants.ATT_CLASS);
143             }
144
145             /*
146              * (non-Javadoc)
147              *
148              * @see org.eclipse.jface.util.SafeRunnable#handleException(java.lang.Throwable)
149              */

150             public void handleException(Throwable JavaDoc e) {
151                 WorkbenchPlugin
152                         .log("Problem creating splash implementation", e); //$NON-NLS-1$
153
}
154         });
155
156         return handler[0];
157     }
158 }
159
Popular Tags