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