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