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