]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
4e8374e70a185168ac9e3c93455fcc1c2110fe0e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.properties.widgets;\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.HashSet;\r
18 import java.util.concurrent.CopyOnWriteArrayList;\r
19 \r
20 import org.eclipse.jface.layout.GridDataFactory;\r
21 import org.eclipse.jface.layout.GridLayoutFactory;\r
22 import org.eclipse.jface.viewers.IStructuredSelection;\r
23 import org.eclipse.swt.SWT;\r
24 import org.eclipse.swt.events.FocusListener;\r
25 import org.eclipse.swt.events.MouseListener;\r
26 import org.eclipse.swt.widgets.Composite;\r
27 import org.eclipse.swt.widgets.TabFolder;\r
28 import org.eclipse.swt.widgets.TabItem;\r
29 import org.eclipse.swt.widgets.Table;\r
30 import org.eclipse.swt.widgets.TableItem;\r
31 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
32 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
33 import org.simantics.db.AsyncReadGraph;\r
34 import org.simantics.db.ReadGraph;\r
35 import org.simantics.db.Resource;\r
36 import org.simantics.db.common.request.ObjectsWithType;\r
37 import org.simantics.db.common.utils.NameUtils;\r
38 import org.simantics.db.exception.DatabaseException;\r
39 import org.simantics.db.management.ISessionContext;\r
40 import org.simantics.db.procedure.AsyncListener;\r
41 import org.simantics.db.request.Read;\r
42 import org.simantics.layer0.Layer0;\r
43 import org.simantics.sysdyn.SysdynResource;\r
44 import org.simantics.ui.SimanticsUI;\r
45 import org.simantics.utils.ui.ISelectionUtils;\r
46 \r
47 public class ShortcutTabWidget implements Widget {\r
48 \r
49     TabFolder tabFolder;\r
50     TabItem variables;\r
51     TabItem functions;\r
52     Table variableTable;\r
53     Table functionTable;\r
54     Composite composite;\r
55     TableItem item2;\r
56     \r
57         CopyOnWriteArrayList<Runnable> dependencyListeners =\r
58                 new CopyOnWriteArrayList<Runnable>();\r
59 \r
60     public ShortcutTabWidget(Composite parent, WidgetSupport support, int style) {\r
61         if(support!=null)\r
62                 support.register(this);\r
63 \r
64         composite = new Composite(parent, style);\r
65         GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);\r
66         \r
67         tabFolder = new TabFolder (composite, SWT.NONE);\r
68         GridDataFactory.fillDefaults().grab(false, true).applyTo(tabFolder);\r
69         GridLayoutFactory.fillDefaults().applyTo(tabFolder);\r
70         variables = new TabItem(tabFolder, SWT.NULL);\r
71         variables.setText("Variables");\r
72         variableTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
73 \r
74         variables.setControl(variableTable);\r
75 \r
76         functions = new TabItem(tabFolder, SWT.NULL);\r
77         functions.setText("Functions"); \r
78 \r
79         functionTable = new Table (tabFolder, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);\r
80         TableItem item = new TableItem(functionTable, SWT.NONE);\r
81         \r
82         //Finding functions\r
83         ArrayList<String> funktio = new ArrayList<String>();\r
84                 try {\r
85                          funktio = SimanticsUI.getSession().syncRequest(new Read<ArrayList<String>>() {\r
86                                 @Override\r
87                                 public ArrayList<String> perform(ReadGraph graph)\r
88                                                 throws DatabaseException {\r
89                                         SysdynResource sr = SysdynResource.getInstance(graph);\r
90                                         Layer0 l0 = Layer0.getInstance(graph);\r
91                                         ArrayList<String> funktiot = new ArrayList<String>();\r
92                                         \r
93                                         Resource funktionlibrary = graph.getPossibleResource(SysdynResource.URIs.Built$in_Functions);\r
94                                         for(Resource r : graph.syncRequest(new ObjectsWithType(funktionlibrary, l0.ConsistsOf, sr.SysdynModelicaFunction))) {\r
95                                                 String name = NameUtils.getSafeName(graph, r);\r
96                                                 funktiot.add(name);\r
97                                         }\r
98                                         \r
99                                         Resource subfunktionlibrary = graph.getPossibleResource(SysdynResource.URIs.Built$in_Functions_Vensim_Functions);\r
100                                         for(Resource r : graph.syncRequest(new ObjectsWithType(subfunktionlibrary, l0.ConsistsOf, sr.SysdynModelicaFunction))) {\r
101                                                 String name = NameUtils.getSafeName(graph, r);\r
102                                                 funktiot.add(name);\r
103                                         }\r
104                                         \r
105                                         return funktiot;\r
106                                 }\r
107                         });\r
108                 }\r
109                 catch (DatabaseException e) {\r
110                         e.printStackTrace();\r
111                 }\r
112         \r
113                 Collections.sort(funktio);\r
114                 \r
115                 for(int a=0; a < funktio.size(); a++){\r
116                         item.setText(funktio.get(a)+"()");\r
117                         item.setData(funktio.get(a)+"({})");\r
118                         item = new TableItem(functionTable, SWT.NONE);\r
119                 }\r
120                 \r
121 //              TableItem item = new TableItem(functionTable, SWT.NONE);\r
122 //              item.setText("min()");\r
123 //              item.setData("min({ })");\r
124 //              item = new TableItem(functionTable, SWT.NONE);\r
125 //              item.setText("max()");\r
126 //              item.setData("max({ })");\r
127 //              item = new TableItem(functionTable, SWT.NONE);\r
128 //              item.setText("abs()");\r
129 //              item.setData("abs({ })");\r
130 //              item = new TableItem(functionTable, SWT.NONE);\r
131 //              item.setText("if then else");\r
132 //              item.setData("if then else");\r
133 //              item = new TableItem(functionTable, SWT.NONE);\r
134 //              item.setText("xidz()");\r
135 //              item.setData("xidz( number, divisor , x)");\r
136 //              item = new TableItem(functionTable, SWT.NONE);\r
137 //              item.setText("interpolate()");\r
138 //              item.setData("interpolate( x, table)");     \r
139 //              item = new TableItem(functionTable, SWT.NONE);\r
140 //              item.setText("delay()");\r
141 //              item.setData("delay( expression, delayTime)");\r
142         functions.setControl(functionTable);\r
143     }\r
144 \r
145     public Composite getWidget() {\r
146         return composite;\r
147     }\r
148     \r
149     @Override\r
150     public void setInput(ISessionContext context, Object input) {\r
151         if(input instanceof IStructuredSelection) {\r
152             final Resource variable = ISelectionUtils.filterSingleSelection((IStructuredSelection)input, Resource.class);\r
153             if(variable != null) {\r
154                 \r
155                 SimanticsUI.getSession().asyncRequest(new Read<HashSet<String>>() {\r
156 \r
157                                         @Override\r
158                                         public HashSet<String> perform(ReadGraph graph)\r
159                                                         throws DatabaseException {\r
160                                                 return getDependencies(graph, variable);\r
161                                         }\r
162                                 }, new AsyncListener<HashSet<String>>() {\r
163 \r
164                                         @Override\r
165                                         public void execute(AsyncReadGraph graph,\r
166                                                         HashSet<String> result) {\r
167                                                 \r
168                                                 final HashSet<String> dependencies = result;\r
169                                                 variableTable.getDisplay().asyncExec(new Runnable() {\r
170                                                         \r
171                                                         @Override\r
172                                                         public void run() {\r
173                                                                 if(variableTable.isDisposed()) return;\r
174                                                                 \r
175                                                                 TableItem[] items = variableTable.getItems();\r
176                                                                 \r
177                                                                 // Remove deleted dependencies and create the list of current dependencies (itemStrings)\r
178                                                                 ArrayList<String> itemStrings = new ArrayList<String>();\r
179                                                 for(TableItem i : items) {\r
180                                                         String text = i.getText();\r
181                                                         if(dependencies.contains(text))\r
182                                                                 itemStrings.add(text);\r
183                                                         else\r
184                                                                 variableTable.remove(variableTable.indexOf(i));\r
185                                                 }\r
186                                                 \r
187                                                 // Add all new dependencies\r
188                                                 TableItem item;\r
189                                                 for(String d : dependencies) {\r
190                                                         if(!itemStrings.contains(d)) {\r
191                                                                 item = new TableItem(variableTable, SWT.NONE);\r
192                                                                 item.setText(d);\r
193                                                                 item.setData(d);\r
194                                                         }\r
195                                                 }\r
196                                                 \r
197                                                                 synchronized(dependencyListeners) {\r
198                                                                         for(Runnable listener : dependencyListeners)\r
199                                                                                 listener.run();\r
200                                                                 }                                                               \r
201                                                         }\r
202                                                 });\r
203                                         }\r
204 \r
205                                         @Override\r
206                                         public void exception(AsyncReadGraph graph,\r
207                                                         Throwable throwable) {\r
208                                                 throwable.printStackTrace();\r
209                                         }\r
210 \r
211                                         @Override\r
212                                         public boolean isDisposed() {\r
213                                                 return variableTable.isDisposed();\r
214                                         }\r
215                                 });\r
216             }\r
217         }\r
218     }\r
219 \r
220     // Returns the names of the related variables (dependencies)\r
221     private HashSet<String> getDependencies(ReadGraph graph, Resource r) throws DatabaseException {\r
222         HashSet<String> variables = new HashSet<String>();\r
223         if(graph != null && r != null) {\r
224                 SysdynResource sr = SysdynResource.getInstance(graph);\r
225                 Layer0 l0 = Layer0.getInstance(graph);\r
226 \r
227                 Collection<Resource> dependencies = graph.getObjects(r, sr.IsHeadOf);\r
228 \r
229                 for(Resource d : dependencies) {\r
230                         if(graph.isInstanceOf(d, sr.Dependency)) {\r
231                                 Resource tail = graph.getPossibleObject(d, sr.HasTail);\r
232                                 if(tail != null) {\r
233                                         Object name = graph.getPossibleRelatedValue(tail, l0.HasName);\r
234                                         if(name != null)\r
235                                                 variables.add((String)name);\r
236                                 }\r
237                         }\r
238                 }\r
239         }\r
240         return variables;\r
241     }\r
242     \r
243     public void addFocusListener(FocusListener listener) {\r
244         this.functionTable.addFocusListener(listener);\r
245         this.variableTable.addFocusListener(listener);\r
246     }\r
247     \r
248     public void addMouseListener(MouseListener listener) {\r
249         this.functionTable.addMouseListener(listener);\r
250         this.variableTable.addMouseListener(listener);\r
251     }\r
252     \r
253         public void addDependencyListener(Runnable listener) {\r
254                 synchronized(dependencyListeners) {\r
255                         dependencyListeners.add(listener);\r
256                 }\r
257         }\r
258 \r
259         public void removeDependencyListener(Runnable listener) {\r
260                 synchronized(dependencyListeners) {\r
261                         dependencyListeners.remove(listener);\r
262                 }\r
263         }\r
264     \r
265     public Table getVariableTable() {\r
266         return variableTable;\r
267     }\r
268 \r
269 \r
270 }\r