/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ 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.ErrorLogger; import com.jme.renderer.Renderer; import com.jme.scene.state.RenderState; public class AppearanceProviderRegistry implements IExtensionChangeHandler { private static AppearanceProviderRegistry instance; //public final static String ELEMENT_NAME = "Appearance"; public final static String NAMESPACE = "org.simantics.proconf.g3d"; public final static String EP_NAME = "appearance"; private ExtensionTracker tracker; private List extensions = new ArrayList(); //private Map providers; private AppearanceProviderRegistry() { //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); } private void close() { tracker.close(); tracker = null; extensions = new ArrayList(); } public static synchronized AppearanceProviderRegistry getInstance() { if (instance==null) instance = new AppearanceProviderRegistry(); return instance; } 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"); AppearanceProviderExtension ext = new AppearanceProviderExtension(id,(AppearanceProvider)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; // } public static Collection getAppearance(IEntity thing, Renderer renderer) { AppearanceProvider provider = getAppearanceProvider(thing); return provider.getAppearanceFromResource(thing, renderer); } public static AppearanceProvider getAppearanceProvider(IEntity thing) { //Collection types = thing.getRelatedObjects(thing.getGraph().getBuiltins().InstanceOf); //Resource types[] = OntologyUtils.getTypes(resource); // for (IEntity t : types) { //AppearanceProvider provider = getInstance().getProviderMap().get(t.getResource()); //if (provider == null) { //for (Extension e : getInstance().getExtensions()) { for (AppearanceProviderExtension e : getInstance().getExtensions()) { if (e.provider.canHandle(thing)) { //getInstance().getProviderMap().put(t.getResource(), e.provider); return e.provider; } } // } else { // return provider; // } // } return null; } @Override public void addExtension(IExtensionTracker tracker, IExtension extension) { loadExtensions(extension.getConfigurationElements()); } @Override public synchronized void removeExtension(IExtension extension, Object[] objects) { for (Object o : objects) { AppearanceProviderExtension ext = (AppearanceProviderExtension) o; tracker.unregisterObject(extension, ext); extensions.remove(ext); } } public class AppearanceProviderExtension { public String id; public AppearanceProvider provider; public AppearanceProviderExtension(String id, AppearanceProvider provider) { this.id = id; this.provider = provider; } } }