X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.swt%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fswt%2FAdapterImagerFactory.java;h=1b35cc89104a76617b659e431ef594640d360a1a;hp=6afe648fac888719ee6b2098ccfc4801d36b3148;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdapterImagerFactory.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdapterImagerFactory.java index 6afe648fa..1b35cc891 100644 --- a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdapterImagerFactory.java +++ b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdapterImagerFactory.java @@ -1,123 +1,123 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * 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.browsing.ui.swt; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; - -import org.eclipse.core.runtime.IAdaptable; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.swt.graphics.Image; -import org.simantics.browsing.ui.BuiltinKeys; -import org.simantics.browsing.ui.BuiltinKeys.ImagerKey; -import org.simantics.browsing.ui.DataSource; -import org.simantics.browsing.ui.NodeContext; -import org.simantics.browsing.ui.PrimitiveQueryUpdater; -import org.simantics.browsing.ui.common.EvaluatorData.Transformer; -import org.simantics.browsing.ui.common.imagers.ContainerImager; -import org.simantics.browsing.ui.content.Imager; -import org.simantics.browsing.ui.content.ImagerFactory; -import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.ui.icons.ImageDescriptorProvider; -import org.simantics.utils.datastructures.cache.ProvisionException; -import org.simantics.utils.ui.ErrorLogger; - -/** - * @author Antti Villberg - */ -public class AdapterImagerFactory implements ImagerFactory { - - final private Transformer transformer; - final private HashMap constants; - final private String resourceColumn; - - public AdapterImagerFactory() { - this(null); - } - - public AdapterImagerFactory(Transformer transformer) { - this(transformer, null, new HashMap(4)); - } - - public AdapterImagerFactory(Transformer transformer, String resourceColumn, HashMap constants) { - this.transformer = transformer; - this.constants = constants; - this.resourceColumn = resourceColumn; - } - - private Resource getInputResource(final NodeContext context) { - Object input = context.getConstant(BuiltinKeys.INPUT); - if (transformer != null) { - input = transformer.transform(input); - } - - if (input instanceof Resource) - return (Resource) context.getConstant(BuiltinKeys.INPUT); - else if (input instanceof IAdaptable) - return (Resource) (((IAdaptable) input).getAdapter(Resource.class)); - return null; - } - - - @SuppressWarnings("unchecked") - @Override - public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) { - @SuppressWarnings("rawtypes") - final ContainerImager result = new ContainerImager((HashMap) constants.clone()); - result.setImage(resourceColumn, MissingImageDescriptor.getInstance()); - - final DataSource source = updater.getDataSource(ReadGraph.class); - final Resource inputResource = getInputResource(context); - - source.schedule(graph -> { - ImageDescriptor image = null; - Collection types = Collections.emptyList(); - try { - types = graph.getPrincipalTypes(inputResource); -// System.out.println("AdapterImagerFactory " + source.adapt(inputResource, String.class)); -// for(Resource r : types) { -// System.out.println("-" + source.adapt(r, String.class)); -// } - } catch (DatabaseException e1) { - ErrorLogger.defaultLogError(e1); - } - for (Resource type : types) { - try { - ImageDescriptorProvider provider = graph.getPossibleAdapter(type, ImageDescriptorProvider.class); - if (provider != null) { - try { - ImageDescriptor descriptor = provider.get(); - if (descriptor != null) - image = descriptor; - //System.out.println("*" + image); - } catch (ProvisionException pe) { - ErrorLogger.defaultLogError(pe); - } - break; - } - } catch (DatabaseException e) { - ErrorLogger.defaultLogError(e); - } - } - - result.setImage(resourceColumn, image); - updater.scheduleReplace(context, key, result); - }); - - return result; - } - -} +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * 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.browsing.ui.swt; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; +import org.simantics.browsing.ui.BuiltinKeys; +import org.simantics.browsing.ui.BuiltinKeys.ImagerKey; +import org.simantics.browsing.ui.DataSource; +import org.simantics.browsing.ui.NodeContext; +import org.simantics.browsing.ui.PrimitiveQueryUpdater; +import org.simantics.browsing.ui.common.EvaluatorData.Transformer; +import org.simantics.browsing.ui.common.imagers.ContainerImager; +import org.simantics.browsing.ui.content.Imager; +import org.simantics.browsing.ui.content.ImagerFactory; +import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.ui.icons.ImageDescriptorProvider; +import org.simantics.utils.datastructures.cache.ProvisionException; +import org.simantics.utils.ui.ErrorLogger; + +/** + * @author Antti Villberg + */ +public class AdapterImagerFactory implements ImagerFactory { + + final private Transformer transformer; + final private HashMap constants; + final private String resourceColumn; + + public AdapterImagerFactory() { + this(null); + } + + public AdapterImagerFactory(Transformer transformer) { + this(transformer, null, new HashMap(4)); + } + + public AdapterImagerFactory(Transformer transformer, String resourceColumn, HashMap constants) { + this.transformer = transformer; + this.constants = constants; + this.resourceColumn = resourceColumn; + } + + private Resource getInputResource(final NodeContext context) { + Object input = context.getConstant(BuiltinKeys.INPUT); + if (transformer != null) { + input = transformer.transform(input); + } + + if (input instanceof Resource) + return (Resource) context.getConstant(BuiltinKeys.INPUT); + else if (input instanceof IAdaptable) + return (Resource) (((IAdaptable) input).getAdapter(Resource.class)); + return null; + } + + + @SuppressWarnings("unchecked") + @Override + public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) { + @SuppressWarnings("rawtypes") + final ContainerImager result = new ContainerImager((HashMap) constants.clone()); + result.setImage(resourceColumn, MissingImageDescriptor.getInstance()); + + final DataSource source = updater.getDataSource(ReadGraph.class); + final Resource inputResource = getInputResource(context); + + source.schedule(graph -> { + ImageDescriptor image = null; + Collection types = Collections.emptyList(); + try { + types = graph.getPrincipalTypes(inputResource); +// System.out.println("AdapterImagerFactory " + source.adapt(inputResource, String.class)); +// for(Resource r : types) { +// System.out.println("-" + source.adapt(r, String.class)); +// } + } catch (DatabaseException e1) { + ErrorLogger.defaultLogError(e1); + } + for (Resource type : types) { + try { + ImageDescriptorProvider provider = graph.getPossibleAdapter(type, ImageDescriptorProvider.class); + if (provider != null) { + try { + ImageDescriptor descriptor = provider.get(); + if (descriptor != null) + image = descriptor; + //System.out.println("*" + image); + } catch (ProvisionException pe) { + ErrorLogger.defaultLogError(pe); + } + break; + } + } catch (DatabaseException e) { + ErrorLogger.defaultLogError(e); + } + } + + result.setImage(resourceColumn, image); + updater.scheduleReplace(context, key, result); + }); + + return result; + } + +}