--- /dev/null
+/*******************************************************************************\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.awt.geom.Point2D;\r
+import java.util.ArrayList;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.diagram.elements.ElementTransforms;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.diagram.DiagramMutator;\r
+import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;\r
+import org.simantics.g2d.diagram.participant.Selection;\r
+import org.simantics.g2d.element.ElementHints;\r
+import org.simantics.g2d.element.IElement;\r
+import org.simantics.g2d.element.handler.Rotate;\r
+import org.simantics.g2d.element.handler.Scale;\r
+import org.simantics.g2d.participant.MouseUtil;\r
+import org.simantics.g2d.participant.MouseUtil.MouseInfo;\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
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+\r
+/**\r
+ * Handles commands {@link Commands#ROTATE_ELEMENT_CCW},\r
+ * {@link Commands#ROTATE_ELEMENT_CW}, {@link Commands#FLIP_ELEMENT_HORIZONTAL},\r
+ * {@link Commands#FLIP_ELEMENT_VERTICAL} and {@link Commands#SCALE_ELEMENT} for\r
+ * rotating (in 90 degree steps), flipping and continuously scaling the current\r
+ * selection.\r
+ *\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SimpleElementTransformHandler extends AbstractDiagramParticipant {\r
+\r
+ @Dependency protected Selection selection;\r
+ @Dependency protected MouseUtil mouseUtil;\r
+\r
+ protected final boolean rotation;\r
+ protected final boolean flip;\r
+ protected final boolean scale;\r
+\r
+ public SimpleElementTransformHandler() {\r
+ this(true, true, false);\r
+ }\r
+\r
+ public SimpleElementTransformHandler(boolean enableRotation, boolean enableFlip, boolean enableScale) {\r
+ this.rotation = enableRotation;\r
+ this.flip = enableFlip;\r
+ this.scale = enableScale;\r
+ }\r
+\r
+ public boolean isRotateEnabled() {\r
+ return rotation;\r
+ }\r
+\r
+ public boolean isFlipEnabled() {\r
+ return flip;\r
+ }\r
+\r
+ public boolean isScaleEnabled() {\r
+ return scale;\r
+ }\r
+\r
+ @EventHandler(priority = 0)\r
+ public boolean handleCommand(CommandEvent ke) {\r
+ if (isRotateEnabled() && (ke.command.equals( Commands.ROTATE_ELEMENT_CCW ) || ke.command.equals( Commands.ROTATE_ELEMENT_CW ))) {\r
+ return rotate(ke.command.equals( Commands.ROTATE_ELEMENT_CW ));\r
+ } else if (isFlipEnabled() && (ke.command.equals( Commands.FLIP_ELEMENT_HORIZONTAL ) || ke.command.equals( Commands.FLIP_ELEMENT_VERTICAL))) {\r
+ return flip(ke.command.equals( Commands.FLIP_ELEMENT_VERTICAL ));\r
+ } else if (isScaleEnabled() && Commands.SCALE_ELEMENT.equals(ke.command)) {\r
+ return startSelectionMouseScale(0);\r
+ }\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * @param xAxis\r
+ */\r
+ private boolean flip(boolean xAxis) {\r
+// final double sx = ke.command.equals( Commands.FLIP_ELEMENT_HORIZONTAL ) ? -1 : 1;\r
+// final double sy = ke.command.equals( Commands.FLIP_ELEMENT_VERTICAL ) ? -1 : 1;\r
+// DiagramUtils.mutateDiagram(diagram, new Callback<DiagramMutator>() {\r
+// @Override\r
+// public void run(DiagramMutator mutator) {\r
+// scaleAllSelections(mutator, sx, sy);\r
+// }\r
+// });\r
+// DiagramUtils.validateAndFix(diagram, getContext());\r
+// setDirty();\r
+\r
+ Resource[] elements = getSelection();\r
+ if (elements.length == 0)\r
+ return false;\r
+\r
+ ElementTransforms.flip(elements, xAxis);\r
+ return true;\r
+ }\r
+\r
+ /**\r
+ * @param clockwise\r
+ */\r
+ private boolean rotate(boolean clockwise) {\r
+// final double rotation = ke.command.equals(Commands.ROTATE_ELEMENT_CCW) ? -90 : 90;\r
+// DiagramUtils.mutateDiagram(diagram, new Callback<DiagramMutator>() {\r
+// @Override\r
+// public void run(DiagramMutator mutator) {\r
+// rotateAllSelections(mutator, rotation);\r
+// }\r
+// });\r
+// DiagramUtils.validateAndFix(diagram, getContext());\r
+// setDirty();\r
+\r
+ Resource[] elements = getSelection();\r
+ if (elements.length == 0)\r
+ return false;\r
+\r
+ ElementTransforms.rotate(elements, clockwise);\r
+ return true;\r
+ }\r
+\r
+ protected Resource[] getSelection() {\r
+ Set<IElement> els = selection.getSelection(0);\r
+ final Set<Resource> resources = new HashSet<Resource>();\r
+ for (IElement el : els) {\r
+ Object o = el.getHint(ElementHints.KEY_OBJECT);\r
+ if (o instanceof Resource)\r
+ resources.add((Resource) o);\r
+ }\r
+ try {\r
+ return SimanticsUI.getSession().syncRequest(new Read<Resource[]>() {\r
+ @Override\r
+ public Resource[] perform(ReadGraph graph) throws DatabaseException {\r
+ List<Resource> result = getSelection(graph, resources);\r
+ return result.toArray(new Resource[result.size()]);\r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ ErrorLogger.defaultLogError(e);\r
+ return Resource.NONE;\r
+ }\r
+ }\r
+\r
+ protected List<Resource> getSelection(ReadGraph graph, Set<Resource> resources) throws DatabaseException {\r
+ DiagramResource dr = DiagramResource.getInstance(graph);\r
+ List<Resource> result = new ArrayList<Resource>();\r
+ for (Resource r : resources) {\r
+ if (graph.isInstanceOf(r, dr.Element) /*&& graph.isInstanceOf(r, dr.Monitor)*/)\r
+ result.add(r);\r
+ }\r
+ return result;\r
+ }\r
+\r
+ protected MouseScaleMode createMouseScaleMode(int mouseId, MouseInfo mi, Set<IElement> s) {\r
+ return new MouseScaleMode(mouseId, mi, s);\r
+ }\r
+ \r
+ boolean startSelectionMouseScale(int mouseId) {\r
+ Set<IElement> s = selection.getSelection(mouseId);\r
+ if (s.isEmpty())\r
+ return false;\r
+ // Prevent multiple mouse scale modes from being activated.\r
+ for (MouseScaleMode msm : getContext().getItemsByClass(MouseScaleMode.class))\r
+ if (msm.getMouseId() == mouseId)\r
+ return false;\r
+ MouseInfo mi = mouseUtil.getMouseInfo(mouseId);\r
+ getContext().add( createMouseScaleMode(mouseId, mi, s) );\r
+ return true;\r
+ }\r
+\r
+ void scaleAllSelections(DiagramMutator mutator, double xscale, double yscale) {\r
+ for (Set<IElement> s : selection.getSelections().values()) {\r
+ for (IElement e : s) {\r
+ Scale scale = e.getElementClass().getAtMostOneItemOfClass(Scale.class);\r
+ if (scale != null) {\r
+ mutator.modifyTransform(e);\r
+ Point2D sc = scale.getScale(e);\r
+ sc.setLocation(xscale*sc.getX(), yscale*sc.getY());\r
+ scale.setScale(e, sc);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ void rotateAllSelections(DiagramMutator mutator, double degrees) {\r
+ double angrad = Math.toRadians(degrees);\r
+ Point2D origin = new Point2D.Double(0, 0);\r
+ for (Set<IElement> s : selection.getSelections().values()) {\r
+ for (IElement e : s) {\r
+ Rotate rotate = e.getElementClass().getAtMostOneItemOfClass(Rotate.class);\r
+ if (rotate != null) {\r
+ mutator.modifyTransform(e);\r
+ rotate.rotate(e, angrad, origin);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+}\r