]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
e01725738a9a997eb061eb3952b117a0af0bf67d
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 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.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
47 \r
48 public class SharedFunctionLibrariesTab extends LabelPropertyTabContributor implements Widget {\r
49         \r
50         GraphExplorerComposite availableSharedFunctionLibraries;\r
51         GraphExplorerComposite usedSharedFunctionLibraries;\r
52         Resource model;\r
53 \r
54     @Override\r
55     public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
56                 support.register(this);\r
57                 \r
58                 GridLayoutFactory.fillDefaults().numColumns(4).applyTo(body);\r
59                 \r
60                 \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
68                     \r
69                         @Override\r
70                         protected void handleDrop(Object data, NodeContext target) {\r
71                         if (!(data instanceof IStructuredSelection))\r
72                                 return;\r
73                         \r
74                         IStructuredSelection iss = (IStructuredSelection)data;\r
75                         if (iss == null || iss.isEmpty())\r
76                                 return;\r
77                         \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
83                                 if(node != null) {\r
84                                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
85 \r
86                                                 @Override\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
90                                                 }\r
91                                         });\r
92                                 }\r
93                             }\r
94                         }\r
95                     }\r
96                 };\r
97                 \r
98                 availableSharedFunctionLibraries\r
99                 .setBrowseContexts(SysdynResource.URIs.AvailableSharedFunctionLibraries);\r
100                 availableSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(\r
101                                 Resource.class));\r
102 \r
103                 availableSharedFunctionLibraries.finish();\r
104 \r
105                 GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
106                                 availableSharedFunctionLibraries);\r
107                 \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
111                 \r
112                 Button add = new Button(middleButtons, support, SWT.NONE);\r
113                 add.setText(" -> ");\r
114                 \r
115                 add.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
116 \r
117                     List<Resource> selectedLibraries;\r
118 \r
119                     public void beforeApply() {\r
120                         selectedLibraries = getSelectedResources(availableSharedFunctionLibraries);\r
121                     }\r
122 \r
123                     @Override\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
130                             }\r
131                         }\r
132                     }\r
133                 });\r
134 \r
135                 Button remove = new Button(middleButtons, support, SWT.NONE);\r
136                 remove.setText(" <- ");\r
137                 \r
138                 remove.addSelectionListener(new SelectionListenerImpl<Resource>(context) {\r
139                     \r
140                     List<Resource> selectedLibraries;\r
141                     \r
142                     public void beforeApply() {\r
143                         selectedLibraries = getSelectedResources(usedSharedFunctionLibraries);\r
144                     }\r
145 \r
146                         @Override\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
153                                 }\r
154                             }\r
155                         }\r
156                 });\r
157                 \r
158                 \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
164                 \r
165                 usedSharedFunctionLibraries = new GraphExplorerComposite(ArrayMap.keys(\r
166                                 "displaySelectors", "displayFilter").values(false, false), site, used, SWT.FULL_SELECTION | SWT.BORDER | SWT.MULTI)  {\r
167                     \r
168                         @Override\r
169                         protected void handleDrop(Object data, NodeContext target) {\r
170                         if (!(data instanceof IStructuredSelection))\r
171                                 return;\r
172                         \r
173                         IStructuredSelection iss = (IStructuredSelection)data;\r
174                         if (iss == null || iss.isEmpty())\r
175                                 return;\r
176                         \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
182                                 if(node != null) {\r
183                                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
184 \r
185                                                 @Override\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
189                                                 }\r
190                                         });\r
191                                 }\r
192                             }\r
193                         }\r
194                     }\r
195                 };\r
196                 \r
197                 usedSharedFunctionLibraries\r
198                 .setBrowseContexts(SysdynResource.URIs.SelectedSharedFunctionLibraries);\r
199                 usedSharedFunctionLibraries.setInputSource(new SingleSelectionInputSource(\r
200                                 Resource.class));\r
201 \r
202                 usedSharedFunctionLibraries.finish();\r
203 \r
204                 GridDataFactory.fillDefaults().grab(true, true).applyTo(\r
205                                 usedSharedFunctionLibraries);\r
206         }\r
207         \r
208         \r
209         private List<Resource> getSelectedResources(GraphExplorerComposite explorer) {\r
210                 List<Resource> result = new ArrayList<Resource>();\r
211                 \r
212                 ISelection selection = ((ISelectionProvider) explorer\r
213                                 .getAdapter(ISelectionProvider.class)).getSelection();\r
214                 if (selection == null)\r
215                         return result;\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
222                 }\r
223                 return result;\r
224         }\r
225 \r
226         private Resource getModel() {\r
227                 return model;\r
228         }\r
229 \r
230         @Override\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
235         }\r
236 \r
237 }\r
238 \r