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