X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fhandler%2FExpandSelectionHandler.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fhandler%2FExpandSelectionHandler.java;h=28a5dc14f225cb21b2e582aea3afd543630d8830;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/ExpandSelectionHandler.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/ExpandSelectionHandler.java new file mode 100644 index 000000000..28a5dc14f --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/ExpandSelectionHandler.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.handler; + +import java.util.Set; + +import org.eclipse.jface.action.IStatusLineManager; +import org.eclipse.swt.widgets.Display; +import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency; +import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant; +import org.simantics.g2d.diagram.participant.Selection; +import org.simantics.g2d.element.IElement; +import org.simantics.g2d.utils.TopologicalSelectionExpander; +import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; +import org.simantics.scenegraph.g2d.events.command.CommandEvent; +import org.simantics.scenegraph.g2d.events.command.Commands; + +/** + * This handler responds to the EXPAND_SELECTION command. It attempts to expand + * the current selection for pointer 0, meaning {@link Selection#SELECTION0}. + * + * @see TopologicalSelectionExpander + * @see Selection the current diagram selection source + * + * @author Tuukka Lehtonen + */ +public class ExpandSelectionHandler extends AbstractDiagramParticipant { + + private final IStatusLineManager statusLine; + + @Dependency Selection selection; + + public ExpandSelectionHandler(IStatusLineManager statusLine) { + this.statusLine = statusLine; + } + + @EventHandler(priority = 0) + public boolean handleExpand(CommandEvent event) { + if (Commands.EXPAND_SELECTION.equals( event.command )) { + return expandSelection(0); + } + return false; + } + + /** + * @param selectionId the id of the selection to expand + * @return true if the selection changed as a result of the + * expansion, false if not + */ + public boolean expandSelection(int selectionId) { + Set start = selection.getSelection(selectionId); + TopologicalSelectionExpander expander = new TopologicalSelectionExpander(diagram, start); + final Set expanded = expander.expandedIfChanged(); + if (expanded == null) + return false; + selection.setSelection(selectionId, expanded); + if(statusLine != null) { + Display.getDefault().asyncExec(new Runnable() { + + @Override + public void run() { + statusLine.setMessage("Expanded selection to include " + expanded.size() + " (from " + diagram.getElements().size() + ") items."); + } + + }); + } + return true; + } + +}