KickJava   Java API By Example, From Geeks To Geeks.

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


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.eclipse.draw2d.geometry.Point;
35 import org.eclipse.gef.EditPart;
36 import org.eclipse.gef.commands.Command;
37
38 import org.nightlabs.editor2d.DrawComponent;
39 import org.nightlabs.editor2d.EditorPlugin;
40 import org.nightlabs.editor2d.request.EditorRotateCenterRequest;
41 import org.nightlabs.editor2d.util.EditorUtil;
42
43
44 public class RotateCenterCommand
45 extends Command
46 {
47   protected Map JavaDoc dc2RotationCenter;
48   protected EditorRotateCenterRequest request;
49   
50   protected Point rotationCenter;
51   public Point getRotationCenter() {
52     return rotationCenter;
53   }
54   public void setRotationCenter(Point rotationCenter) {
55     this.rotationCenter = rotationCenter;
56   }
57   
58   protected boolean multiple = false;
59   public boolean isMultiple() {
60     return multiple;
61   }
62   public void setMultiple(boolean multiple) {
63     this.multiple = multiple;
64   }
65   
66   public RotateCenterCommand(EditorRotateCenterRequest request)
67   {
68     super();
69     this.request = request;
70     this.multiple = request.isMultiple();
71     setLabel(EditorPlugin.getResourceString("command.rotate.center"));
72   }
73
74   public void execute()
75   {
76     dc2RotationCenter = new HashMap JavaDoc(request.getEditParts().size());
77     for (Iterator JavaDoc it = request.getEditParts().iterator(); it.hasNext(); )
78     {
79       EditPart editPart = (EditPart) it.next();
80       DrawComponent dc = (DrawComponent) editPart.getModel();
81       Point oldRotationCenter = new Point(dc.getRotationX(), dc.getRotationY());
82       dc2RotationCenter.put(dc, oldRotationCenter);
83       if (isMultiple()) {
84         dc.setTmpRotationX(rotationCenter.x);
85         dc.setTmpRotationY(rotationCenter.y);
86       } else {
87         dc.setRotationX(rotationCenter.x);
88         dc.setRotationY(rotationCenter.y);
89       }
90     }
91     EditorUtil.selectEditParts(request.getEditParts());
92   }
93   
94   public void redo()
95   {
96     for (Iterator JavaDoc it = dc2RotationCenter.keySet().iterator(); it.hasNext(); )
97     {
98       DrawComponent dc = (DrawComponent) it.next();
99       if (isMultiple()) {
100         dc.setTmpRotationX(rotationCenter.x);
101         dc.setTmpRotationY(rotationCenter.y);
102       } else {
103         dc.setRotationX(rotationCenter.x);
104         dc.setRotationY(rotationCenter.y);
105       }
106     }
107     EditorUtil.selectEditParts(request.getEditParts());
108   }
109   
110   public void undo()
111   {
112     for (Iterator JavaDoc it = dc2RotationCenter.keySet().iterator(); it.hasNext(); )
113     {
114       DrawComponent dc = (DrawComponent) it.next();
115       Point oldRotationCenter = (Point) dc2RotationCenter.get(dc);
116       if (isMultiple()) {
117         dc.setTmpRotationX(DrawComponent.ROTATION_X_DEFAULT);
118         dc.setTmpRotationY(DrawComponent.ROTATION_Y_DEFAULT);
119       } else {
120         dc.setRotationX(oldRotationCenter.x);
121         dc.setRotationY(oldRotationCenter.y);
122       }
123     }
124     EditorUtil.selectEditParts(request.getEditParts());
125   }
126 }
127
Popular Tags