]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
7fec15f74295d01f8efbfebf7a8dcc0dc0fe49a8
[simantics/sysdyn.git] /
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011, 2014 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.sysdyn.ui.properties;
13
14 import java.util.ArrayList;
15 import java.util.Iterator;
16 import java.util.List;
17
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;
50
51 public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor implements Widget {
52         
53         public SharedFunctionLibrariesTab(Object id) {
54         super(id);
55     }
56
57     GraphExplorerComposite availableSharedFunctionLibraries;
58         GraphExplorerComposite usedSharedFunctionLibraries;
59         Resource model;
60
61     @Override
62     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {
63                 support.register(this);
64                 
65                 GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body);
66                 
67                 
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) {
75                     
76                         @Override
77                         protected void handleDrop(Object data, NodeContext target) {
78                         if (!(data instanceof IStructuredSelection))
79                                 return;
80                         
81                         IStructuredSelection iss = (IStructuredSelection)data;
82                         if (iss == null || iss.isEmpty())
83                                 return;
84                         
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);
90                                 if(node != null) {
91                                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {
92
93                                                 @Override
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);                                                                
97                                                 }
98                                         });
99                                 }
100                             }
101                         }
102                     }
103                 };
104                 
105                 availableSharedFunctionLibraries
106                 .setBrowseContexts(SysdynResource.URIs.AvailableSharedFunctionLibraries);
107                 availableSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
108                                 Resource.class));
109
110                 availableSharedFunctionLibraries.finish();
111
112                 GridDataFactory.fillDefaults().grab(true, true).applyTo(
113                                 availableSharedFunctionLibraries);
114                 
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);
118                 
119                 Button add = new Button(middleButtons, support, SWT.NONE);
120                 add.setText(" -> ");
121                 
122                 add.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
123
124                     List<Resource> selectedLibraries;
125
126                     public void beforeApply() {
127                         selectedLibraries = getSelectedResources(availableSharedFunctionLibraries);
128                     }
129
130                     @Override
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) + " ");
140                     }
141                     sb.append("to " + NameUtils.getSafeName(graph, input));
142                     Layer0Utils.addCommentMetadata(graph,  sb.toString());
143                         }
144                     }
145                 });
146
147                 Button remove = new Button(middleButtons, support, SWT.NONE);
148                 remove.setText(" <- ");
149                 
150                 remove.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
151                     
152                     List<Resource> selectedLibraries;
153                     
154                     public void beforeApply() {
155                         selectedLibraries = getSelectedResources(usedSharedFunctionLibraries);
156                     }
157
158                         @Override
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) + " ");
168                                 }
169                                 sb.append("from " + NameUtils.getSafeName(graph, input));
170                                 Layer0Utils.addCommentMetadata(graph,  sb.toString());
171                             }
172                         }
173                 });
174                 
175                 
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");
181                 
182                 usedSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(
183                                 "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI)  {
184                     
185                         @Override
186                         protected void handleDrop(Object data, NodeContext target) {
187                         if (!(data instanceof IStructuredSelection))
188                                 return;
189                         
190                         IStructuredSelection iss = (IStructuredSelection)data;
191                         if (iss == null || iss.isEmpty())
192                                 return;
193                         
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);
199                                 if(node != null) {
200                                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {
201
202                                                 @Override
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);                                                               
206                                                 }
207                                         });
208                                 }
209                             }
210                         }
211                     }
212                 };
213                 
214                 usedSharedFunctionLibraries
215                 .setBrowseContexts(SysdynResource.URIs.SelectedSharedFunctionLibraries);
216                 usedSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(
217                                 Resource.class));
218
219                 usedSharedFunctionLibraries.finish();
220
221                 GridDataFactory.fillDefaults().grab(true, true).applyTo(
222                                 usedSharedFunctionLibraries);
223         }
224         
225         
226         private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {
227                 List<Resource> result = new ArrayList<Resource>();
228                 
229                 ISelection selection = ((ISelectionProvider) explorer
230                                 .getAdapter(ISelectionProvider.class)).getSelection();
231                 if (selection == null)
232                         return result;
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);
239                 }
240                 return result;
241         }
242
243         private Resource getModel() {
244                 return model;
245         }
246
247         @Override
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);
252         }
253 }
254