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.ReadGraph;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.exception.PendingVariableException;
28 import org.simantics.db.procedure.Listener;
29 import org.simantics.db.procedure.Procedure;
30 import org.simantics.utils.ui.ErrorLogger;
32 public abstract class FinalImagerContributionImpl implements Imager {
34 private final static Map<String, ImageDescriptor> PENDING = Collections.emptyMap();
36 protected final PrimitiveQueryUpdater updater;
37 private final ResourceQuery<Map<String, ImageDescriptor>> imagerQuery;
39 protected final NodeContext context;
40 private final BuiltinKeys.ImagerKey key;
42 protected Map<String, ImageDescriptor> content = null;
44 public Object getIdentity(ImagerKey key) {
48 public FinalImagerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {
50 this.updater = updater;
51 this.context = context;
54 imagerQuery = new ResourceQuery<Map<String, ImageDescriptor>>(getIdentity(key), context) {
57 public Map<String, ImageDescriptor> perform(ReadGraph graph) throws DatabaseException {
59 return getDescriptors(graph, context);
60 } catch (PendingVariableException e) {
62 } catch (DatabaseException e) {
64 } catch (Throwable t) {
65 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
71 public String toString() {
72 return FinalImagerContributionImpl.this + " with context " + context;
79 protected Procedure<Map<String, ImageDescriptor>> createProcedure() {
81 return new Procedure<Map<String, ImageDescriptor>>() {
84 public void execute(Map<String, ImageDescriptor> result) {
85 replaceResult(result);
89 public void exception(Throwable t) {
90 ErrorLogger.defaultLogError("FinalImagerContributionImpl.imagerQuery failed, see exception for details.", t);
97 protected void replaceResult(Map<String, ImageDescriptor> desc) {
99 updater.scheduleReplace(context, key, this);
102 @SuppressWarnings("unchecked")
104 public ImageDescriptor getImage(String key) {
106 if (content == null) {
110 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
111 assert(source != null);
113 final Procedure<Map<String, ImageDescriptor>> procedure = createProcedure();
114 source.schedule(graph -> {
115 if(procedure instanceof Listener<?>)
116 graph.asyncRequest(imagerQuery, (Listener<Map<String, ImageDescriptor>>)procedure);
118 graph.asyncRequest(imagerQuery, procedure);
125 ImageDescriptor descriptor = content.get(key);
131 public abstract Map<String, ImageDescriptor> getDescriptors(ReadGraph graph, NodeContext context) throws DatabaseException;