1 /*******************************************************************************
2 * Copyright (c) 2007, 2011, 2014 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.sysdyn.ui.properties;
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.jface.layout.GridDataFactory;
20 import org.eclipse.jface.layout.GridLayoutFactory;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionProvider;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Label;
27 import org.eclipse.ui.IWorkbenchSite;
28 import org.simantics.browsing.ui.NodeContext;
29 import org.simantics.browsing.ui.common.AdaptableHintContext;
30 import org.simantics.browsing.ui.swt.SingleSelectionInputSource;
31 import org.simantics.browsing.ui.swt.widgets.Button;
32 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
33 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
34 import org.simantics.browsing.ui.swt.widgets.impl.Widget;
35 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
36 import org.simantics.db.Resource;
37 import org.simantics.db.WriteGraph;
38 import org.simantics.db.common.request.WriteRequest;
39 import org.simantics.db.common.utils.NameUtils;
40 import org.simantics.db.exception.DatabaseException;
41 import org.simantics.db.layer0.util.Layer0Utils;
42 import org.simantics.db.management.ISessionContext;
43 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;
44 import org.simantics.layer0.Layer0;
45 import org.simantics.sysdyn.SysdynResource;
46 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode;
47 import org.simantics.ui.SimanticsUI;
48 import org.simantics.utils.datastructures.ArrayMap;
49 import org.simantics.utils.ui.AdaptionUtils;
51 public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor implements Widget {
53 public SharedFunctionLibrariesTab(Object id) {
57 GraphExplorerComposite availableSharedFunctionLibraries;
58 GraphExplorerComposite usedSharedFunctionLibraries;
62 public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
63 support.register(this);
65 GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body);
68 Composite available = new Composite(body, SWT.NONE);
69 GridLayoutFactory.fillDefaults().applyTo(available);
70 GridDataFactory.fillDefaults().grab(true, true).applyTo(available);
71 Label label = new Label(available, SWT.None);
72 label.setText("Available Shared Function Libraries");
73 availableSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
74 "displaySelectors", "displayFilter").values(false, false), site, available, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI) {
77 protected void handleDrop(Object data, NodeContext target) {
78 if (!(data instanceof IStructuredSelection))
81 IStructuredSelection iss = (IStructuredSelection)data;
82 if (iss == null || iss.isEmpty())
85 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
86 Object o = iterator.next();
87 if(o instanceof IAdaptable) {
88 IAdaptable a = (IAdaptable)o;
89 final SharedFunctionLibraryNode node = (SharedFunctionLibraryNode)a.getAdapter(SharedFunctionLibraryNode.class);
91 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
94 public void perform(WriteGraph graph) throws DatabaseException {
95 if(getModel() != null && node.data != null)
96 graph.deny(getModel(), Layer0.getInstance(graph).IsLinkedTo, node.data);
105 availableSharedFunctionLibraries
106 .setBrowseContexts(SysdynResource.URIs.AvailableSharedFunctionLibraries);
107 availableSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
110 availableSharedFunctionLibraries.finish();
112 GridDataFactory.fillDefaults().grab(true, true).applyTo(
113 availableSharedFunctionLibraries);
115 Composite middleButtons = new Composite(body, SWT.NONE);
116 GridLayoutFactory.fillDefaults().applyTo(middleButtons);
117 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, true).applyTo(middleButtons);
119 Button add = new Button(middleButtons, support, SWT.NONE);
122 add.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
124 List<Resource> selectedLibraries;
126 public void beforeApply() {
127 selectedLibraries = getSelectedResources(availableSharedFunctionLibraries);
131 public void apply(WriteGraph graph, Resource input)
132 throws DatabaseException {
133 if(selectedLibraries != null) {
134 Layer0 l0 = Layer0.getInstance(graph);
135 StringBuilder sb = new StringBuilder();
136 sb.append("Added Shared Function Library ");
137 for(Resource library : selectedLibraries) {
138 graph.claim(input, l0.IsLinkedTo, library);
139 sb.append(NameUtils.getSafeName(graph, library) + " ");
141 sb.append("to " + NameUtils.getSafeName(graph, input));
142 Layer0Utils.addCommentMetadata(graph, sb.toString());
147 Button remove = new Button(middleButtons, support, SWT.NONE);
148 remove.setText(" <- ");
150 remove.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
152 List<Resource> selectedLibraries;
154 public void beforeApply() {
155 selectedLibraries = getSelectedResources(usedSharedFunctionLibraries);
159 public void apply(WriteGraph graph, Resource input)
160 throws DatabaseException {
161 if(selectedLibraries != null) {
162 Layer0 l0 = Layer0.getInstance(graph);
163 StringBuilder sb = new StringBuilder();
164 sb.append("Removed Shared Function Library ");
165 for(Resource library : selectedLibraries) {
166 graph.deny(input, l0.IsLinkedTo, library);
167 sb.append(NameUtils.getSafeName(graph, library) + " ");
169 sb.append("from " + NameUtils.getSafeName(graph, input));
170 Layer0Utils.addCommentMetadata(graph, sb.toString());
176 Composite used = new Composite(body, SWT.NONE);
177 GridLayoutFactory.fillDefaults().applyTo(used);
178 GridDataFactory.fillDefaults().grab(true, true).applyTo(used);
179 label = new Label(used, SWT.None);
180 label.setText("Selected Shared Function Libraries");
182 usedSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
183 "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI) {
186 protected void handleDrop(Object data, NodeContext target) {
187 if (!(data instanceof IStructuredSelection))
190 IStructuredSelection iss = (IStructuredSelection)data;
191 if (iss == null || iss.isEmpty())
194 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
195 Object o = iterator.next();
196 if(o instanceof IAdaptable) {
197 IAdaptable a = (IAdaptable)o;
198 final SharedFunctionLibraryNode node = (SharedFunctionLibraryNode)a.getAdapter(SharedFunctionLibraryNode.class);
200 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
203 public void perform(WriteGraph graph) throws DatabaseException {
204 if(getModel() != null && node.data != null)
205 graph.claim(getModel(), Layer0.getInstance(graph).IsLinkedTo, node.data);
214 usedSharedFunctionLibraries
215 .setBrowseContexts(SysdynResource.URIs.SelectedSharedFunctionLibraries);
216 usedSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
219 usedSharedFunctionLibraries.finish();
221 GridDataFactory.fillDefaults().grab(true, true).applyTo(
222 usedSharedFunctionLibraries);
226 private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {
227 List<Resource> result = new ArrayList<Resource>();
229 ISelection selection = ((ISelectionProvider) explorer
230 .getAdapter(ISelectionProvider.class)).getSelection();
231 if (selection == null)
233 IStructuredSelection iss = (IStructuredSelection) selection;
234 @SuppressWarnings("unchecked")
235 List<AdaptableHintContext> selections = iss.toList();
236 for(AdaptableHintContext ahc : selections) {
237 Resource resource = (Resource) ahc.getAdapter(Resource.class);
238 result.add(resource);
243 private Resource getModel() {
248 public void setInput(ISessionContext context, Object input) {
249 availableSharedFunctionLibraries.setInput(context, input);
250 usedSharedFunctionLibraries.setInput(context, input);
251 this.model = AdaptionUtils.adaptToSingle(input, Resource.class);