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