]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/chart/property/AvailableTemplateFactory.java
Contextual adapters must throw instead of returning null
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / chart / property / AvailableTemplateFactory.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * 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.modeling.ui.chart.property;
13
14 import java.util.Collections;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;
19 import org.simantics.charts.ontology.ChartResource;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.adapter.Instances;
24 import org.simantics.db.layer0.request.PossibleModel;
25 import org.simantics.layer0.Layer0;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class AvailableTemplateFactory extends ReadFactoryImpl<Resource, Map<String, Object>> {
31
32     @Override
33     public Map<String, Object> perform(ReadGraph graph, Resource input) throws DatabaseException {
34         Layer0 L0 = Layer0.getInstance(graph);
35         ChartResource CHART = ChartResource.getInstance(graph);
36         Instances query = graph.adapt(CHART.ChartTemplate, Instances.class);
37         Resource model = graph.syncRequest(new PossibleModel(input));
38         if (model == null)
39             return Collections.emptyMap();
40         Map<String, Object> result = new TreeMap<String, Object>();
41         for (Resource template : query.find(graph, model)) {
42             if (!graph.isInstanceOf(template, CHART.ChartTemplate))
43                 continue;
44             String label = graph.getPossibleRelatedAdapter(template, L0.HasLabel, String.class);
45             if (label != null)
46                 result.put(label, template);
47         }
48         return result;
49     }
50
51 }