]> gerrit.simantics Code Review - simantics/platform.git/blob - diagram/monitor/AvailableTemplateFactory.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / diagram / monitor / AvailableTemplateFactory.java
1 package org.simantics.modeling.ui.diagram.monitor;
2
3 import java.util.Collections;
4 import java.util.Map;
5 import java.util.TreeMap;
6
7 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.adapter.Instances;
12 import org.simantics.db.layer0.request.PossibleModel;
13 import org.simantics.diagram.stubs.DiagramResource;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.utils.strings.AlphanumComparator;
16 import org.simantics.utils.strings.StringUtils;
17
18 /**
19  * @author Tuukka Lehtonen
20  */
21 public class AvailableTemplateFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
22
23     @Override
24     public Map<String, Object> perform(ReadGraph graph, Resource input) throws DatabaseException {
25
26         Layer0 L0 = Layer0.getInstance(graph);
27         DiagramResource DIA = DiagramResource.getInstance(graph);
28         Instances query = graph.adapt(DIA.MonitorTemplate, Instances.class);
29         Resource model = graph.syncRequest(new PossibleModel(input));
30         if (model == null)
31             return Collections.emptyMap();
32         Map<String, Object> result = new TreeMap<>(AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
33         for (Resource template : query.find(graph, model)) {
34             String label = graph.getPossibleRelatedAdapter(template, L0.HasLabel, String.class);
35             result.put(StringUtils.safeString(label), template);
36         }
37         return result;
38
39     }
40
41 }