]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/SimpleElementTransformHandler.java
6d55112c2847c9471408f778dc1e4a1670c128b4
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / SimpleElementTransformHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.handler;
13
14 import java.awt.geom.Point2D;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
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;
41
42 /**
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
47  * selection.
48  *
49  * @author Tuukka Lehtonen
50  */
51 public class SimpleElementTransformHandler extends AbstractDiagramParticipant {
52
53     @Dependency protected Selection selection;
54     @Dependency protected MouseUtil mouseUtil;
55
56     protected final boolean rotation;
57     protected final boolean flip;
58     protected final boolean scale;
59
60     public SimpleElementTransformHandler() {
61         this(true, true, false);
62     }
63
64     public SimpleElementTransformHandler(boolean enableRotation, boolean enableFlip, boolean enableScale) {
65         this.rotation = enableRotation;
66         this.flip = enableFlip;
67         this.scale = enableScale;
68     }
69
70     public boolean isRotateEnabled() {
71         return rotation;
72     }
73
74     public boolean isFlipEnabled() {
75         return flip;
76     }
77
78     public boolean isScaleEnabled() {
79         return scale;
80     }
81
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);
90         }
91         return false;
92     }
93
94     /**
95      * @param xAxis
96      */
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>() {
101 //          @Override
102 //          public void run(DiagramMutator mutator) {
103 //              scaleAllSelections(mutator, sx, sy);
104 //          }
105 //      });
106 //      DiagramUtils.validateAndFix(diagram, getContext());
107 //      setDirty();
108
109         Resource[] elements = getSelection();
110         if (elements.length == 0)
111             return false;
112
113         ElementTransforms.flip(elements, xAxis);
114         return true;
115     }
116
117     /**
118      * @param clockwise
119      */
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>() {
123 //          @Override
124 //          public void run(DiagramMutator mutator) {
125 //              rotateAllSelections(mutator, rotation);
126 //          }
127 //      });
128 //      DiagramUtils.validateAndFix(diagram, getContext());
129 //      setDirty();
130
131         Resource[] elements = getSelection();
132         if (elements.length == 0)
133             return false;
134
135         ElementTransforms.rotate(elements, clockwise);
136         return true;
137     }
138
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);
146         }
147         try {
148             return SimanticsUI.getSession().syncRequest(new Read<Resource[]>() {
149                 @Override
150                 public Resource[] perform(ReadGraph graph) throws DatabaseException {
151                     List<Resource> result = getSelection(graph, resources);
152                     return result.toArray(new Resource[result.size()]);
153                 }
154             });
155         } catch (DatabaseException e) {
156             ErrorLogger.defaultLogError(e);
157             return Resource.NONE;
158         }
159     }
160
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)*/)
166                 result.add(r);
167         }
168         return result;
169     }
170
171     protected MouseScaleMode createMouseScaleMode(int mouseId, MouseInfo mi, Set<IElement> s) {
172         return new MouseScaleMode(mouseId, mi, s);
173     }
174     
175     boolean startSelectionMouseScale(int mouseId) {
176         Set<IElement> s = selection.getSelection(mouseId);
177         if (s.isEmpty())
178             return false;
179         // Prevent multiple mouse scale modes from being activated.
180         for (MouseScaleMode msm : getContext().getItemsByClass(MouseScaleMode.class))
181             if (msm.getMouseId() == mouseId)
182                 return false;
183         MouseInfo mi = mouseUtil.getMouseInfo(mouseId);
184         getContext().add( createMouseScaleMode(mouseId, mi, s) );
185         return true;
186     }
187
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);
192                 if (scale != null) {
193                     mutator.modifyTransform(e);
194                     Point2D sc = scale.getScale(e);
195                     sc.setLocation(xscale*sc.getX(), yscale*sc.getY());
196                     scale.setScale(e, sc);
197                 }
198             }
199         }
200     }
201
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);
211                 }
212             }
213         }
214     }
215
216 }