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.databoard.Bindings;\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.request.ReadRequest;\r
import org.simantics.db.common.utils.ListUtils;\r
import org.simantics.db.common.utils.NameUtils;\r
import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
import org.simantics.db.management.ISessionContext;\r
import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;\r
import org.simantics.layer0.Layer0;\r
import org.simantics.sysdyn.ui.properties.widgets.ColumnKeys;\r
import org.simantics.utils.RunnableWithObject;\r
import org.simantics.utils.datastructures.ArrayMap;\r
+import org.simantics.utils.threads.SWTThread;\r
\r
public class ArrayIndexesTab extends LabelPropertyTabContributor implements Widget{\r
\r
GraphExplorerComposite usedEnumerationsExplorer;\r
private WidgetSupportImpl buttonSupport;\r
private org.simantics.browsing.ui.swt.widgets.Label usedEnumerationsLabel;\r
+ private Button down;\r
+ private Button up;\r
+ private Button remove;\r
\r
public ArrayIndexesTab(Object variables) {\r
super(variables);\r
List<Resource> enumerationResources;\r
\r
@Override\r
- public void beforeApply() {\r
+ public void beforeApply() { \r
enumerationResources = getSelectedResources(availableEnumerationsExplorer);\r
}\r
\r
SysdynResource sr = SysdynResource.getInstance(graph);\r
for(Resource r : input.resources) {\r
Resource arrayIndexes = graph.getPossibleObject(r, sr.Variable_arrayIndexesList);\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("Added new enumeration(s) ");\r
if(arrayIndexes == null) {\r
- graph.claim(r, sr.Variable_arrayIndexesList, ListUtils.create(graph, enumerationResources));\r
+ arrayIndexes = ListUtils.create(graph, enumerationResources);\r
+ graph.claim(r, sr.Variable_arrayIndexesList, arrayIndexes);\r
+ sb.append(enumerationResources.toString() + " ");\r
} else {\r
ArrayList<Resource> filtered = new ArrayList<Resource>();\r
for(Resource enumeration : enumerationResources) {\r
- if(ListUtils.getNode(graph, arrayIndexes, enumeration) == null)\r
+ if(ListUtils.getNode(graph, arrayIndexes, enumeration) == null) {\r
filtered.add(enumeration);\r
+ sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+ }\r
}\r
+ if (filtered.isEmpty())\r
+ return;\r
ListUtils.insertBack(graph, arrayIndexes, filtered);\r
}\r
+ sb.append("to " + graph.getPossibleRelatedValue2(r, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
+ Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+ updateUsedButtons(graph, arrayIndexes);\r
}\r
}\r
});\r
Composite buttons = new Composite(body, SWT.None);\r
GridLayoutFactory.fillDefaults().applyTo(buttons);\r
\r
- Button up = new Button(buttons, buttonSupport, SWT.NONE);\r
+ up = new Button(buttons, buttonSupport, SWT.NONE);\r
up.setText("Up");\r
up.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
if(arrayIndexes != null) {\r
List<Resource> enumerations = ListUtils.toList(graph, arrayIndexes);\r
List<Resource> toBeMoved = new ArrayList<Resource>();\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("Moved enumeration(s) ");\r
for(Integer index : selectedIndexes) {\r
if(index < enumerations.size()) {\r
Resource enumeration = enumerations.get(index);\r
}\r
\r
}\r
-\r
+ boolean moved = false;\r
for(Resource enumeration : toBeMoved) {\r
Resource node = ListUtils.getNode(graph, arrayIndexes, enumeration);\r
Resource prev = graph.getSingleObject(node, L0.List_Previous);\r
if(!arrayIndexes.equals(prev) && !toBeMoved.contains(graph.getPossibleObject(prev, L0.List_Element))) {\r
- ListUtils.swapWithPrevious(graph, arrayIndexes, enumeration);\r
+ sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+ moved = ListUtils.swapWithPrevious(graph, arrayIndexes, enumeration);\r
}\r
}\r
+ if (moved) {\r
+ sb.append("up");\r
+ Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+ }\r
+\r
}\r
}\r
\r
}\r
});\r
\r
- Button down = new Button(buttons, buttonSupport, SWT.NONE);\r
+ down = new Button(buttons, buttonSupport, SWT.NONE);\r
down.setText("Down");\r
down.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
if(arrayIndexes != null) {\r
List<Resource> enumerations = ListUtils.toList(graph, arrayIndexes);\r
List<Resource> toBeMoved = new ArrayList<Resource>();\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("Moved enumeration(s) ");\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
+ boolean moved = false;\r
for(Resource enumeration : toBeMoved) {\r
Resource node = ListUtils.getNode(graph, arrayIndexes, enumeration);\r
Resource next = graph.getSingleObject(node, L0.List_Next);\r
+ \r
if(!arrayIndexes.equals(next) && !toBeMoved.contains(graph.getPossibleObject(next, L0.List_Element))) {\r
- ListUtils.swapWithNext(graph, arrayIndexes, enumeration);\r
+ sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+ moved = ListUtils.swapWithNext(graph, arrayIndexes, enumeration);\r
}\r
}\r
+ if (moved) {\r
+ sb.append("down");\r
+ Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+ }\r
}\r
}\r
\r
});\r
\r
\r
- Button remove = new Button(buttons, buttonSupport, SWT.NONE);\r
+ remove = new Button(buttons, buttonSupport, SWT.NONE);\r
remove.setText("Remove");\r
remove.addSelectionListener(new SelectionListenerImpl<ResourceArray>(context) {\r
\r
}\r
\r
}\r
- \r
- for(Resource enumeration : toBeRemoved)\r
+ StringBuilder sb = new StringBuilder();\r
+ sb.append("Removed enumeration(s) ");\r
+ for(Resource enumeration : toBeRemoved) {\r
ListUtils.removeElement(graph, arrayIndexes, enumeration);\r
+ sb.append(graph.getPossibleRelatedValue2(enumeration, Layer0.getInstance(graph).HasName, Bindings.STRING) + " ");\r
+ }\r
+ sb.append("from " + graph.getPossibleRelatedValue2(variable, Layer0.getInstance(graph).HasName, Bindings.STRING));\r
+ Layer0Utils.addCommentMetadata(graph, sb.toString());\r
+ updateUsedButtons(graph, arrayIndexes);\r
}\r
}\r
}\r
}\r
\r
\r
+ protected void updateUsedButtons(ReadGraph graph, Resource arrayIndexes) {\r
+ boolean enabled = false;\r
+ if (arrayIndexes == null)\r
+ enabled = false;\r
+ else {\r
+ try {\r
+ List<Resource> currEnumerations = ListUtils.toList(graph, arrayIndexes);\r
+ if (!currEnumerations.isEmpty()) {\r
+ enabled = true;\r
+ }\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+ final boolean finalEnabled = enabled;\r
+ SWTThread.getThreadAccess().asyncExec(new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ up.getControl().setEnabled(finalEnabled);\r
+ down.getControl().setEnabled(finalEnabled);\r
+ remove.getControl().setEnabled(finalEnabled); \r
+ }\r
+ });\r
+ }\r
+\r
+\r
private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {\r
List<Resource> result = new ArrayList<Resource>();\r
\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
+ final ResourceArray finalResourceArray = resourceArray;\r
if(resourceArray != null) {\r
- StructuredSelection selection = new StructuredSelection(resourceArray);\r
+ StructuredSelection selection = new StructuredSelection(finalResourceArray);\r
availableEnumerationsExplorer.setInput(context, selection);\r
usedEnumerationsExplorer.setInput(context, selection); \r
buttonSupport.fireInput(context, selection);\r
+ \r
+ try {\r
+ context.getSession().syncRequest(new ReadRequest() {\r
+ \r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ Resource r = finalResourceArray.resources[0];\r
+ Resource arrayIndexes = graph.getPossibleObject(r, SysdynResource.getInstance(graph).Variable_arrayIndexesList);\r
+ updateUsedButtons(graph, arrayIndexes);\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
}\r
}\r
- \r
-\r
}\r
}\r