]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
dbdf9514b952b6d07d6f8994cd7d1b33d56574d1
[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;\r
13 \r
14 import org.eclipse.jface.layout.GridDataFactory;\r
15 import org.eclipse.jface.layout.GridLayoutFactory;\r
16 import org.eclipse.swt.SWT;\r
17 import org.eclipse.swt.widgets.Composite;\r
18 import org.eclipse.swt.widgets.Control;\r
19 import org.eclipse.swt.widgets.Group;\r
20 import org.simantics.browsing.ui.swt.widgets.Button;\r
21 import org.simantics.browsing.ui.swt.widgets.WidgetImpl;\r
22 import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl;\r
23 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;\r
24 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
25 import org.simantics.db.ReadGraph;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.WriteGraph;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 import org.simantics.db.management.ISessionContext;\r
30 import org.simantics.modeling.ModelingResources;\r
31 import org.simantics.sysdyn.SysdynResource;\r
32 import org.simantics.utils.datastructures.Quad;\r
33 \r
34 public class ValveOrientationGroup extends WidgetImpl {\r
35 \r
36     Group group;\r
37     Button vertical, horizontal;\r
38     \r
39     public ValveOrientationGroup(Composite parent, ISessionContext context, WidgetSupport support, int style) {\r
40         super(support);\r
41         group = new Group(parent, SWT.NONE);\r
42         group.setText("Valve orientation");\r
43         GridDataFactory.fillDefaults().applyTo(group);\r
44         GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(group);\r
45         \r
46         horizontal = new Button(group, support, SWT.RADIO);\r
47         horizontal.setText("Horizontal");\r
48         horizontal.setSelectionFactory(new OrientationSelectionFactory(horizontal, SysdynResource.URIs.Horizontal, true));\r
49         horizontal.addSelectionListener(new OrientationSelectionListener(context, SysdynResource.URIs.Horizontal));\r
50         \r
51         vertical = new Button(group, support, SWT.RADIO);\r
52         vertical.setText("Vertical");\r
53         vertical.setSelectionFactory(new OrientationSelectionFactory(vertical, SysdynResource.URIs.Vertical));\r
54         vertical.addSelectionListener(new OrientationSelectionListener(context, SysdynResource.URIs.Vertical));\r
55     }\r
56 \r
57     @Override\r
58     public void setInput(ISessionContext context, Object input) {\r
59         horizontal.setInput(context, input);\r
60         vertical.setInput(context, input);\r
61     }\r
62 \r
63     @Override\r
64     public Control getControl() {\r
65         return this.group;\r
66     }\r
67     \r
68     private class OrientationSelectionFactory extends ReadFactoryImpl<Resource, Boolean> {\r
69         \r
70         boolean defaultSelected;\r
71         String uri;\r
72         Button button;\r
73         \r
74         public OrientationSelectionFactory(Button button, String uri) {\r
75             this(button, uri, false);\r
76         }\r
77 \r
78         public OrientationSelectionFactory(Button button, String uri, boolean defaultSelected) {\r
79             this.uri = uri;\r
80             this.defaultSelected = defaultSelected;\r
81             this.button = button;\r
82         }\r
83         \r
84         public Object getIdentity(Object inputContents) {\r
85             return new Quad<Object, Object, Object, Class<?>>(button, uri, defaultSelected, getClass());\r
86         }\r
87 \r
88         @Override\r
89         public Boolean perform(ReadGraph graph, Resource valve) throws DatabaseException {\r
90             SysdynResource sr = SysdynResource.getInstance(graph);\r
91             ModelingResources mr = ModelingResources.getInstance(graph);\r
92             if(!graph.isInstanceOf(valve, sr.Valve))\r
93                 return Boolean.FALSE;\r
94             \r
95             Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);\r
96             if(symbol == null)\r
97                 return Boolean.FALSE;\r
98             \r
99             Resource orientation = graph.getPossibleObject(symbol, sr.ValveSymbol_orientation);\r
100             \r
101             if(orientation == null)\r
102                 return defaultSelected;\r
103             \r
104             return orientation.equals(graph.getResource(uri));\r
105         }\r
106         \r
107     }\r
108     \r
109     \r
110     private class OrientationSelectionListener extends SelectionListenerImpl<Resource> {\r
111         \r
112         String uri;\r
113         \r
114         public OrientationSelectionListener(ISessionContext context, String uri) {\r
115             super(context);\r
116             this.uri = uri;\r
117         }\r
118 \r
119         @Override\r
120         public void apply(WriteGraph graph, Resource valve) throws DatabaseException {\r
121             SysdynResource sr = SysdynResource.getInstance(graph);\r
122             ModelingResources mr = ModelingResources.getInstance(graph);\r
123             if(!graph.isInstanceOf(valve, sr.Valve))\r
124                 return;\r
125             \r
126             Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);\r
127             if(symbol == null)\r
128                 return;\r
129             \r
130             if(graph.hasStatement(symbol, sr.ValveSymbol_orientation))\r
131                 graph.deny(symbol, sr.ValveSymbol_orientation);\r
132             graph.claim(symbol, sr.ValveSymbol_orientation, graph.getResource(uri));\r
133             \r
134             if(graph.hasStatement(symbol, sr.ValveSymbol_textLocation))\r
135                 graph.deny(symbol, sr.ValveSymbol_textLocation);\r
136             if(sr.Vertical.equals(graph.getResource(uri)))\r
137                 graph.claim(symbol, sr.ValveSymbol_textLocation, sr.Right);\r
138                 \r
139         }\r
140         \r
141     }\r
142 \r
143 }\r