]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/ModelBrowser.java
(refs #7358) Initial 4.7 update commit
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / ModelBrowser.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 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.swt.widgets;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
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;
39
40 public class ModelBrowser extends GraphExplorerComposite {
41
42     volatile DndBrowseContext dndBrowseContext;
43
44     private static final String CONTEXT_MENU_ID = "#GraphExplorerPopup";
45
46     protected Set<String>                                 propertyBrowseContexts = Collections.emptySet();
47
48     protected boolean                                     hideComparatorSelector = false;
49     protected boolean                                     hideViewpointSelector = false;
50     protected boolean                                     hideFilter = false;
51
52     @Override
53     protected String getContextMenuId() {
54         if(contextMenuId != null) return contextMenuId;
55         else return CONTEXT_MENU_ID;
56     }
57
58     @Override
59     protected void createControls(Composite parent) {
60         this.propertyBrowseContexts = loadBrowseContexts(getBrowseContexts());
61         super.createControls(parent);
62     }
63
64     protected static Set<String> loadBrowseContexts(final Set<String> browseContexts) {
65         try {
66             return Simantics.getSession().syncRequest(new Read<Set<String>>() {
67                 @Override
68                 public Set<String> perform(ReadGraph graph) throws DatabaseException {
69                     Collection<Resource> browseContextResources = new ArrayList<>(browseContexts.size());
70                     for (String browseContext : browseContexts) {
71                         try {
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);
78                         }
79                     }
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));
84                     return result;
85                 }
86             });
87         } catch (DatabaseException e) {
88             ExceptionUtils.logAndShowError("Failed to load modeled browse contexts for property page, see exception for details.", e);
89             return browseContexts;
90         }
91     }
92     
93     public ModelBrowser(final Set<String> _browseContexts, Map<String, Object> args, IWorkbenchSite site, Composite parent, WidgetSupport support, int style) {
94         
95         super(args, site, parent, support, style);
96     
97         setBrowseContexts(_browseContexts);
98         
99         // As long as these are not useful, don't shown them by default.
100         this.hideComparatorSelector = true;
101         this.hideViewpointSelector = true;
102
103         Simantics.getSession().asyncRequest(new ReadRequest() {
104             @Override
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);
111                 }
112                 try {
113                     dndBrowseContext = DndBrowseContext.create(graph, browseContexts);
114                 } catch (InvalidContribution e) {
115                     ErrorLogger.defaultLogError(e);
116                 }
117             }
118         });
119     }
120
121     @Override
122     protected void handleDrop(final Object data, final NodeContext target) {
123         if (target == null)
124             return;
125
126         Simantics.getSession().asyncRequest(new Read<Runnable>() {
127             @Override
128             public Runnable perform(ReadGraph graph) throws DatabaseException {
129                 if (dndBrowseContext == null)
130                     return null;
131                 return dndBrowseContext.getAction(graph, target, data);
132             }
133         }, new Procedure<Runnable>() {
134             @Override
135             public void execute(Runnable result) {
136                 Display.getDefault().asyncExec(() -> {
137                     if (!Display.getCurrent().isDisposed() && result != null)
138                         result.run();
139                 });
140             }
141
142             @Override
143             public void exception(Throwable t) {
144                 ErrorLogger.defaultLogError(t);
145             }
146         });
147     }
148
149 }