]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/GeometryProviderRegistry.java
git-svn-id: https://www.simantics.org/svn/simantics/3d/branches/dev@7251 ac1ea38d...
[simantics/3d.git] / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / base / GeometryProviderRegistry.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.base;\r
12 \r
13 import java.util.Collection;\r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 \r
17 import org.simantics.db.Resource;\r
18 import org.simantics.layer0.utils.IEntity;\r
19 import org.simantics.utils.ui.plugin.Extension;\r
20 import org.simantics.utils.ui.plugin.ExtensionLoader;\r
21 \r
22 import com.jme.scene.Geometry;\r
23 \r
24 public class GeometryProviderRegistry {\r
25     private static GeometryProviderRegistry instance;\r
26     public final static String ELEMENT_NAME = "Geometry";\r
27     public final static String NAME_SPACE = "org.simantics.proconf.g3d";\r
28     public final static String EP_NAME = "geometry";\r
29     \r
30     private ExtensionLoader<GeometryProvider> loader;\r
31     private Map<Resource,GeometryProvider> providers;\r
32     \r
33     private GeometryProviderRegistry() {\r
34         loader = new ExtensionLoader<GeometryProvider>(ELEMENT_NAME, NAME_SPACE, EP_NAME);\r
35         providers = new HashMap<Resource,GeometryProvider>();\r
36     }\r
37     \r
38     public static GeometryProviderRegistry getInstance() {\r
39         if (instance==null) instance = new GeometryProviderRegistry();\r
40         return instance;\r
41     }\r
42 \r
43     public Extension<GeometryProvider>[] getExtensions() {\r
44         return loader.getExtensions();\r
45     }\r
46     \r
47     private Map<Resource,GeometryProvider> getProviderMap() {\r
48         return providers;\r
49     }\r
50     \r
51     public static Geometry[] getGeometry(IEntity thing, boolean transform) {\r
52         GeometryProvider provider = getGeometryProvider(thing);\r
53         return provider.getGeometryFromResource(thing, transform);\r
54     }\r
55     \r
56     public static GeometryProvider getGeometryProvider(IEntity thing) {\r
57         Collection<IEntity> types = thing.getRelatedObjects(thing.getGraph().getBuiltins().InstanceOf);\r
58         \r
59         //Resource types[] = OntologyUtils.getTypes(resource);\r
60         for (IEntity t : types) {\r
61                 GeometryProvider provider = getInstance().getProviderMap().get(t.getResource());\r
62                 if (provider == null) {\r
63                 for (Extension<GeometryProvider> e : getInstance().getExtensions()) {\r
64                     if (e.getInstance().canHandle(thing)) {\r
65                         getInstance().getProviderMap().put(t.getResource(), e.getInstance());\r
66                         return e.getInstance();\r
67                     }\r
68                 }\r
69                 \r
70             } else {\r
71                 return provider;\r
72             }\r
73         }\r
74         \r
75         throw new UnsupportedOperationException("Cannot handle resource " + thing);\r
76     }\r
77 }\r