1 /*******************************************************************************
\r
2 * Copyright (c) 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.template2d.ui.diagram.dialogs;
\r
14 import java.util.Collection;
\r
15 import java.util.HashMap;
\r
16 import java.util.Map;
\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
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
56 public BindToIOTableDialog(Shell parentShell) {
\r
58 // TODO Auto-generated constructor stub
\r
61 public boolean init(ExecutionEvent event){
\r
62 ISelection selection = HandlerUtil.getCurrentSelection(event);
\r
63 flag = ResourceAdaptionUtils.toSingleResource(selection);
\r
67 session = SimanticsUI.getSession();
\r
68 if (session == null)
\r
71 session.syncRequest(new ReadRequest(){
\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
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
84 if (parent != null && g.isInstanceOf(parent, DIA.Diagram)){
\r
86 // diagram = g.getPossibleObject(parent, DIA.RuntimeDiagram_HasConfiguration);
\r
87 // if(diagram != null)
\r
88 template = g.getPossibleObject(diagram, TEMPLATE2D.HasDrawingTemplate);
\r
90 if(template == null)
\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
97 String name = g.getPossibleRelatedValue2(child, L0.HasName, Bindings.STRING);
\r
99 entries.put(name, child);
\r
102 currentAlignment = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableBinding, Bindings.STRING);
\r
103 currentIndex = g.getPossibleRelatedValue(flag, DIA.Flag_HasIOTableRowIndex, Bindings.INTEGER);
\r
105 } catch (DatabaseException e) {
\r
106 e.printStackTrace();
\r
113 protected Control createDialogArea(Composite parent) {
\r
114 Composite c = (Composite) super.createDialogArea(parent);
\r
116 GridLayoutFactory.fillDefaults().margins(8, 8).numColumns(2).applyTo(c);
\r
117 GridDataFactory gd1 = GridDataFactory.fillDefaults().span(1, 1);
\r
120 Label label = new Label(c, 0);
\r
121 label.setText("IO table:");
\r
122 gd1.applyTo(label);
\r
124 combo = new Combo(c, 0);
\r
126 int selectedIndex = -1;
\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
134 if (selectedIndex != -1)
\r
135 combo.select(selectedIndex);
\r
137 combo.clearSelection();
\r
138 gd1.applyTo(combo);
\r
141 label = new Label(c, 0);
\r
142 label.setText("Row index:");
\r
143 gd1.applyTo(label);
\r
145 indexTxT = new Text(c, SWT.BORDER);
\r
146 indexTxT.setText("" + currentIndex);
\r
147 gd1.applyTo(indexTxT);
\r
152 protected void okPressed() {
\r
156 currentIndex = Integer.parseInt(indexTxT.getText());
\r
157 } catch (NumberFormatException e){}
\r
158 final Integer index = currentIndex;
\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
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
178 session.syncRequest(new WriteRequest(){
\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
187 // session.syncRequest(new WriteRequest(){
\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
193 } catch (DatabaseException e) {
\r
194 ShowError.showError("Error", "Unexpected database error.", e);
\r
197 catch (NumberFormatException e) {
\r
198 ShowError.showError("Error", "Index has to be a number.", e);
\r
205 protected int getShellStyle() {
\r
206 return SWT.RESIZE | SWT.TITLE | SWT.CLOSE | SWT.BORDER;
\r
210 protected void configureShell(Shell newShell) {
\r
211 super.configureShell(newShell);
\r
212 newShell.setText("Select IO Table Location");
\r