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.arrays;
\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.events.SelectionListener;
\r
18 import org.eclipse.swt.widgets.Button;
\r
19 import org.eclipse.swt.widgets.Composite;
\r
20 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
\r
21 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
22 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
23 import org.simantics.db.ReadGraph;
\r
24 import org.simantics.db.Resource;
\r
25 import org.simantics.db.WriteGraph;
\r
26 import org.simantics.db.common.request.ReadRequest;
\r
27 import org.simantics.db.exception.DatabaseException;
\r
28 import org.simantics.db.management.ISessionContext;
\r
29 import org.simantics.sysdyn.SysdynResource;
\r
30 import org.simantics.utils.ui.ISelectionUtils;
\r
32 public class ReplaceableIndexesWidget implements Widget {
\r
34 Resource variable = null;
\r
35 boolean selected = false;
\r
36 org.simantics.browsing.ui.swt.widgets.Button isReplaceableButton;
\r
38 public ReplaceableIndexesWidget(Composite parent, WidgetSupport support, int style) {
\r
39 support.register(this);
\r
40 isReplaceableButton = new org.simantics.browsing.ui.swt.widgets.Button(parent, support, style |= SWT.CHECK);
\r
41 isReplaceableButton.setText("Can be replaced by parent module");
\r
45 public void setInput(ISessionContext context, Object input) {
\r
46 if(input instanceof ISelection) {
\r
47 ISelection selection = (ISelection)input;
\r
48 if(selection instanceof IStructuredSelection) {
\r
49 Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class);
\r
50 if(resource != null) {
\r
51 variable = resource;
\r
56 if(variable == null) return;
\r
59 context.getSession().syncRequest(new ReadRequest() {
\r
62 public void run(ReadGraph graph) throws DatabaseException {
\r
63 SysdynResource sr = SysdynResource.getInstance(graph);
\r
64 final Boolean replaceable = graph.getPossibleRelatedValue(variable, sr.Enumeration_isReplaceable);
\r
65 if(replaceable != null)
\r
66 selected = replaceable;
\r
67 final Button button = getWidget();
\r
68 button.getDisplay().asyncExec(new Runnable() {
\r
72 if(button.isDisposed()) return;
\r
75 button.setSelection(true);
\r
77 button.setSelection(false);
\r
84 } catch (DatabaseException e) {
\r
85 e.printStackTrace();
\r
88 isReplaceableButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
\r
91 public void apply(WriteGraph graph, Resource input) throws DatabaseException {
\r
92 SysdynResource sr = SysdynResource.getInstance(graph);
\r
93 Boolean replaceable = graph.getPossibleRelatedValue(input, sr.Enumeration_isReplaceable);
\r
94 if(Boolean.TRUE.equals(replaceable)) {
\r
95 graph.claimLiteral(input, sr.Enumeration_isReplaceable, false);
\r
98 graph.claimLiteral(input, sr.Enumeration_isReplaceable, true);
\r
105 public Button getWidget() {
\r
106 return isReplaceableButton.getWidget();
\r
109 public boolean getSelection() {
\r
113 public void addSelectionListener(SelectionListener listener) {
\r
114 isReplaceableButton.addSelectionListener(listener);
\r