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