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.handler;
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;
28 * This handler responds to the EXPAND_SELECTION command. It attempts to expand
29 * the current selection for pointer 0, meaning {@link Selection#SELECTION0}.
31 * @see TopologicalSelectionExpander
32 * @see Selection the current diagram selection source
34 * @author Tuukka Lehtonen
36 public class ExpandSelectionHandler extends AbstractDiagramParticipant {
38 private final IStatusLineManager statusLine;
40 @Dependency Selection selection;
42 public ExpandSelectionHandler(IStatusLineManager statusLine) {
43 this.statusLine = statusLine;
46 @EventHandler(priority = 0)
47 public boolean handleExpand(CommandEvent event) {
48 if (Commands.EXPAND_SELECTION.equals( event.command )) {
49 return expandSelection(0);
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
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();
65 selection.setSelection(selectionId, expanded);
66 if(statusLine != null) {
67 Display.getDefault().asyncExec(new Runnable() {
71 statusLine.setMessage("Expanded selection to include " + expanded.size() + " (from " + diagram.getElements().size() + ") items.");