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.graph.impl.contribution;
14 import java.util.Collections;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;
20 import org.simantics.browsing.ui.DataSource;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
23 import org.simantics.browsing.ui.content.Imager;
24 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
25 import org.simantics.db.AsyncReadGraph;
26 import org.simantics.db.ReadGraph;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.exception.PendingVariableException;
29 import org.simantics.db.procedure.Listener;
30 import org.simantics.db.procedure.Procedure;
31 import org.simantics.utils.ui.ErrorLogger;
33 public abstract class FinalImagerContributionImpl implements Imager {
35 private final static Map<String, ImageDescriptor> PENDING = Collections.emptyMap();
37 protected final PrimitiveQueryUpdater updater;
38 private final ResourceQuery<Map<String, ImageDescriptor>> imagerQuery;
40 protected final NodeContext context;
41 private final BuiltinKeys.ImagerKey key;
43 protected Map<String, ImageDescriptor> content = null;
45 public Object getIdentity(ImagerKey key) {
49 public FinalImagerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
51 this.updater = updater;
52 this.context = context;
55 imagerQuery = new ResourceQuery<Map<String, ImageDescriptor>>(getIdentity(key), context) {
58 public Map<String, ImageDescriptor> perform(ReadGraph graph) throws DatabaseException {
60 return getDescriptors(graph, context);
61 } catch (PendingVariableException e) {
63 } catch (DatabaseException e) {
65 } catch (Throwable t) {
66 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
72 public String toString() {
73 return FinalImagerContributionImpl.this + " with context " + context;
80 protected Procedure<Map<String, ImageDescriptor>> createProcedure() {
82 return new Procedure<Map<String, ImageDescriptor>>() {
85 public void execute(Map<String, ImageDescriptor> result) {
86 replaceResult(result);
90 public void exception(Throwable t) {
91 ErrorLogger.defaultLogError("FinalImagerContributionImpl.imagerQuery failed, see exception for details.", t);
98 protected void replaceResult(Map<String, ImageDescriptor> desc) {
100 updater.scheduleReplace(context, key, this);
103 @SuppressWarnings("unchecked")
105 public ImageDescriptor getImage(String key) {
107 if (content == null) {
111 final DataSource<AsyncReadGraph> source = updater.getDataSource(AsyncReadGraph.class);
112 assert(source != null);
114 final Procedure<Map<String, ImageDescriptor>> procedure = createProcedure();
115 source.schedule(graph -> {
116 if(procedure instanceof Listener<?>)
117 graph.asyncRequest(imagerQuery, (Listener<Map<String, ImageDescriptor>>)procedure);
119 graph.asyncRequest(imagerQuery, procedure);
126 ImageDescriptor descriptor = content.get(key);
132 public abstract Map<String, ImageDescriptor> getDescriptors(ReadGraph graph, NodeContext context) throws DatabaseException;