csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 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.wst.xsd.ui.internal.common.properties.sections.appinfo.custom; |
| 12 | |
| 13 | import java.util.HashMap; |
| 14 | import org.eclipse.core.runtime.IConfigurationElement; |
| 15 | import org.eclipse.core.runtime.Platform; |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 16 | import org.eclipse.jface.viewers.ILabelProvider; |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 17 | |
| 18 | public class NodeCustomizationRegistry |
| 19 | { |
| 20 | private static final String NAMESPACE = "namespace"; //$NON-NLS-1$ |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 21 | private static final String LABEL_PROVIDER_CLASS_ATTRIBUTE_NAME = "labelProviderClass"; |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 22 | private static final String NODE_EDITOR_PROVIDER_CLASS_ATTRIBUTE_NAME = "nodeEditorProviderClass"; //$NON-NLS-1$ |
| 23 | |
| 24 | |
| 25 | protected String extensionId; |
| 26 | protected HashMap map; |
| 27 | |
| 28 | public NodeCustomizationRegistry(String propertyEditorExtensionId) |
| 29 | { |
| 30 | extensionId = "org.eclipse.wst.xsd.ui.extensibilityNodeCustomizations";//propertyEditorExtensionId; |
| 31 | } |
| 32 | |
| 33 | private class Descriptor |
| 34 | { |
| 35 | IConfigurationElement configurationElement; |
| 36 | |
| 37 | NodeEditorProvider nodeEditorProvider; |
| 38 | boolean nodeEditorProviderFailedToLoad = false; |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 39 | boolean labelProviderFailedToLoad = false; |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 40 | |
| 41 | Descriptor(IConfigurationElement element) |
| 42 | { |
| 43 | this.configurationElement = element; |
| 44 | } |
| 45 | |
| 46 | NodeEditorProvider lookupOrCreateNodeEditorProvider() |
| 47 | { |
| 48 | if (nodeEditorProvider == null && !nodeEditorProviderFailedToLoad) |
| 49 | { |
| 50 | try |
| 51 | { |
| 52 | nodeEditorProvider = (NodeEditorProvider)configurationElement.createExecutableExtension(NODE_EDITOR_PROVIDER_CLASS_ATTRIBUTE_NAME); |
| 53 | } |
| 54 | catch (Exception e) |
| 55 | { |
| 56 | nodeEditorProviderFailedToLoad = true; |
| 57 | } |
| 58 | } |
| 59 | return nodeEditorProvider; |
| 60 | } |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 61 | |
| 62 | ILabelProvider createLabelProvider() |
| 63 | { |
| 64 | if (!labelProviderFailedToLoad) |
| 65 | { |
| 66 | try |
| 67 | { |
| 68 | return (ILabelProvider)configurationElement.createExecutableExtension(LABEL_PROVIDER_CLASS_ATTRIBUTE_NAME); |
| 69 | } |
| 70 | catch (Exception e) |
| 71 | { |
| 72 | labelProviderFailedToLoad = true; |
| 73 | } |
| 74 | } |
| 75 | return null; |
| 76 | } |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
| 80 | private HashMap initMap() |
| 81 | { |
| 82 | HashMap theMap = new HashMap(); |
| 83 | IConfigurationElement[] extensions = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.wst.xsd.ui.extensibilityNodeCustomizations"); |
| 84 | for (int i = 0; i < extensions.length; i++) |
| 85 | { |
| 86 | IConfigurationElement configurationElement = extensions[i]; |
| 87 | String namespace = configurationElement.getAttribute(NAMESPACE); |
| 88 | if (namespace != null) |
| 89 | { |
| 90 | theMap.put(namespace, new Descriptor(configurationElement)); |
| 91 | } |
| 92 | } |
| 93 | return theMap; |
| 94 | } |
| 95 | |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 96 | private Descriptor getDescriptor(String namespace) |
| 97 | { |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 98 | map = null; |
| 99 | if (namespace != null) |
| 100 | { |
| 101 | if (map == null) |
| 102 | { |
| 103 | map = initMap(); |
| 104 | } |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 105 | return (Descriptor)map.get(namespace); |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | public NodeEditorProvider getNodeEditorProvider(String namespace) |
| 111 | { |
| 112 | Descriptor descriptor = getDescriptor(namespace); |
| 113 | if (descriptor != null) |
| 114 | { |
| 115 | return descriptor.lookupOrCreateNodeEditorProvider(); |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 116 | } |
| 117 | return null; |
| 118 | } |
csalter | b6d3bc3 | 2006-05-28 08:04:01 +0000 | [diff] [blame] | 119 | |
| 120 | public ILabelProvider getLabelProvider(String namespace) |
| 121 | { |
| 122 | Descriptor descriptor = getDescriptor(namespace); |
| 123 | if (descriptor != null) |
| 124 | { |
| 125 | return descriptor.createLabelProvider(); |
| 126 | } |
| 127 | return null; |
| 128 | } |
csalter | df29c4e | 2006-05-16 21:43:16 +0000 | [diff] [blame] | 129 | } |