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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.handler;
\r
14 import java.util.Set;
\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
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
31 * @see TopologicalSelectionExpander
\r
32 * @see Selection the current diagram selection source
\r
34 * @author Tuukka Lehtonen
\r
36 public class ExpandSelectionHandler extends AbstractDiagramParticipant {
\r
38 private final IStatusLineManager statusLine;
\r
40 @Dependency Selection selection;
\r
42 public ExpandSelectionHandler(IStatusLineManager statusLine) {
\r
43 this.statusLine = statusLine;
\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
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
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
65 selection.setSelection(selectionId, expanded);
\r
66 if(statusLine != null) {
\r
67 Display.getDefault().asyncExec(new Runnable() {
\r
71 statusLine.setMessage("Expanded selection to include " + expanded.size() + " (from " + diagram.getElements().size() + ") items.");
\r