From 373536b7b94967d976d95e504ccb61cfb44df60f Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 23 Jul 2010 07:28:09 +0000 Subject: [PATCH] Input and output handling in module properties git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16799 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/properties/ModuleInputTab.java | 8 +- .../sysdyn/ui/properties/ModuleOutputTab.java | 68 +++++++++- .../ui/properties/VariableInformationTab.java | 2 + ...rt.java => ModuleInputEditingSupport.java} | 8 +- .../widgets/ModuleOutputEditingSupport.java | 126 ++++++++++++++++++ .../ui/properties/widgets/ReferenceRow.java | 34 ++--- .../widgets/ReferenceRowLabelProvider.java | 2 +- 7 files changed, 221 insertions(+), 27 deletions(-) rename org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/{ReferenceRowEditingSupport.java => ModuleInputEditingSupport.java} (92%) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleOutputEditingSupport.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java index 74f4b1dc..08a6c612 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleInputTab.java @@ -17,7 +17,7 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.structural.stubs.StructuralResource2; import org.simantics.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.properties.widgets.ReferenceRowEditingSupport; +import org.simantics.sysdyn.ui.properties.widgets.ModuleInputEditingSupport; import org.simantics.sysdyn.ui.properties.widgets.ReferenceTable; import org.simantics.sysdyn.ui.properties.widgets.ReferenceRow; import org.simantics.sysdyn.ui.properties.widgets.RowProvider; @@ -25,8 +25,8 @@ import org.simantics.sysdyn.ui.properties.widgets.ReferenceRowLabelProvider; public class ModuleInputTab extends PropertyTabContributorImpl { - public static final String FIRSTCOLUMN = "FIRST COLUMN"; - public static final String SECONDCOLUMN = "SECOND COLUMN"; + public static final String FIRSTCOLUMN = "Input in Module"; + public static final String SECONDCOLUMN = "Refers to output"; @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { @@ -41,7 +41,7 @@ public class ModuleInputTab extends PropertyTabContributorImpl { column.getColumn().setResizable(true); column.getColumn().setMoveable(false); // enable editing support - column.setEditingSupport(new ReferenceRowEditingSupport(referenceTable.getTableViewer(), i)); + column.setEditingSupport(new ModuleInputEditingSupport(referenceTable.getTableViewer(), i)); } referenceTable.setContentProvider (new ArrayContentProvider()); referenceTable.setLabelProvider (new ReferenceRowLabelProvider()); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java index af512a54..0cd2b29c 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleOutputTab.java @@ -1,16 +1,78 @@ package org.simantics.sysdyn.ui.properties; +import java.util.ArrayList; + +import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.TableViewerColumn; +import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.ModuleOutputEditingSupport; +import org.simantics.sysdyn.ui.properties.widgets.ReferenceRow; +import org.simantics.sysdyn.ui.properties.widgets.ReferenceRowLabelProvider; +import org.simantics.sysdyn.ui.properties.widgets.ReferenceTable; +import org.simantics.sysdyn.ui.properties.widgets.RowProvider; -public class ModuleOutputTab extends PropertyTabContributorImpl { +public class ModuleOutputTab extends PropertyTabContributorImpl { + public static final String FIRSTCOLUMN = "Output in module"; + public static final String SECONDCOLUMN = "Referes to input"; + @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + ReferenceTable referenceTable = new ReferenceTable(body, support, SWT.NONE); + + String[] titles = { FIRSTCOLUMN, SECONDCOLUMN}; + int[] bounds = { 100, 100, 100, 100 }; + for (int i = 0; i < titles.length; i++) { + TableViewerColumn column = new TableViewerColumn(referenceTable.getTableViewer(), SWT.NONE); + column.getColumn().setText(titles[i]); + column.getColumn().setWidth(bounds[i]); + column.getColumn().setResizable(true); + column.getColumn().setMoveable(false); + // enable editing support + column.setEditingSupport(new ModuleOutputEditingSupport(referenceTable.getTableViewer(), i)); + } + referenceTable.setContentProvider (new ArrayContentProvider()); + referenceTable.setLabelProvider (new ReferenceRowLabelProvider()); + RowProvider rp = new RowProvider() { + + @Override + public ArrayList getRows(ReadGraph graph, Resource module) throws DatabaseException { + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + StructuralResource2 sr2 = StructuralResource2.getInstance(graph); + ArrayList result = new ArrayList(); + Resource instanceOf = graph.getSingleObject(module, b.InstanceOf); + Resource configuration = graph.getSingleObject(instanceOf, sr2.IsDefinedBy); + for(Resource variable : graph.syncRequest(new ObjectsWithType(configuration, b.ConsistsOf, sr.Variable))) { + if(!graph.hasStatement(variable, sr.IsOutput)) continue; + + Resource dependency = null; + for(Resource dep : graph.getObjects(module, sr.IsTailOf)) { + Resource refersTo = graph.getPossibleObject(dep, sr.RefersTo); + if(refersTo != null && refersTo.equals(variable)) { + dependency = dep; + break; + } + } + ReferenceRow rr = new ReferenceRow(module, dependency, variable); + result.add(rr); + } + return result; + } + }; + referenceTable.setRowProvider(rp); } - -} +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java index e179e9d7..8a3e3fa8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/VariableInformationTab.java @@ -16,6 +16,7 @@ import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; import org.simantics.db.Builtins; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier; @@ -71,6 +72,7 @@ public class VariableInformationTab extends PropertyTabContributorImpl { rangeStep.setInputValidator(new DoubleValidator()); GridDataFactory.fillDefaults().grab(true, false).applyTo(rangeStep.getWidget()); + new IsOutputWidget(composite, support, SWT.NULL); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowEditingSupport.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleInputEditingSupport.java similarity index 92% rename from org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowEditingSupport.java rename to org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleInputEditingSupport.java index 3bb2e686..1031a792 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowEditingSupport.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleInputEditingSupport.java @@ -17,14 +17,14 @@ import org.simantics.db.request.Read; import org.simantics.sysdyn.SysdynResource; import org.simantics.ui.SimanticsUI; -public class ReferenceRowEditingSupport extends EditingSupport { +public class ModuleInputEditingSupport extends EditingSupport { private CellEditor editor; private int column; private HashMap optionResources; private String[] options; private TableViewer tableViewer; - public ReferenceRowEditingSupport(TableViewer viewer, int column) { + public ModuleInputEditingSupport(TableViewer viewer, int column) { super(viewer); this.tableViewer = (TableViewer)viewer; this.column = column; @@ -48,7 +48,7 @@ public class ReferenceRowEditingSupport extends EditingSupport { case 1: ReferenceRow row = (ReferenceRow)element; final Resource module = row.getModule(); - final Resource inputVariable = row.getInput(); + final Resource inputVariable = row.getVariable(); optionResources = new HashMap(); try { @@ -94,7 +94,7 @@ public class ReferenceRowEditingSupport extends EditingSupport { case 0: return referenceRow.getName(); case 1: - String refersToName = referenceRow.getOutputName(); + String refersToName = referenceRow.getValue(); if (refersToName == null) return options.length - 1; for(int i = 0; i < options.length ; i++) { if(refersToName.equals(options[i])) return i; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleOutputEditingSupport.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleOutputEditingSupport.java new file mode 100644 index 00000000..50da2dbc --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ModuleOutputEditingSupport.java @@ -0,0 +1,126 @@ +package org.simantics.sysdyn.ui.properties.widgets; + +import java.util.HashMap; + +import org.eclipse.jface.viewers.CellEditor; +import org.eclipse.jface.viewers.ComboBoxCellEditor; +import org.eclipse.jface.viewers.EditingSupport; +import org.eclipse.jface.viewers.TableViewer; +import org.eclipse.jface.viewers.TextCellEditor; +import org.eclipse.swt.SWT; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.ui.SimanticsUI; + +public class ModuleOutputEditingSupport extends EditingSupport { + private CellEditor editor; + private int column; + private HashMap optionResources; + private String[] options; + private TableViewer tableViewer; + + public ModuleOutputEditingSupport(TableViewer viewer, int column) { + super(viewer); + this.tableViewer = (TableViewer)viewer; + this.column = column; + } + + @Override + protected boolean canEdit(Object element) { + switch (this.column) { + case 0: return false; + default: return true; + } + } + + + @Override + protected CellEditor getCellEditor(Object element) { + // Create the correct editor based on the column index + switch (column) { + case 0: + editor = new TextCellEditor(this.tableViewer.getTable()); + case 1: + ReferenceRow row = (ReferenceRow)element; + final Resource module = row.getModule(); + final Resource outputVariable = row.getVariable(); + + optionResources = new HashMap(); + try { + optionResources = SimanticsUI.getSession().syncRequest(new Read>() { + + @Override + public HashMap perform(ReadGraph graph) throws DatabaseException { + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + HashMap result = new HashMap(); + for(Resource dependency : graph.syncRequest(new ObjectsWithType(module, sr.IsTailOf, sr.Dependency))) { + if(graph.getPossibleObject(dependency, sr.RefersTo) == null || + graph.getPossibleObject(dependency, sr.RefersTo).equals(outputVariable)) { + Resource input = graph.getSingleObject(dependency, sr.HasHead); + result.put((String)graph.getRelatedValue(input, b.HasName), dependency); + } + } + return result; + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + options = optionResources.keySet().toArray(new String[optionResources.keySet().size() + 1]); + options[optionResources.keySet().size()] = ""; + ComboBoxCellEditor ceditor = new ComboBoxCellEditor(this.tableViewer.getTable(), options, SWT.READ_ONLY); + ceditor.setActivationStyle(1); + editor = ceditor; + break; + default: + editor = null; + } + + return editor; + } + + @Override + protected Object getValue(Object element) { + ReferenceRow referenceRow = (ReferenceRow) element; + + switch (this.column) { + case 0: + return referenceRow.getName(); + case 1: + String refersToName = referenceRow.getValue(); + if (refersToName == null) return options.length - 1; + for(int i = 0; i < options.length ; i++) { + if(refersToName.equals(options[i])) return i; + } + return options[options.length - 1]; + default: + break; + } + return null; + } + + @Override + protected void setValue(Object element, Object value) { + ReferenceRow referenceRow = (ReferenceRow) element; + String valueString = String.valueOf(value); + switch (this.column) { + case 0: + break; + case 1: + referenceRow.setRefersTo(optionResources.get(options[Integer.parseInt(valueString)])); + break; + default: + break; + } + + getViewer().update(element, null); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRow.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRow.java index 10eba6dd..d3e4f8a5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRow.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRow.java @@ -13,12 +13,12 @@ import org.simantics.ui.SimanticsUI; public class ReferenceRow { - Resource input, module, dependency; + Resource variable, module, dependency; String name; - public ReferenceRow(Resource module, Resource dependency, Resource input) { + public ReferenceRow(Resource module, Resource dependency, Resource variable) { this.module = module; - this.input = input; + this.variable = variable; this.dependency = dependency; } @@ -27,8 +27,8 @@ public class ReferenceRow { return this.module; } - public Resource getInput() { - return this.input; + public Resource getVariable() { + return this.variable; } public Resource getDependency() { @@ -42,7 +42,7 @@ public class ReferenceRow { @Override public String perform(ReadGraph graph) throws DatabaseException { - return (String)graph.getRelatedValue(input, graph.getBuiltins().HasName); + return (String)graph.getRelatedValue(getVariable(), graph.getBuiltins().HasName); } }); } catch (DatabaseException e) { @@ -51,25 +51,29 @@ public class ReferenceRow { return name; } - public String getOutputName() { + public String getValue() { if(dependency == null) return ""; - String outputName = null; + String value = null; try { - outputName = SimanticsUI.getSession().syncRequest(new Read() { + value = SimanticsUI.getSession().syncRequest(new Read() { @Override public String perform(ReadGraph graph) throws DatabaseException { Builtins b = graph.getBuiltins(); SysdynResource sr = SysdynResource.getInstance(graph); - Resource output = graph.getPossibleObject(dependency, sr.HasTail); - return (String)graph.getRelatedValue(output, b.HasName, StringBindingDefault.INSTANCE); + Resource valueResource = graph.getPossibleObject(dependency, sr.HasTail); + if(!graph.isInstanceOf(valueResource, sr.Variable)) + valueResource = graph.getPossibleObject(dependency, sr.HasHead); + if(!graph.isInstanceOf(valueResource, sr.Variable)) + return ""; + return (String)graph.getRelatedValue(valueResource, b.HasName, StringBindingDefault.INSTANCE); } }); } catch (DatabaseException e) { e.printStackTrace(); } - return outputName; + return value; } public void setRefersTo(final Resource dependency) { @@ -80,15 +84,15 @@ public class ReferenceRow { SysdynResource sr = SysdynResource.getInstance(graph); if(getDependency() != null && graph.hasStatement(getDependency(), sr.RefersTo)) graph.deny(getDependency(), sr.RefersTo); - setDependency(null); + setDependency(null); if(dependency != null) { setDependency(dependency); - graph.claim(getDependency(), SysdynResource.getInstance(graph).RefersTo, input); + graph.claim(getDependency(), SysdynResource.getInstance(graph).RefersTo, getVariable()); } } }); } - + private void setDependency(Resource dependency) { this.dependency = dependency; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowLabelProvider.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowLabelProvider.java index a618e427..93668d26 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowLabelProvider.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/ReferenceRowLabelProvider.java @@ -18,7 +18,7 @@ public class ReferenceRowLabelProvider extends LabelProvider implements ITableLa case 0: return referenceRow.getName(); case 1: - return referenceRow.getOutputName(); + return referenceRow.getValue(); default: throw new RuntimeException("Should not happen"); } -- 2.47.1