]> gerrit.simantics Code Review - simantics/platform.git/blob
8fbd0494e17b53ee7982418baa852d14044a6bb3
[simantics/platform.git] /
1 package org.simantics.document.linking.ge;
2
3 import java.util.ArrayList;
4 import java.util.Map;
5
6 import org.eclipse.jface.viewers.ISelectionProvider;
7 import org.eclipse.swt.widgets.Composite;
8 import org.eclipse.ui.IWorkbenchSite;
9 import org.simantics.browsing.ui.NodeContext;
10 import org.simantics.browsing.ui.common.ErrorLogger;
11 import org.simantics.browsing.ui.model.InvalidContribution;
12 import org.simantics.browsing.ui.model.dnd.DndBrowseContext;
13 import org.simantics.browsing.ui.swt.DefaultExplorerSelectionListener;
14 import org.simantics.browsing.ui.swt.widgets.GraphExplorerComposite;
15 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.procedure.Procedure;
21 import org.simantics.db.request.Read;
22 import org.simantics.ui.SimanticsUI;
23
24 public class SourceLinkExplorerComposite extends GraphExplorerComposite {
25         
26         volatile DndBrowseContext dndBrowseContext;
27         private IWorkbenchSite site;
28         private MultiSelectionProvider provider;
29         
30     public SourceLinkExplorerComposite(Map<String, Object> args, MultiSelectionProvider provider, IWorkbenchSite site, Composite parent,  WidgetSupport support, int style) {
31         super(args, site, parent, support, style);
32         this.site = site;
33         this.provider = provider;
34         attachDND();
35         
36     }
37     
38     public SourceLinkExplorerComposite(Map<String, Object> args, MultiSelectionProvider provider, IWorkbenchSite site, Composite parent,  WidgetSupport support, boolean useDND, int style) {
39         super(args, site, parent, support, style);
40         this.site = site;
41         this.provider = provider;
42         if (useDND)
43                 attachDND();
44     }
45     
46     private void attachDND() {
47         SimanticsUI.getSession().asyncRequest(new ReadRequest() {
48             @Override
49             public void run(ReadGraph graph) throws DatabaseException { 
50                 ArrayList<Resource> browseContexts = new ArrayList<Resource>();
51                 for (String uri : getBrowseContexts()) {
52                     Resource browseContext = graph.getPossibleResource(uri);
53                     if (browseContext != null)
54                         browseContexts.add(browseContext);
55                 }
56                 try {
57                     dndBrowseContext = DndBrowseContext.create(graph, browseContexts);
58                 } catch (InvalidContribution e) {
59                     ErrorLogger.defaultLogError(e);
60                 }
61             }
62         });
63     }
64     
65     @Override
66     protected void handleDrop(final Object data, final NodeContext target) {
67         if (target == null)
68             return;
69
70         SimanticsUI.getSession().asyncRequest(new Read<Runnable>() {
71             @Override
72             public Runnable perform(ReadGraph graph) throws DatabaseException {
73                 if (dndBrowseContext == null)
74                     return null;
75                 return dndBrowseContext.getAction(graph, target, data);
76             }
77         }, new Procedure<Runnable>() {
78             @Override
79             public void execute(Runnable result) {
80                 if (result != null)
81                     result.run();
82             }
83
84             @Override
85             public void exception(Throwable t) {
86                 ErrorLogger.defaultLogError(t);
87             }
88         });
89     }
90     
91     public void setWorkbenchListeners() {
92         if (workbenchSelectionListener == null && site != null) {
93             ISelectionProvider selectionProvider = (ISelectionProvider) explorer.getAdapter(ISelectionProvider.class);
94             //site.setSelectionProvider(selectionProvider);
95             provider.addSelectionProvider(selectionProvider);
96
97             // Listen to the workbench selection also to propagate it to
98             // the explorer also.
99             workbenchSelectionListener = new DefaultExplorerSelectionListener(site.getPage().getActivePart(), explorer);
100             //System.out.println("ADD WORKBENCH SELECTION LISTENER: " + workbenchSelectionListener);
101             site.getWorkbenchWindow().getSelectionService().addPostSelectionListener(workbenchSelectionListener);
102         }
103     }
104         
105
106 }