package org.simantics.sysdyn.ui.properties;\r
\r
import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
import java.util.List;\r
+import java.util.Set;\r
\r
import org.eclipse.jface.layout.GridDataFactory;\r
import org.eclipse.jface.layout.GridLayoutFactory;\r
import org.eclipse.jface.viewers.ISelection;\r
import org.eclipse.jface.viewers.ISelectionProvider;\r
import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.viewers.StructuredSelection;\r
import org.eclipse.swt.SWT;\r
import org.eclipse.swt.widgets.Composite;\r
import org.eclipse.swt.widgets.Control;\r
import org.eclipse.swt.widgets.Label;\r
import org.eclipse.swt.widgets.Tree;\r
+import org.eclipse.swt.widgets.TreeItem;\r
import org.eclipse.ui.IWorkbenchSite;\r
import org.simantics.browsing.ui.swt.AdaptableHintContext;\r
import org.simantics.browsing.ui.swt.SingleSelectionInputSource;\r
import org.simantics.browsing.ui.swt.widgets.Button;\r
import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
+import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
+import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupportImpl;\r
+import org.simantics.db.ReadGraph;\r
import org.simantics.db.Resource;\r
import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.common.utils.NameUtils;\r
import org.simantics.db.common.utils.OrderedSetUtils;\r
import org.simantics.db.exception.DatabaseException;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.sysdyn.SysdynResource;\r
import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
+import org.simantics.utils.RunnableWithObject;\r
import org.simantics.utils.datastructures.ArrayMap;\r
\r
public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widget{\r
\r
GraphExplorerComposite availableEnumerationsExplorer;\r
GraphExplorerComposite usedEnumerationsExplorer;\r
+ private WidgetSupportImpl buttonSupport;\r
+ private org.simantics.browsing.ui.swt.widgets.Label usedEnumerationsLabel;\r
\r
@Override\r
public void createControls(Composite body, IWorkbenchSite site,\r
ISessionContext context, WidgetSupport support) {\r
support.register(this);\r
\r
+ buttonSupport = new WidgetSupportImpl();\r
+ \r
GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body);\r
\r
\r
.setBrowseContexts(SysdynResource.URIs.AvailableVariableIndexes);\r
availableEnumerationsExplorer.setColumns(ColumnKeys.ENUMERATION_TABLE_COLUMNS);\r
availableEnumerationsExplorer.setInputSource(new SingleSelectionInputSource(\r
- Resource.class));\r
+ ResourceArray.class));\r
\r
availableEnumerationsExplorer.finish();\r
\r
((Tree) c).setLinesVisible(true);\r
\r
\r
- Button add = new Button(body, support, SWT.NONE);\r
+ Button add = new Button(body, buttonSupport, SWT.NONE);\r
add.setText(" -> ");\r
\r
- add.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
+ add.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
List<Resource> enumerationResources;\r
\r
}\r
\r
@Override\r
- public void apply(WriteGraph graph, Resource input)\r
+ public void apply(WriteGraph graph, ResourceArray input)\r
throws DatabaseException {\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes);\r
- if(arrayIndexes == null) {\r
- arrayIndexes = OrderedSetUtils.create(graph, sr.ArrayIndexes);\r
- graph.claim(input, sr.Variable_arrayIndexes, arrayIndexes);\r
+ for(Resource r : input.resources) {\r
+ Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexes);\r
+ if(arrayIndexes == null) {\r
+ arrayIndexes = OrderedSetUtils.create(graph, sr.ArrayIndexes);\r
+ graph.claim(r, sr.Variable_arrayIndexes, arrayIndexes);\r
+ }\r
+ OrderedSetUtils.addAll(graph, arrayIndexes, enumerationResources);\r
}\r
- OrderedSetUtils.addAll(graph, arrayIndexes, enumerationResources);\r
}\r
});\r
\r
Composite used = new Composite(body, SWT.NONE);\r
GridLayoutFactory.fillDefaults().applyTo(used);\r
GridDataFactory.fillDefaults().grab(true, true).applyTo(used);\r
- label = new Label(used, SWT.None);\r
- label.setText("Used Enumerations");\r
+ usedEnumerationsLabel = new org.simantics.browsing.ui.swt.widgets.Label(used, buttonSupport, SWT.None);\r
+ usedEnumerationsLabel.setTextFactory(new ReadFactoryImpl<ResourceArray, String>() {\r
+\r
+ @Override\r
+ public String perform(ReadGraph graph, ResourceArray input) throws DatabaseException {\r
+ if(input.size() < 2)\r
+ return "Used Enumerations";\r
+ else {\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("Enumerations in: ");\r
+ boolean first = true;\r
+ for(Resource r : input) {\r
+ if(!first)\r
+ sb.append(", ");\r
+ first = false;\r
+ \r
+ String name = NameUtils.getSafeName(graph, r);\r
+ sb.append(name);\r
+ }\r
+ return sb.toString();\r
+ }\r
+ }\r
+\r
+ });\r
\r
usedEnumerationsExplorer = new GraphExplorerComposite(ArrayMap.keys(\r
- "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER);\r
+ "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI);\r
\r
usedEnumerationsExplorer\r
.setBrowseContexts(SysdynResource.URIs.UsedVariableIndexes);\r
usedEnumerationsExplorer.setColumns(ColumnKeys.ENUMERATION_TABLE_COLUMNS);\r
usedEnumerationsExplorer.setInputSource(new SingleSelectionInputSource(\r
- Resource.class));\r
+ ResourceArray.class));\r
\r
usedEnumerationsExplorer.finish();\r
\r
Composite buttons = new Composite(body, SWT.None);\r
GridLayoutFactory.fillDefaults().applyTo(buttons);\r
\r
- Button up = new Button(buttons, support, SWT.NONE);\r
+ Button up = new Button(buttons, buttonSupport, SWT.NONE);\r
up.setText("Up");\r
- up.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
+ up.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
- List<Resource> enumerationResources;\r
+ List<Integer> selectedIndexes;\r
\r
@Override\r
public void beforeApply() {\r
- enumerationResources = getSelectedResources(usedEnumerationsExplorer);\r
+ selectedIndexes = getSelectedIndexes(usedEnumerationsExplorer);\r
}\r
\r
@Override\r
- public void apply(WriteGraph graph, Resource input)\r
+ public void apply(WriteGraph graph, ResourceArray input)\r
throws DatabaseException {\r
- if(enumerationResources != null && enumerationResources.size() == 1) {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes);\r
- if(arrayIndexes == null)\r
- return;\r
-\r
- Resource enumeration = enumerationResources.get(0);\r
- Resource prev = OrderedSetUtils.prev(graph, arrayIndexes, enumeration);\r
- if(prev.equals(arrayIndexes))\r
- return;\r
-\r
- OrderedSetUtils.remove(graph, arrayIndexes, enumeration);\r
- OrderedSetUtils.addBefore(graph, arrayIndexes, prev, enumeration);\r
- }\r
+ \r
+ \r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ for(Resource variable : input) {\r
+ Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes);\r
+ if(arrayIndexes != null) {\r
+ List<Resource> enumerations = OrderedSetUtils.toList(graph, arrayIndexes);\r
+ List<Resource> toBeMoved = new ArrayList<Resource>();\r
+ for(Integer index : selectedIndexes) {\r
+ if(index < enumerations.size()) {\r
+ Resource enumeration = enumerations.get(index);\r
+ toBeMoved.add(enumeration);\r
+ }\r
+\r
+ }\r
+\r
+ for(Resource enumeration : toBeMoved) {\r
+ Resource prev = OrderedSetUtils.prev(graph, arrayIndexes, enumeration);\r
+ if(!prev.equals(arrayIndexes) && !toBeMoved.contains(prev)) {\r
+ OrderedSetUtils.remove(graph, arrayIndexes, enumeration);\r
+ OrderedSetUtils.addBefore(graph, arrayIndexes, prev, enumeration);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ \r
+ // Move selection\r
+ usedEnumerationsExplorer.getDisplay().asyncExec(new RunnableWithObject(usedEnumerationsExplorer, selectedIndexes) {\r
+ @Override\r
+ public void run() {\r
+ Control c = ((GraphExplorerComposite)object).getExplorerControl();\r
+ if(c != null && c instanceof Tree && object2 instanceof List<?>) {\r
+ Tree tree = (Tree) c;\r
+ tree.deselectAll();\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ List<Integer> list = (List<Integer>) object2;\r
+ Set<TreeItem> selection = new HashSet<TreeItem>();\r
+ for(Integer i : list) {\r
+ TreeItem item = null;\r
+ if(i - 1 >= 0)\r
+ item = tree.getItem(i - 1);\r
+ if(item == null || selection.contains(item))\r
+ item = tree.getItem(i);\r
+ if(item != null && !selection.contains(item))\r
+ selection.add(item);\r
+ }\r
+ tree.setSelection(selection.toArray(new TreeItem[selection.size()]));\r
+ }\r
+ }\r
+ });\r
}\r
});\r
\r
- Button down = new Button(buttons, support, SWT.NONE);\r
+ Button down = new Button(buttons, buttonSupport, SWT.NONE);\r
down.setText("Down");\r
- down.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
+ down.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
- List<Resource> enumerationResources;\r
+ List<Integer> selectedIndexes;\r
\r
@Override\r
public void beforeApply() {\r
- enumerationResources = getSelectedResources(usedEnumerationsExplorer);\r
+ selectedIndexes = getSelectedIndexes(usedEnumerationsExplorer);\r
}\r
\r
\r
@Override\r
- public void apply(WriteGraph graph, Resource input)\r
+ public void apply(WriteGraph graph, ResourceArray input)\r
throws DatabaseException {\r
- if(enumerationResources != null && enumerationResources.size() == 1) {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes);\r
- if(arrayIndexes == null)\r
- return;\r
- Resource enumeration = enumerationResources.get(0);\r
- Resource next = OrderedSetUtils.next(graph, arrayIndexes, enumeration);\r
- if(next.equals(arrayIndexes))\r
- return;\r
- OrderedSetUtils.remove(graph, arrayIndexes, enumeration);\r
- OrderedSetUtils.addAfter(graph, arrayIndexes, next, enumeration);\r
+\r
+\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ for(Resource variable : input) {\r
+ Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes);\r
+ if(arrayIndexes != null) {\r
+ List<Resource> enumerations = OrderedSetUtils.toList(graph, arrayIndexes);\r
+ List<Resource> toBeMoved = new ArrayList<Resource>();\r
+ for(Integer index : selectedIndexes) {\r
+ if(index < enumerations.size()) {\r
+ Resource enumeration = enumerations.get(index);\r
+ toBeMoved.add(0, enumeration); // Make an inverted list. \r
+ }\r
+\r
+ }\r
+\r
+ for(Resource enumeration : toBeMoved) {\r
+ Resource next = OrderedSetUtils.next(graph, arrayIndexes, enumeration);\r
+ if(!next.equals(arrayIndexes) && !toBeMoved.contains(next)) {\r
+ OrderedSetUtils.remove(graph, arrayIndexes, enumeration);\r
+ OrderedSetUtils.addAfter(graph, arrayIndexes, next, enumeration);\r
+ }\r
+ }\r
+ }\r
}\r
+ \r
+ // Move selection\r
+ usedEnumerationsExplorer.getDisplay().asyncExec(new RunnableWithObject(usedEnumerationsExplorer, selectedIndexes) {\r
+ @Override\r
+ public void run() {\r
+ Control c = ((GraphExplorerComposite)object).getExplorerControl();\r
+ if(c != null && c instanceof Tree && object2 instanceof List<?>) {\r
+ Tree tree = (Tree) c;\r
+ tree.deselectAll();\r
+ \r
+ @SuppressWarnings("unchecked")\r
+ List<Integer> list = (List<Integer>) object2;\r
+ Set<TreeItem> selection = new HashSet<TreeItem>();\r
+ Collections.reverse(list);\r
+ for(Integer i : list) {\r
+ TreeItem item = null;\r
+ if(i + 1 < tree.getItemCount())\r
+ item = tree.getItem(i + 1);\r
+ if(item == null || selection.contains(item))\r
+ item = tree.getItem(i);\r
+ if(item != null && !selection.contains(item))\r
+ selection.add(item);\r
+ }\r
+ tree.setSelection(selection.toArray(new TreeItem[selection.size()]));\r
+ }\r
+ }\r
+ });\r
}\r
});\r
\r
\r
- Button remove = new Button(buttons, support, SWT.NONE);\r
+ Button remove = new Button(buttons, buttonSupport, SWT.NONE);\r
remove.setText("Remove");\r
- remove.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
+ remove.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
- List<Resource> enumerationResources;\r
+ List<Integer> selectedIndexes;\r
\r
@Override\r
public void beforeApply() {\r
- enumerationResources = getSelectedResources(usedEnumerationsExplorer);\r
+ selectedIndexes = getSelectedIndexes(usedEnumerationsExplorer);\r
}\r
\r
@Override\r
- public void apply(WriteGraph graph, Resource input)\r
+ public void apply(WriteGraph graph, ResourceArray input)\r
throws DatabaseException {\r
- if(enumerationResources != null && enumerationResources.size() == 1) {\r
- SysdynResource sr = SysdynResource.getInstance(graph);\r
- Resource arrayIndexes = graph.getPossibleObject(input, sr.Variable_arrayIndexes);\r
- if(arrayIndexes == null)\r
- return;\r
-\r
- Resource enumeration = enumerationResources.get(0);\r
- OrderedSetUtils.remove(graph, arrayIndexes, enumeration);\r
+ SysdynResource sr = SysdynResource.getInstance(graph);\r
+\r
+ for(Resource variable : input) {\r
+ Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexes);\r
+ if(arrayIndexes != null) {\r
+ List<Resource> enumerations = OrderedSetUtils.toList(graph, arrayIndexes);\r
+ List<Resource> toBeRemoved = new ArrayList<Resource>();\r
+ for(Integer index : selectedIndexes) {\r
+ if(index < enumerations.size()) {\r
+ Resource enumeration = enumerations.get(index);\r
+ toBeRemoved.add(enumeration);\r
+ }\r
+\r
+ }\r
+ \r
+ for(Resource enumeration : toBeRemoved)\r
+ OrderedSetUtils.remove(graph, arrayIndexes, enumeration);\r
+ }\r
}\r
}\r
});\r
}\r
return result;\r
}\r
+ \r
+ private List<Integer> getSelectedIndexes(final GraphExplorerComposite explorer) {\r
+ final List<Integer> result = new ArrayList<Integer>();\r
+\r
+ explorer.getDisplay().syncExec(new Runnable() {\r
+\r
+ @Override\r
+ public void run() {\r
+ Control c = explorer.getExplorerControl();\r
+ if (c instanceof Tree) {\r
+ Tree tree = (Tree) c;\r
+ TreeItem[] selection = tree.getSelection();\r
+ \r
+ for(TreeItem item : selection) {\r
+ result.add(tree.indexOf(item));\r
+ }\r
+ }\r
+ }\r
+ });\r
+\r
+ return result;\r
+ }\r
\r
\r
@Override\r
public void setInput(ISessionContext context, Object input) {\r
- availableEnumerationsExplorer.setInput(context, input);\r
- usedEnumerationsExplorer.setInput(context, input);\r
+ \r
+ if(input != null && input instanceof IStructuredSelection) {\r
+ Object first = ((IStructuredSelection)input).getFirstElement();\r
+ ResourceArray resourceArray = null;\r
+ if(first instanceof Resource)\r
+ resourceArray = new ResourceArray((Resource)first);\r
+ else if( first instanceof Collection<?>)\r
+ resourceArray = new ResourceArray(((Collection<?>)first).toArray(new Resource[((Collection<?>)first).size()]));\r
+ \r
+ if(resourceArray != null) {\r
+ StructuredSelection selection = new StructuredSelection(resourceArray);\r
+ availableEnumerationsExplorer.setInput(context, selection);\r
+ usedEnumerationsExplorer.setInput(context, selection); \r
+ buttonSupport.fireInput(context, selection);\r
+ }\r
+ }\r
+ \r
+\r
}\r
\r
}\r