]> gerrit.simantics Code Review - simantics/platform.git/blob - diagramEditor/handlers/WorkbenchStructuralSelectionProvider.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / diagramEditor / handlers / WorkbenchStructuralSelectionProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.modeling.ui.diagramEditor.handlers;
13
14 import java.util.ArrayList;
15
16 import org.eclipse.jface.viewers.IPostSelectionProvider;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.ui.IWorkbenchPartSite;
20 import org.simantics.db.Resource;
21 import org.simantics.db.common.ResourceArray;
22 import org.simantics.diagram.ui.WorkbenchSelectionProvider;
23 import org.simantics.g2d.element.ElementHints;
24 import org.simantics.g2d.element.IElement;
25 import org.simantics.utils.threads.IThreadWorkQueue;
26
27 /**
28  * A canvas participant that listens to the #0 mouse selection and provides it
29  * forward through the {@link IPostSelectionProvider} interface.
30  * 
31  * @author Tuukka Lehtonen
32  */
33 public class WorkbenchStructuralSelectionProvider extends WorkbenchSelectionProvider {
34
35     ResourceArray                                   suffix;
36
37     public WorkbenchStructuralSelectionProvider(IThreadWorkQueue swt, ResourceArray appendToSelections) {
38         this(swt, null, appendToSelections);
39     }
40
41     public WorkbenchStructuralSelectionProvider(IThreadWorkQueue swt, IWorkbenchPartSite site, ResourceArray appendToSelections) {
42         super(swt, site);
43         this.suffix = appendToSelections;
44     }
45
46     protected ISelection constructAdaptableSelection(Iterable<?> selection) {
47         ArrayList<Object> objects = new ArrayList<Object>();
48         for (Object o : selection) {
49             if (o instanceof IElement) {
50                 IElement e = (IElement) o;
51                 Object object = e.getHint(ElementHints.KEY_OBJECT);
52                 if (object instanceof Resource) {
53                     objects.add(suffix.prepended((Resource) object));
54                 } else {
55                     objects.add(object);
56                     objects.add(suffix);
57                 }
58             } else {
59                 System.out.println("  unrecognized selection: " + o.getClass() + ": " + o);
60             }
61         }
62         return new StructuredSelection(objects);
63     }
64
65 }