KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > command > RotateCommand


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * Project author: Daniel Mazurek <Daniel.Mazurek [at] nightlabs [dot] org> *
5  * *
6  * This library is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin St, Fifth Floor, *
20  * Boston, MA 02110-1301 USA *
21  * *
22  * Or get it online : *
23  * http://www.gnu.org/copyleft/lesser.html *
24  * *
25  * *
26  ******************************************************************************/

27
28 package org.nightlabs.editor2d.command;
29
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import org.apache.log4j.Logger;
35 import org.eclipse.draw2d.geometry.Point;
36 import org.eclipse.gef.EditPart;
37 import org.eclipse.gef.commands.Command;
38
39 import org.nightlabs.editor2d.DrawComponent;
40 import org.nightlabs.editor2d.EditorPlugin;
41 import org.nightlabs.editor2d.request.EditorRotateRequest;
42
43 public class RotateCommand
44 extends Command
45 {
46   public static final Logger LOGGER = Logger.getLogger(RotateCommand.class);
47   protected Map JavaDoc dc2Rotation;
48   protected Map JavaDoc dc2RotationCenter;
49   protected EditorRotateRequest request;
50   protected boolean multiple;
51   protected Point rotationCenter;
52   
53   public RotateCommand(EditorRotateRequest request)
54   {
55     super();
56     setLabel(EditorPlugin.getResourceString("command.rotate"));
57     this.request = request;
58     this.multiple = request.isMultiple();
59     this.rotationCenter = request.getRotationCenter();
60   }
61
62   public void execute()
63   {
64     dc2Rotation = new HashMap JavaDoc(request.getEditParts().size());
65     dc2RotationCenter = new HashMap JavaDoc(request.getEditParts().size());
66     for (Iterator JavaDoc it = request.getEditParts().iterator(); it.hasNext(); )
67     {
68       EditPart editPart = (EditPart) it.next();
69       DrawComponent dc = (DrawComponent) editPart.getModel();
70       dc2Rotation.put(dc, new Double JavaDoc(dc.getRotation()));
71       if (multiple) {
72         dc2RotationCenter.put(dc, new Point(dc.getTmpRotationX(), dc.getTmpRotationY()));
73       } else {
74         dc2RotationCenter.put(dc, new Point(dc.getRotationX(), dc.getRotationY()));
75       }
76       double realRotation = rotation + dc.getRotation();
77       if (multiple) {
78         dc.setTmpRotationX(rotationCenter.x);
79         dc.setTmpRotationY(rotationCenter.y);
80       } else {
81         dc.setRotationX(rotationCenter.x);
82         dc.setRotationY(rotationCenter.y);
83       }
84       dc.setRotation(realRotation);
85       dc.setTmpRotationX(DrawComponent.ROTATION_X_DEFAULT);
86       dc.setTmpRotationY(DrawComponent.ROTATION_Y_DEFAULT);
87     }
88   }
89   
90   public void redo()
91   {
92     for (Iterator JavaDoc it = dc2Rotation.keySet().iterator(); it.hasNext(); )
93     {
94       DrawComponent dc = (DrawComponent) it.next();
95       if (multiple) {
96         dc.setTmpRotationX(rotationCenter.x);
97         dc.setTmpRotationY(rotationCenter.y);
98       } else {
99         dc.setRotationX(rotationCenter.x);
100         dc.setRotationY(rotationCenter.y);
101       }
102       double realRotation = rotation + dc.getRotation();
103       dc.setRotation(realRotation);
104       dc.setTmpRotationX(DrawComponent.ROTATION_X_DEFAULT);
105       dc.setTmpRotationY(DrawComponent.ROTATION_Y_DEFAULT);
106     }
107   }
108
109   public void undo()
110   {
111     for (Iterator JavaDoc it = dc2Rotation.keySet().iterator(); it.hasNext(); )
112     {
113       DrawComponent dc = (DrawComponent) it.next();
114       double oldRotation = ((Double JavaDoc)dc2Rotation.get(dc)).doubleValue();
115       Point oldRotationCenter = (Point) dc2RotationCenter.get(dc);
116       if (multiple) {
117         dc.setTmpRotationX(rotationCenter.x);
118         dc.setTmpRotationY(rotationCenter.y);
119       } else {
120         dc.setRotationX(oldRotationCenter.x);
121         dc.setRotationY(oldRotationCenter.y);
122       }
123       dc.setRotation(oldRotation);
124       dc.setTmpRotationX(DrawComponent.ROTATION_X_DEFAULT);
125       dc.setTmpRotationY(DrawComponent.ROTATION_Y_DEFAULT);
126     }
127   }
128
129   protected double rotation;
130   public double getRotation() {
131     return rotation;
132   }
133   public void setRotation(double rotation) {
134     this.rotation = rotation;
135   }
136 }
137
Popular Tags