]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.platform/src/org/simantics/browsing/ui/platform/VariablesPage.java
394859d095a40d957ec43e592a0a7b46e7415edd
[simantics/platform.git] / bundles / org.simantics.browsing.ui.platform / src / org / simantics / browsing / ui / platform / VariablesPage.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in 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.browsing.ui.platform;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.HashMap;\r
17 import java.util.HashSet;\r
18 import java.util.Map;\r
19 import java.util.Set;\r
20 import java.util.function.Consumer;\r
21 \r
22 import org.eclipse.core.runtime.IAdaptable;\r
23 import org.eclipse.jface.viewers.ISelection;\r
24 import org.eclipse.swt.SWT;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Control;\r
27 import org.eclipse.ui.IPartListener;\r
28 import org.eclipse.ui.IWorkbenchPart;\r
29 import org.eclipse.ui.IWorkbenchPartSite;\r
30 import org.eclipse.ui.part.IPageBookViewPage;\r
31 import org.eclipse.ui.part.Page;\r
32 import org.simantics.browsing.ui.GraphExplorer;\r
33 import org.simantics.browsing.ui.swt.IVariablesPage;\r
34 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;\r
35 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite.InputSource;\r
36 import org.simantics.db.ReadGraph;\r
37 import org.simantics.db.Resource;\r
38 import org.simantics.db.common.ResourceArray;\r
39 import org.simantics.db.common.utils.NameUtils;\r
40 import org.simantics.db.exception.AdaptionException;\r
41 import org.simantics.db.exception.DatabaseException;\r
42 import org.simantics.db.management.ISessionContext;\r
43 import org.simantics.db.management.ISessionContextChangedListener;\r
44 import org.simantics.db.management.ISessionContextProvider;\r
45 import org.simantics.db.management.SessionContextChangedEvent;\r
46 import org.simantics.db.procedure.Listener;\r
47 import org.simantics.db.request.Read;\r
48 import org.simantics.ui.utils.ResourceAdaptionUtils;\r
49 import org.simantics.utils.ui.AdaptionUtils;\r
50 import org.simantics.utils.ui.ErrorLogger;\r
51 \r
52 /**\r
53  * <p>\r
54  * Subclasses may extend or reimplement the following methods as required:\r
55  * <ul>\r
56  *   <li><code>createPageControls</code> - to create the page's controls</li>\r
57  *   <li><code>getControl</code> - to retrieve the page's control</li>\r
58  *   <li><code>setFocus</code> - implement to accept focus</li>\r
59  *   <li><code>sourceSelectionChanged</code> - puts the incoming ISelection into use in this page</li>\r
60  *   <li><code>sourcePartClosed</code> - cleans up the page controls after a current selection source part has been closed</li>\r
61  *   <li><code>dispose</code> - extend to provide additional cleanup</li>\r
62  *   <li><code>setActionBars</code> - reimplement to make contributions</li>\r
63  *   <li><code>makeContributions</code> - this method exists to support previous versions</li>\r
64  *   <li><code>setActionBars</code> - this method exists to support previous versions</li>\r
65  *   <li><code>init</code> - extend to provide additional setup</li>\r
66  *   <li><code>sessionContextChanged</code> - reimplement to take actions when the source database session changes</li>\r
67  * </ul>\r
68  * </p>\r
69  * \r
70  * @author Tuukka Lehtonen\r
71  */\r
72 public class VariablesPage extends Page implements IPageBookViewPage, IVariablesPage {\r
73 \r
74     protected static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;\r
75 \r
76     protected ISessionContext sessionContext;\r
77 \r
78     protected GraphExplorerComposite explorer;\r
79 \r
80     /**\r
81      * @param site the workbench part site that contains this page or\r
82      *        <code>null</code> if there is no site, i.e. the page is within a\r
83      *        dialog or a plain shell.\r
84      */\r
85     public VariablesPage(IWorkbenchPartSite site) {\r
86         this.site = site;\r
87     }\r
88 \r
89     /**\r
90      * @param site the workbench part site that contains this page or\r
91      *        <code>null</code> if there is no site, i.e. the page is within a\r
92      *        dialog or a plain shell.\r
93      * @param adapter must provide an adapter for\r
94      *        <code>ISessionContextProvider.class</code>\r
95      */\r
96     public VariablesPage(IWorkbenchPartSite site, IAdaptable adapter) {\r
97         this(site);\r
98         setAdapter(adapter);\r
99     }\r
100 \r
101     @Override\r
102     public void dispose() {\r
103         \r
104         // Stop listening for title changes.\r
105         if (currentPartNameListener != null)\r
106             currentPartNameListener.dispose();\r
107 \r
108         if (adapter != null) {\r
109             ISessionContextProvider contextProvider = getSessionContextProvider();\r
110             contextProvider.removeContextChangedListener(contextChangeListener);\r
111         }\r
112 \r
113         if (sourcePart != null) {\r
114             sourcePart.getSite().getPage().removePartListener(partListener);\r
115             sourcePart = null;\r
116         }\r
117 \r
118         site = null;\r
119         adapter = null;\r
120         explorer = null;\r
121         sessionContext = null;\r
122         \r
123     }\r
124 \r
125     protected ISessionContextProvider getSessionContextProvider() {\r
126         return (ISessionContextProvider) getAdapter().getAdapter(ISessionContextProvider.class);\r
127     }\r
128 \r
129     protected ISessionContext getSessionContext() {\r
130         return sessionContext;\r
131     }\r
132 \r
133     @Override\r
134     public final void createControl(Composite parent) {\r
135         createPageControls(parent);\r
136 \r
137         // Attach to current session context provider to keep the UI intact even\r
138         // when the current UI session changes.\r
139         ISessionContextProvider contextProvider = getSessionContextProvider();\r
140         contextProvider.addContextChangedListener(contextChangeListener);\r
141         setSessionContext(contextProvider.getSessionContext());\r
142     }\r
143 \r
144     /**\r
145      * Override to customize the UI component created on this page.\r
146      * \r
147      * @param parent\r
148      */\r
149     protected void createPageControls(Composite parent) {\r
150 \r
151         Map<String, Object> args = new HashMap<String, Object>();\r
152         Set<String> browseContexts = new HashSet<String>();\r
153         \r
154         browseContexts.add("org.simantics.browsing.ui.graph.variablesView");\r
155         \r
156         args.put("browseContexts", browseContexts);\r
157         \r
158         explorer = new GraphExplorerComposite(args, site, parent, SWT.NONE);\r
159         \r
160         explorer.setInputSource(new InputSource() {\r
161 \r
162             @Override\r
163             public Object get(ISessionContext ctx, Object selection) {\r
164 \r
165                 final Resource input = AdaptionUtils.adaptToSingle(selection, Resource.class);\r
166                 if(input == null) {\r
167                     return GraphExplorer.EMPTY_INPUT;\r
168                 }\r
169                 \r
170                 final VariablePrefix prefix =  AdaptionUtils.adaptToSingle(selection, VariablePrefix.class);\r
171                 if(prefix == null) {\r
172                         return new VariablesInput(null, input);\r
173                 } else {\r
174                         return new VariablesInput(prefix.getPrefix(), input);\r
175                 }\r
176 \r
177             }\r
178                 \r
179         });\r
180         \r
181         }\r
182         \r
183 \r
184     /**\r
185      * @param newContext\r
186      */\r
187     protected final void setSessionContext(ISessionContext newContext) {\r
188         ISessionContext oldContext = this.sessionContext;\r
189         this.sessionContext = newContext;\r
190 //        System.out.println("AbstractPropertyPage.setSessionContext: " + oldContext + " -> " + newContext);\r
191 \r
192         sessionContextChanged(oldContext, newContext);\r
193     }\r
194 \r
195     /**\r
196      * @param oldContext\r
197      * @param newContext\r
198      */\r
199     protected void sessionContextChanged(ISessionContext oldContext, ISessionContext newContext) {\r
200         explorer.applySessionContext(newContext);\r
201 //        explorer.setSessionContext(newContext);\r
202     }\r
203 \r
204     protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {\r
205         @Override\r
206         public void sessionContextChanged(SessionContextChangedEvent event) {\r
207             setSessionContext(event.getNewValue());\r
208         }\r
209     };\r
210 \r
211     @Override\r
212     public Control getControl() {\r
213         return (explorer != null) ? explorer : null;\r
214     }\r
215 \r
216     @Override\r
217     public ISelection getSelection() {\r
218 //        if (!explorer.isDisposed()) {\r
219 //            return explorer.getSelection();\r
220 //        }\r
221         return null;\r
222     }\r
223 \r
224     /**\r
225      * Sets focus to a part in the page.\r
226      * @see org.eclipse.ui.part.Page#setFocus()\r
227      */\r
228     @Override\r
229     public void setFocus() {\r
230 //        if (explorer != null && !explorer.isDisposed()) {\r
231 //              explorer.requestFocus();\r
232 //        }\r
233     }\r
234 \r
235     protected void sourcePartClosed(IWorkbenchPart part) {\r
236 //        if (!explorer.isDisposed()) {\r
237 //              explorer.setInput(StructuredSelection.EMPTY, false);\r
238 //        }\r
239     }\r
240 \r
241     protected void sourceSelectionChanged(ISelection selection) {\r
242         if (!explorer.isDisposed()) {\r
243                 explorer.setInput(selection, false);\r
244         }\r
245     }\r
246 \r
247     static class PartNameListener implements Listener<String> {\r
248         private boolean disposed = false;\r
249         private final Consumer<String> updateCallback;\r
250 \r
251         public PartNameListener(Consumer<String> updateCallback) {\r
252             assert updateCallback != null;\r
253             this.updateCallback = updateCallback;\r
254         }\r
255 \r
256         public void dispose() {\r
257             disposed = true;\r
258         }\r
259 \r
260         @Override\r
261         public boolean isDisposed() {\r
262             return disposed;\r
263         }\r
264 \r
265         @Override\r
266         public void execute(String result) {\r
267             //System.out.println("part name changed: " + result);\r
268             updateCallback.accept(result);\r
269         }\r
270 \r
271         @Override\r
272         public void exception(Throwable t) {\r
273             ErrorLogger.defaultLogError(t);\r
274         }\r
275     }\r
276 \r
277     PartNameListener currentPartNameListener = null;\r
278 \r
279     @Override\r
280     public void updatePartName(final ISelection forSelection, Consumer<String> updateCallback) {\r
281         PartNameListener oldListener = currentPartNameListener;\r
282         PartNameListener newListener = new PartNameListener(updateCallback);\r
283         if (oldListener != null)\r
284             oldListener.dispose();\r
285 \r
286         if (sessionContext != null) {\r
287             sessionContext.getSession().asyncRequest(new Read<String>() {\r
288                 @Override\r
289                 public String perform(ReadGraph graph) throws DatabaseException {\r
290                     return computeTitle(graph, forSelection);\r
291                 }\r
292             }, newListener);\r
293         }\r
294     }\r
295 \r
296     protected static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {\r
297         try {\r
298             return g.adapt(r, String.class);\r
299         } catch (AdaptionException e) {\r
300             return NameUtils.getSafeName(g, r);\r
301         }\r
302     }\r
303 \r
304     protected String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {\r
305         boolean sameTypes = true;\r
306 \r
307         try {\r
308 \r
309             final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);\r
310             if (ras.length == 0)\r
311                 return null;\r
312 \r
313             // Check if all the input resource are of the same type.\r
314             Collection<Resource> types = null;\r
315             for (ResourceArray ra : ras) {\r
316                 if (ra.isEmpty()) {\r
317                     return null;\r
318                 }\r
319                 if (types == null) {\r
320                     types = graph.getPrincipalTypes(ra.resources[0]);\r
321                 } else {\r
322                     Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);\r
323                     ts.removeAll(types);\r
324                     if (!ts.isEmpty()) {\r
325                         sameTypes = false;\r
326                         break;\r
327                     }\r
328                 }\r
329             }\r
330             if (sameTypes) {\r
331                 // If the resource no longer exists, provide default name only.\r
332                 if (!graph.hasStatement(ras[0].resources[0])) {\r
333                     return null;\r
334                 }\r
335 \r
336                 String name = safeGetName(graph, ras[0].resources[0]);\r
337                 if (ras.length > 1)\r
338                     name += " [" + ras.length +"]";\r
339                 return name;\r
340             } else {\r
341                 Collection<String> names = new ArrayList<String>(ras.length);\r
342                 boolean truncate = ras.length > MAX_SELECTION_LENGTH_TO_SHOW;\r
343                 int end = Math.min(ras.length, MAX_SELECTION_LENGTH_TO_SHOW);\r
344                 int missing = ras.length - end;\r
345                 for (int i = 0; i < end; ++i) {\r
346                     // If the resource no longer exists, provide default name only.\r
347                     if (!graph.hasStatement(ras[i].resources[0]))\r
348                         continue;\r
349 \r
350                     names.add(safeGetName(graph, ras[i].resources[0]));\r
351                 }\r
352                 if (names.isEmpty()) {\r
353                     return null;\r
354                 }\r
355 \r
356                 if (truncate)\r
357                     names.add("+ " + missing + " more...");\r
358 \r
359                 String name = names.toString();\r
360                 return name;\r
361             }\r
362 \r
363         } catch (Throwable t) {\r
364             t.printStackTrace();\r
365             return null;\r
366         }\r
367 \r
368     }\r
369 \r
370     /**\r
371      * Part listener which cleans up this page when the source part is closed.\r
372      * This is hooked only when there is a source part.\r
373      */\r
374     private class PartListener implements IPartListener {\r
375         public void partActivated(IWorkbenchPart part) {\r
376         }\r
377 \r
378         public void partBroughtToTop(IWorkbenchPart part) {\r
379         }\r
380 \r
381         public void partClosed(IWorkbenchPart part) {\r
382             if (sourcePart == part) {\r
383                 sourcePart = null;\r
384                 sourcePartClosed(part);\r
385             }\r
386         }\r
387 \r
388         public void partDeactivated(IWorkbenchPart part) {\r
389         }\r
390 \r
391         public void partOpened(IWorkbenchPart part) {\r
392         }\r
393     }\r
394 \r
395     private PartListener         partListener = new PartListener();\r
396 \r
397     protected IWorkbenchPartSite site;\r
398 \r
399     private IWorkbenchPart       sourcePart;\r
400 \r
401     protected IAdaptable         adapter;\r
402 \r
403     @Override\r
404     public void setAdapter(IAdaptable adapter) {\r
405         assert adapter != null;\r
406         this.adapter = adapter;\r
407     }\r
408 \r
409     public IAdaptable getAdapter() {\r
410         assert adapter != null;\r
411         return adapter;\r
412     }\r
413 \r
414     @Override\r
415     public void selectionChanged(IWorkbenchPart part, ISelection selection) {\r
416         if (getControl() == null) {\r
417             return;\r
418         }\r
419 \r
420         if (sourcePart != null) {\r
421             sourcePart.getSite().getPage().removePartListener(partListener);\r
422             sourcePart = null;\r
423         }\r
424 \r
425         // change the viewer input since the workbench selection has changed.\r
426         sourceSelectionChanged(selection);\r
427         sourcePart = part;\r
428 \r
429         if (sourcePart != null) {\r
430             sourcePart.getSite().getPage().addPartListener(partListener);\r
431         }\r
432     }\r
433     \r
434 }\r