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