]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/ExpandSelectionHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / ExpandSelectionHandler.java
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 (file)
index 0000000..28a5dc1
--- /dev/null
@@ -0,0 +1,79 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.handler;\r
+\r
+import java.util.Set;\r
+\r
+import org.eclipse.jface.action.IStatusLineManager;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.utils.TopologicalSelectionExpander;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
+import org.simantics.scenegraph.g2d.events.command.Commands;\r
+\r
+/**\r
+ * This handler responds to the EXPAND_SELECTION command. It attempts to expand\r
+ * the current selection for pointer 0, meaning {@link Selection#SELECTION0}.\r
+ * \r
+ * @see TopologicalSelectionExpander\r
+ * @see Selection the current diagram selection source\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ExpandSelectionHandler extends AbstractDiagramParticipant {\r
+\r
+       private final IStatusLineManager statusLine;\r
+       \r
+    @Dependency Selection selection;\r
+\r
+    public ExpandSelectionHandler(IStatusLineManager statusLine) {\r
+       this.statusLine = statusLine;\r
+    }\r
+    \r
+    @EventHandler(priority = 0)\r
+    public boolean handleExpand(CommandEvent event) {\r
+        if (Commands.EXPAND_SELECTION.equals( event.command )) {\r
+            return expandSelection(0);\r
+        }\r
+        return false;\r
+    }\r
+\r
+    /**\r
+     * @param selectionId the id of the selection to expand\r
+     * @return <code>true</code> if the selection changed as a result of the\r
+     *         expansion, <code>false</code> if not\r
+     */\r
+    public boolean expandSelection(int selectionId) {\r
+        Set<IElement> start = selection.getSelection(selectionId);\r
+        TopologicalSelectionExpander expander = new TopologicalSelectionExpander(diagram, start);\r
+        final Set<IElement> expanded = expander.expandedIfChanged();\r
+        if (expanded == null)\r
+            return false;\r
+        selection.setSelection(selectionId, expanded);\r
+        if(statusLine != null) {\r
+               Display.getDefault().asyncExec(new Runnable() {\r
+\r
+                               @Override\r
+                               public void run() {\r
+                               statusLine.setMessage("Expanded selection to include " + expanded.size() + " (from " + diagram.getElements().size() + ") items.");\r
+                               }\r
+                       \r
+               });\r
+        }\r
+        return true;\r
+    }\r
+\r
+}\r