1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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.sysdyn.ui.trend.chart.properties;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.Collections;
\r
17 import java.util.List;
\r
19 import org.eclipse.jface.fieldassist.SimpleContentProposalProvider;
\r
20 import org.eclipse.swt.widgets.Control;
\r
21 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.common.request.ObjectsWithType;
\r
26 import org.simantics.db.common.utils.NameUtils;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.management.ISessionContext;
\r
29 import org.simantics.db.procedure.Listener;
\r
30 import org.simantics.db.request.Read;
\r
31 import org.simantics.layer0.Layer0;
\r
32 import org.simantics.simulation.ontology.SimulationResource;
\r
33 import org.simantics.structural.stubs.StructuralResource2;
\r
34 import org.simantics.sysdyn.SysdynResource;
\r
35 import org.simantics.ui.SimanticsUI;
\r
36 import org.simantics.ui.utils.AdaptionUtils;
\r
37 import org.simantics.utils.strings.AlphanumComparator;
\r
40 * Provides all variables a model contains
\r
42 * @author Teemu Lempinen
\r
45 public class VariableProposalProvider extends SimpleContentProposalProvider implements Widget {
\r
48 * Provides all variables a model contains. Given resource needs to be
\r
49 * part of a model (i.e. using PartOf leads eventually to a SysdynModel).
\r
51 * @param control Control that is using this provider
\r
52 * @param resource A resource that is part of a model
\r
54 public VariableProposalProvider(final Control control, WidgetSupport support) {
\r
55 super(new String [] {});
\r
56 support.register(this);
\r
57 this.control = control;
\r
60 private Resource resource;
\r
61 private Control control;
\r
64 public void setInput(ISessionContext context, Object input) {
\r
66 final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);
\r
67 if(resource == null)
\r
69 this.resource = resource;
\r
71 SimanticsUI.getSession().asyncRequest(new Read<String[]>() {
\r
74 public String[] perform(ReadGraph graph) throws DatabaseException {
\r
75 Layer0 l0 = Layer0.getInstance(graph);
\r
76 SimulationResource simu = SimulationResource.getInstance(graph);
\r
77 SysdynResource sr = SysdynResource.getInstance(graph);
\r
79 // Find the model of this resource
\r
80 Resource model = resource;
\r
81 while(model != null && !graph.isInstanceOf(model, sr.SysdynModel))
\r
82 model = graph.getPossibleObject(model, l0.PartOf);
\r
85 return new String[0];
\r
87 // Find the models configuration
\r
88 Resource conf = graph.getSingleObject(model, simu.HasConfiguration);
\r
89 List<String> items = new ArrayList<String>();
\r
91 // Recursively read all configurations and add items
\r
92 ReadConfiguration(graph, conf, "", items);
\r
94 // Finally sort the results
\r
95 Collections.sort(items, AlphanumComparator.CASE_INSENSITIVE_COMPARATOR);
\r
96 return items.toArray(new String[items.size()]);
\r
99 }, new Listener<String[]>() {
\r
102 public void execute(String[] result) {
\r
103 setProposals(result);
\r
107 public void exception(Throwable t) {
\r
108 t.printStackTrace();
\r
112 public boolean isDisposed() {
\r
113 return control == null ||
\r
114 control.isDisposed() ||
\r
115 !resource.equals(VariableProposalProvider.this.resource);
\r
123 * Read components in a configuration and recursively all module configurations
\r
125 * @param graph ReadGraph
\r
126 * @param configuration Resource to be read
\r
127 * @param path Current path from base realization
\r
128 * @param items Found variables
\r
129 * @throws DatabaseException
\r
131 private void ReadConfiguration(ReadGraph graph, Resource configuration, String path, Collection<String> items) throws DatabaseException {
\r
132 SysdynResource sr = SysdynResource.getInstance(graph);
\r
133 Layer0 l0 = Layer0.getInstance(graph);
\r
134 StructuralResource2 sr2 = StructuralResource2.getInstance(graph);
\r
137 for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) {
\r
138 name = path + NameUtils.getSafeName(graph, resource);
\r
142 for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Input))) {
\r
143 name = path + NameUtils.getSafeName(graph, resource);
\r
147 for(Resource module : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Module))) {
\r
148 Resource instanceOf = graph.getPossibleObject(module, l0.InstanceOf);
\r
149 Resource conf = graph.getPossibleObject(instanceOf, sr2.IsDefinedBy);
\r
151 String p = path + NameUtils.getSafeName(graph, module) + ".";
\r
152 ReadConfiguration(graph, conf, p, items);
\r