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