1 /*******************************************************************************
2 * Copyright (c) 2007, 2012 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.swt.widgets;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.ui.IWorkbenchSite;
24 import org.simantics.Simantics;
25 import org.simantics.browsing.ui.NodeContext;
26 import org.simantics.browsing.ui.common.ErrorLogger;
27 import org.simantics.browsing.ui.model.InvalidContribution;
28 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
29 import org.simantics.browsing.ui.model.dnd.DndBrowseContext;
30 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
31 import org.simantics.db.ReadGraph;
32 import org.simantics.db.Resource;
33 import org.simantics.db.common.request.ReadRequest;
34 import org.simantics.db.exception.DatabaseException;
35 import org.simantics.db.exception.ResourceNotFoundException;
36 import org.simantics.db.procedure.Procedure;
37 import org.simantics.db.request.Read;
38 import org.simantics.utils.ui.ExceptionUtils;
40 public class ModelBrowser extends GraphExplorerComposite {
42 volatile DndBrowseContext dndBrowseContext;
44 private static final String CONTEXT_MENU_ID = "#GraphExplorerPopup";
46 protected Set<String> propertyBrowseContexts = Collections.emptySet();
48 protected boolean hideComparatorSelector = false;
49 protected boolean hideViewpointSelector = false;
50 protected boolean hideFilter = false;
53 protected String getContextMenuId() {
54 if(contextMenuId != null) return contextMenuId;
55 else return CONTEXT_MENU_ID;
59 protected void createControls(Composite parent) {
60 this.propertyBrowseContexts = loadBrowseContexts(getBrowseContexts());
61 super.createControls(parent);
64 protected static Set<String> loadBrowseContexts(final Set<String> browseContexts) {
66 return Simantics.getSession().syncRequest(new Read<Set<String>>() {
68 public Set<String> perform(ReadGraph graph) throws DatabaseException {
69 Collection<Resource> browseContextResources = new ArrayList<>(browseContexts.size());
70 for (String browseContext : browseContexts) {
72 browseContextResources.add(graph.getResource(browseContext));
73 } catch (ResourceNotFoundException e) {
74 // Expected result, if no modelled contributions exist.
75 //System.err.println("Didn't find " + browseContext + " while loading model browser.");
76 } catch (DatabaseException e) {
77 ExceptionUtils.logError("Didn't find " + browseContext + " while loading model browser.", e);
80 Collection<Resource> allBrowseContextResources = BrowseContext.findSubcontexts(graph, browseContextResources);
81 Set<String> result = new HashSet<>();
82 for (Resource r : allBrowseContextResources)
83 result.add(graph.getURI(r));
87 } catch (DatabaseException e) {
88 ExceptionUtils.logAndShowError("Failed to load modeled browse contexts for property page, see exception for details.", e);
89 return browseContexts;
93 public ModelBrowser(final Set<String> _browseContexts, Map<String, Object> args, IWorkbenchSite site, Composite parent, WidgetSupport support, int style) {
95 super(args, site, parent, support, style);
97 setBrowseContexts(_browseContexts);
99 // As long as these are not useful, don't shown them by default.
100 this.hideComparatorSelector = true;
101 this.hideViewpointSelector = true;
103 Simantics.getSession().asyncRequest(new ReadRequest() {
105 public void run(ReadGraph graph) throws DatabaseException {
106 ArrayList<Resource> browseContexts = new ArrayList<>();
107 for (String uri : _browseContexts) {
108 Resource browseContext = graph.getPossibleResource(uri);
109 if (browseContext != null)
110 browseContexts.add(browseContext);
113 dndBrowseContext = DndBrowseContext.create(graph, browseContexts);
114 } catch (InvalidContribution e) {
115 ErrorLogger.defaultLogError(e);
122 protected void handleDrop(final Object data, final NodeContext target) {
126 Simantics.getSession().asyncRequest(new Read<Runnable>() {
128 public Runnable perform(ReadGraph graph) throws DatabaseException {
129 if (dndBrowseContext == null)
131 return dndBrowseContext.getAction(graph, target, data);
133 }, new Procedure<Runnable>() {
135 public void execute(Runnable result) {
136 Display.getDefault().asyncExec(() -> {
137 if (!Display.getCurrent().isDisposed() && result != null)
143 public void exception(Throwable t) {
144 ErrorLogger.defaultLogError(t);