]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/AdapterImagerFactory.java
Use RuntimeEnvironmentRequest2 where applicable
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / AdapterImagerFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.swt;
13
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashMap;
17
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;
37
38 /**
39  * @author Antti Villberg
40  */
41 public class AdapterImagerFactory implements ImagerFactory {
42
43     final private Transformer transformer;
44     final private HashMap<String, Image> constants;
45     final private String resourceColumn;
46
47     public AdapterImagerFactory() {
48         this(null);
49     }
50
51     public AdapterImagerFactory(Transformer transformer) {
52         this(transformer, null, new HashMap<String, Image>(4));
53     }
54
55     public AdapterImagerFactory(Transformer transformer, String resourceColumn, HashMap<String, Image> constants) {
56         this.transformer = transformer;
57         this.constants = constants;
58         this.resourceColumn = resourceColumn;
59     }
60
61     private Resource getInputResource(final NodeContext context) {
62         Object input = context.getConstant(BuiltinKeys.INPUT);
63         if (transformer != null) {
64             input = transformer.transform(input);
65         }
66
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));
71         return null;
72     }
73
74
75     @SuppressWarnings("unchecked")
76     @Override
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());
81
82         final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
83         final Resource inputResource = getInputResource(context);
84
85         source.schedule(graph -> {
86             ImageDescriptor image = null;
87             Collection<Resource> types = Collections.emptyList();
88             try {
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));
93 //                }
94             } catch (DatabaseException e1) {
95                 ErrorLogger.defaultLogError(e1);
96             }
97             for (Resource type : types) {
98                 try {
99                     ImageDescriptorProvider provider = graph.getPossibleAdapter(type, ImageDescriptorProvider.class);
100                     if (provider != null) {
101                         try {
102                             ImageDescriptor descriptor = provider.get();
103                             if (descriptor != null)
104                                 image = descriptor;
105                             //System.out.println("*" + image);
106                         } catch (ProvisionException pe) {
107                             ErrorLogger.defaultLogError(pe);
108                         }
109                         break;
110                     }
111                 } catch (DatabaseException e) {
112                     ErrorLogger.defaultLogError(e);
113                 }
114             }
115
116             result.setImage(resourceColumn, image);
117             updater.scheduleReplace(context, key, result);
118         });
119
120         return result;
121     }
122
123 }