1 package org.simantics.sysdyn.ui.properties.widgets;
\r
3 import java.util.HashMap;
\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.ReadGraph;
\r
12 import org.simantics.db.Resource;
\r
13 import org.simantics.db.common.request.ObjectsWithType;
\r
14 import org.simantics.db.exception.DatabaseException;
\r
15 import org.simantics.db.request.Read;
\r
16 import org.simantics.layer0.Layer0;
\r
17 import org.simantics.sysdyn.SysdynResource;
\r
18 import org.simantics.ui.SimanticsUI;
\r
20 public class ModuleOutputEditingSupport extends EditingSupport {
\r
21 private CellEditor editor;
\r
23 private HashMap<String, Resource> optionResources;
\r
24 private String[] options;
\r
25 private TableViewer tableViewer;
\r
27 public ModuleOutputEditingSupport(TableViewer viewer, int column) {
\r
29 this.tableViewer = (TableViewer)viewer;
\r
30 this.column = column;
\r
34 protected boolean canEdit(Object element) {
\r
35 switch (this.column) {
\r
36 case 0: return false;
\r
37 default: return true;
\r
43 protected CellEditor getCellEditor(Object element) {
\r
44 // Create the correct editor based on the column index
\r
47 editor = new TextCellEditor(this.tableViewer.getTable());
\r
49 ReferenceRow row = (ReferenceRow)element;
\r
50 final Resource module = row.getModule();
\r
51 final Resource outputVariable = row.getVariable();
\r
53 optionResources = new HashMap<String, Resource>();
\r
55 optionResources = SimanticsUI.getSession().syncRequest(new Read<HashMap<String, Resource>>() {
\r
58 public HashMap<String, Resource> perform(ReadGraph graph) throws DatabaseException {
\r
59 Layer0 l0 = Layer0.getInstance(graph);
\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.IsTailOf, sr.Dependency))) {
\r
63 if(graph.getPossibleObject(dependency, sr.RefersTo) == null ||
\r
64 graph.getPossibleObject(dependency, sr.RefersTo).equals(outputVariable)) {
\r
65 Resource input = graph.getSingleObject(dependency, sr.HasHead);
\r
66 result.put((String)graph.getRelatedValue(input, l0.HasName), dependency);
\r
72 } catch (DatabaseException e) {
\r
73 e.printStackTrace();
\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
90 protected Object getValue(Object element) {
\r
91 ReferenceRow referenceRow = (ReferenceRow) element;
\r
93 switch (this.column) {
\r
95 return referenceRow.getName();
\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
102 return options[options.length - 1];
\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
117 referenceRow.setRefersTo(optionResources.get(options[Integer.parseInt(valueString)]));
\r
123 getViewer().update(element, null);
\r