From: luukkainen Date: Mon, 12 Jan 2009 11:15:23 +0000 (+0000) Subject: Replaced deprecated ExtensionLoader X-Git-Tag: simantics-1.19.0~20^2~16 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=8e0be29176259b3290fb364c37167df44f63f4c6;p=simantics%2F3d.git Replaced deprecated ExtensionLoader git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@8583 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/GeometryProviderRegistry.java b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/GeometryProviderRegistry.java index fa92b87f..07a48e49 100644 --- a/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/GeometryProviderRegistry.java +++ b/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/GeometryProviderRegistry.java @@ -10,39 +10,86 @@ *******************************************************************************/ package org.simantics.proconf.g3d.base; +import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker; +import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; +import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; +import org.eclipse.core.runtime.dynamichelpers.IFilter; import org.simantics.db.Resource; import org.simantics.layer0.utils.IEntity; -import org.simantics.utils.ui.plugin.Extension; -import org.simantics.utils.ui.plugin.ExtensionLoader; +import org.simantics.utils.ui.ErrorLogger; import com.jme.scene.Geometry; -public class GeometryProviderRegistry { +public class GeometryProviderRegistry implements IExtensionChangeHandler { private static GeometryProviderRegistry instance; public final static String ELEMENT_NAME = "Geometry"; - public final static String NAME_SPACE = "org.simantics.proconf.g3d"; + public final static String NAMESPACE = "org.simantics.proconf.g3d"; public final static String EP_NAME = "geometry"; - private ExtensionLoader loader; + private ExtensionTracker tracker; + + private List extensions = new ArrayList(); private Map providers; private GeometryProviderRegistry() { - loader = new ExtensionLoader(ELEMENT_NAME, NAME_SPACE, EP_NAME); providers = new HashMap(); + + tracker = new ExtensionTracker(); + + IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE,EP_NAME); + loadExtensions(ep.getConfigurationElements()); + + IFilter filter = ExtensionTracker.createExtensionPointFilter(ep); + tracker.registerHandler(this, filter); } - public static GeometryProviderRegistry getInstance() { + private void close() { + tracker.close(); + tracker = null; + extensions = new ArrayList(); + } + + public static synchronized GeometryProviderRegistry getInstance() { if (instance==null) instance = new GeometryProviderRegistry(); return instance; } - - public Extension[] getExtensions() { - return loader.getExtensions(); - } + + public static synchronized void dispose() { + if (instance != null) { + instance.close(); + instance = null; + } + } + + public synchronized List getExtensions() { + return Collections.unmodifiableList(extensions); + } + + private synchronized void loadExtensions(IConfigurationElement[] elements) { + for (IConfigurationElement el : elements) { + String id = el.getAttribute("id"); + try { + Object o = el.createExecutableExtension("class"); + GeometryProviderExtension ext = new GeometryProviderExtension(id,(GeometryProvider)o); + tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG); + extensions.add(ext); + } catch (CoreException e) { + ErrorLogger.defaultLogError("Cannot create java interface for GeometryProvider " + id, e); + } + } + } private Map getProviderMap() { return providers; @@ -60,10 +107,11 @@ public class GeometryProviderRegistry { for (IEntity t : types) { GeometryProvider provider = getInstance().getProviderMap().get(t.getResource()); if (provider == null) { - for (Extension e : getInstance().getExtensions()) { - if (e.getInstance().canHandle(thing)) { - getInstance().getProviderMap().put(t.getResource(), e.getInstance()); - return e.getInstance(); + //for (Extension e : getInstance().getExtensions()) { + for (GeometryProviderExtension e : getInstance().getExtensions()) { + if (e.provider.canHandle(thing)) { + getInstance().getProviderMap().put(t.getResource(), e.provider); + return e.provider; } } @@ -74,4 +122,28 @@ public class GeometryProviderRegistry { throw new UnsupportedOperationException("Cannot handle resource " + thing); } + + @Override + public void addExtension(IExtensionTracker tracker, IExtension extension) { + loadExtensions(extension.getConfigurationElements()); + } + + @Override + public synchronized void removeExtension(IExtension extension, Object[] objects) { + for (Object o : objects) { + GeometryProviderExtension ext = (GeometryProviderExtension) o; + tracker.unregisterObject(extension, ext); + extensions.remove(ext); + } + } + + public class GeometryProviderExtension { + public String id; + public GeometryProvider provider; + + public GeometryProviderExtension(String id, GeometryProvider provider) { + this.id = id; + this.provider = provider; + } + } }