1 27 28 package org.nightlabs.editor2d.command; 29 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.Map ; 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 dc2Rotation; 48 protected Map 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 (request.getEditParts().size()); 65 dc2RotationCenter = new HashMap (request.getEditParts().size()); 66 for (Iterator 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 (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 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 it = dc2Rotation.keySet().iterator(); it.hasNext(); ) 112 { 113 DrawComponent dc = (DrawComponent) it.next(); 114 double oldRotation = ((Double )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 |