|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004-2007 Sybase, Inc. |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials are made |
| 5 |
* available under the terms of the Eclipse Public License v1.0 which |
| 6 |
* accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
| 9 |
* Contributors: brianf - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
package org.eclipse.datatools.connectivity.internal.ui.drivers; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.HashMap; |
| 15 |
import java.util.Iterator; |
| 16 |
import java.util.List; |
| 17 |
import java.util.Map; |
| 18 |
|
| 19 |
import org.eclipse.core.runtime.Assert; |
| 20 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 21 |
import org.eclipse.core.runtime.IExtensionRegistry; |
| 22 |
import org.eclipse.core.runtime.Platform; |
| 23 |
import org.eclipse.core.runtime.SafeRunner; |
| 24 |
import org.eclipse.datatools.connectivity.internal.ui.ConnectivityUIPlugin; |
| 25 |
|
| 26 |
import com.ibm.icu.text.Collator; |
| 27 |
|
| 28 |
/** |
| 29 |
* Represents a way to specify a driver property descriptor which is provided by the |
| 30 |
* "org.eclipse.datatools.connectivity.ui.driverPropertyEditor" extension point. |
| 31 |
* |
| 32 |
* @author brianf |
| 33 |
*/ |
| 34 |
public class DriverPropertyEditorDescriptor implements Comparable { |
| 35 |
|
| 36 |
// extension details |
| 37 |
public static final String PROPERTY_EDITOR_TAG = "propertyEditor";//$NON-NLS-1$ |
| 38 |
private static final String EXTENSION_POINT_NAME = "driverPropertyEditor"; //$NON-NLS-1$ |
| 39 |
|
| 40 |
// attributes |
| 41 |
public static final String ATTR_ID = "driverTemplateID"; //$NON-NLS-1$ |
| 42 |
public static final String ATTR_TEMPLATE_ID = "driverTemplateID"; //$NON-NLS-1$ |
| 43 |
public static final String ATTR_PROP_ID = "driverPropertyID"; //$NON-NLS-1$ |
| 44 |
public static final String ATTR_PROPERTY_EDITOR = "customPropertyDescriptor"; //$NON-NLS-1$ |
| 45 |
|
| 46 |
private static final DriverPropertyEditorDescriptor[] EMPTY = {}; |
| 47 |
|
| 48 |
// local list of driver templates |
| 49 |
private static Map fgDriverPropertyEditorDescriptors; |
| 50 |
|
| 51 |
// local copy of configuration element |
| 52 |
private IConfigurationElement fElement; |
| 53 |
|
| 54 |
/** |
| 55 |
* Creates a new driver property editor template descriptor for the given configuration |
| 56 |
* element. |
| 57 |
*/ |
| 58 |
protected DriverPropertyEditorDescriptor(IConfigurationElement element) { |
| 59 |
this.fElement = element; |
| 60 |
|
| 61 |
/* |
| 62 |
* "An extension for extension-point |
| 63 |
* org.eclipse.datatools.connectivity.driverExtension does not provide a |
| 64 |
* valid ID"); |
| 65 |
*/ |
| 66 |
Assert.isNotNull(getId(), |
| 67 |
ConnectivityUIPlugin.getDefault().getResourceString("DriverPropertyEditorDescriptor.InvalidID")); //$NON-NLS-1$ |
| 68 |
Assert.isNotNull(getTargetTemplateId(), |
| 69 |
ConnectivityUIPlugin.getDefault().getResourceString("DriverPropertyEditorDescriptor.InvalidTargetTemplateID")); //$NON-NLS-1$ |
| 70 |
Assert.isNotNull(getTargetPropertyId(), |
| 71 |
ConnectivityUIPlugin.getDefault().getResourceString("DriverPropertyEditorDescriptor.InvalidTargetPropertyId")); //$NON-NLS-1$ |
| 72 |
Assert.isNotNull(getCustomPropertyEditor(), |
| 73 |
ConnectivityUIPlugin.getDefault().getResourceString("DriverPropertyEditorDescriptor.InvalidCustomPropertyEditorClass")); //$NON-NLS-1$ |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns any driver property editor descriptors for the template/ |
| 78 |
* property ID combo. |
| 79 |
* |
| 80 |
* @param driverTemplateId |
| 81 |
* @param driverPropertyId |
| 82 |
* @return |
| 83 |
*/ |
| 84 |
public static DriverPropertyEditorDescriptor[] getByDriverTemplateAndProperty(String driverTemplateId, String driverPropertyId) { |
| 85 |
if (fgDriverPropertyEditorDescriptors == null) { |
| 86 |
IExtensionRegistry registry = Platform.getExtensionRegistry(); |
| 87 |
IConfigurationElement[] elements = registry |
| 88 |
.getConfigurationElementsFor(ConnectivityUIPlugin |
| 89 |
.getDefault().getBundle().getSymbolicName(), |
| 90 |
EXTENSION_POINT_NAME); |
| 91 |
createDriverTemplateDescriptors(elements); |
| 92 |
} |
| 93 |
|
| 94 |
ArrayList descriptors = (ArrayList) fgDriverPropertyEditorDescriptors.get(driverTemplateId); |
| 95 |
ArrayList result = new ArrayList(); |
| 96 |
if (descriptors != null) { |
| 97 |
Iterator iter = descriptors.iterator(); |
| 98 |
while (iter.hasNext()) { |
| 99 |
DriverPropertyEditorDescriptor dped = (DriverPropertyEditorDescriptor) iter.next(); |
| 100 |
if (dped.getTargetPropertyId().equals(driverPropertyId)) { |
| 101 |
result.add(dped); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return result != null ? |
| 107 |
(DriverPropertyEditorDescriptor[]) result.toArray(new DriverPropertyEditorDescriptor[result.size()]) : EMPTY; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Returns all contributed driver property editor descriptors. |
| 112 |
*/ |
| 113 |
public static DriverPropertyEditorDescriptor[] getDriverPropertyEditorDescriptors() { |
| 114 |
if (fgDriverPropertyEditorDescriptors == null) { |
| 115 |
IExtensionRegistry registry = Platform.getExtensionRegistry(); |
| 116 |
IConfigurationElement[] elements = registry |
| 117 |
.getConfigurationElementsFor(ConnectivityUIPlugin |
| 118 |
.getDefault().getBundle().getSymbolicName(), |
| 119 |
EXTENSION_POINT_NAME); |
| 120 |
createDriverTemplateDescriptors(elements); |
| 121 |
} |
| 122 |
return (DriverPropertyEditorDescriptor[]) fgDriverPropertyEditorDescriptors.values().toArray(new DriverPropertyEditorDescriptor[fgDriverPropertyEditorDescriptors.size()]); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Returns the property editor id. |
| 127 |
*/ |
| 128 |
public String getId() { |
| 129 |
return this.fElement.getAttribute(ATTR_ID); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Returns the target property id |
| 134 |
*/ |
| 135 |
public String getTargetPropertyId() { |
| 136 |
return this.fElement.getAttribute(ATTR_PROP_ID); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Returns the target template id |
| 141 |
*/ |
| 142 |
public String getTargetTemplateId() { |
| 143 |
return this.fElement.getAttribute(ATTR_TEMPLATE_ID); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the custom property editor class name |
| 148 |
*/ |
| 149 |
public String getCustomPropertyEditor() { |
| 150 |
return this.fElement.getAttribute(ATTR_PROPERTY_EDITOR); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Returns the configuration element. |
| 155 |
*/ |
| 156 |
public IConfigurationElement getElement() { |
| 157 |
return this.fElement; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* Implements a method from IComparable |
| 162 |
*/ |
| 163 |
public int compareTo(Object o) { |
| 164 |
if (o instanceof DriverPropertyEditorDescriptor) |
| 165 |
return Collator.getInstance().compare(getId(), |
| 166 |
((DriverPropertyEditorDescriptor) o).getId()); |
| 167 |
return Integer.MIN_VALUE; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Creates the property editor descriptors. |
| 172 |
*/ |
| 173 |
private static void createDriverTemplateDescriptors( |
| 174 |
IConfigurationElement[] elements) { |
| 175 |
fgDriverPropertyEditorDescriptors = new HashMap(); |
| 176 |
|
| 177 |
for (int i = 0; i < elements.length; ++i) { |
| 178 |
final IConfigurationElement element = elements[i]; |
| 179 |
if (PROPERTY_EDITOR_TAG.equals(element.getName())) { |
| 180 |
|
| 181 |
final DriverPropertyEditorDescriptor[] desc = new DriverPropertyEditorDescriptor[1]; |
| 182 |
SafeRunner |
| 183 |
.run(new MySafeRunnable ( desc, element)); |
| 184 |
|
| 185 |
if (desc[0] != null) { |
| 186 |
List descriptors = (List)fgDriverPropertyEditorDescriptors.get(desc[0].getTargetTemplateId()); |
| 187 |
if (descriptors == null) { |
| 188 |
descriptors = new ArrayList(1); |
| 189 |
fgDriverPropertyEditorDescriptors.put(desc[0].getTargetTemplateId(), descriptors); |
| 190 |
} |
| 191 |
descriptors.add(desc[0]); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/* (non-Javadoc) |
| 198 |
* @see java.lang.Object#equals(java.lang.Object) |
| 199 |
*/ |
| 200 |
public boolean equals(Object obj) { |
| 201 |
if (obj instanceof DriverPropertyEditorDescriptor) { |
| 202 |
DriverPropertyEditorDescriptor compare = (DriverPropertyEditorDescriptor) obj; |
| 203 |
return this.getId().equals(compare.getId()); |
| 204 |
} |
| 205 |
return super.equals(obj); |
| 206 |
} |
| 207 |
|
| 208 |
/* (non-Javadoc) |
| 209 |
* @see java.lang.Object#hashCode() |
| 210 |
*/ |
| 211 |
public int hashCode() { |
| 212 |
if (this.getId() != null) |
| 213 |
return this.getId().hashCode(); |
| 214 |
return super.hashCode(); |
| 215 |
} |
| 216 |
} |