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;
14 import java.util.Collections;
17 import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
18 import org.simantics.browsing.ui.DataSource;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
21 import org.simantics.browsing.ui.common.labelers.LabelerContent;
22 import org.simantics.browsing.ui.common.labelers.LabelerStub;
23 import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.procedure.Listener;
27 import org.simantics.utils.ui.ErrorLogger;
28 import org.slf4j.Logger;
30 public abstract class LazyGraphLabeler extends LabelerStub {
32 protected final PrimitiveQueryUpdater updater;
33 private final ResourceQuery<LabelerContent> labelQuery;
34 private final Listener<LabelerContent> procedure;
36 public Object getIdentity(LabelerKey key) {
40 public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
41 this.updater = updater;
43 labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
46 public LabelerContent perform(ReadGraph graph) throws DatabaseException {
48 int cat = category(graph);
49 Map<String, String> lbls = labels(graph);
50 // Make sure that null is not returned.
52 throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it");
53 return new LabelerContent(cat, lbls);
54 } catch (DatabaseException e) {
56 } catch (Throwable t) {
57 ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
58 return LabelerContent.NO_CONTENT;
63 public String toString() {
64 return LazyGraphLabeler.this + " with context " + context;
69 procedure = new Listener<LabelerContent>() {
72 public void execute(LabelerContent result) {
74 updater.scheduleReplace(context, key, LazyGraphLabeler.this);
78 public boolean isDisposed() {
79 return updater.isDisposed();
83 public void exception(Throwable t) {
84 getLogger().error("LazyGraphLabeler2: ", t);
91 public Map<String, String> getLabels() {
93 if (content == LabelerContent.NO_CONTENT) {
95 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
96 assert(source != null);
97 source.schedule(graph -> graph.asyncRequest(labelQuery, procedure));
101 if(content == null) return Collections.emptyMap();
103 return content.labels;
109 public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;
111 public int category(ReadGraph graph) throws DatabaseException {
113 if(content == null) return 0;
115 return content.category;
119 public abstract Logger getLogger();