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