]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
69d8376af750d9f61a0643f054b334304b41c7df
[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 ValveTextLocationGroup extends WidgetImpl {\r
35 \r
36     Group group;\r
37     Button top, bottom, left, right;\r
38     \r
39     public ValveTextLocationGroup(Composite parent, ISessionContext context, WidgetSupport support, int style) {\r
40         super(support);\r
41         \r
42         group = new Group(parent, SWT.NONE);\r
43         group.setText("Text location");\r
44         GridDataFactory.fillDefaults().applyTo(group);\r
45         GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(group);\r
46         \r
47         top = new Button(group, support, SWT.RADIO);\r
48         top.setText("Top");\r
49         top.setSelectionFactory(new LocationSelectionFactory(top, SysdynResource.URIs.Top));\r
50         top.addSelectionListener(new LocationSelectionListener(context, SysdynResource.URIs.Top));\r
51         \r
52         bottom = new Button(group, support, SWT.RADIO);\r
53         bottom.setText("Bottom");\r
54         bottom.setSelectionFactory(new LocationSelectionFactory(bottom, SysdynResource.URIs.Bottom, true));\r
55         bottom.addSelectionListener(new LocationSelectionListener(context, SysdynResource.URIs.Bottom));\r
56         \r
57         left = new Button(group, support, SWT.RADIO);\r
58         left.setText("Left");\r
59         left.setSelectionFactory(new LocationSelectionFactory(left, SysdynResource.URIs.Left));\r
60         left.addSelectionListener(new LocationSelectionListener(context, SysdynResource.URIs.Left));\r
61         \r
62         right = new Button(group, support, SWT.RADIO);\r
63         right.setText("Right");\r
64         right.setSelectionFactory(new LocationSelectionFactory(right, SysdynResource.URIs.Right));\r
65         right.addSelectionListener(new LocationSelectionListener(context, SysdynResource.URIs.Right));\r
66         \r
67     }\r
68 \r
69     @Override\r
70     public void setInput(ISessionContext context, Object input) {\r
71         top.setInput(context, input);\r
72         bottom.setInput(context, input);\r
73         left.setInput(context, input);\r
74         right.setInput(context, input);\r
75     }\r
76 \r
77     @Override\r
78     public Control getControl() {\r
79         return this.group;\r
80     }\r
81     \r
82     \r
83     private class LocationSelectionFactory extends ReadFactoryImpl<Resource, Boolean> {\r
84         \r
85         boolean defaultSelected;\r
86         String uri;\r
87         Button button;\r
88         \r
89         public LocationSelectionFactory(Button button, String uri) {\r
90             this(button, uri, false);\r
91         }\r
92 \r
93         public LocationSelectionFactory(Button button, String uri, boolean defaultSelected) {\r
94             this.uri = uri;\r
95             this.defaultSelected = defaultSelected;\r
96             this.button = button;\r
97         }\r
98         \r
99         public Object getIdentity(Object inputContents) {\r
100             return new Quad<Object, Object, Object, Class<?>>(button, uri, defaultSelected, getClass());\r
101         }\r
102 \r
103         @Override\r
104         public Boolean perform(ReadGraph graph, Resource valve) throws DatabaseException {\r
105             SysdynResource sr = SysdynResource.getInstance(graph);\r
106             ModelingResources mr = ModelingResources.getInstance(graph);\r
107             if(!graph.isInstanceOf(valve, sr.Valve))\r
108                 return Boolean.FALSE;\r
109             \r
110             Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);\r
111             if(symbol == null)\r
112                 return Boolean.FALSE;\r
113             \r
114             Resource location = graph.getPossibleObject(symbol, sr.HasTextLocation);\r
115             \r
116             if(location == null)\r
117                 return defaultSelected;\r
118             \r
119             return location.equals(graph.getResource(uri));\r
120         }\r
121         \r
122     }\r
123     \r
124     \r
125     private class LocationSelectionListener extends SelectionListenerImpl<Resource> {\r
126         \r
127         String uri;\r
128         \r
129         public LocationSelectionListener(ISessionContext context, String uri) {\r
130             super(context);\r
131             this.uri = uri;\r
132         }\r
133 \r
134         @Override\r
135         public void apply(WriteGraph graph, Resource valve) throws DatabaseException {\r
136             SysdynResource sr = SysdynResource.getInstance(graph);\r
137             ModelingResources mr = ModelingResources.getInstance(graph);\r
138             if(!graph.isInstanceOf(valve, sr.Valve))\r
139                 return;\r
140             \r
141             Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);\r
142             if(symbol == null)\r
143                 return;\r
144             \r
145             if(graph.hasStatement(symbol, sr.HasTextLocation))\r
146                 graph.deny(symbol, sr.HasTextLocation);\r
147             graph.claim(symbol, sr.HasTextLocation, graph.getResource(uri));\r
148         }\r
149         \r
150     }\r
151 \r
152 }\r