1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.handler;
14 import java.awt.geom.Point2D;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.request.Read;
24 import org.simantics.diagram.elements.ElementTransforms;
25 import org.simantics.diagram.stubs.DiagramResource;
26 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
27 import org.simantics.g2d.diagram.DiagramMutator;
28 import org.simantics.g2d.diagram.participant.AbstractDiagramParticipant;
29 import org.simantics.g2d.diagram.participant.Selection;
30 import org.simantics.g2d.element.ElementHints;
31 import org.simantics.g2d.element.IElement;
32 import org.simantics.g2d.element.handler.Rotate;
33 import org.simantics.g2d.element.handler.Scale;
34 import org.simantics.g2d.participant.MouseUtil;
35 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
36 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
37 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
38 import org.simantics.scenegraph.g2d.events.command.Commands;
39 import org.simantics.ui.SimanticsUI;
40 import org.simantics.utils.ui.ErrorLogger;
43 * Handles commands {@link Commands#ROTATE_ELEMENT_CCW},
44 * {@link Commands#ROTATE_ELEMENT_CW}, {@link Commands#FLIP_ELEMENT_HORIZONTAL},
45 * {@link Commands#FLIP_ELEMENT_VERTICAL} and {@link Commands#SCALE_ELEMENT} for
46 * rotating (in 90 degree steps), flipping and continuously scaling the current
49 * @author Tuukka Lehtonen
51 public class SimpleElementTransformHandler extends AbstractDiagramParticipant {
53 @Dependency protected Selection selection;
54 @Dependency protected MouseUtil mouseUtil;
56 protected final boolean rotation;
57 protected final boolean flip;
58 protected final boolean scale;
60 public SimpleElementTransformHandler() {
61 this(true, true, false);
64 public SimpleElementTransformHandler(boolean enableRotation, boolean enableFlip, boolean enableScale) {
65 this.rotation = enableRotation;
66 this.flip = enableFlip;
67 this.scale = enableScale;
70 public boolean isRotateEnabled() {
74 public boolean isFlipEnabled() {
78 public boolean isScaleEnabled() {
82 @EventHandler(priority = 0)
83 public boolean handleCommand(CommandEvent ke) {
84 if (isRotateEnabled() && (ke.command.equals( Commands.ROTATE_ELEMENT_CCW ) || ke.command.equals( Commands.ROTATE_ELEMENT_CW ))) {
85 return rotate(ke.command.equals( Commands.ROTATE_ELEMENT_CW ));
86 } else if (isFlipEnabled() && (ke.command.equals( Commands.FLIP_ELEMENT_HORIZONTAL ) || ke.command.equals( Commands.FLIP_ELEMENT_VERTICAL))) {
87 return flip(ke.command.equals( Commands.FLIP_ELEMENT_VERTICAL ));
88 } else if (isScaleEnabled() && Commands.SCALE_ELEMENT.equals(ke.command)) {
89 return startSelectionMouseScale(0);
97 private boolean flip(boolean xAxis) {
98 // final double sx = ke.command.equals( Commands.FLIP_ELEMENT_HORIZONTAL ) ? -1 : 1;
99 // final double sy = ke.command.equals( Commands.FLIP_ELEMENT_VERTICAL ) ? -1 : 1;
100 // DiagramUtils.mutateDiagram(diagram, new Callback<DiagramMutator>() {
102 // public void run(DiagramMutator mutator) {
103 // scaleAllSelections(mutator, sx, sy);
106 // DiagramUtils.validateAndFix(diagram, getContext());
109 Resource[] elements = getSelection();
110 if (elements.length == 0)
113 ElementTransforms.flip(elements, xAxis);
120 private boolean rotate(boolean clockwise) {
121 // final double rotation = ke.command.equals(Commands.ROTATE_ELEMENT_CCW) ? -90 : 90;
122 // DiagramUtils.mutateDiagram(diagram, new Callback<DiagramMutator>() {
124 // public void run(DiagramMutator mutator) {
125 // rotateAllSelections(mutator, rotation);
128 // DiagramUtils.validateAndFix(diagram, getContext());
131 Resource[] elements = getSelection();
132 if (elements.length == 0)
135 ElementTransforms.rotate(elements, clockwise);
139 protected Resource[] getSelection() {
140 Set<IElement> els = selection.getSelection(0);
141 final Set<Resource> resources = new HashSet<Resource>();
142 for (IElement el : els) {
143 Object o = el.getHint(ElementHints.KEY_OBJECT);
144 if (o instanceof Resource)
145 resources.add((Resource) o);
148 return SimanticsUI.getSession().syncRequest(new Read<Resource[]>() {
150 public Resource[] perform(ReadGraph graph) throws DatabaseException {
151 List<Resource> result = getSelection(graph, resources);
152 return result.toArray(new Resource[result.size()]);
155 } catch (DatabaseException e) {
156 ErrorLogger.defaultLogError(e);
157 return Resource.NONE;
161 protected List<Resource> getSelection(ReadGraph graph, Set<Resource> resources) throws DatabaseException {
162 DiagramResource dr = DiagramResource.getInstance(graph);
163 List<Resource> result = new ArrayList<Resource>();
164 for (Resource r : resources) {
165 if (graph.isInstanceOf(r, dr.Element) /*&& graph.isInstanceOf(r, dr.Monitor)*/)
171 protected MouseScaleMode createMouseScaleMode(int mouseId, MouseInfo mi, Set<IElement> s) {
172 return new MouseScaleMode(mouseId, mi, s);
175 boolean startSelectionMouseScale(int mouseId) {
176 Set<IElement> s = selection.getSelection(mouseId);
179 // Prevent multiple mouse scale modes from being activated.
180 for (MouseScaleMode msm : getContext().getItemsByClass(MouseScaleMode.class))
181 if (msm.getMouseId() == mouseId)
183 MouseInfo mi = mouseUtil.getMouseInfo(mouseId);
184 getContext().add( createMouseScaleMode(mouseId, mi, s) );
188 void scaleAllSelections(DiagramMutator mutator, double xscale, double yscale) {
189 for (Set<IElement> s : selection.getSelections().values()) {
190 for (IElement e : s) {
191 Scale scale = e.getElementClass().getAtMostOneItemOfClass(Scale.class);
193 mutator.modifyTransform(e);
194 Point2D sc = scale.getScale(e);
195 sc.setLocation(xscale*sc.getX(), yscale*sc.getY());
196 scale.setScale(e, sc);
202 void rotateAllSelections(DiagramMutator mutator, double degrees) {
203 double angrad = Math.toRadians(degrees);
204 Point2D origin = new Point2D.Double(0, 0);
205 for (Set<IElement> s : selection.getSelections().values()) {
206 for (IElement e : s) {
207 Rotate rotate = e.getElementClass().getAtMostOneItemOfClass(Rotate.class);
208 if (rotate != null) {
209 mutator.modifyTransform(e);
210 rotate.rotate(e, angrad, origin);