]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Replaced deprecated ExtensionLoader
authorluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 12 Jan 2009 11:15:23 +0000 (11:15 +0000)
committerluukkainen <luukkainen@ac1ea38d-2e2b-0410-8846-a27921b304fc>
Mon, 12 Jan 2009 11:15:23 +0000 (11:15 +0000)
git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@8583 ac1ea38d-2e2b-0410-8846-a27921b304fc

org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/GeometryProviderRegistry.java

index fa92b87f21b859ce8257b6af4d25c9da56738277..07a48e492576957e370ad09a9ac14dfd4e6c8ae4 100644 (file)
  *******************************************************************************/\r
 package org.simantics.proconf.g3d.base;\r
 \r
+import java.util.ArrayList;\r
 import java.util.Collection;\r
+import java.util.Collections;\r
 import java.util.HashMap;\r
+import java.util.List;\r
 import java.util.Map;\r
 \r
+import org.eclipse.core.runtime.CoreException;\r
+import org.eclipse.core.runtime.IConfigurationElement;\r
+import org.eclipse.core.runtime.IExtension;\r
+import org.eclipse.core.runtime.IExtensionPoint;\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;\r
+import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;\r
+import org.eclipse.core.runtime.dynamichelpers.IFilter;\r
 import org.simantics.db.Resource;\r
 import org.simantics.layer0.utils.IEntity;\r
-import org.simantics.utils.ui.plugin.Extension;\r
-import org.simantics.utils.ui.plugin.ExtensionLoader;\r
+import org.simantics.utils.ui.ErrorLogger;\r
 \r
 import com.jme.scene.Geometry;\r
 \r
-public class GeometryProviderRegistry {\r
+public class GeometryProviderRegistry implements IExtensionChangeHandler {\r
     private static GeometryProviderRegistry instance;\r
     public final static String ELEMENT_NAME = "Geometry";\r
-    public final static String NAME_SPACE = "org.simantics.proconf.g3d";\r
+    public final static String NAMESPACE = "org.simantics.proconf.g3d";\r
     public final static String EP_NAME = "geometry";\r
     \r
-    private ExtensionLoader<GeometryProvider> loader;\r
+    private ExtensionTracker tracker;\r
+    \r
+    private List<GeometryProviderExtension> extensions = new ArrayList<GeometryProviderExtension>();\r
     private Map<Resource,GeometryProvider> providers;\r
     \r
     private GeometryProviderRegistry() {\r
-        loader = new ExtensionLoader<GeometryProvider>(ELEMENT_NAME, NAME_SPACE, EP_NAME);\r
         providers = new HashMap<Resource,GeometryProvider>();\r
+        \r
+        tracker = new ExtensionTracker();\r
+               \r
+               IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(NAMESPACE,EP_NAME);\r
+               loadExtensions(ep.getConfigurationElements());\r
+               \r
+               IFilter filter = ExtensionTracker.createExtensionPointFilter(ep);\r
+               tracker.registerHandler(this, filter);\r
     }\r
     \r
-    public static GeometryProviderRegistry getInstance() {\r
+    private void close() {\r
+               tracker.close();\r
+               tracker = null;\r
+               extensions = new ArrayList<GeometryProviderExtension>();\r
+       }\r
+    \r
+    public static synchronized GeometryProviderRegistry getInstance() {\r
         if (instance==null) instance = new GeometryProviderRegistry();\r
         return instance;\r
     }\r
-\r
-    public Extension<GeometryProvider>[] getExtensions() {\r
-        return loader.getExtensions();\r
-    }\r
+    \r
+    public static synchronized void dispose() {\r
+               if (instance != null) {\r
+                       instance.close();\r
+                       instance = null;\r
+               }       \r
+       }\r
+    \r
+    public synchronized List<GeometryProviderExtension> getExtensions() {\r
+               return Collections.unmodifiableList(extensions);\r
+       }\r
+    \r
+    private synchronized void loadExtensions(IConfigurationElement[] elements) {\r
+               for (IConfigurationElement el : elements) {\r
+                       String id = el.getAttribute("id");\r
+                       try {\r
+                               Object o = el.createExecutableExtension("class");\r
+                               GeometryProviderExtension ext = new GeometryProviderExtension(id,(GeometryProvider)o);\r
+                               tracker.registerObject(el.getDeclaringExtension(), ext, IExtensionTracker.REF_STRONG);\r
+                               extensions.add(ext);\r
+                       } catch (CoreException e) {\r
+                               ErrorLogger.defaultLogError("Cannot create java interface for GeometryProvider " + id, e);\r
+                       }\r
+               }\r
+       }\r
     \r
     private Map<Resource,GeometryProvider> getProviderMap() {\r
         return providers;\r
@@ -60,10 +107,11 @@ public class GeometryProviderRegistry {
        for (IEntity t : types) {\r
                GeometryProvider provider = getInstance().getProviderMap().get(t.getResource());\r
                if (provider == null) {\r
-                for (Extension<GeometryProvider> e : getInstance().getExtensions()) {\r
-                    if (e.getInstance().canHandle(thing)) {\r
-                        getInstance().getProviderMap().put(t.getResource(), e.getInstance());\r
-                        return e.getInstance();\r
+                //for (Extension<GeometryProvider> e : getInstance().getExtensions()) {\r
+                       for (GeometryProviderExtension e : getInstance().getExtensions()) {\r
+                    if (e.provider.canHandle(thing)) {\r
+                        getInstance().getProviderMap().put(t.getResource(), e.provider);\r
+                        return e.provider;\r
                     }\r
                 }\r
                 \r
@@ -74,4 +122,28 @@ public class GeometryProviderRegistry {
        \r
        throw new UnsupportedOperationException("Cannot handle resource " + thing);\r
     }\r
+    \r
+    @Override\r
+    public void addExtension(IExtensionTracker tracker, IExtension extension) {\r
+        loadExtensions(extension.getConfigurationElements());\r
+    }\r
+    \r
+    @Override\r
+       public synchronized void removeExtension(IExtension extension, Object[] objects) {\r
+               for (Object o : objects) {\r
+                       GeometryProviderExtension ext = (GeometryProviderExtension) o;\r
+                       tracker.unregisterObject(extension, ext);\r
+                       extensions.remove(ext);\r
+               }\r
+       }\r
+    \r
+    public class GeometryProviderExtension {\r
+               public String id;\r
+               public GeometryProvider provider;\r
+               \r
+               public GeometryProviderExtension(String id, GeometryProvider provider) {\r
+                       this.id = id;\r
+                       this.provider = provider;\r
+               }\r
+       }\r
 }\r