]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/diagram/dialogs/BindToIOTableDialog.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / diagram / dialogs / BindToIOTableDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.template2d.ui.diagram.dialogs;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.HashMap;\r
16 import java.util.Map;\r
17 \r
18 import org.eclipse.core.commands.ExecutionEvent;\r
19 import org.eclipse.jface.dialogs.Dialog;\r
20 import org.eclipse.jface.layout.GridDataFactory;\r
21 import org.eclipse.jface.layout.GridLayoutFactory;\r
22 import org.eclipse.jface.viewers.ISelection;\r
23 import org.eclipse.swt.SWT;\r
24 import org.eclipse.swt.widgets.Combo;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Control;\r
27 import org.eclipse.swt.widgets.Label;\r
28 import org.eclipse.swt.widgets.Shell;\r
29 import org.eclipse.swt.widgets.Text;\r
30 import org.eclipse.ui.handlers.HandlerUtil;\r
31 import org.simantics.databoard.Bindings;\r
32 import org.simantics.db.ReadGraph;\r
33 import org.simantics.db.Resource;\r
34 import org.simantics.db.Session;\r
35 import org.simantics.db.WriteGraph;\r
36 import org.simantics.db.common.request.ReadRequest;\r
37 import org.simantics.db.common.request.WriteRequest;\r
38 import org.simantics.db.common.utils.OrderedSetUtils;\r
39 import org.simantics.db.exception.DatabaseException;\r
40 import org.simantics.diagram.stubs.DiagramResource;\r
41 import org.simantics.layer0.Layer0;\r
42 import org.simantics.modeling.template2d.ontology.Template2dResource;\r
43 import org.simantics.ui.SimanticsUI;\r
44 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
45 import org.simantics.utils.ui.dialogs.ShowError;\r
46 \r
47 public class BindToIOTableDialog extends Dialog {\r
48         private Map<String,Resource> entries = new HashMap<String,Resource>();\r
49         private Session session = null;\r
50         private Combo combo = null;\r
51         private Resource flag = null;\r
52         private String currentAlignment = null;\r
53         private int currentIndex = -1;\r
54         private Text indexTxT = null;\r
55 \r
56         public BindToIOTableDialog(Shell parentShell) {\r
57                 super(parentShell);\r
58                 // TODO Auto-generated constructor stub\r
59         }\r
60         \r
61         public boolean init(ExecutionEvent event){\r
62                 ISelection selection = HandlerUtil.getCurrentSelection(event);\r
63         flag = ResourceAdaptionUtils.toSingleResource(selection);\r
64         if (flag == null)\r
65             return false;\r
66 \r
67         session = SimanticsUI.getSession();\r
68         if (session == null)\r
69                 return false;\r
70         try {\r
71                         session.syncRequest(new ReadRequest(){\r
72                                 @Override\r
73                                 public void run(ReadGraph g) throws DatabaseException {\r
74                                 entries.put("", null);\r
75                                         DiagramResource DIA = DiagramResource.getInstance(g);\r
76                                         Layer0 L0 = Layer0.getInstance(g);\r
77                     Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);\r
78 \r
79                                         Resource parent = OrderedSetUtils.getSingleOwnerList(g, flag, DiagramResource.getInstance(g).Composite);\r
80                                         //Resource parent = g.getPossibleObject(flag, L0.PartOf);\r
81                                         Resource diagram = null;\r
82                                         Resource template = null;\r
83                                         \r
84                                         if (parent != null && g.isInstanceOf(parent, DIA.Diagram)){\r
85                                                 diagram = parent;\r
86 //                                      diagram = g.getPossibleObject(parent, DIA.RuntimeDiagram_HasConfiguration);\r
87 //                                      if(diagram != null)\r
88                                                 template = g.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);\r
89                                         }\r
90                                 if(template == null)\r
91                                         return;\r
92                                 \r
93                                 Collection<Resource> children = g.getObjects(template, L0.ConsistsOf);\r
94                                 for (Resource child:children){\r
95                                         if (!g.isInstanceOf(child, TEMPLATE2D.FlagTable))\r
96                                                 continue;\r
97                                         String name = g.getPossibleRelatedValue2(child, L0.HasName, Bindings.STRING);\r
98                                         if (name != null)\r
99                                                 entries.put(name, child);\r
100                                 }\r
101 \r
102                                 currentAlignment = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);\r
103                                 currentIndex = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);\r
104                                 }});\r
105                 } catch (DatabaseException e) {\r
106                         e.printStackTrace();\r
107                         return false;\r
108                 }\r
109                 return true;\r
110         }\r
111 \r
112         @Override\r
113         protected Control createDialogArea(Composite parent) {\r
114                 Composite c = (Composite) super.createDialogArea(parent);\r
115                 \r
116         GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c);\r
117         GridDataFactory gd1 = GridDataFactory.fillDefaults().span(1, 1);\r
118 \r
119         // 1st line\r
120         Label label = new Label(c, 0);\r
121         label.setText("IO table:");\r
122         gd1.applyTo(label);\r
123         \r
124         combo = new Combo(c, 0);\r
125         int i = 0;\r
126         int selectedIndex = -1;\r
127         \r
128         for (Map.Entry<String,Resource> entry:entries.entrySet()){\r
129                 combo.add(entry.getKey());\r
130                 if (currentAlignment != null && entry.getKey().equals(currentAlignment))\r
131                         selectedIndex = i;\r
132                 i++;    \r
133         }\r
134         if (selectedIndex != -1)\r
135                 combo.select(selectedIndex);\r
136         else\r
137                 combo.clearSelection();\r
138         gd1.applyTo(combo);\r
139         \r
140         // 2nd line\r
141         label = new Label(c, 0);\r
142         label.setText("Row index:");\r
143         gd1.applyTo(label);\r
144 \r
145         indexTxT = new Text(c, SWT.BORDER);\r
146         indexTxT.setText("" + currentIndex);\r
147         gd1.applyTo(indexTxT);\r
148         \r
149                 return c;\r
150         }\r
151         @Override\r
152         protected void okPressed() {\r
153         try {\r
154                 currentIndex = -1;\r
155                 try {\r
156                         currentIndex = Integer.parseInt(indexTxT.getText());\r
157                 } catch (NumberFormatException e){}\r
158                 final Integer index = currentIndex;\r
159                 \r
160                 int selectedAlignmentIndex = combo.getSelectionIndex();\r
161                 String selectedAligmentTxt = null;\r
162                 if (selectedAlignmentIndex != -1)\r
163                         selectedAligmentTxt = combo.getItem(selectedAlignmentIndex);\r
164                 //String oldAlignment = currentAlignment; \r
165                 if (selectedAligmentTxt != null)\r
166                         currentAlignment = selectedAligmentTxt;//entries.get(selectedAligmentTxt);\r
167                 if (currentAlignment != null){\r
168                         if (currentAlignment.length() > 0){\r
169                                 session.syncRequest(new WriteRequest(){\r
170                                         @Override\r
171                                         public void perform(WriteGraph g) throws DatabaseException {\r
172                                                 DiagramResource DIA = DiagramResource.getInstance(g);\r
173                                                 g.claimLiteral(flag, DIA.Flag_HasIOTableBinding, currentAlignment, Bindings.STRING);\r
174                                         g.claimLiteral(flag, DIA.Flag_HasIOTableRowIndex, index, Bindings.INTEGER);\r
175 \r
176                                         }});\r
177                         } else {\r
178                                 session.syncRequest(new WriteRequest(){\r
179                                         @Override\r
180                                         public void perform(WriteGraph g) throws DatabaseException {\r
181                                                 DiagramResource DIA = DiagramResource.getInstance(g);\r
182                                                 g.claimLiteral(flag, DIA.Flag_HasIOTableBinding, "", Bindings.STRING);\r
183                                         g.claimLiteral(flag, DIA.Flag_HasIOTableRowIndex, -1, Bindings.INTEGER);\r
184                                         }});\r
185                         }\r
186                 }\r
187 //                      session.syncRequest(new WriteRequest(){\r
188 //                              @Override\r
189 //                              public void perform(WriteGraph g) throws DatabaseException {\r
190 //                                      DiagramResource DIA = DiagramResource.getInstance(g);\r
191 //                              g.claimLiteral(flag, DIA.Flag_HasIOTableRowIndex, index, Bindings.INTEGER);\r
192 //                              }});\r
193         } catch (DatabaseException e) {\r
194                 ShowError.showError("Error", "Unexpected database error.", e);\r
195                 return;\r
196         }\r
197         catch (NumberFormatException e) {\r
198                 ShowError.showError("Error", "Index has to be a number.", e);\r
199                 return;\r
200         }\r
201         super.okPressed();\r
202         }\r
203 \r
204         @Override\r
205     protected int getShellStyle() {\r
206         return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER;\r
207     }\r
208 \r
209     @Override\r
210     protected void configureShell(Shell newShell) {\r
211         super.configureShell(newShell);\r
212         newShell.setText("Select IO Table Location");\r
213     }\r
214 }\r