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