1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.platform;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
20 import java.util.function.Consumer;
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;
54 * Subclasses may extend or reimplement the following methods as required:
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>
70 * @author Tuukka Lehtonen
72 public class VariablesPage extends Page implements IPageBookViewPage, IVariablesPage {
74 protected static final int MAX_SELECTION_LENGTH_TO_SHOW = 5;
76 protected ISessionContext sessionContext;
78 protected GraphExplorerComposite explorer;
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.
85 public VariablesPage(IWorkbenchPartSite site) {
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>
96 public VariablesPage(IWorkbenchPartSite site, IAdaptable adapter) {
102 public void dispose() {
104 // Stop listening for title changes.
105 if (currentPartNameListener != null)
106 currentPartNameListener.dispose();
108 if (adapter != null) {
109 ISessionContextProvider contextProvider = getSessionContextProvider();
110 contextProvider.removeContextChangedListener(contextChangeListener);
113 if (sourcePart != null) {
114 sourcePart.getSite().getPage().removePartListener(partListener);
121 sessionContext = null;
125 protected ISessionContextProvider getSessionContextProvider() {
126 return (ISessionContextProvider) getAdapter().getAdapter(ISessionContextProvider.class);
129 protected ISessionContext getSessionContext() {
130 return sessionContext;
134 public final void createControl(Composite parent) {
135 createPageControls(parent);
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());
145 * Override to customize the UI component created on this page.
149 protected void createPageControls(Composite parent) {
151 Map<String, Object> args = new HashMap<String, Object>();
152 Set<String> browseContexts = new HashSet<String>();
154 browseContexts.add("org.simantics.browsing.ui.graph.variablesView");
156 args.put("browseContexts", browseContexts);
158 explorer = new GraphExplorerComposite(args, site, parent, SWT.NONE);
160 explorer.setInputSource(new InputSource() {
163 public Object get(ISessionContext ctx, Object selection) {
165 final Resource input = AdaptionUtils.adaptToSingle(selection, Resource.class);
167 return GraphExplorer.EMPTY_INPUT;
170 final VariablePrefix prefix = AdaptionUtils.adaptToSingle(selection, VariablePrefix.class);
172 return new VariablesInput(null, input);
174 return new VariablesInput(prefix.getPrefix(), input);
187 protected final void setSessionContext(ISessionContext newContext) {
188 ISessionContext oldContext = this.sessionContext;
189 this.sessionContext = newContext;
190 // System.out.println("AbstractPropertyPage.setSessionContext: " + oldContext + " -> " + newContext);
192 sessionContextChanged(oldContext, newContext);
199 protected void sessionContextChanged(ISessionContext oldContext, ISessionContext newContext) {
200 explorer.applySessionContext(newContext);
201 // explorer.setSessionContext(newContext);
204 protected ISessionContextChangedListener contextChangeListener = new ISessionContextChangedListener() {
206 public void sessionContextChanged(SessionContextChangedEvent event) {
207 setSessionContext(event.getNewValue());
212 public Control getControl() {
213 return (explorer != null) ? explorer : null;
217 public ISelection getSelection() {
218 // if (!explorer.isDisposed()) {
219 // return explorer.getSelection();
225 * Sets focus to a part in the page.
226 * @see org.eclipse.ui.part.Page#setFocus()
229 public void setFocus() {
230 // if (explorer != null && !explorer.isDisposed()) {
231 // explorer.requestFocus();
235 protected void sourcePartClosed(IWorkbenchPart part) {
236 // if (!explorer.isDisposed()) {
237 // explorer.setInput(StructuredSelection.EMPTY, false);
241 protected void sourceSelectionChanged(ISelection selection) {
242 if (!explorer.isDisposed()) {
243 explorer.setInput(selection, false);
247 static class PartNameListener implements Listener<String> {
248 private boolean disposed = false;
249 private final Consumer<String> updateCallback;
251 public PartNameListener(Consumer<String> updateCallback) {
252 assert updateCallback != null;
253 this.updateCallback = updateCallback;
256 public void dispose() {
261 public boolean isDisposed() {
266 public void execute(String result) {
267 //System.out.println("part name changed: " + result);
268 updateCallback.accept(result);
272 public void exception(Throwable t) {
273 ErrorLogger.defaultLogError(t);
277 PartNameListener currentPartNameListener = null;
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();
286 if (sessionContext != null) {
287 sessionContext.getSession().asyncRequest(new Read<String>() {
289 public String perform(ReadGraph graph) throws DatabaseException {
290 return computeTitle(graph, forSelection);
296 protected static String safeGetName(ReadGraph g, Resource r) throws DatabaseException {
298 return g.adapt(r, String.class);
299 } catch (AdaptionException e) {
300 return NameUtils.getSafeName(g, r);
304 protected String computeTitle(ReadGraph graph, ISelection selection) throws DatabaseException {
305 boolean sameTypes = true;
309 final ResourceArray[] ras = ResourceAdaptionUtils.toResourceArrays(selection);
313 // Check if all the input resource are of the same type.
314 Collection<Resource> types = null;
315 for (ResourceArray ra : ras) {
320 types = graph.getPrincipalTypes(ra.resources[0]);
322 Collection<Resource> ts = graph.getPrincipalTypes(ra.resources[0]);
331 // If the resource no longer exists, provide default name only.
332 if (!graph.hasStatement(ras[0].resources[0])) {
336 String name = safeGetName(graph, ras[0].resources[0]);
338 name += " [" + ras.length +"]";
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]))
350 names.add(safeGetName(graph, ras[i].resources[0]));
352 if (names.isEmpty()) {
357 names.add("+ " + missing + " more...");
359 String name = names.toString();
363 } catch (Throwable t) {
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.
374 private class PartListener implements IPartListener {
375 public void partActivated(IWorkbenchPart part) {
378 public void partBroughtToTop(IWorkbenchPart part) {
381 public void partClosed(IWorkbenchPart part) {
382 if (sourcePart == part) {
384 sourcePartClosed(part);
388 public void partDeactivated(IWorkbenchPart part) {
391 public void partOpened(IWorkbenchPart part) {
395 private PartListener partListener = new PartListener();
397 protected IWorkbenchPartSite site;
399 private IWorkbenchPart sourcePart;
401 protected IAdaptable adapter;
404 public void setAdapter(IAdaptable adapter) {
405 assert adapter != null;
406 this.adapter = adapter;
409 public IAdaptable getAdapter() {
410 assert adapter != null;
415 public void selectionChanged(IWorkbenchPart part, ISelection selection) {
416 if (getControl() == null) {
420 if (sourcePart != null) {
421 sourcePart.getSite().getPage().removePartListener(partListener);
425 // change the viewer input since the workbench selection has changed.
426 sourceSelectionChanged(selection);
429 if (sourcePart != null) {
430 sourcePart.getSite().getPage().addPartListener(partListener);