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 ValveTextLocationGroup extends WidgetImpl {
\r
37 Button top, bottom, left, right;
\r
39 public ValveTextLocationGroup(Composite parent, ISessionContext context, WidgetSupport support, int style) {
\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
47 top = new Button(group, support, SWT.RADIO);
\r
49 top.setSelectionFactory(new LocationSelectionFactory(top, SysdynResource.URIs.Top));
\r
50 top.addSelectionListener(new LocationSelectionListener(context, SysdynResource.URIs.Top));
\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
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
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
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
78 public Control getControl() {
\r
83 private class LocationSelectionFactory extends ReadFactoryImpl<Resource, Boolean> {
\r
85 boolean defaultSelected;
\r
89 public LocationSelectionFactory(Button button, String uri) {
\r
90 this(button, uri, false);
\r
93 public LocationSelectionFactory(Button button, String uri, boolean defaultSelected) {
\r
95 this.defaultSelected = defaultSelected;
\r
96 this.button = button;
\r
99 public Object getIdentity(Object inputContents) {
\r
100 return new Quad<Object, Object, Object, Class<?>>(button, uri, defaultSelected, getClass());
\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
110 Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);
\r
112 return Boolean.FALSE;
\r
114 Resource location = graph.getPossibleObject(symbol, sr.HasTextLocation);
\r
116 if(location == null)
\r
117 return defaultSelected;
\r
119 return location.equals(graph.getResource(uri));
\r
125 private class LocationSelectionListener extends SelectionListenerImpl<Resource> {
\r
129 public LocationSelectionListener(ISessionContext context, String uri) {
\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
141 Resource symbol = graph.getPossibleObject(valve, mr.ComponentToElement);
\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