From: lempinen Date: Wed, 21 Nov 2012 09:05:54 +0000 (+0000) Subject: Enumeration definition to multiple variables at the same time (fixes #2921) X-Git-Tag: simantics-1.10.1~111 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=df29dd799042c533a464c6c88e4bf51a506ddc37;p=simantics%2Fsysdyn.git Enumeration definition to multiple variables at the same time (fixes #2921) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@26380 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 5fe8735b..ba2cf754 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -1842,6 +1842,10 @@ class="org.simantics.sysdyn.ui.properties.widgets.arrays.EnumerationLabeler" preference="2.0"> + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java index 004b8c1d..964a1417 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ArrayIndexesTab.java @@ -1,45 +1,61 @@ package org.simantics.sysdyn.ui.properties; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionProvider; import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Tree; +import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.AdaptableHintContext; import org.simantics.browsing.ui.swt.SingleSelectionInputSource; import org.simantics.browsing.ui.swt.widgets.Button; import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.Widget; import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.ResourceArray; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys; +import org.simantics.utils.RunnableWithObject; import org.simantics.utils.datastructures.ArrayMap; public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widget{ GraphExplorerComposite availableEnumerationsExplorer; GraphExplorerComposite usedEnumerationsExplorer; + private WidgetSupportImpl buttonSupport; + private org.simantics.browsing.ui.swt.widgets.Label usedEnumerationsLabel; @Override public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { support.register(this); + buttonSupport = new WidgetSupportImpl(); + GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body); @@ -55,7 +71,7 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg .setBrowseContexts(SysdynResource.URIs.AvailableVariableIndexes); availableEnumerationsExplorer.setColumns(ColumnKeys.ENUMERATION_TABLE_COLUMNS); availableEnumerationsExplorer.setInputSource(new SingleSelectionInputSource( - Resource.class)); + ResourceArray.class)); availableEnumerationsExplorer.finish(); @@ -67,10 +83,10 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg ((Tree) c).setLinesVisible(true); - Button add = new Button(body, support, SWT.NONE); + Button add = new Button(body, buttonSupport, SWT.NONE); add.setText(" -> "); - add.addSelectionListener(new SelectionListenerImpl(context) { + add.addSelectionListener(new SelectionListenerImpl(context) { List enumerationResources; @@ -80,32 +96,56 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg } @Override - public void apply(WriteGraph graph, Resource input) + public void apply(WriteGraph graph, ResourceArray input) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes); - if(arrayIndexes == null) { - arrayIndexes = OrderedSetUtils.create(graph, sr.ArrayIndexes); - graph.claim(input, sr.Variable_arrayIndexes, arrayIndexes); + for(Resource r : input.resources) { + Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexes); + if(arrayIndexes == null) { + arrayIndexes = OrderedSetUtils.create(graph, sr.ArrayIndexes); + graph.claim(r, sr.Variable_arrayIndexes, arrayIndexes); + } + OrderedSetUtils.addAll(graph, arrayIndexes, enumerationResources); } - OrderedSetUtils.addAll(graph, arrayIndexes, enumerationResources); } }); Composite used = new Composite(body, SWT.NONE); GridLayoutFactory.fillDefaults().applyTo(used); GridDataFactory.fillDefaults().grab(true, true).applyTo(used); - label = new Label(used, SWT.None); - label.setText("Used Enumerations"); + usedEnumerationsLabel = new org.simantics.browsing.ui.swt.widgets.Label(used, buttonSupport, SWT.None); + usedEnumerationsLabel.setTextFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, ResourceArray input) throws DatabaseException { + if(input.size() < 2) + return "Used Enumerations"; + else { + StringBuilder sb = new StringBuilder(); + sb.append("Enumerations in: "); + boolean first = true; + for(Resource r : input) { + if(!first) + sb.append(", "); + first = false; + + String name = NameUtils.getSafeName(graph, r); + sb.append(name); + } + return sb.toString(); + } + } + + }); usedEnumerationsExplorer = new GraphExplorerComposite(ArrayMap.keys( - "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER); + "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI); usedEnumerationsExplorer .setBrowseContexts(SysdynResource.URIs.UsedVariableIndexes); usedEnumerationsExplorer.setColumns(ColumnKeys.ENUMERATION_TABLE_COLUMNS); usedEnumerationsExplorer.setInputSource(new SingleSelectionInputSource( - Resource.class)); + ResourceArray.class)); usedEnumerationsExplorer.finish(); @@ -119,90 +159,179 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg Composite buttons = new Composite(body, SWT.None); GridLayoutFactory.fillDefaults().applyTo(buttons); - Button up = new Button(buttons, support, SWT.NONE); + Button up = new Button(buttons, buttonSupport, SWT.NONE); up.setText("Up"); - up.addSelectionListener(new SelectionListenerImpl(context) { + up.addSelectionListener(new SelectionListenerImpl(context) { - List enumerationResources; + List selectedIndexes; @Override public void beforeApply() { - enumerationResources = getSelectedResources(usedEnumerationsExplorer); + selectedIndexes = getSelectedIndexes(usedEnumerationsExplorer); } @Override - public void apply(WriteGraph graph, Resource input) + public void apply(WriteGraph graph, ResourceArray input) throws DatabaseException { - if(enumerationResources != null && enumerationResources.size() == 1) { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes); - if(arrayIndexes == null) - return; - - Resource enumeration = enumerationResources.get(0); - Resource prev = OrderedSetUtils.prev(graph, arrayIndexes, enumeration); - if(prev.equals(arrayIndexes)) - return; - - OrderedSetUtils.remove(graph, arrayIndexes, enumeration); - OrderedSetUtils.addBefore(graph, arrayIndexes, prev, enumeration); - } + + + SysdynResource sr = SysdynResource.getInstance(graph); + + for(Resource variable : input) { + Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes); + if(arrayIndexes != null) { + List enumerations = OrderedSetUtils.toList(graph, arrayIndexes); + List toBeMoved = new ArrayList(); + for(Integer index : selectedIndexes) { + if(index < enumerations.size()) { + Resource enumeration = enumerations.get(index); + toBeMoved.add(enumeration); + } + + } + + for(Resource enumeration : toBeMoved) { + Resource prev = OrderedSetUtils.prev(graph, arrayIndexes, enumeration); + if(!prev.equals(arrayIndexes) && !toBeMoved.contains(prev)) { + OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + OrderedSetUtils.addBefore(graph, arrayIndexes, prev, enumeration); + } + } + } + } + + // Move selection + usedEnumerationsExplorer.getDisplay().asyncExec(new RunnableWithObject(usedEnumerationsExplorer, selectedIndexes) { + @Override + public void run() { + Control c = ((GraphExplorerComposite)object).getExplorerControl(); + if(c != null && c instanceof Tree && object2 instanceof List) { + Tree tree = (Tree) c; + tree.deselectAll(); + + @SuppressWarnings("unchecked") + List list = (List) object2; + Set selection = new HashSet(); + for(Integer i : list) { + TreeItem item = null; + if(i - 1 >= 0) + item = tree.getItem(i - 1); + if(item == null || selection.contains(item)) + item = tree.getItem(i); + if(item != null && !selection.contains(item)) + selection.add(item); + } + tree.setSelection(selection.toArray(new TreeItem[selection.size()])); + } + } + }); } }); - Button down = new Button(buttons, support, SWT.NONE); + Button down = new Button(buttons, buttonSupport, SWT.NONE); down.setText("Down"); - down.addSelectionListener(new SelectionListenerImpl(context) { + down.addSelectionListener(new SelectionListenerImpl(context) { - List enumerationResources; + List selectedIndexes; @Override public void beforeApply() { - enumerationResources = getSelectedResources(usedEnumerationsExplorer); + selectedIndexes = getSelectedIndexes(usedEnumerationsExplorer); } @Override - public void apply(WriteGraph graph, Resource input) + public void apply(WriteGraph graph, ResourceArray input) throws DatabaseException { - if(enumerationResources != null && enumerationResources.size() == 1) { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes); - if(arrayIndexes == null) - return; - Resource enumeration = enumerationResources.get(0); - Resource next = OrderedSetUtils.next(graph, arrayIndexes, enumeration); - if(next.equals(arrayIndexes)) - return; - OrderedSetUtils.remove(graph, arrayIndexes, enumeration); - OrderedSetUtils.addAfter(graph, arrayIndexes, next, enumeration); + + + SysdynResource sr = SysdynResource.getInstance(graph); + + for(Resource variable : input) { + Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes); + if(arrayIndexes != null) { + List enumerations = OrderedSetUtils.toList(graph, arrayIndexes); + List toBeMoved = new ArrayList(); + for(Integer index : selectedIndexes) { + if(index < enumerations.size()) { + Resource enumeration = enumerations.get(index); + toBeMoved.add(0, enumeration); // Make an inverted list. + } + + } + + for(Resource enumeration : toBeMoved) { + Resource next = OrderedSetUtils.next(graph, arrayIndexes, enumeration); + if(!next.equals(arrayIndexes) && !toBeMoved.contains(next)) { + OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + OrderedSetUtils.addAfter(graph, arrayIndexes, next, enumeration); + } + } + } } + + // Move selection + usedEnumerationsExplorer.getDisplay().asyncExec(new RunnableWithObject(usedEnumerationsExplorer, selectedIndexes) { + @Override + public void run() { + Control c = ((GraphExplorerComposite)object).getExplorerControl(); + if(c != null && c instanceof Tree && object2 instanceof List) { + Tree tree = (Tree) c; + tree.deselectAll(); + + @SuppressWarnings("unchecked") + List list = (List) object2; + Set selection = new HashSet(); + Collections.reverse(list); + for(Integer i : list) { + TreeItem item = null; + if(i + 1 < tree.getItemCount()) + item = tree.getItem(i + 1); + if(item == null || selection.contains(item)) + item = tree.getItem(i); + if(item != null && !selection.contains(item)) + selection.add(item); + } + tree.setSelection(selection.toArray(new TreeItem[selection.size()])); + } + } + }); } }); - Button remove = new Button(buttons, support, SWT.NONE); + Button remove = new Button(buttons, buttonSupport, SWT.NONE); remove.setText("Remove"); - remove.addSelectionListener(new SelectionListenerImpl(context) { + remove.addSelectionListener(new SelectionListenerImpl(context) { - List enumerationResources; + List selectedIndexes; @Override public void beforeApply() { - enumerationResources = getSelectedResources(usedEnumerationsExplorer); + selectedIndexes = getSelectedIndexes(usedEnumerationsExplorer); } @Override - public void apply(WriteGraph graph, Resource input) + public void apply(WriteGraph graph, ResourceArray input) throws DatabaseException { - if(enumerationResources != null && enumerationResources.size() == 1) { - SysdynResource sr = SysdynResource.getInstance(graph); - Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes); - if(arrayIndexes == null) - return; - - Resource enumeration = enumerationResources.get(0); - OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + SysdynResource sr = SysdynResource.getInstance(graph); + + for(Resource variable : input) { + Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes); + if(arrayIndexes != null) { + List enumerations = OrderedSetUtils.toList(graph, arrayIndexes); + List toBeRemoved = new ArrayList(); + for(Integer index : selectedIndexes) { + if(index < enumerations.size()) { + Resource enumeration = enumerations.get(index); + toBeRemoved.add(enumeration); + } + + } + + for(Resource enumeration : toBeRemoved) + OrderedSetUtils.remove(graph, arrayIndexes, enumeration); + } } } }); @@ -225,12 +354,50 @@ public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widg } return result; } + + private List getSelectedIndexes(final GraphExplorerComposite explorer) { + final List result = new ArrayList(); + + explorer.getDisplay().syncExec(new Runnable() { + + @Override + public void run() { + Control c = explorer.getExplorerControl(); + if (c instanceof Tree) { + Tree tree = (Tree) c; + TreeItem[] selection = tree.getSelection(); + + for(TreeItem item : selection) { + result.add(tree.indexOf(item)); + } + } + } + }); + + return result; + } @Override public void setInput(ISessionContext context, Object input) { - availableEnumerationsExplorer.setInput(context, input); - usedEnumerationsExplorer.setInput(context, input); + + if(input != null && input instanceof IStructuredSelection) { + Object first = ((IStructuredSelection)input).getFirstElement(); + ResourceArray resourceArray = null; + if(first instanceof Resource) + resourceArray = new ResourceArray((Resource)first); + else if( first instanceof Collection) + resourceArray = new ResourceArray(((Collection)first).toArray(new Resource[((Collection)first).size()])); + + if(resourceArray != null) { + StructuredSelection selection = new StructuredSelection(resourceArray); + availableEnumerationsExplorer.setInput(context, selection); + usedEnumerationsExplorer.setInput(context, selection); + buttonSupport.fireInput(context, selection); + } + } + + } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index ad10dd61..31e0c5bd 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -49,7 +49,7 @@ import org.simantics.sysdyn.ui.trend.chart.properties.xyline.XYLineAxisAndVariab import org.simantics.sysdyn.ui.trend.chart.properties.xyline.XYLineGeneralPropertiesTab; import org.simantics.ui.selection.AnyVariable; import org.simantics.ui.selection.WorkbenchSelectionElement; -import org.simantics.ui.utils.AdaptionUtils; +import org.simantics.utils.ui.AdaptionUtils; import org.simantics.utils.ui.ISelectionUtils; /** @@ -78,22 +78,26 @@ public class ResourceSelectionProcessor implements SelectionProcessor && ((ArrayList) selection).size() > 1) { - List independentVariables = new ArrayList(); + List variables = new ArrayList(); Resource model = null; for(Object o : (ArrayList)selection) { Resource r = AdaptionUtils.adaptToSingle(o, Resource.class); - if(r != null && backend.isInstanceOf(r, sr.IndependentVariable)) { + Resource component = backend.getPossibleObject(r, mr.ElementToComponent); + if (component != null) { + r = component; + } + if(r != null && backend.isInstanceOf(r, sr.Variable)) { if(model == null) model = backend.getSingleObject(r, Layer0.getInstance(backend).PartOf); if(model.equals( backend.getSingleObject(r, Layer0.getInstance(backend).PartOf))) - independentVariables.add(r); + variables.add(r); } } tabs.add(new ComparableTabContributor( new ArrayIndexesTab(), 1, - independentVariables, + variables, "Indexes")); return tabs; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java index 9b2c37e2..ea785c0a 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/AvailableEnumerations.java @@ -2,24 +2,38 @@ package org.simantics.sysdyn.ui.properties.widgets.arrays; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import org.simantics.browsing.ui.common.node.AbstractNode; import org.simantics.browsing.ui.graph.impl.contributor.viewpoint.ViewpointContributorImpl; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.ResourceArray; import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; -public class AvailableEnumerations extends ViewpointContributorImpl { +public class AvailableEnumerations extends ViewpointContributorImpl { @Override - public Collection getContribution(ReadGraph graph, Resource input) + public Collection getContribution(ReadGraph graph, ResourceArray input) throws DatabaseException { - if(input == null) return null; + Resource[] selection = input.resources; + if(selection.length < 1) + return Collections.emptyList(); Layer0 l0 = Layer0.getInstance(graph); - Resource configuration = graph.getPossibleObject(input, l0.PartOf); + Resource configuration = null; + + // Variables need to be from the same configuration + for(Resource r : selection) { + Resource conf = graph.getPossibleObject(r, l0.PartOf); + if(configuration == null || conf.equals(configuration)) + configuration = conf; + else + return Collections.emptyList(); + } + ArrayList> result = new ArrayList>(); SysdynResource sr = SysdynResource.getInstance(graph); if(configuration == null) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ConflictingEnumerationLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ConflictingEnumerationLabeler.java new file mode 100644 index 00000000..4fa7ef9e --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ConflictingEnumerationLabeler.java @@ -0,0 +1,41 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import java.util.HashMap; +import java.util.Map; + +import org.simantics.browsing.ui.graph.impl.contributor.labeler.ColumnLabelerContributorImpl; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys; + +public class ConflictingEnumerationLabeler extends ColumnLabelerContributorImpl{ + + @Override + public Map getLabel(ReadGraph graph, ConflictingEnumerationNode input) + throws DatabaseException { + HashMap map = new HashMap(); + + boolean first = true; + StringBuilder names = new StringBuilder(); + for(Resource r : (Resource[])input.data) { + if(!first) + names.append(", "); + first = false; + + String name = "empty"; + if(r != null) + name = NameUtils.getSafeName(graph, r); + names.append(name); + } + map.put(ColumnKeys.ENUMERATION, names.toString()); + + map.put(ColumnKeys.INDEXES, "Conflicting enumerations"); + + return map; + } + + + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ConflictingEnumerationNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ConflictingEnumerationNode.java new file mode 100644 index 00000000..b93f849c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/ConflictingEnumerationNode.java @@ -0,0 +1,11 @@ +package org.simantics.sysdyn.ui.properties.widgets.arrays; + +import org.simantics.browsing.ui.common.node.AbstractNode; + +public class ConflictingEnumerationNode extends AbstractNode { + + public ConflictingEnumerationNode(Object resources) { + super(resources); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java index 7d67aa43..681b9ef2 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/arrays/UsedEnumerations.java @@ -2,31 +2,109 @@ package org.simantics.sysdyn.ui.properties.widgets.arrays; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.List; import org.simantics.browsing.ui.common.node.AbstractNode; import org.simantics.browsing.ui.graph.impl.contributor.viewpoint.ViewpointContributorImpl; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.ResourceArray; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.SysdynResource; -public class UsedEnumerations extends ViewpointContributorImpl { +public class UsedEnumerations extends ViewpointContributorImpl { @Override - public Collection getContribution(ReadGraph graph, Resource input) + public Collection getContribution(ReadGraph graph, ResourceArray input) throws DatabaseException { SysdynResource sr = SysdynResource.getInstance(graph); - ArrayList> result = new ArrayList>(); + Resource[] selection = input.resources; + if(selection.length < 1) + return Collections.emptyList(); - Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes); - if(arrayIndexes == null) { - return result; + /* Collect enumerations for all variables + * var1 + * - enum1 + * - enum2 + * var2 + * - enum1 + * - enum2 + * - enum3 + */ + ArrayList> variableEnumerations = new ArrayList>(); + for(Resource variable : selection) { + Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes); + if(arrayIndexes != null) { + variableEnumerations.add(OrderedSetUtils.toList(graph, arrayIndexes)); + } else { + variableEnumerations.add(new ArrayList()); + } } - for(Resource r : OrderedSetUtils.toList(graph, arrayIndexes)) { - result.add(new EnumerationNode(r)); + + + /* Construct a list of enumerations + * FirstEnumeration + * - var1.first + * - var2.first + * - var3.first + * SecondEnumeration + * - var1.second + * - ... + */ + ArrayList> enumerations = new ArrayList>(); + + int index = 0; + while(true) { + + // Break loop if enumerations for all variables have been recorded + boolean b = true; + for(List varEnums : variableEnumerations) { + if(index < varEnums.size()) { + b = false; + break; + } + } + + if(b) break; + + for(List current : variableEnumerations) { + if(index == enumerations.size()) + enumerations.add(index, new ArrayList()); + Resource enumeration = index < current.size()? current.get(index) : null; + enumerations.get(index).add(enumeration); + } + + index++; } + + // Build nodes + ArrayList> result = new ArrayList>(); + + if(enumerations.size() == 1 && (enumerations.get(0).size() == 0 || (enumerations.get(0).size() == 1 && enumerations.get(0).get(0) == null))) + return result; + + for(List enumeration : enumerations) { + boolean same = true; + for(int i = 0; i < enumeration.size() - 1; i++) { + if(enumeration.get(i) == null || enumeration.get(i + 1) == null || + !enumeration.get(i).equals(enumeration.get(i + 1))) { + same = false; + break; + } + } + + if(!same) { + result.add(new ConflictingEnumerationNode(enumeration.toArray(new Resource[enumeration.size()]))); + } else { + result.add(new EnumerationNode(enumeration.get(0))); + } + } + + + return result; }