1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.swt.graphics.Image;
21 import org.simantics.browsing.ui.BuiltinKeys;
22 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
23 import org.simantics.browsing.ui.DataSource;
24 import org.simantics.browsing.ui.NodeContext;
25 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
26 import org.simantics.browsing.ui.common.EvaluatorData.Transformer;
27 import org.simantics.browsing.ui.common.imagers.ContainerImager;
28 import org.simantics.browsing.ui.content.Imager;
29 import org.simantics.browsing.ui.content.ImagerFactory;
30 import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.ui.icons.ImageDescriptorProvider;
35 import org.simantics.utils.datastructures.cache.ProvisionException;
36 import org.simantics.utils.ui.ErrorLogger;
39 * @author Antti Villberg
41 public class AdapterImagerFactory implements ImagerFactory {
43 final private Transformer transformer;
44 final private HashMap<String, Image> constants;
45 final private String resourceColumn;
47 public AdapterImagerFactory() {
51 public AdapterImagerFactory(Transformer transformer) {
52 this(transformer, null, new HashMap<String, Image>(4));
55 public AdapterImagerFactory(Transformer transformer, String resourceColumn, HashMap<String, Image> constants) {
56 this.transformer = transformer;
57 this.constants = constants;
58 this.resourceColumn = resourceColumn;
61 private Resource getInputResource(final NodeContext context) {
62 Object input = context.getConstant(BuiltinKeys.INPUT);
63 if (transformer != null) {
64 input = transformer.transform(input);
67 if (input instanceof Resource)
68 return (Resource) context.getConstant(BuiltinKeys.INPUT);
69 else if (input instanceof IAdaptable)
70 return (Resource) (((IAdaptable) input).getAdapter(Resource.class));
75 @SuppressWarnings("unchecked")
77 public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
78 @SuppressWarnings("rawtypes")
79 final ContainerImager result = new ContainerImager((HashMap<String, Image>) constants.clone());
80 result.setImage(resourceColumn, MissingImageDescriptor.getInstance());
82 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
83 final Resource inputResource = getInputResource(context);
85 source.schedule(graph -> {
86 ImageDescriptor image = null;
87 Collection<Resource> types = Collections.emptyList();
89 types = graph.getPrincipalTypes(inputResource);
90 // System.out.println("AdapterImagerFactory " + source.adapt(inputResource, String.class));
91 // for(Resource r : types) {
92 // System.out.println("-" + source.adapt(r, String.class));
94 } catch (DatabaseException e1) {
95 ErrorLogger.defaultLogError(e1);
97 for (Resource type : types) {
99 ImageDescriptorProvider provider = graph.getPossibleAdapter(type, ImageDescriptorProvider.class);
100 if (provider != null) {
102 ImageDescriptor descriptor = provider.get();
103 if (descriptor != null)
105 //System.out.println("*" + image);
106 } catch (ProvisionException pe) {
107 ErrorLogger.defaultLogError(pe);
111 } catch (DatabaseException e) {
112 ErrorLogger.defaultLogError(e);
116 result.setImage(resourceColumn, image);
117 updater.scheduleReplace(context, key, result);