]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
f4382101d9beee3c21b2a50ad59c58640614d0aa
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\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
13 \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
18 \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
38 \r
39 /**\r
40  * Provides all variables a model contains\r
41  * \r
42  * @author Teemu Lempinen\r
43  *\r
44  */\r
45 public class VariableProposalProvider extends SimpleContentProposalProvider implements Widget {\r
46 \r
47     /**\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
50      *  \r
51      * @param control Control that is using this provider\r
52      * @param resource A resource that is part of a model\r
53      */\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
58     }\r
59 \r
60     private Resource resource;\r
61     private Control control;\r
62     \r
63     @Override\r
64     public void setInput(ISessionContext context, Object input) {\r
65 \r
66         final Resource resource = AdaptionUtils.adaptToSingle(input, Resource.class);\r
67         if(resource == null)\r
68             return;\r
69         this.resource = resource;\r
70         \r
71         SimanticsUI.getSession().asyncRequest(new Read<String[]>() {\r
72 \r
73             @Override\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
78                 \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
83                 \r
84                 if(model == null)\r
85                     return new String[0];\r
86                 \r
87                 // Find the models configuration\r
88                 Resource conf = graph.getSingleObject(model, simu.HasConfiguration);\r
89                 List<String> items = new ArrayList<String>();\r
90                 \r
91                 // Recursively read all configurations and add items\r
92                 ReadConfiguration(graph, conf, "", items);\r
93                 \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
97             }\r
98 \r
99         }, new Listener<String[]>() {\r
100 \r
101             @Override\r
102             public void execute(String[] result) {\r
103                 setProposals(result);\r
104             }\r
105 \r
106             @Override\r
107             public void exception(Throwable t) {\r
108                 t.printStackTrace();\r
109             }\r
110 \r
111             @Override\r
112             public boolean isDisposed() {\r
113                 return control == null || \r
114                         control.isDisposed() || \r
115                         !resource.equals(VariableProposalProvider.this.resource);\r
116             }\r
117 \r
118         }); \r
119         \r
120     }\r
121     \r
122     /**\r
123      * Read components in a configuration and recursively all module configurations\r
124      * \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
130      */\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
135 \r
136         String name;\r
137         for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.IndependentVariable))) {\r
138             name = path + NameUtils.getSafeName(graph, resource);\r
139             items.add(name);\r
140         }\r
141         \r
142         for(Resource resource : graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, sr.Input))) {\r
143             name = path + NameUtils.getSafeName(graph, resource);\r
144             items.add(name);\r
145         }\r
146 \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
150             if(conf != null) {\r
151                 String p = path + NameUtils.getSafeName(graph, module)  + ".";\r
152                 ReadConfiguration(graph, conf, p, items);\r
153             }\r
154         }\r
155     }\r
156 \r
157 }\r