blob: 1189a4a9a6a2362562a3a9b07b0aaf3ae4e31c77 [file] [log] [blame]
csalterdf29c4e2006-05-16 21:43:16 +00001/*******************************************************************************
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 *******************************************************************************/
11package org.eclipse.wst.xsd.ui.internal.common.properties.sections.appinfo.custom;
12
13import java.util.HashMap;
14import org.eclipse.core.runtime.IConfigurationElement;
15import org.eclipse.core.runtime.Platform;
csalterb6d3bc32006-05-28 08:04:01 +000016import org.eclipse.jface.viewers.ILabelProvider;
csalterdf29c4e2006-05-16 21:43:16 +000017
18public class NodeCustomizationRegistry
19{
20 private static final String NAMESPACE = "namespace"; //$NON-NLS-1$
csalterb6d3bc32006-05-28 08:04:01 +000021 private static final String LABEL_PROVIDER_CLASS_ATTRIBUTE_NAME = "labelProviderClass";
csalterdf29c4e2006-05-16 21:43:16 +000022 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;
csalterb6d3bc32006-05-28 08:04:01 +000039 boolean labelProviderFailedToLoad = false;
csalterdf29c4e2006-05-16 21:43:16 +000040
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 }
csalterb6d3bc32006-05-28 08:04:01 +000061
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 }
csalterdf29c4e2006-05-16 21:43:16 +000077 }
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
csalterb6d3bc32006-05-28 08:04:01 +000096 private Descriptor getDescriptor(String namespace)
97 {
csalterdf29c4e2006-05-16 21:43:16 +000098 map = null;
99 if (namespace != null)
100 {
101 if (map == null)
102 {
103 map = initMap();
104 }
csalterb6d3bc32006-05-28 08:04:01 +0000105 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();
csalterdf29c4e2006-05-16 21:43:16 +0000116 }
117 return null;
118 }
csalterb6d3bc32006-05-28 08:04:01 +0000119
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 }
csalterdf29c4e2006-05-16 21:43:16 +0000129}