Logger fixes after merge commit:fdbe8762
[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.db.exception.DatabaseException;\r
27 import org.simantics.diagram.elements.AdaptableImmutableProxyElement;\r
28 import org.simantics.g2d.canvas.ICanvasContext;\r
29 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
30 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
31 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;\r
32 import org.simantics.g2d.diagram.participant.ElementJSON;\r
33 import org.simantics.g2d.diagram.participant.Selection;\r
34 import org.simantics.g2d.element.ElementHints;\r
35 import org.simantics.g2d.element.IElement;\r
36 import org.simantics.ui.selection.WorkbenchSelectionUtils;\r
37 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
38 import org.simantics.utils.datastructures.hints.IHintObservable;\r
39 import org.simantics.utils.threads.IThreadWorkQueue;\r
40 import org.simantics.utils.threads.ThreadUtils;\r
41 import org.slf4j.Logger;\r
42 import org.slf4j.LoggerFactory;\r
43 \r
44 /**\r
45  * A canvas participant that listens to the #0 mouse selection and provides it\r
46  * forward through the {@link IPostSelectionProvider} interface.\r
47  * \r
48  * @author Tuukka Lehtonen\r
49  */\r
50 public class WorkbenchSelectionProvider extends AbstractCanvasParticipant implements IPostSelectionProvider, ElementJSON {\r
51 \r
52     private static final Logger LOGGER = LoggerFactory.getLogger(WorkbenchSelectionProvider.class);\r
53 \r
54     private static final long                                 POST_SELECTION_DELAY = 300;\r
55 \r
56     @Dependency protected Selection selection;\r
57 \r
58     protected IThreadWorkQueue                                swt;\r
59     protected IWorkbenchPartSite                              site;\r
60     protected ISelection                                      currentSelection = StructuredSelection.EMPTY;\r
61     protected CopyOnWriteArrayList<ISelectionChangedListener> listeners        = new CopyOnWriteArrayList<ISelectionChangedListener>();\r
62     protected CopyOnWriteArrayList<ISelectionChangedListener> postListeners    = new CopyOnWriteArrayList<ISelectionChangedListener>();\r
63 \r
64     public WorkbenchSelectionProvider(IThreadWorkQueue swt) {\r
65         this(swt, null);\r
66     }\r
67 \r
68     public WorkbenchSelectionProvider(IThreadWorkQueue swt, IWorkbenchPartSite site) {\r
69         this.swt = swt;\r
70         this.site = site;\r
71         if (site != null)\r
72             site.setSelectionProvider(this);\r
73     }\r
74 \r
75     @Override\r
76     public void addedToContext(ICanvasContext ctx) {\r
77         super.addedToContext(ctx);\r
78         if (site != null && site.getSelectionProvider() != this) {\r
79             swt.asyncExec(new Runnable() {\r
80                 @Override\r
81                 public void run() {\r
82                     if (site.getSelectionProvider() != WorkbenchSelectionProvider.this)\r
83                         site.setSelectionProvider(WorkbenchSelectionProvider.this);\r
84                 }\r
85             });\r
86         }\r
87     }\r
88 \r
89     @Override\r
90     public void removedFromContext(ICanvasContext ctx) {\r
91         if (site != null && site.getSelectionProvider() == this) {\r
92             swt.asyncExec(new Runnable() {\r
93                 @Override\r
94                 public void run() {\r
95                     if (site.getSelectionProvider() == WorkbenchSelectionProvider.this)\r
96                         site.setSelectionProvider(null);\r
97                 }\r
98             });\r
99         }\r
100         super.removedFromContext(ctx);\r
101     }\r
102 \r
103     @HintListener(Class = Selection.class, Field = "SELECTION0")\r
104     public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
105         Iterable<?> selection = (Iterable<?>) newValue;\r
106         final ISelection s = constructAdaptableSelection(selection);\r
107         swt.asyncExec(new Runnable() {\r
108             @Override\r
109             public void run() {\r
110                 currentSelection = s;\r
111                 fireSelectionChanged(s);\r
112                 schedulePostSelectionChanged(s);\r
113             }\r
114         });\r
115     }\r
116 \r
117     @HintListener(Class = Selection.class, Field = "SELECTION0")\r
118     public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {\r
119         //System.out.println("selection removed: " + oldValue);\r
120         final ISelection s = constructAdaptableSelection(Collections.emptyList());\r
121         swt.asyncExec(new Runnable() {\r
122             @Override\r
123             public void run() {\r
124                 currentSelection = s;\r
125                 fireSelectionChanged(s);\r
126                 schedulePostSelectionChanged(s);\r
127             }\r
128         });\r
129     }\r
130 \r
131     // Post selection changed scheduling helper.\r
132     private int modCount = 0;\r
133 \r
134     protected void schedulePostSelectionChanged(final ISelection s) {\r
135         final int count = ++modCount;\r
136         ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {\r
137             @Override\r
138             public void run() {\r
139                 int newCount = modCount;\r
140                 if (count != newCount)\r
141                     return;\r
142                 if (isRemoved())\r
143                     return;\r
144                 swt.asyncExec(new Runnable() {\r
145                     @Override\r
146                     public void run() {\r
147                         if (isRemoved())\r
148                             return;\r
149                         firePostSelectionChanged(s);\r
150                     }\r
151                 });\r
152             }\r
153         }, POST_SELECTION_DELAY, TimeUnit.MILLISECONDS);\r
154     }\r
155 \r
156     protected ISelection constructAdaptableSelection(Iterable<?> selection) {\r
157         ArrayList<Object> objects = new ArrayList<Object>();\r
158         for (Object o : selection) {\r
159             if (o instanceof IElement) {\r
160                 IElement e = (IElement) o;\r
161                 Object object = e.getHint(ElementHints.KEY_OBJECT);\r
162                 if (object != null) {\r
163                     objects.add(new AdaptableImmutableProxyElement(e));\r
164                 } else {\r
165                     System.out.println("  discarding element from selection, null object for " + e);\r
166                 }\r
167             } else {\r
168                 System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);\r
169             }\r
170         }\r
171         return new StructuredSelection(objects);\r
172     }\r
173 \r
174     void fireSelectionChanged(ISelection selection) {\r
175         SelectionChangedEvent e = new SelectionChangedEvent(this, selection);\r
176         for (ISelectionChangedListener l : listeners)\r
177             l.selectionChanged(e);\r
178     }\r
179 \r
180     void firePostSelectionChanged(ISelection selection) {\r
181         SelectionChangedEvent e = new SelectionChangedEvent(this, selection);\r
182         for (ISelectionChangedListener l : postListeners)\r
183             l.selectionChanged(e);\r
184     }\r
185 \r
186     @Override\r
187     public void addPostSelectionChangedListener(ISelectionChangedListener listener) {\r
188         postListeners.add(listener);\r
189     }\r
190 \r
191     @Override\r
192     public void removePostSelectionChangedListener(ISelectionChangedListener listener) {\r
193         postListeners.remove(listener);\r
194     }\r
195 \r
196     @Override\r
197     public void addSelectionChangedListener(ISelectionChangedListener listener) {\r
198         listeners.add(listener);\r
199     }\r
200 \r
201     @Override\r
202     public void removeSelectionChangedListener(ISelectionChangedListener listener) {\r
203         listeners.remove(listener);\r
204     }\r
205 \r
206     @Override\r
207     public ISelection getSelection() {\r
208         return currentSelection;\r
209     }\r
210 \r
211     @Override\r
212     public void setSelection(ISelection selection) {\r
213         System.out.println("WorkbenchSelectionProvider: TODO: set selection: " + selection);\r
214     }\r
215 \r
216     @Override\r
217     public Optional<String> getJSON(IElement element) {\r
218         ISelection sel = constructAdaptableSelection(Collections.singleton(element));\r
219         try {\r
220             return Optional.ofNullable( WorkbenchSelectionUtils.getPossibleJSON(sel) );\r
221         } catch (DatabaseException e) {\r
222             LOGGER.error("Unexpected failure while constructing JSON from element " + element, e);\r
223             return Optional.empty();\r
224         }\r
225     }\r
226 \r
227 }\r