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