]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/ExpandSelectionHandler.java
Logger fixes after merge commit:fdbe8762
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / ExpandSelectionHandler.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.handler;\r
13 \r
14 import java.util.Set;\r
15 \r
16 import org.eclipse.jface.action.IStatusLineManager;\r
17 import org.eclipse.swt.widgets.Display;\r
18 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
19 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
20 import org.simantics.g2d.diagram.participant.Selection;\r
21 import org.simantics.g2d.element.IElement;\r
22 import org.simantics.g2d.utils.TopologicalSelectionExpander;\r
23 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
24 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
25 import org.simantics.scenegraph.g2d.events.command.Commands;\r
26 \r
27 /**\r
28  * This handler responds to the EXPAND_SELECTION command. It attempts to expand\r
29  * the current selection for pointer 0, meaning {@link Selection#SELECTION0}.\r
30  * \r
31  * @see TopologicalSelectionExpander\r
32  * @see Selection the current diagram selection source\r
33  * \r
34  * @author Tuukka Lehtonen\r
35  */\r
36 public class ExpandSelectionHandler extends AbstractDiagramParticipant {\r
37 \r
38         private final IStatusLineManager statusLine;\r
39         \r
40     @Dependency Selection selection;\r
41 \r
42     public ExpandSelectionHandler(IStatusLineManager statusLine) {\r
43         this.statusLine = statusLine;\r
44     }\r
45     \r
46     @EventHandler(priority = 0)\r
47     public boolean handleExpand(CommandEvent event) {\r
48         if (Commands.EXPAND_SELECTION.equals( event.command )) {\r
49             return expandSelection(0);\r
50         }\r
51         return false;\r
52     }\r
53 \r
54     /**\r
55      * @param selectionId the id of the selection to expand\r
56      * @return <code>true</code> if the selection changed as a result of the\r
57      *         expansion, <code>false</code> if not\r
58      */\r
59     public boolean expandSelection(int selectionId) {\r
60         Set<IElement> start = selection.getSelection(selectionId);\r
61         TopologicalSelectionExpander expander = new TopologicalSelectionExpander(diagram, start);\r
62         final Set<IElement> expanded = expander.expandedIfChanged();\r
63         if (expanded == null)\r
64             return false;\r
65         selection.setSelection(selectionId, expanded);\r
66         if(statusLine != null) {\r
67                 Display.getDefault().asyncExec(new Runnable() {\r
68 \r
69                                 @Override\r
70                                 public void run() {\r
71                                 statusLine.setMessage("Expanded selection to include " + expanded.size() + " (from " + diagram.getElements().size() + ") items.");\r
72                                 }\r
73                         \r
74                 });\r
75         }\r
76         return true;\r
77     }\r
78 \r
79 }\r