]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
42a751a185af9e43c56ab65ff2806882bd9c2e02
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 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.modules;\r
13 \r
14 import java.util.HashMap;\r
15 \r
16 import org.eclipse.jface.viewers.CellEditor;\r
17 import org.eclipse.jface.viewers.ComboBoxCellEditor;\r
18 import org.eclipse.jface.viewers.EditingSupport;\r
19 import org.eclipse.jface.viewers.TableViewer;\r
20 import org.eclipse.jface.viewers.TextCellEditor;\r
21 import org.eclipse.swt.SWT;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.common.request.ObjectsWithType;\r
25 import org.simantics.db.exception.DatabaseException;\r
26 import org.simantics.db.request.Read;\r
27 import org.simantics.layer0.Layer0;\r
28 import org.simantics.sysdyn.SysdynResource;\r
29 import org.simantics.ui.SimanticsUI;\r
30 \r
31 public class ModuleInputEditingSupport extends EditingSupport {\r
32     private CellEditor editor;\r
33     private int column;\r
34     private HashMap<String, Resource> optionResources;\r
35     private String[] options;\r
36     private TableViewer tableViewer;\r
37 \r
38     public ModuleInputEditingSupport(TableViewer viewer, int column) {\r
39         super(viewer);\r
40         this.tableViewer = (TableViewer)viewer;\r
41         this.column = column;\r
42     }\r
43 \r
44     @Override\r
45     protected boolean canEdit(Object element) {\r
46         switch (this.column) {\r
47             case 0: return false;\r
48             default: return true;\r
49         }\r
50     }\r
51 \r
52     \r
53     @Override\r
54     protected CellEditor getCellEditor(Object element) {\r
55         // Create the correct editor based on the column index\r
56         switch (column) {\r
57             case 0: \r
58                 editor = new TextCellEditor(this.tableViewer.getTable());\r
59             case 1:\r
60                 ReferenceRow row = (ReferenceRow)element;\r
61                 final Resource module = row.getModule();\r
62                 final Resource inputVariable = row.getVariable();\r
63 \r
64                 optionResources = new HashMap<String, Resource>();\r
65                 try {\r
66                     optionResources = SimanticsUI.getSession().syncRequest(new Read<HashMap<String, Resource>>() {\r
67 \r
68                         @Override\r
69                         public HashMap<String, Resource> perform(ReadGraph graph) throws DatabaseException {\r
70                                 Layer0 l0 = Layer0.getInstance(graph);\r
71                             SysdynResource sr = SysdynResource.getInstance(graph);\r
72                             HashMap<String, Resource> result = new HashMap<String, Resource>();\r
73                             for(Resource dependency : graph.syncRequest(new ObjectsWithType(module, sr.Variable_isHeadOf, sr.Dependency))) {\r
74                                 if(graph.getPossibleObject(dependency, sr.Dependency_refersTo) == null ||\r
75                                         graph.getPossibleObject(dependency, sr.Dependency_refersTo).equals(inputVariable)) {\r
76                                     Resource output = graph.getSingleObject(dependency, sr.Variable_HasTail);\r
77                                     \r
78                                     if(graph.isInstanceOf(output, sr.Shadow)) {\r
79                                         output = graph.getPossibleObject(output, sr.Shadow_original);\r
80                                     }\r
81                                     \r
82                                     if(output != null)\r
83                                         result.put((String)graph.getRelatedValue(output, l0.HasName), dependency);\r
84                                 }\r
85                             }\r
86                             return result;\r
87                         }\r
88                     });\r
89                 } catch (DatabaseException e) {\r
90                     e.printStackTrace();\r
91                 }\r
92 \r
93                 options = optionResources.keySet().toArray(new String[optionResources.keySet().size() + 1]);\r
94                 options[optionResources.keySet().size()] = "";\r
95                 ComboBoxCellEditor ceditor = new ComboBoxCellEditor(this.tableViewer.getTable(), options, SWT.READ_ONLY);\r
96                 ceditor.setActivationStyle(1);\r
97                 editor = ceditor;\r
98                 break;\r
99             default:\r
100                 editor = null;\r
101         }\r
102 \r
103         return editor;\r
104     }\r
105 \r
106     @Override\r
107     protected Object getValue(Object element) {\r
108         ReferenceRow referenceRow = (ReferenceRow) element;\r
109 \r
110         switch (this.column) {\r
111             case 0:\r
112                 return referenceRow.getName();\r
113             case 1:\r
114                 String refersToName = referenceRow.getValue();\r
115                 if (refersToName == null) return options.length - 1;\r
116                 for(int i = 0; i < options.length ; i++) {\r
117                     if(refersToName.equals(options[i])) return i;\r
118                 }\r
119                 return options[options.length - 1];\r
120             default:\r
121                 break;\r
122         }\r
123         return null;\r
124     }\r
125 \r
126     @Override\r
127     protected void setValue(Object element, Object value) {\r
128         ReferenceRow referenceRow = (ReferenceRow) element;\r
129         String valueString = String.valueOf(value);\r
130         switch (this.column) {\r
131             case 0:\r
132                 break;\r
133             case 1:\r
134                 referenceRow.setRefersTo(optionResources.get(options[Integer.parseInt(valueString)]));\r
135                 break;\r
136             default:\r
137                 break;\r
138         }\r
139 \r
140         getViewer().update(element, null);\r
141     }\r
142 \r
143 }\r