001: /**
002: *
003: */package net.refractions.udig.catalog.ui;
004:
005: import java.util.ArrayList;
006: import java.util.Collection;
007: import java.util.Collections;
008: import java.util.Comparator;
009: import java.util.HashMap;
010: import java.util.List;
011: import java.util.Map;
012:
013: import org.eclipse.core.runtime.CoreException;
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IExtension;
016: import org.eclipse.core.runtime.Platform;
017:
018: /**
019: * Provides access to the connection Factories and their wizardpages.
020: *
021: * @author jeichar
022: */
023: public class ConnectionFactoryManager {
024: Map<Descriptor<UDIGConnectionFactory>, Descriptor<UDIGConnectionPage>> factoryToPage = new HashMap<Descriptor<UDIGConnectionFactory>, Descriptor<UDIGConnectionPage>>();
025: Map<Descriptor<UDIGConnectionPage>, Descriptor<UDIGConnectionFactory>> pageToFactory = new HashMap<Descriptor<UDIGConnectionPage>, Descriptor<UDIGConnectionFactory>>();
026: List<UDIGConnectionFactoryDescriptor> descriptors;
027: private static ConnectionFactoryManager manager;
028:
029: protected ConnectionFactoryManager() {
030: IExtension[] extension = Platform.getExtensionRegistry()
031: .getExtensionPoint(UDIGConnectionFactory.XPID)
032: .getExtensions();
033:
034: Descriptor<UDIGConnectionFactory> factory = null;
035: Descriptor<UDIGConnectionPage> wizardPage = null;
036:
037: for (IExtension e : extension) {
038:
039: IConfigurationElement[] elements = e
040: .getConfigurationElements();
041: wizardPage = null;
042: factory = null;
043: for (IConfigurationElement element : elements) {
044: if ("factory".equals(element.getName())) { //$NON-NLS-1$
045: factory = new Descriptor<UDIGConnectionFactory>(
046: "class", element); //$NON-NLS-1$
047: }
048: if ("wizardPage".equals(element.getName())) { //$NON-NLS-1$
049: wizardPage = new Descriptor<UDIGConnectionPage>(
050: "class", element); //$NON-NLS-1$
051: }
052: }
053: if (factory != null)
054: factoryToPage.put(factory, wizardPage);
055: if (factory != null)
056: pageToFactory.put(wizardPage, factory);
057: }
058: }
059:
060: public static synchronized ConnectionFactoryManager instance() {
061: if (manager == null) {
062: manager = new ConnectionFactoryManager();
063: }
064:
065: return manager;
066: }
067:
068: public Descriptor<UDIGConnectionPage> getPageDescriptor(
069: Descriptor<UDIGConnectionFactory> factory)
070: throws CoreException {
071: return factoryToPage.get(factory);
072: }
073:
074: public Descriptor<UDIGConnectionFactory> getFactoryDescriptor(
075: Descriptor<UDIGConnectionPage> page) throws CoreException {
076: return pageToFactory.get(page);
077: }
078:
079: public Collection<Descriptor<UDIGConnectionPage>> getPages() {
080: return factoryToPage.values();
081: }
082:
083: public Collection<Descriptor<UDIGConnectionFactory>> getFactories() {
084: return factoryToPage.keySet();
085: }
086:
087: public synchronized List<UDIGConnectionFactoryDescriptor> getConnectionFactoryDescriptors() {
088: if (descriptors == null) {
089: descriptors = new ArrayList<UDIGConnectionFactoryDescriptor>();
090: Collection<Descriptor<UDIGConnectionFactory>> factories = getFactories();
091: for (Descriptor<UDIGConnectionFactory> factoryDescriptor : factories) {
092: try {
093: if (getPageDescriptor(factoryDescriptor) != null)
094: descriptors
095: .add(new UDIGConnectionFactoryDescriptor(
096: factoryDescriptor));
097: } catch (CoreException e) {
098: CatalogUIPlugin.log("", e); //$NON-NLS-1$
099: }
100: }
101: Collections.sort(descriptors,
102: new Comparator<UDIGConnectionFactoryDescriptor>() {
103: public int compare(
104: UDIGConnectionFactoryDescriptor o1,
105: UDIGConnectionFactoryDescriptor o2) {
106: String s1 = o1.getLabel();
107: String s2 = o2.getLabel();
108: return s1.compareTo(s2);
109: }
110: });
111: }
112:
113: return descriptors;
114: }
115:
116: /**
117: * Gets the UDIGConnectionFactoryDescriptors that match the ids in the list
118: */
119: public List<UDIGConnectionFactoryDescriptor> getConnectionFactoryDescriptors(
120: List<String> ids) {
121: List<UDIGConnectionFactoryDescriptor> tmp = instance()
122: .getConnectionFactoryDescriptors();
123: List<UDIGConnectionFactoryDescriptor> result = new ArrayList<UDIGConnectionFactoryDescriptor>();
124: for (UDIGConnectionFactoryDescriptor descriptor : tmp) {
125: for (String id : ids) {
126: if (id.equals(descriptor.getId()))
127: result.add(descriptor);
128: }
129: }
130: return result;
131: }
132:
133: /**
134: * Provides lazy loading for a class declared in an extension
135: *
136: * @author jeichar
137: *
138: * @param <T> The class type that will be created
139: */
140: public static class Descriptor<T> {
141: private IConfigurationElement element;
142: private String classAttribute;
143: private T instance;
144:
145: /**
146: * Creates a new Descriptor
147: * @param classAttribute the attribute name of the element that is to be create
148: */
149: Descriptor(String classAttribute, IConfigurationElement element) {
150: this .classAttribute = classAttribute;
151: this .element = element;
152: }
153:
154: @SuppressWarnings("unchecked")
155: public synchronized T getConcreteInstance()
156: throws CoreException {
157: if (instance == null) {
158: instance = (T) element
159: .createExecutableExtension(classAttribute);
160: }
161: return instance;
162: }
163:
164: public IConfigurationElement getConfigurationElement() {
165: return element;
166: }
167: }
168: }
|