]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
0dd0a49a46e977bb422194b63f2e32cea1c7808f
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.arrays;\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.events.SelectionListener;\r
18 import org.eclipse.swt.widgets.Button;\r
19 import org.eclipse.swt.widgets.Composite;\r
20 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
21 import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
23 import org.simantics.db.ReadGraph;\r
24 import org.simantics.db.Resource;\r
25 import org.simantics.db.WriteGraph;\r
26 import org.simantics.db.common.request.ReadRequest;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.db.management.ISessionContext;\r
29 import org.simantics.sysdyn.SysdynResource;\r
30 import org.simantics.utils.ui.ISelectionUtils;\r
31 \r
32 public class ReplaceableIndexesWidget implements Widget {\r
33         \r
34     Resource variable = null; \r
35     boolean selected = false;\r
36     org.simantics.browsing.ui.swt.widgets.Button isReplaceableButton;\r
37     \r
38     public ReplaceableIndexesWidget(Composite parent, WidgetSupport support, int style) {\r
39         support.register(this);\r
40         isReplaceableButton = new org.simantics.browsing.ui.swt.widgets.Button(parent, support, style |= SWT.CHECK);\r
41         isReplaceableButton.setText("Can be replaced by parent module");\r
42     }\r
43 \r
44     @Override\r
45     public void setInput(ISessionContext context, Object input) {\r
46         if(input instanceof ISelection) {\r
47             ISelection selection = (ISelection)input;\r
48             if(selection instanceof IStructuredSelection) {\r
49                 Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);\r
50                 if(resource != null) {\r
51                     variable = resource;\r
52                 }\r
53             }\r
54         }\r
55         \r
56         if(variable == null) return;\r
57         \r
58         try {\r
59             context.getSession().syncRequest(new ReadRequest() {\r
60                 \r
61                 @Override\r
62                 public void run(ReadGraph graph) throws DatabaseException {\r
63                     SysdynResource sr = SysdynResource.getInstance(graph);\r
64                     final Boolean replaceable = graph.getPossibleRelatedValue(variable, sr.Enumeration_isReplaceable);\r
65                     if(replaceable != null)\r
66                         selected = replaceable;\r
67                     final Button button = getWidget();\r
68                     button.getDisplay().asyncExec(new Runnable() {\r
69                         \r
70                         @Override\r
71                         public void run() {\r
72                             if(button.isDisposed()) return;\r
73                             \r
74                             if(replaceable)\r
75                                 button.setSelection(true);\r
76                             else\r
77                                 button.setSelection(false);\r
78                         }\r
79                     });\r
80 \r
81                         \r
82                 }\r
83             });\r
84         } catch (DatabaseException e) {\r
85             e.printStackTrace();\r
86         }\r
87         \r
88         isReplaceableButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
89 \r
90             @Override\r
91             public void apply(WriteGraph graph, Resource input) throws DatabaseException {\r
92                 SysdynResource sr =  SysdynResource.getInstance(graph);\r
93                 Boolean replaceable = graph.getPossibleRelatedValue(input, sr.Enumeration_isReplaceable);\r
94                 if(Boolean.TRUE.equals(replaceable)) {\r
95                     graph.claimLiteral(input, sr.Enumeration_isReplaceable, false);\r
96                     selected = false;\r
97                 } else {\r
98                         graph.claimLiteral(input, sr.Enumeration_isReplaceable, true);\r
99                         selected = true;\r
100                 }\r
101             }\r
102         });\r
103     }\r
104     \r
105     public Button getWidget() {\r
106         return isReplaceableButton.getWidget();\r
107     }\r
108     \r
109     public boolean getSelection() {\r
110         return selected;\r
111     }\r
112     \r
113     public void addSelectionListener(SelectionListener listener) {\r
114         isReplaceableButton.addSelectionListener(listener);\r
115     }\r
116 \r
117 }\r