]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/LazyGraphLabeler.java
cf410444ed7753fc9a4d6df73882211fdc811f9e
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / LazyGraphLabeler.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.Collections;
15 import java.util.Map;
16
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
29 public abstract class LazyGraphLabeler extends LabelerStub {
30
31     protected final PrimitiveQueryUpdater       updater;
32     private final ResourceQuery<LabelerContent> labelQuery;
33     private final Listener<LabelerContent>      procedure;
34
35     public Object getIdentity(LabelerKey key) {
36         return key;
37     }
38
39     public LazyGraphLabeler(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
40         this.updater = updater;
41
42         labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
43
44             @Override
45             public LabelerContent perform(ReadGraph graph) throws DatabaseException {
46                 try {
47                     int cat = category(graph);
48                     Map<String, String> lbls = labels(graph);
49                     // Make sure that null is not returned.
50                     if (lbls == null)
51                         throw new NullPointerException("LazyGraphLabeler.labels is not allowed to return null, but " + LazyGraphLabeler.this.getClass() + " just did it");
52                     return new LabelerContent(cat, lbls);
53                 } catch (DatabaseException e) {
54                     throw e;
55                 } catch (Throwable t) {
56                     ErrorLogger.defaultLogError("LazyGraphLabeler.labelQuery produced unexpected exception.", t);
57                     return LabelerContent.NO_CONTENT;
58                 }
59             }
60
61             @Override
62             public String toString() {
63                 return LazyGraphLabeler.this + " with context " + context;
64             }
65
66         };
67
68         procedure = new Listener<LabelerContent>() {
69
70             @Override
71             public void execute(LabelerContent result) {
72                 setContent(result);
73                 updater.scheduleReplace(context, key, LazyGraphLabeler.this);
74             }
75
76             @Override
77             public boolean isDisposed() {
78                 return updater.isDisposed();
79             }
80
81             @Override
82             public void exception(Throwable t) {
83                 System.err.println("LazyGraphLabeler2: ");
84                 t.printStackTrace();
85             }
86
87         };
88     }
89
90     @Override
91     public Map<String, String> getLabels() {
92
93         if (content == LabelerContent.NO_CONTENT) {
94
95             final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
96             assert(source != null);
97             source.schedule(graph -> graph.asyncRequest(labelQuery, procedure));
98
99         }
100
101         if(content == null) return Collections.emptyMap();
102
103         return content.labels;
104
105     }
106
107     // OVERRIDE
108
109     public abstract Map<String, String> labels(ReadGraph graph) throws DatabaseException;
110
111     public int category(ReadGraph graph) throws DatabaseException {
112
113         if(content == null) return 0;
114
115         return content.category;
116
117     }
118
119 }