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.graph.impl;
\r
14 import java.util.Map;
\r
16 import org.simantics.browsing.ui.BuiltinKeys;
\r
17 import org.simantics.browsing.ui.DataSource;
\r
18 import org.simantics.browsing.ui.NodeContext;
\r
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
\r
20 import org.simantics.browsing.ui.common.labelers.LabelerContent;
\r
21 import org.simantics.browsing.ui.common.labelers.LabelerStub;
\r
22 import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.simantics.db.procedure.Listener;
\r
28 * A version of LazyLabeler that uses a set of arbitrary parameters to describe
\r
29 * the label resource query within the class.
\r
31 * @author Tuukka Lehtonen
\r
33 * @see LazyGraphLabeler a non-parametrized version (more light-weight)
\r
35 public abstract class LazyParametrizedGraphLabeler extends LabelerStub {
\r
37 private final ParametrizedResourceQuery<LabelerContent> labelQuery;
\r
38 private final Listener<LabelerContent> labelProcedure;
\r
39 private final PrimitiveQueryUpdater updater;
\r
42 static int debugCounter = 0;
\r
44 public LazyParametrizedGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final BuiltinKeys.LabelerKey key, Object... parameters) {
\r
45 this.updater = updater;
\r
46 this.labelQuery = new ParametrizedResourceQuery<LabelerContent>(getClass(), context, parameters) {
\r
48 public LabelerContent perform(ReadGraph graph) throws DatabaseException {
\r
49 return new LabelerContent(category(graph), labels(graph));
\r
53 this.labelProcedure = new Listener<LabelerContent>() {
\r
56 public void execute(LabelerContent result) {
\r
58 updater.scheduleReplace(context, key, LazyParametrizedGraphLabeler.this);
\r
62 public void exception(Throwable t) {
\r
63 t.printStackTrace();
\r
67 public boolean isDisposed() {
\r
68 return updater.isDisposed();
\r
75 public Map<String, String> getLabels() {
\r
76 // new Exception().printStackTrace();
\r
77 if (content == LabelerContent.NO_CONTENT) {
\r
78 final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
\r
80 source.schedule(graph -> {
\r
82 graph.syncRequest(labelQuery, labelProcedure);
\r
83 } catch (DatabaseException e) {
\r
84 e.printStackTrace();
\r
89 return content.labels;
\r
94 public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;
\r
96 public int category(ReadGraph graph) {
\r
97 return content.category;
\r