1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.ui;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Optional;
17 import java.util.concurrent.CopyOnWriteArrayList;
18 import java.util.concurrent.TimeUnit;
20 import org.eclipse.jface.viewers.IPostSelectionProvider;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.ISelectionChangedListener;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.ui.IWorkbenchPartSite;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.diagram.elements.AdaptableImmutableProxyElement;
28 import org.simantics.g2d.canvas.ICanvasContext;
29 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
30 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
31 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;
32 import org.simantics.g2d.diagram.participant.ElementJSON;
33 import org.simantics.g2d.diagram.participant.Selection;
34 import org.simantics.g2d.element.ElementHints;
35 import org.simantics.g2d.element.IElement;
36 import org.simantics.ui.selection.WorkbenchSelectionUtils;
37 import org.simantics.utils.datastructures.hints.IHintContext.Key;
38 import org.simantics.utils.datastructures.hints.IHintObservable;
39 import org.simantics.utils.threads.IThreadWorkQueue;
40 import org.simantics.utils.threads.ThreadUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * A canvas participant that listens to the #0 mouse selection and provides it
46 * forward through the {@link IPostSelectionProvider} interface.
48 * @author Tuukka Lehtonen
50 public class WorkbenchSelectionProvider extends AbstractCanvasParticipant implements IPostSelectionProvider, ElementJSON {
52 private static final Logger LOGGER = LoggerFactory.getLogger(WorkbenchSelectionProvider.class);
54 private static final long POST_SELECTION_DELAY = 300;
56 @Dependency protected Selection selection;
58 protected IThreadWorkQueue swt;
59 protected IWorkbenchPartSite site;
60 protected ISelection currentSelection = StructuredSelection.EMPTY;
61 protected CopyOnWriteArrayList<ISelectionChangedListener> listeners = new CopyOnWriteArrayList<ISelectionChangedListener>();
62 protected CopyOnWriteArrayList<ISelectionChangedListener> postListeners = new CopyOnWriteArrayList<ISelectionChangedListener>();
64 public WorkbenchSelectionProvider(IThreadWorkQueue swt) {
68 public WorkbenchSelectionProvider(IThreadWorkQueue swt, IWorkbenchPartSite site) {
72 site.setSelectionProvider(this);
76 public void addedToContext(ICanvasContext ctx) {
77 super.addedToContext(ctx);
78 if (site != null && site.getSelectionProvider() != this) {
79 swt.asyncExec(new Runnable() {
82 if (site.getSelectionProvider() != WorkbenchSelectionProvider.this)
83 site.setSelectionProvider(WorkbenchSelectionProvider.this);
90 public void removedFromContext(ICanvasContext ctx) {
91 if (site != null && site.getSelectionProvider() == this) {
92 swt.asyncExec(new Runnable() {
95 if (site.getSelectionProvider() == WorkbenchSelectionProvider.this)
96 site.setSelectionProvider(null);
100 super.removedFromContext(ctx);
103 public void fireSelection(Iterable<?> selection) {
104 final ISelection s = constructAdaptableSelection(selection);
105 swt.asyncExec(new Runnable() {
108 currentSelection = s;
109 fireSelectionChanged(s);
110 schedulePostSelectionChanged(s);
115 @HintListener(Class = Selection.class, Field = "SELECTION0")
116 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
117 Iterable<?> selection = (Iterable<?>) newValue;
118 fireSelection(selection);
121 @HintListener(Class = Selection.class, Field = "SELECTION0")
122 public void hintRemoved(IHintObservable sender, Key key, Object oldValue) {
123 //System.out.println("selection removed: " + oldValue);
124 fireSelection(Collections.emptyList());
127 // Post selection changed scheduling helper.
128 private int modCount = 0;
130 protected void schedulePostSelectionChanged(final ISelection s) {
131 final int count = ++modCount;
132 ThreadUtils.getNonBlockingWorkExecutor().schedule(new Runnable() {
135 int newCount = modCount;
136 if (count != newCount)
140 swt.asyncExec(new Runnable() {
145 firePostSelectionChanged(s);
149 }, POST_SELECTION_DELAY, TimeUnit.MILLISECONDS);
152 protected ISelection constructAdaptableSelection(Iterable<?> selection) {
153 ArrayList<Object> objects = new ArrayList<Object>();
154 for (Object o : selection) {
155 if (o instanceof IElement) {
156 IElement e = (IElement) o;
157 Object object = e.getHint(ElementHints.KEY_OBJECT);
158 if (object != null) {
159 objects.add(new AdaptableImmutableProxyElement(e));
161 System.out.println(" discarding element from selection, null object for " + e);
164 System.out.println(" unrecognized selection: " + o.getClass() + ": " + o);
167 return new StructuredSelection(objects);
170 void fireSelectionChanged(ISelection selection) {
171 SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
172 for (ISelectionChangedListener l : listeners)
173 l.selectionChanged(e);
176 void firePostSelectionChanged(ISelection selection) {
177 SelectionChangedEvent e = new SelectionChangedEvent(this, selection);
178 for (ISelectionChangedListener l : postListeners)
179 l.selectionChanged(e);
183 public void addPostSelectionChangedListener(ISelectionChangedListener listener) {
184 postListeners.add(listener);
188 public void removePostSelectionChangedListener(ISelectionChangedListener listener) {
189 postListeners.remove(listener);
193 public void addSelectionChangedListener(ISelectionChangedListener listener) {
194 listeners.add(listener);
198 public void removeSelectionChangedListener(ISelectionChangedListener listener) {
199 listeners.remove(listener);
203 public ISelection getSelection() {
204 return currentSelection;
208 public void setSelection(ISelection selection) {
209 System.out.println("WorkbenchSelectionProvider: TODO: set selection: " + selection);
213 public Optional<String> getJSON(IElement element) {
214 ISelection sel = constructAdaptableSelection(Collections.singleton(element));
216 return Optional.ofNullable( WorkbenchSelectionUtils.getPossibleJSON(sel) );
217 } catch (DatabaseException e) {
218 LOGGER.error("Unexpected failure while constructing JSON from element " + element, e);
219 return Optional.empty();