1 8 package com.nightlabs.editor2d.command; 9 10 import java.awt.geom.PathIterator ; 11 12 import org.eclipse.draw2d.geometry.Point; 13 import org.eclipse.gef.commands.Command; 14 15 import com.nightlabs.editor2d.EditorPlugin; 16 import com.nightlabs.editor2d.ShapeDrawComponent; 17 import com.nightlabs.editor2d.j2d.GeneralShape; 18 19 20 public class EditShapeCommand 21 extends Command 22 { 23 protected GeneralShape oldGeneralShape; 24 protected GeneralShape generalShape; 25 26 protected int pathSegmentIndex; 27 public void setPathSegmentIndex(int pathSegmentIndex) { 28 this.pathSegmentIndex = pathSegmentIndex; 29 } 30 31 protected Point location; 32 public void setLocation(Point location) { 33 this.location = location; 34 } 35 36 37 protected ShapeDrawComponent shape; 38 public void setShapeDrawComponent(ShapeDrawComponent sdc) { 39 shape = sdc; 40 } 41 42 45 public EditShapeCommand() 46 { 47 super(); 48 setLabel(EditorPlugin.getResourceString("command_edit_shape")); 49 } 50 51 54 public EditShapeCommand(String label) 55 { 56 super(label); 57 } 58 59 public void execute() 60 { 61 oldGeneralShape = (GeneralShape) shape.getGeneralShape().clone(); 62 70 generalShape = new GeneralShape(); 71 float[] coords = new float[6]; 72 int index = 0; 73 boolean indexSet = false; 74 for (PathIterator pi = oldGeneralShape.getPathIterator(null); !pi.isDone(); pi.next()) 75 { 76 if (index == pathSegmentIndex) 77 { 78 if (pathSegmentIndex == PathIterator.SEG_MOVETO) 79 generalShape.moveTo(location.x, location.y); 80 else 81 generalShape.lineTo(location.x, location.y); 82 83 index = -1; 84 indexSet = true; 85 continue; 86 } 87 int segType = pi.currentSegment(coords); 88 switch (segType) 89 { 90 case (PathIterator.SEG_MOVETO): 91 generalShape.moveTo(coords[0], coords[1]); 92 if (!indexSet) 93 index++; 94 break; 95 case (PathIterator.SEG_LINETO): 96 generalShape.lineTo(coords[0], coords[1]); 97 if (!indexSet) 98 index++; 99 break; 100 case (PathIterator.SEG_QUADTO): 101 generalShape.quadTo(coords[0], coords[1], coords[2], coords[3]); 102 if (!indexSet) 103 index++; 104 break; 105 case (PathIterator.SEG_CUBICTO): 106 generalShape.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); 107 if (!indexSet) 108 index++; 109 break; 110 case (PathIterator.SEG_CLOSE): 111 generalShape.closePath(); 112 if (!indexSet) 113 index++; 114 break; 115 } 116 } 117 shape.setGeneralShape(generalShape); 118 } 119 120 public void undo() 121 { 122 shape.setGeneralShape(oldGeneralShape); 123 } 124 125 public void redo() 126 { 127 shape.setGeneralShape(generalShape); 128 } 129 } 130 | Popular Tags |