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