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.HashMap;
15 import java.util.function.Consumer;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.simantics.browsing.ui.BuiltinKeys;
20 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
21 import org.simantics.browsing.ui.DataSource;
22 import org.simantics.browsing.ui.NodeContext;
23 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
24 import org.simantics.browsing.ui.common.EvaluatorData.Transformer;
25 import org.simantics.browsing.ui.common.imagers.ContainerImager;
26 import org.simantics.browsing.ui.content.Imager;
27 import org.simantics.browsing.ui.content.ImagerFactory;
28 import org.simantics.browsing.ui.graph.impl.MissingImageDescriptor;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.exception.DatabaseException;
32 import org.simantics.ui.icons.ImageUtil;
33 import org.simantics.utils.datastructures.cache.ProvisionException;
34 import org.simantics.utils.ui.ErrorLogger;
37 * @deprecated favor {@link AdapterImagerFactory} instead
40 public class OldAdapterImagerFactory implements ImagerFactory {
42 private final Transformer transformer;
43 private final HashMap<String, ImageDescriptor> constants;
44 private final String resourceColumn;
46 public OldAdapterImagerFactory() {
50 public OldAdapterImagerFactory(Transformer transformer) {
51 this(transformer, null, new HashMap<String, ImageDescriptor>(4));
54 public OldAdapterImagerFactory(Transformer transformer,
55 String resourceColumn, HashMap<String, ImageDescriptor> 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) input;
69 else if (input instanceof IAdaptable)
70 return (Resource) (((IAdaptable) input).getAdapter(Resource.class));
74 @SuppressWarnings("unchecked")
76 public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
77 @SuppressWarnings("rawtypes")
78 final ContainerImager result = new ContainerImager((HashMap<String, ImageDescriptor>) constants.clone());
79 result.setImage(resourceColumn, MissingImageDescriptor.getInstance());
81 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
82 final Resource inputResource = getInputResource(context);
84 source.schedule(new Consumer<ReadGraph>() {
86 public void accept(ReadGraph source) {
88 ImageDescriptor descriptor = ImageUtil.adaptImageDescriptor(source, inputResource);
89 result.setImage(resourceColumn, descriptor);
90 updater.scheduleReplace(context, key, result);
91 } catch (ProvisionException e) {
92 ErrorLogger.defaultLogError(e);
93 } catch (DatabaseException e) {
94 ErrorLogger.defaultLogError(e);