001: /*******************************************************************************
002: * Copyright (c) 2007 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.splash;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.IExtension;
017: import org.eclipse.core.runtime.IExtensionPoint;
018: import org.eclipse.core.runtime.IProduct;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.core.runtime.SafeRunner;
021: import org.eclipse.jface.util.SafeRunnable;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.internal.WorkbenchPlugin;
024: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
025: import org.eclipse.ui.splash.AbstractSplashHandler;
026:
027: /**
028: * Simple non-caching access to the splashHandler extension point.
029: *
030: * @since 3.3
031: */
032: public final class SplashHandlerFactory {
033:
034: /**
035: * Find the splash handler for the given product or <code>null</code> if
036: * it cannot be found.
037: *
038: * @param product
039: * the product
040: * @return the splash or <code>null</code>
041: */
042: public static AbstractSplashHandler findSplashHandlerFor(
043: IProduct product) {
044: if (product == null)
045: return null;
046:
047: IExtensionPoint point = Platform.getExtensionRegistry()
048: .getExtensionPoint(PlatformUI.PLUGIN_ID,
049: IWorkbenchRegistryConstants.PL_SPLASH_HANDLERS);
050:
051: if (point == null)
052: return null;
053:
054: IExtension[] extensions = point.getExtensions();
055: Map idToSplash = new HashMap(); // String->ConfigurationElement
056: String[] targetId = new String[1];
057: for (int i = 0; i < extensions.length; i++) {
058: IConfigurationElement[] children = extensions[i]
059: .getConfigurationElements();
060: for (int j = 0; j < children.length; j++) {
061: AbstractSplashHandler handler = processElement(
062: children[j], idToSplash, targetId, product);
063: if (handler != null)
064: return handler;
065:
066: }
067: }
068: return null;
069: }
070:
071: /**
072: * Process a given element.
073: *
074: * @param configurationElement
075: * the element to process
076: * @param idToSplash
077: * the map of current splash elements
078: * @param targetId
079: * the target id if known
080: * @param product
081: * the product to search for
082: * @return a splash matching the target id from this element or
083: * <code>null</code>
084: */
085: private static AbstractSplashHandler processElement(
086: IConfigurationElement configurationElement, Map idToSplash,
087: String[] targetId, IProduct product) {
088: String type = configurationElement.getName();
089: if (IWorkbenchRegistryConstants.TAG_SPLASH_HANDLER.equals(type)) {
090: String id = configurationElement
091: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
092: if (id == null)
093: return null;
094:
095: // we know the target and this element is it
096: if (targetId[0] != null && id.equals(targetId[0])) {
097: return create(configurationElement);
098: }
099: // store for later examination
100: idToSplash.put(id, configurationElement);
101:
102: } else if (IWorkbenchRegistryConstants.TAG_SPLASH_HANDLER_PRODUCT_BINDING
103: .equals(type)) {
104: String productId = configurationElement
105: .getAttribute(IWorkbenchRegistryConstants.ATT_PRODUCTID);
106: if (product.getId().equals(productId)
107: && 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 {
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 e) {
151: WorkbenchPlugin.log(
152: "Problem creating splash implementation", e); //$NON-NLS-1$
153: }
154: });
155:
156: return handler[0];
157: }
158: }
|