1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.swt;
\r
14 import java.util.HashMap;
\r
15 import java.util.function.Consumer;
\r
17 import org.eclipse.core.runtime.IAdaptable;
\r
18 import org.eclipse.jface.resource.ImageDescriptor;
\r
19 import org.simantics.browsing.ui.BuiltinKeys;
\r
20 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
\r
21 import org.simantics.browsing.ui.DataSource;
\r
22 import org.simantics.browsing.ui.NodeContext;
\r
23 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
24 import org.simantics.browsing.ui.common.EvaluatorData.Transformer;
\r
25 import org.simantics.browsing.ui.common.imagers.ContainerImager;
\r
26 import org.simantics.browsing.ui.content.Imager;
\r
27 import org.simantics.browsing.ui.content.ImagerFactory;
\r
28 import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor;
\r
29 import org.simantics.db.ReadGraph;
\r
30 import org.simantics.db.Resource;
\r
31 import org.simantics.db.exception.DatabaseException;
\r
32 import org.simantics.ui.icons.ImageUtil;
\r
33 import org.simantics.utils.datastructures.cache.ProvisionException;
\r
34 import org.simantics.utils.ui.ErrorLogger;
\r
37 * @deprecated favor {@link AdapterImagerFactory} instead
\r
40 public class OldAdapterImagerFactory implements ImagerFactory {
\r
42 private final Transformer transformer;
\r
43 private final HashMap<String, ImageDescriptor> constants;
\r
44 private final String resourceColumn;
\r
46 public OldAdapterImagerFactory() {
\r
50 public OldAdapterImagerFactory(Transformer transformer) {
\r
51 this(transformer, null, new HashMap<String, ImageDescriptor>(4));
\r
54 public OldAdapterImagerFactory(Transformer transformer,
\r
55 String resourceColumn, HashMap<String, ImageDescriptor> constants) {
\r
56 this.transformer = transformer;
\r
57 this.constants = constants;
\r
58 this.resourceColumn = resourceColumn;
\r
61 private Resource getInputResource(final NodeContext context) {
\r
62 Object input = context.getConstant(BuiltinKeys.INPUT);
\r
63 if (transformer != null) {
\r
64 input = transformer.transform(input);
\r
67 if (input instanceof Resource)
\r
68 return (Resource) input;
\r
69 else if (input instanceof IAdaptable)
\r
70 return (Resource) (((IAdaptable) input).getAdapter(Resource.class));
\r
74 @SuppressWarnings("unchecked")
\r
76 public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
\r
77 @SuppressWarnings("rawtypes")
\r
78 final ContainerImager result = new ContainerImager((HashMap<String, ImageDescriptor>) constants.clone());
\r
79 result.setImage(resourceColumn, MissingImageDescriptor.getInstance());
\r
81 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
\r
82 final Resource inputResource = getInputResource(context);
\r
84 source.schedule(new Consumer<ReadGraph>() {
\r
86 public void accept(ReadGraph source) {
\r
88 ImageDescriptor descriptor = ImageUtil.adaptImageDescriptor(source, inputResource);
\r
89 result.setImage(resourceColumn, descriptor);
\r
90 updater.scheduleReplace(context, key, result);
\r
91 } catch (ProvisionException e) {
\r
92 ErrorLogger.defaultLogError(e);
\r
93 } catch (DatabaseException e) {
\r
94 ErrorLogger.defaultLogError(e);
\r