1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011, 2014 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.common.utils.NameUtils;
\r
40 import org.simantics.db.exception.DatabaseException;
\r
41 import org.simantics.db.layer0.util.Layer0Utils;
\r
42 import org.simantics.db.management.ISessionContext;
\r
43 import org.simantics.jfreechart.chart.properties.LabelPropertyTabContributor;
\r
44 import org.simantics.layer0.Layer0;
\r
45 import org.simantics.sysdyn.SysdynResource;
\r
46 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode;
\r
47 import org.simantics.ui.SimanticsUI;
\r
48 import org.simantics.utils.datastructures.ArrayMap;
\r
49 import org.simantics.utils.ui.AdaptionUtils;
\r
51 public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor implements Widget {
\r
53 public SharedFunctionLibrariesTab(Object id) {
\r
57 GraphExplorerComposite availableSharedFunctionLibraries;
\r
58 GraphExplorerComposite usedSharedFunctionLibraries;
\r
62 public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
\r
63 support.register(this);
\r
65 GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body);
\r
68 Composite available = new Composite(body, SWT.NONE);
\r
69 GridLayoutFactory.fillDefaults().applyTo(available);
\r
70 GridDataFactory.fillDefaults().grab(true, true).applyTo(available);
\r
71 Label label = new Label(available, SWT.None);
\r
72 label.setText("Available Shared Function Libraries");
\r
73 availableSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
\r
74 "displaySelectors", "displayFilter").values(false, false), site, available, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI) {
\r
77 protected void handleDrop(Object data, NodeContext target) {
\r
78 if (!(data instanceof IStructuredSelection))
\r
81 IStructuredSelection iss = (IStructuredSelection)data;
\r
82 if (iss == null || iss.isEmpty())
\r
85 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
\r
86 Object o = iterator.next();
\r
87 if(o instanceof IAdaptable) {
\r
88 IAdaptable a = (IAdaptable)o;
\r
89 final SharedFunctionLibraryNode node = (SharedFunctionLibraryNode)a.getAdapter(SharedFunctionLibraryNode.class);
\r
91 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
94 public void perform(WriteGraph graph) throws DatabaseException {
\r
95 if(getModel() != null && node.data != null)
\r
96 graph.deny(getModel(), Layer0.getInstance(graph).IsLinkedTo, node.data);
\r
105 availableSharedFunctionLibraries
\r
106 .setBrowseContexts(SysdynResource.URIs.AvailableSharedFunctionLibraries);
\r
107 availableSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
\r
110 availableSharedFunctionLibraries.finish();
\r
112 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
113 availableSharedFunctionLibraries);
\r
115 Composite middleButtons = new Composite(body, SWT.NONE);
\r
116 GridLayoutFactory.fillDefaults().applyTo(middleButtons);
\r
117 GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).grab(false, true).applyTo(middleButtons);
\r
119 Button add = new Button(middleButtons, support, SWT.NONE);
\r
120 add.setText(" -> ");
\r
122 add.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
\r
124 List<Resource> selectedLibraries;
\r
126 public void beforeApply() {
\r
127 selectedLibraries = getSelectedResources(availableSharedFunctionLibraries);
\r
131 public void apply(WriteGraph graph, Resource input)
\r
132 throws DatabaseException {
\r
133 if(selectedLibraries != null) {
\r
134 Layer0 l0 = Layer0.getInstance(graph);
\r
135 StringBuilder sb = new StringBuilder();
\r
136 sb.append("Added Shared Function Library ");
\r
137 for(Resource library : selectedLibraries) {
\r
138 graph.claim(input, l0.IsLinkedTo, library);
\r
139 sb.append(NameUtils.getSafeName(graph, library) + " ");
\r
141 sb.append("to " + NameUtils.getSafeName(graph, input));
\r
142 Layer0Utils.addCommentMetadata(graph, sb.toString());
\r
147 Button remove = new Button(middleButtons, support, SWT.NONE);
\r
148 remove.setText(" <- ");
\r
150 remove.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
\r
152 List<Resource> selectedLibraries;
\r
154 public void beforeApply() {
\r
155 selectedLibraries = getSelectedResources(usedSharedFunctionLibraries);
\r
159 public void apply(WriteGraph graph, Resource input)
\r
160 throws DatabaseException {
\r
161 if(selectedLibraries != null) {
\r
162 Layer0 l0 = Layer0.getInstance(graph);
\r
163 StringBuilder sb = new StringBuilder();
\r
164 sb.append("Removed Shared Function Library ");
\r
165 for(Resource library : selectedLibraries) {
\r
166 graph.deny(input, l0.IsLinkedTo, library);
\r
167 sb.append(NameUtils.getSafeName(graph, library) + " ");
\r
169 sb.append("from " + NameUtils.getSafeName(graph, input));
\r
170 Layer0Utils.addCommentMetadata(graph, sb.toString());
\r
176 Composite used = new Composite(body, SWT.NONE);
\r
177 GridLayoutFactory.fillDefaults().applyTo(used);
\r
178 GridDataFactory.fillDefaults().grab(true, true).applyTo(used);
\r
179 label = new Label(used, SWT.None);
\r
180 label.setText("Selected Shared Function Libraries");
\r
182 usedSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
\r
183 "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI) {
\r
186 protected void handleDrop(Object data, NodeContext target) {
\r
187 if (!(data instanceof IStructuredSelection))
\r
190 IStructuredSelection iss = (IStructuredSelection)data;
\r
191 if (iss == null || iss.isEmpty())
\r
194 for (Iterator<?> iterator = iss.iterator(); iterator.hasNext();) {
\r
195 Object o = iterator.next();
\r
196 if(o instanceof IAdaptable) {
\r
197 IAdaptable a = (IAdaptable)o;
\r
198 final SharedFunctionLibraryNode node = (SharedFunctionLibraryNode)a.getAdapter(SharedFunctionLibraryNode.class);
\r
200 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
\r
203 public void perform(WriteGraph graph) throws DatabaseException {
\r
204 if(getModel() != null && node.data != null)
\r
205 graph.claim(getModel(), Layer0.getInstance(graph).IsLinkedTo, node.data);
\r
214 usedSharedFunctionLibraries
\r
215 .setBrowseContexts(SysdynResource.URIs.SelectedSharedFunctionLibraries);
\r
216 usedSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
\r
219 usedSharedFunctionLibraries.finish();
\r
221 GridDataFactory.fillDefaults().grab(true, true).applyTo(
\r
222 usedSharedFunctionLibraries);
\r
226 private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {
\r
227 List<Resource> result = new ArrayList<Resource>();
\r
229 ISelection selection = ((ISelectionProvider) explorer
\r
230 .getAdapter(ISelectionProvider.class)).getSelection();
\r
231 if (selection == null)
\r
233 IStructuredSelection iss = (IStructuredSelection) selection;
\r
234 @SuppressWarnings("unchecked")
\r
235 List<AdaptableHintContext> selections = iss.toList();
\r
236 for(AdaptableHintContext ahc : selections) {
\r
237 Resource resource = (Resource) ahc.getAdapter(Resource.class);
\r
238 result.add(resource);
\r
243 private Resource getModel() {
\r
248 public void setInput(ISessionContext context, Object input) {
\r
249 availableSharedFunctionLibraries.setInput(context, input);
\r
250 usedSharedFunctionLibraries.setInput(context, input);
\r
251 this.model = AdaptionUtils.adaptToSingle(input, Resource.class);
\r