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;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Iterator;
\r
16 import java.util.List;
\r
18 import org.eclipse.core.runtime.IAdaptable;
\r
19 import org.eclipse.jface.layout.GridDataFactory;
\r
20 import org.eclipse.jface.layout.GridLayoutFactory;
\r
21 import org.eclipse.jface.viewers.ISelection;
\r
22 import org.eclipse.jface.viewers.ISelectionProvider;
\r
23 import org.eclipse.jface.viewers.IStructuredSelection;
\r
24 import org.eclipse.swt.SWT;
\r
25 import org.eclipse.swt.widgets.Composite;
\r
26 import org.eclipse.swt.widgets.Label;
\r
27 import org.eclipse.ui.IWorkbenchSite;
\r
28 import org.simantics.browsing.ui.NodeContext;
\r
29 import org.simantics.browsing.ui.swt.AdaptableHintContext;
\r
30 import org.simantics.browsing.ui.swt.SingleSelectionInputSource;
\r
31 import org.simantics.browsing.ui.swt.widgets.Button;
\r
32 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
\r
33 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
\r
34 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
\r
35 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
36 import org.simantics.db.Resource;
\r
37 import org.simantics.db.WriteGraph;
\r
38 import org.simantics.db.common.request.WriteRequest;
\r
39 import org.simantics.db.exception.DatabaseException;
\r
40 import org.simantics.db.management.ISessionContext;
\r
41 import org.simantics.layer0.Layer0;
\r
42 import org.simantics.sysdyn.SysdynResource;
\r
43 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode;
\r
44 import org.simantics.ui.SimanticsUI;
\r
45 import org.simantics.ui.utils.AdaptionUtils;
\r
46 import org.simantics.utils.datastructures.ArrayMap;
\r
48 public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor implements Widget {
\r
50 GraphExplorerComposite availableSharedFunctionLibraries;
\r
51 GraphExplorerComposite usedSharedFunctionLibraries;
\r
55 public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
\r
56 support.register(this);
\r
58 GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body);
\r
61 Composite available = new Composite(body, SWT.NONE);
\r
62 GridLayoutFactory.fillDefaults().applyTo(available);
\r
63 GridDataFactory.fillDefaults().grab(true, true).applyTo(available);
\r
64 Label label = new Label(available, SWT.None);
\r
65 label.setText("Available Shared Function Libraries");
\r
66 availableSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
\r
67 "displaySelectors", "displayFilter").values(false, false), site, available, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI) {
\r
70 protected void handleDrop(Object data, NodeContext target) {
\r
71 if (!(data instanceof IStructuredSelection))
\r
74 IStructuredSelection iss = (IStructuredSelection)data;
\r
75 if (iss == null || iss.isEmpty())
\r
78 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
\r
79 Object o = iterator.next();
\r
80 if(o instanceof IAdaptable) {
\r
81 IAdaptable a = (IAdaptable)o;
\r
82 final SharedFunctionLibraryNode node = (SharedFunctionLibraryNode)a.getAdapter(SharedFunctionLibraryNode.class);
\r
84 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
87 public void perform(WriteGraph graph) throws DatabaseException {
\r
88 if(getModel() != null && node.data != null)
\r
89 graph.deny(getModel(), Layer0.getInstance(graph).IsLinkedTo, node.data);
\r
98 availableSharedFunctionLibraries
\r
99 .setBrowseContexts(SysdynResource.URIs.AvailableSharedFunctionLibraries);
\r
100 availableSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
\r
103 availableSharedFunctionLibraries.finish();
\r
105 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
106 availableSharedFunctionLibraries);
\r
108 Composite middleButtons = new Composite(body, SWT.NONE);
\r
109 GridLayoutFactory.fillDefaults().applyTo(middleButtons);
\r
110 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, true).applyTo(middleButtons);
\r
112 Button add = new Button(middleButtons, support, SWT.NONE);
\r
113 add.setText(" -> ");
\r
115 add.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
\r
117 List<Resource> selectedLibraries;
\r
119 public void beforeApply() {
\r
120 selectedLibraries = getSelectedResources(availableSharedFunctionLibraries);
\r
124 public void apply(WriteGraph graph, Resource input)
\r
125 throws DatabaseException {
\r
126 if(selectedLibraries != null) {
\r
127 Layer0 l0 = Layer0.getInstance(graph);
\r
128 for(Resource library : selectedLibraries) {
\r
129 graph.claim(input, l0.IsLinkedTo, library);
\r
135 Button remove = new Button(middleButtons, support, SWT.NONE);
\r
136 remove.setText(" <- ");
\r
138 remove.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
\r
140 List<Resource> selectedLibraries;
\r
142 public void beforeApply() {
\r
143 selectedLibraries = getSelectedResources(usedSharedFunctionLibraries);
\r
147 public void apply(WriteGraph graph, Resource input)
\r
148 throws DatabaseException {
\r
149 if(selectedLibraries != null) {
\r
150 Layer0 l0 = Layer0.getInstance(graph);
\r
151 for(Resource library : selectedLibraries) {
\r
152 graph.deny(input, l0.IsLinkedTo, library);
\r
159 Composite used = new Composite(body, SWT.NONE);
\r
160 GridLayoutFactory.fillDefaults().applyTo(used);
\r
161 GridDataFactory.fillDefaults().grab(true, true).applyTo(used);
\r
162 label = new Label(used, SWT.None);
\r
163 label.setText("Selected Shared Function Libraries");
\r
165 usedSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
\r
166 "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI) {
\r
169 protected void handleDrop(Object data, NodeContext target) {
\r
170 if (!(data instanceof IStructuredSelection))
\r
173 IStructuredSelection iss = (IStructuredSelection)data;
\r
174 if (iss == null || iss.isEmpty())
\r
177 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
\r
178 Object o = iterator.next();
\r
179 if(o instanceof IAdaptable) {
\r
180 IAdaptable a = (IAdaptable)o;
\r
181 final SharedFunctionLibraryNode node = (SharedFunctionLibraryNode)a.getAdapter(SharedFunctionLibraryNode.class);
\r
183 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
186 public void perform(WriteGraph graph) throws DatabaseException {
\r
187 if(getModel() != null && node.data != null)
\r
188 graph.claim(getModel(), Layer0.getInstance(graph).IsLinkedTo, node.data);
\r
197 usedSharedFunctionLibraries
\r
198 .setBrowseContexts(SysdynResource.URIs.SelectedSharedFunctionLibraries);
\r
199 usedSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
\r
202 usedSharedFunctionLibraries.finish();
\r
204 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
205 usedSharedFunctionLibraries);
\r
209 private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {
\r
210 List<Resource> result = new ArrayList<Resource>();
\r
212 ISelection selection = ((ISelectionProvider) explorer
\r
213 .getAdapter(ISelectionProvider.class)).getSelection();
\r
214 if (selection == null)
\r
216 IStructuredSelection iss = (IStructuredSelection) selection;
\r
217 @SuppressWarnings("unchecked")
\r
218 List<AdaptableHintContext> selections = iss.toList();
\r
219 for(AdaptableHintContext ahc : selections) {
\r
220 Resource resource = (Resource) ahc.getAdapter(Resource.class);
\r
221 result.add(resource);
\r
226 private Resource getModel() {
\r
231 public void setInput(ISessionContext context, Object input) {
\r
232 availableSharedFunctionLibraries.setInput(context, input);
\r
233 usedSharedFunctionLibraries.setInput(context, input);
\r
234 this.model = AdaptionUtils.adaptToSingle(input, Resource.class);
\r