--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2012 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.modeling.template2d.ui.diagram.dialogs;\r
+\r
+import java.util.Collection;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.eclipse.core.commands.ExecutionEvent;\r
+import org.eclipse.jface.dialogs.Dialog;\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.swt.SWT;\r
+import org.eclipse.swt.widgets.Combo;\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.Shell;\r
+import org.eclipse.swt.widgets.Text;\r
+import org.eclipse.ui.handlers.HandlerUtil;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.OrderedSetUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.modeling.template2d.ontology.Template2dResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.ui.dialogs.ShowError;\r
+\r
+public class BindToIOTableDialog extends Dialog {\r
+ private Map<String,Resource> entries = new HashMap<String,Resource>();\r
+ private Session session = null;\r
+ private Combo combo = null;\r
+ private Resource flag = null;\r
+ private String currentAlignment = null;\r
+ private int currentIndex = -1;\r
+ private Text indexTxT = null;\r
+\r
+ public BindToIOTableDialog(Shell parentShell) {\r
+ super(parentShell);\r
+ // TODO Auto-generated constructor stub\r
+ }\r
+ \r
+ public boolean init(ExecutionEvent event){\r
+ ISelection selection = HandlerUtil.getCurrentSelection(event);\r
+ flag = ResourceAdaptionUtils.toSingleResource(selection);\r
+ if (flag == null)\r
+ return false;\r
+\r
+ session = SimanticsUI.getSession();\r
+ if (session == null)\r
+ return false;\r
+ try {\r
+ session.syncRequest(new ReadRequest(){\r
+ @Override\r
+ public void run(ReadGraph g) throws DatabaseException {\r
+ entries.put("", null);\r
+ DiagramResource DIA = DiagramResource.getInstance(g);\r
+ Layer0 L0 = Layer0.getInstance(g);\r
+ Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);\r
+\r
+ Resource parent = OrderedSetUtils.getSingleOwnerList(g, flag, DiagramResource.getInstance(g).Composite);\r
+ //Resource parent = g.getPossibleObject(flag, L0.PartOf);\r
+ Resource diagram = null;\r
+ Resource template = null;\r
+ \r
+ if (parent != null && g.isInstanceOf(parent, DIA.Diagram)){\r
+ diagram = parent;\r
+// diagram = g.getPossibleObject(parent, DIA.RuntimeDiagram_HasConfiguration);\r
+// if(diagram != null)\r
+ template = g.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);\r
+ }\r
+ if(template == null)\r
+ return;\r
+ \r
+ Collection<Resource> children = g.getObjects(template, L0.ConsistsOf);\r
+ for (Resource child:children){\r
+ if (!g.isInstanceOf(child, TEMPLATE2D.FlagTable))\r
+ continue;\r
+ String name = g.getPossibleRelatedValue2(child, L0.HasName, Bindings.STRING);\r
+ if (name != null)\r
+ entries.put(name, child);\r
+ }\r
+\r
+ currentAlignment = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);\r
+ currentIndex = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);\r
+ }});\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ return false;\r
+ }\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ protected Control createDialogArea(Composite parent) {\r
+ Composite c = (Composite) super.createDialogArea(parent);\r
+ \r
+ GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c);\r
+ GridDataFactory gd1 = GridDataFactory.fillDefaults().span(1, 1);\r
+\r
+ // 1st line\r
+ Label label = new Label(c, 0);\r
+ label.setText("IO table:");\r
+ gd1.applyTo(label);\r
+ \r
+ combo = new Combo(c, 0);\r
+ int i = 0;\r
+ int selectedIndex = -1;\r
+ \r
+ for (Map.Entry<String,Resource> entry:entries.entrySet()){\r
+ combo.add(entry.getKey());\r
+ if (currentAlignment != null && entry.getKey().equals(currentAlignment))\r
+ selectedIndex = i;\r
+ i++; \r
+ }\r
+ if (selectedIndex != -1)\r
+ combo.select(selectedIndex);\r
+ else\r
+ combo.clearSelection();\r
+ gd1.applyTo(combo);\r
+ \r
+ // 2nd line\r
+ label = new Label(c, 0);\r
+ label.setText("Row index:");\r
+ gd1.applyTo(label);\r
+\r
+ indexTxT = new Text(c, SWT.BORDER);\r
+ indexTxT.setText("" + currentIndex);\r
+ gd1.applyTo(indexTxT);\r
+ \r
+ return c;\r
+ }\r
+ @Override\r
+ protected void okPressed() {\r
+ try {\r
+ currentIndex = -1;\r
+ try {\r
+ currentIndex = Integer.parseInt(indexTxT.getText());\r
+ } catch (NumberFormatException e){}\r
+ final Integer index = currentIndex;\r
+ \r
+ int selectedAlignmentIndex = combo.getSelectionIndex();\r
+ String selectedAligmentTxt = null;\r
+ if (selectedAlignmentIndex != -1)\r
+ selectedAligmentTxt = combo.getItem(selectedAlignmentIndex);\r
+ //String oldAlignment = currentAlignment; \r
+ if (selectedAligmentTxt != null)\r
+ currentAlignment = selectedAligmentTxt;//entries.get(selectedAligmentTxt);\r
+ if (currentAlignment != null){\r
+ if (currentAlignment.length() > 0){\r
+ session.syncRequest(new WriteRequest(){\r
+ @Override\r
+ public void perform(WriteGraph g) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(g);\r
+ g.claimLiteral(flag, DIA.Flag_HasIOTableBinding, currentAlignment, Bindings.STRING);\r
+ g.claimLiteral(flag, DIA.Flag_HasIOTableRowIndex, index, Bindings.INTEGER);\r
+\r
+ }});\r
+ } else {\r
+ session.syncRequest(new WriteRequest(){\r
+ @Override\r
+ public void perform(WriteGraph g) throws DatabaseException {\r
+ DiagramResource DIA = DiagramResource.getInstance(g);\r
+ g.claimLiteral(flag, DIA.Flag_HasIOTableBinding, "", Bindings.STRING);\r
+ g.claimLiteral(flag, DIA.Flag_HasIOTableRowIndex, -1, Bindings.INTEGER);\r
+ }});\r
+ }\r
+ }\r
+// session.syncRequest(new WriteRequest(){\r
+// @Override\r
+// public void perform(WriteGraph g) throws DatabaseException {\r
+// DiagramResource DIA = DiagramResource.getInstance(g);\r
+// g.claimLiteral(flag, DIA.Flag_HasIOTableRowIndex, index, Bindings.INTEGER);\r
+// }});\r
+ } catch (DatabaseException e) {\r
+ ShowError.showError("Error", "Unexpected database error.", e);\r
+ return;\r
+ }\r
+ catch (NumberFormatException e) {\r
+ ShowError.showError("Error", "Index has to be a number.", e);\r
+ return;\r
+ }\r
+ super.okPressed();\r
+ }\r
+\r
+ @Override\r
+ protected int getShellStyle() {\r
+ return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER;\r
+ }\r
+\r
+ @Override\r
+ protected void configureShell(Shell newShell) {\r
+ super.configureShell(newShell);\r
+ newShell.setText("Select IO Table Location");\r
+ }\r
+}\r