]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
5297182de0526ce2a9452f5d4241e066dd4f76dc
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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.sysdyn.ui.properties.widgets;\r
13 \r
14 import org.eclipse.jface.viewers.ISelection;\r
15 import org.eclipse.jface.viewers.IStructuredSelection;\r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.widgets.Button;\r
18 import org.eclipse.swt.widgets.Composite;\r
19 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
20 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
21 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
22 import org.simantics.db.ReadGraph;\r
23 import org.simantics.db.Resource;\r
24 import org.simantics.db.WriteGraph;\r
25 import org.simantics.db.common.request.ReadRequest;\r
26 import org.simantics.db.exception.DatabaseException;\r
27 import org.simantics.db.management.ISessionContext;\r
28 import org.simantics.sysdyn.SysdynResource;\r
29 import org.simantics.utils.ui.ISelectionUtils;\r
30 \r
31 public class IsOutputWidget implements Widget{\r
32     \r
33     Resource variable = null; \r
34     org.simantics.browsing.ui.swt.widgets.Button isOutputButton;\r
35     \r
36     public IsOutputWidget(Composite parent, WidgetSupport support, int style) {\r
37         support.register(this);\r
38         isOutputButton = new org.simantics.browsing.ui.swt.widgets.Button(parent, support, style |= SWT.CHECK);\r
39         isOutputButton.setText("Is Output");\r
40     }\r
41 \r
42     @Override\r
43     public void setInput(ISessionContext context, Object input) {\r
44         if(input instanceof ISelection) {\r
45             ISelection selection = (ISelection)input;\r
46             if(selection instanceof IStructuredSelection) {\r
47                 Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);\r
48                 if(resource != null) {\r
49                     variable = resource;\r
50                 }\r
51             }\r
52         }\r
53         \r
54         if(variable == null) return;\r
55         \r
56         try {\r
57             context.getSession().syncRequest(new ReadRequest() {\r
58                 \r
59                 @Override\r
60                 public void run(ReadGraph graph) throws DatabaseException {\r
61                     SysdynResource sr = SysdynResource.getInstance(graph);\r
62                     final boolean isOutput = graph.hasStatement(variable, sr.IsOutput);\r
63                     final Button button = getWidget();\r
64                     button.getDisplay().asyncExec(new Runnable() {\r
65                         \r
66                         @Override\r
67                         public void run() {\r
68                             if(button.isDisposed()) return;\r
69                             \r
70                             if(isOutput)\r
71                                 button.setSelection(true);\r
72                             else\r
73                                 button.setSelection(false);\r
74                         }\r
75                     });\r
76 \r
77                         \r
78                 }\r
79             });\r
80         } catch (DatabaseException e) {\r
81             e.printStackTrace();\r
82         }\r
83         \r
84         isOutputButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
85 \r
86             @Override\r
87             public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
88                 SysdynResource sr =  SysdynResource.getInstance(graph);\r
89                 if(graph.hasStatement(input, sr.IsOutput)) {\r
90                     graph.deny(input, sr.IsOutput);\r
91                 } else {\r
92                     graph.claim(input, SysdynResource.getInstance(graph).IsOutput, null, input);\r
93                 }\r
94             }\r
95         });\r
96     }\r
97     \r
98     public Button getWidget() {\r
99         return isOutputButton.getWidget();\r
100     }\r
101 \r
102 }\r