1 /*******************************************************************************
\r
2 * Copyright (c) 2010 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.viewers.ISelection;
\r
15 import org.eclipse.jface.viewers.IStructuredSelection;
\r
16 import org.eclipse.swt.SWT;
\r
17 import org.eclipse.swt.widgets.Button;
\r
18 import org.eclipse.swt.widgets.Composite;
\r
19 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
\r
20 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
21 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.Resource;
\r
24 import org.simantics.db.WriteGraph;
\r
25 import org.simantics.db.common.request.ReadRequest;
\r
26 import org.simantics.db.exception.DatabaseException;
\r
27 import org.simantics.db.management.ISessionContext;
\r
28 import org.simantics.sysdyn.SysdynResource;
\r
29 import org.simantics.utils.ui.ISelectionUtils;
\r
31 public class IsOutputWidget implements Widget{
\r
33 Resource variable = null;
\r
34 org.simantics.browsing.ui.swt.widgets.Button isOutputButton;
\r
36 public IsOutputWidget(Composite parent, WidgetSupport support, int style) {
\r
37 support.register(this);
\r
38 isOutputButton = new org.simantics.browsing.ui.swt.widgets.Button(parent, support, style |= SWT.CHECK);
\r
39 isOutputButton.setText("Is Output");
\r
43 public void setInput(ISessionContext context, Object input) {
\r
44 if(input instanceof ISelection) {
\r
45 ISelection selection = (ISelection)input;
\r
46 if(selection instanceof IStructuredSelection) {
\r
47 Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);
\r
48 if(resource != null) {
\r
49 variable = resource;
\r
54 if(variable == null) return;
\r
57 context.getSession().syncRequest(new ReadRequest() {
\r
60 public void run(ReadGraph graph) throws DatabaseException {
\r
61 SysdynResource sr = SysdynResource.getInstance(graph);
\r
62 final boolean isOutput = graph.hasStatement(variable, sr.IsOutput);
\r
63 final Button button = getWidget();
\r
64 button.getDisplay().asyncExec(new Runnable() {
\r
68 if(button.isDisposed()) return;
\r
71 button.setSelection(true);
\r
73 button.setSelection(false);
\r
80 } catch (DatabaseException e) {
\r
81 e.printStackTrace();
\r
84 isOutputButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
\r
87 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\r
88 SysdynResource sr = SysdynResource.getInstance(graph);
\r
89 if(graph.hasStatement(input, sr.IsOutput)) {
\r
90 graph.deny(input, sr.IsOutput);
\r
92 graph.claim(input, SysdynResource.getInstance(graph).IsOutput, null, input);
\r
98 public Button getWidget() {
\r
99 return isOutputButton.getWidget();
\r