Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / ui / WorkbenchSelectionProvider.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.ui;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collections;\r
16 import java.util.Optional;\r
17 import java.util.concurrent.CopyOnWriteArrayList;\r
18 import java.util.concurrent.TimeUnit;\r
19 \r
20 import org.eclipse.jface.viewers.IPostSelectionProvider;\r
21 import org.eclipse.jface.viewers.ISelection;\r
22 import org.eclipse.jface.viewers.ISelectionChangedListener;\r
23 import org.eclipse.jface.viewers.SelectionChangedEvent;\r
24 import org.eclipse.jface.viewers.StructuredSelection;\r
25 import org.eclipse.ui.IWorkbenchPartSite;\r
26 import org.simantics.Logger;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.diagram.elements.AdaptableImmutableProxyElement;\r
29 import org.simantics.g2d.canvas.ICanvasContext;\r
30 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
31 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
32 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;\r
33 import org.simantics.g2d.diagram.participant.ElementJSON;\r
34 import org.simantics.g2d.diagram.participant.Selection;\r
35 import org.simantics.g2d.element.ElementHints;\r
36 import org.simantics.g2d.element.IElement;\r
37 import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
38 import org.simantics.utils.datastructures.hints.IHintObservable;\r
39 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
40 import org.simantics.utils.threads.IThreadWorkQueue;\r
41 import org.simantics.utils.threads.ThreadUtils;\r
42 \r
43 /**\r
44  * A canvas participant that listens to the #0 mouse selection and provides it\r
45  * forward through the {@link IPostSelectionProvider} interface.\r
46  * \r
47  * @author Tuukka Lehtonen\r
48  */\r
49 public class WorkbenchSelectionProvider extends AbstractCanvasParticipant implements IPostSelectionProvider, ElementJSON {\r
50 \r
51     private static final long                                 POST_SELECTION_DELAY = 300;\r
52 \r
53     @Dependency protected Selection selection;\r
54 \r
55     protected IThreadWorkQueue                                swt;\r
56     protected IWorkbenchPartSite                              site;\r
57     protected ISelection                                      currentSelection = StructuredSelection.EMPTY;\r
58     protected CopyOnWriteArrayList<ISelectionChangedListener> listeners        = new CopyOnWriteArrayList<ISelectionChangedListener>();\r
59     protected CopyOnWriteArrayList<ISelectionChangedListener> postListeners    = new CopyOnWriteArrayList<ISelectionChangedListener>();\r
60 \r
61     public WorkbenchSelectionProvider(IThreadWorkQueue swt) {\r
62         this(swt, null);\r
63     }\r
64 \r
65     public WorkbenchSelectionProvider(IThreadWorkQueue swt, IWorkbenchPartSite site) {\r
66         this.swt = swt;\r
67         this.site = site;\r
68         if (site != null)\r
69             site.setSelectionProvider(this);\r
70     }\r
71 \r
72     @Override\r
73     public void addedToContext(ICanvasContext ctx) {\r
74         super.addedToContext(ctx);\r
75         if (site != null && site.getSelectionProvider() != this) {\r
76             swt.asyncExec(new Runnable() {\r
77                 @Override\r
78                 public void run() {\r
79                     if (site.getSelectionProvider() != WorkbenchSelectionProvider.this)\r
80                         site.setSelectionProvider(WorkbenchSelectionProvider.this);\r
81                 }\r
82             });\r
83         }\r
84     }\r
85 \r
86     @Override\r
87     public void removedFromContext(ICanvasContext ctx) {\r
88         if (site != null && site.getSelectionProvider() == this) {\r
89             swt.asyncExec(new Runnable() {\r
90                 @Override\r
91                 public void run() {\r
92                     if (site.getSelectionProvider() == WorkbenchSelectionProvider.this)\r
93                         site.setSelectionProvider(null);\r
94                 }\r
95             });\r
96         }\r
97         super.removedFromContext(ctx);\r
98     }\r
99 \r
100     @HintListener(Class = Selection.class, Field = "SELECTION0")\r
101     public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
102         Iterable<?> selection = (Iterable<?>) newValue;\r
103         final ISelection s = constructAdaptableSelection(selection);\r
104         swt.asyncExec(new Runnable() {\r
105             @Override\r
106             public void run() {\r
107                 currentSelection = s;\r
108                 fireSelectionChanged(s);\r
109                 schedulePostSelectionChanged(s);\r
110             }\r
111         });\r
112     }\r
113 \r
114     @HintListener(Class = Selection.class, Field = "SELECTION0")\r
115     public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
116         //System.out.println("selection removed: " + oldValue);\r
117         final ISelection s = constructAdaptableSelection(Collections.emptyList());\r
118         swt.asyncExec(new Runnable() {\r
119             @Override\r
120             public void run() {\r
121                 currentSelection = s;\r
122                 fireSelectionChanged(s);\r
123                 schedulePostSelectionChanged(s);\r
124             }\r
125         });\r
126     }\r
127 \r
128     // Post selection changed scheduling helper.\r
129     private int modCount = 0;\r
130 \r
131     protected void schedulePostSelectionChanged(final ISelection s) {\r
132         final int count = ++modCount;\r
133         ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {\r
134             @Override\r
135             public void run() {\r
136                 int newCount = modCount;\r
137                 if (count != newCount)\r
138                     return;\r
139                 if (isRemoved())\r
140                     return;\r
141                 swt.asyncExec(new Runnable() {\r
142                     @Override\r
143                     public void run() {\r
144                         if (isRemoved())\r
145                             return;\r
146                         firePostSelectionChanged(s);\r
147                     }\r
148                 });\r
149             }\r
150         }, POST_SELECTION_DELAY, TimeUnit.MILLISECONDS);\r
151     }\r
152 \r
153     protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
154         ArrayList<Object> objects = new ArrayList<Object>();\r
155         for (Object o : selection) {\r
156             if (o instanceof IElement) {\r
157                 IElement e = (IElement) o;\r
158                 Object object = e.getHint(ElementHints.KEY_OBJECT);\r
159                 if (object != null) {\r
160                     objects.add(new AdaptableImmutableProxyElement(e));\r
161                 } else {\r
162                     System.out.println("  discarding element from selection, null object for " + e);\r
163                 }\r
164             } else {\r
165                 System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
166             }\r
167         }\r
168         return new StructuredSelection(objects);\r
169     }\r
170 \r
171     void fireSelectionChanged(ISelection selection) {\r
172         SelectionChangedEvent e = new SelectionChangedEvent(this, selection);\r
173         for (ISelectionChangedListener l : listeners)\r
174             l.selectionChanged(e);\r
175     }\r
176 \r
177     void firePostSelectionChanged(ISelection selection) {\r
178         SelectionChangedEvent e = new SelectionChangedEvent(this, selection);\r
179         for (ISelectionChangedListener l : postListeners)\r
180             l.selectionChanged(e);\r
181     }\r
182 \r
183     @Override\r
184     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {\r
185         postListeners.add(listener);\r
186     }\r
187 \r
188     @Override\r
189     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {\r
190         postListeners.remove(listener);\r
191     }\r
192 \r
193     @Override\r
194     public void addSelectionChangedListener(ISelectionChangedListener listener) {\r
195         listeners.add(listener);\r
196     }\r
197 \r
198     @Override\r
199     public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
200         listeners.remove(listener);\r
201     }\r
202 \r
203     @Override\r
204     public ISelection getSelection() {\r
205         return currentSelection;\r
206     }\r
207 \r
208     @Override\r
209     public void setSelection(ISelection selection) {\r
210         System.out.println("WorkbenchSelectionProvider: TODO: set selection: " + selection);\r
211     }\r
212 \r
213     @Override\r
214     public Optional<String> getJSON(IElement element) {\r
215         ISelection sel = constructAdaptableSelection(Collections.singleton(element));\r
216         try {\r
217             return Optional.ofNullable( WorkbenchSelectionUtils.getPossibleJSON(sel) );\r
218         } catch (DatabaseException e) {\r
219             Logger.defaultLogError(e);\r
220             return Optional.empty();\r
221         }\r
222     }\r
223 \r
224 }\r