]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyParametrizedGraphLabeler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyParametrizedGraphLabeler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.graph.impl;
13
14 import java.util.Map;
15
16 import org.simantics.browsing.ui.BuiltinKeys;
17 import org.simantics.browsing.ui.DataSource;
18 import org.simantics.browsing.ui.NodeContext;
19 import org.simantics.browsing.ui.PrimitiveQueryUpdater;
20 import org.simantics.browsing.ui.common.labelers.LabelerContent;
21 import org.simantics.browsing.ui.common.labelers.LabelerStub;
22 import org.simantics.browsing.ui.graph.impl.request.ParametrizedResourceQuery;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.procedure.Listener;
26
27 /**
28  * A version of LazyLabeler that uses a set of arbitrary parameters to describe
29  * the label resource query within the class.
30  * 
31  * @author Tuukka Lehtonen
32  * 
33  * @see LazyGraphLabeler a non-parametrized version (more light-weight)
34  */
35 public abstract class LazyParametrizedGraphLabeler extends LabelerStub {
36
37     private final ParametrizedResourceQuery<LabelerContent> labelQuery;
38     private final Listener<LabelerContent>     labelProcedure;
39     private final PrimitiveQueryUpdater                     updater;
40
41     // DEBUG
42     static int debugCounter = 0;
43
44     public LazyParametrizedGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final BuiltinKeys.LabelerKey key, Object... parameters) {
45         this.updater = updater;
46         this.labelQuery = new ParametrizedResourceQuery<LabelerContent>(getClass(), context, parameters) {
47             @Override
48             public LabelerContent perform(ReadGraph graph) throws DatabaseException {
49                 return new LabelerContent(category(graph), labels(graph));
50             }
51
52         };
53         this.labelProcedure = new Listener<LabelerContent>() {
54
55             @Override
56             public void execute(LabelerContent result) {
57                 setContent(result);
58                 updater.scheduleReplace(context, key, LazyParametrizedGraphLabeler.this);
59             }
60
61             @Override
62             public void exception(Throwable t) {
63                 t.printStackTrace();
64             }
65
66             @Override
67             public boolean isDisposed() {
68                 return updater.isDisposed();
69             }
70
71         };
72     }
73
74     @Override
75     public Map<String, String> getLabels() {
76 //        new Exception().printStackTrace();
77         if (content == LabelerContent.NO_CONTENT) {
78             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
79
80             source.schedule(graph -> {
81                 try {
82                     graph.syncRequest(labelQuery, labelProcedure);
83                 } catch (DatabaseException e) {
84                     e.printStackTrace();
85                 }
86             });
87         }
88
89         return content.labels;
90     }
91
92     // OVERRIDE
93
94     public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;
95
96     public int category(ReadGraph graph) {
97         return content.category;
98     }
99
100 }