001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.core.internal;
018:
019: import java.text.MessageFormat;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.List;
023:
024: import org.eclipse.core.runtime.IConfigurationElement;
025: import org.eclipse.core.runtime.IExtension;
026: import org.eclipse.core.runtime.IExtensionPoint;
027: import org.eclipse.core.runtime.IExtensionRegistry;
028: import org.eclipse.core.runtime.IStatus;
029: import org.eclipse.core.runtime.Platform;
030: import org.eclipse.core.runtime.Plugin;
031: import org.eclipse.core.runtime.Status;
032:
033: /**
034: * A utility class to assist in processing extensions
035: *
036: * @see net.refractions.udig.core.internal.ExtensionPointProcessor
037: * @author Jesse Eichar
038: * @version $Revision: 1.9 $
039: */
040: public class ExtensionPointUtil {
041:
042: /**
043: * Finds all the Extension or the Extension point identified by the xpid method and calls a
044: * callback method on the processor class for processing of the extension.
045: *
046: * @see net.refractions.udig.core.internal.ExtensionPointProcessor#process(IExtension,
047: * IConfigurationElement)
048: * @param xpid The id of the ExtensionPoint for which the extensions are to be processed
049: * @param processor The object that wishes to process the extension for the ExtensionPoint
050: * @deprecated Please use process( Plugin, String, ExtentionPointProcessor ) - so we can
051: */
052: public static void process(String xpid,
053: ExtensionPointProcessor processor) {
054: process(CorePlugin.getDefault(), xpid, processor);
055: }
056:
057: /**
058: * Finds all the Extension or the Extension point identified by the xpid method and calls a
059: * callback method on the processor class for processing of the extension.
060: *
061: * @see net.refractions.udig.core.internal.ExtensionPointProcessor#process(IExtension,
062: * IConfigurationElement)
063: * @param plugin plugin processing this extention point
064: * @param xpid The id of the ExtensionPoint for which the extensions are to be processed
065: * @param processor The object that wishes to process the extension for the ExtensionPoint
066: */
067: public static void process(Plugin plugin, String xpid,
068: ExtensionPointProcessor processor) {
069: IExtensionRegistry registry = Platform.getExtensionRegistry();
070: IExtensionPoint extensionPoint = registry
071: .getExtensionPoint(xpid);
072: if (extensionPoint == null)
073: return;
074: IExtension[] extensions = extensionPoint.getExtensions();
075:
076: // For each extension ...
077: for (int i = 0; i < extensions.length; i++) {
078: IExtension extension = extensions[i];
079: IConfigurationElement[] elements = extension
080: .getConfigurationElements();
081:
082: // For each member of the extension ...
083: for (int j = 0; j < elements.length; j++) {
084: IConfigurationElement element = elements[j];
085: try {
086: processor.process(extension, element);
087: } catch (Throwable exception) {
088: plugin
089: .getLog()
090: .log(
091: new Status(
092: IStatus.ERROR,
093: element
094: .getNamespaceIdentifier(),
095: 0,
096: MessageFormat
097: .format(
098: "Error processing extension {0}", new Object[] { exception }), exception)); //$NON-NLS-1$
099: }
100: }
101: }
102: }
103:
104: /**
105: * Process extentionpoint with an itemCreator.
106: * <p>
107: * Example Use:
108: *
109: * <pre><code>
110: * List<Thingy> stuff = ExtentionPointUtil.list( new ExtentionPointProcessor2(){
111: * public Object process( IExtention extention, IConfigurationElement element ){
112: * return new Thingy( element );
113: * }
114: * }
115: * </code></pre>
116: *
117: * </p>
118: *
119: * @param xpid
120: * @param itemCreator Used to process extention points into items for a list
121: * @return List
122: */
123: public static List list(String xpid,
124: ExtensionPointItemCreator itemCreator) {
125: IExtensionRegistry registry = Platform.getExtensionRegistry();
126: IExtensionPoint extensionPoint = registry
127: .getExtensionPoint(xpid);
128: if (extensionPoint == null) {
129: return Collections.EMPTY_LIST;
130: }
131: IExtension[] extensions = extensionPoint.getExtensions();
132: List<Object> list = new ArrayList<Object>();
133: // For each extension ...
134: for (int i = 0; i < extensions.length; i++) {
135: IExtension extension = extensions[i];
136: IConfigurationElement[] elements = extension
137: .getConfigurationElements();
138:
139: // For each member of the extension ...
140: for (int j = 0; j < elements.length; j++) {
141: IConfigurationElement element = elements[j];
142: try {
143: Object obj = itemCreator.createItem(extension,
144: element);
145: if (obj == null)
146: continue; // warning?
147:
148: list.add(obj);
149: } catch (Throwable exception) {
150: CorePlugin
151: .getDefault()
152: .getLog()
153: .log(
154: new Status(
155: IStatus.WARNING,
156: extension.getNamespace(),
157: 0,
158: MessageFormat
159: .format(
160: "Error processing extension {0}", new Object[] { exception }), exception)); //$NON-NLS-1$
161: }
162: }
163: }
164: return list;
165: }
166: }
|