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.ui.IWorkbenchSite;
23 import org.simantics.browsing.ui.NodeContext;
24 import org.simantics.browsing.ui.common.ErrorLogger;
25 import org.simantics.browsing.ui.model.InvalidContribution;
26 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
27 import org.simantics.browsing.ui.model.dnd.DndBrowseContext;
28 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.common.request.ReadRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.exception.ResourceNotFoundException;
34 import org.simantics.db.procedure.Procedure;
35 import org.simantics.db.request.Read;
36 import org.simantics.ui.SimanticsUI;
37 import org.simantics.utils.ui.ExceptionUtils;
39 public class ModelBrowser extends GraphExplorerComposite {
41 volatile DndBrowseContext dndBrowseContext;
43 private static final String CONTEXT_MENU_ID = "#GraphExplorerPopup";
45 protected Set<String> propertyBrowseContexts = Collections.emptySet();
47 protected boolean hideComparatorSelector = false;
48 protected boolean hideViewpointSelector = false;
49 protected boolean hideFilter = false;
52 protected String getContextMenuId() {
53 if(contextMenuId != null) return contextMenuId;
54 else return CONTEXT_MENU_ID;
58 protected void createControls(Composite parent) {
59 this.propertyBrowseContexts = loadBrowseContexts(getBrowseContexts());
60 super.createControls(parent);
63 protected static Set<String> loadBrowseContexts(final Set<String> browseContexts) {
65 return SimanticsUI.getSession().syncRequest(new Read<Set<String>>() {
67 public Set<String> perform(ReadGraph graph) throws DatabaseException {
68 Collection<Resource> browseContextResources = new ArrayList<Resource>(browseContexts.size());
69 for (String browseContext : browseContexts) {
71 browseContextResources.add(graph.getResource(browseContext));
72 } catch (ResourceNotFoundException e) {
73 // Expected result, if no modelled contributions exist.
74 //System.err.println("Didn't find " + browseContext + " while loading model browser.");
75 } catch (DatabaseException e) {
76 ExceptionUtils.logError("Didn't find " + browseContext + " while loading model browser.", e);
79 Collection<Resource> allBrowseContextResources = BrowseContext.findSubcontexts(graph, browseContextResources);
80 Set<String> result = new HashSet<String>();
81 for (Resource r : allBrowseContextResources)
82 result.add(graph.getURI(r));
86 } catch (DatabaseException e) {
87 ExceptionUtils.logAndShowError("Failed to load modeled browse contexts for property page, see exception for details.", e);
88 return browseContexts;
92 public ModelBrowser(final Set<String> _browseContexts, Map<String, Object> args, IWorkbenchSite site, Composite parent, WidgetSupport support, int style) {
94 super(args, site, parent, support, style);
96 setBrowseContexts(_browseContexts);
98 // As long as these are not useful, don't shown them by default.
99 this.hideComparatorSelector = true;
100 this.hideViewpointSelector = true;
102 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
104 public void run(ReadGraph graph) throws DatabaseException {
105 ArrayList<Resource> browseContexts = new ArrayList<Resource>();
106 for (String uri : _browseContexts) {
107 Resource browseContext = graph.getPossibleResource(uri);
108 if (browseContext != null)
109 browseContexts.add(browseContext);
112 dndBrowseContext = DndBrowseContext.create(graph, browseContexts);
113 } catch (InvalidContribution e) {
114 ErrorLogger.defaultLogError(e);
121 protected void handleDrop(final Object data, final NodeContext target) {
125 SimanticsUI.getSession().asyncRequest(new Read<Runnable>() {
127 public Runnable perform(ReadGraph graph) throws DatabaseException {
128 if (dndBrowseContext == null)
130 return dndBrowseContext.getAction(graph, target, data);
132 }, new Procedure<Runnable>() {
134 public void execute(Runnable result) {
140 public void exception(Throwable t) {
141 ErrorLogger.defaultLogError(t);