1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets;
\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
34 public class ValveOrientationGroup extends WidgetImpl {
\r
37 Button vertical, horizontal;
\r
39 public ValveOrientationGroup(Composite parent, ISessionContext context, WidgetSupport support, int style) {
\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
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
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
58 public void setInput(ISessionContext context, Object input) {
\r
59 horizontal.setInput(context, input);
\r
60 vertical.setInput(context, input);
\r
64 public Control getControl() {
\r
68 private class OrientationSelectionFactory extends ReadFactoryImpl<Resource, Boolean> {
\r
70 boolean defaultSelected;
\r
74 public OrientationSelectionFactory(Button button, String uri) {
\r
75 this(button, uri, false);
\r
78 public OrientationSelectionFactory(Button button, String uri, boolean defaultSelected) {
\r
80 this.defaultSelected = defaultSelected;
\r
81 this.button = button;
\r
84 public Object getIdentity(Object inputContents) {
\r
85 return new Quad<Object, Object, Object, Class<?>>(button, uri, defaultSelected, getClass());
\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
95 Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);
\r
97 return Boolean.FALSE;
\r
99 Resource orientation = graph.getPossibleObject(symbol, sr.ValveSymbol_orientation);
\r
101 if(orientation == null)
\r
102 return defaultSelected;
\r
104 return orientation.equals(graph.getResource(uri));
\r
110 private class OrientationSelectionListener extends SelectionListenerImpl<Resource> {
\r
114 public OrientationSelectionListener(ISessionContext context, String uri) {
\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
126 Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);
\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
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