1 27 28 package org.nightlabs.editor2d.command; 29 30 import java.awt.geom.PathIterator ; 31 32 import org.eclipse.draw2d.geometry.Point; 33 import org.eclipse.gef.commands.Command; 34 35 import org.nightlabs.editor2d.EditorPlugin; 36 import org.nightlabs.editor2d.ShapeDrawComponent; 37 import org.nightlabs.editor2d.j2d.GeneralShape; 38 39 40 public class EditShapeCommand 41 extends Command 42 { 43 protected GeneralShape oldGeneralShape; 44 protected GeneralShape generalShape; 45 46 protected int pathSegmentIndex; 47 public void setPathSegmentIndex(int pathSegmentIndex) { 48 this.pathSegmentIndex = pathSegmentIndex; 49 } 50 51 protected Point location; 52 public void setLocation(Point location) { 53 this.location = location; 54 } 55 56 57 protected ShapeDrawComponent shape; 58 public void setShapeDrawComponent(ShapeDrawComponent sdc) { 59 shape = sdc; 60 } 61 62 65 public EditShapeCommand() 66 { 67 super(); 68 setLabel(EditorPlugin.getResourceString("command.edit.shape")); 69 } 70 71 74 public EditShapeCommand(String label) 75 { 76 super(label); 77 } 78 79 public void execute() 80 { 81 oldGeneralShape = (GeneralShape) shape.getGeneralShape().clone(); 82 90 generalShape = new GeneralShape(); 91 float[] coords = new float[6]; 92 int index = 0; 93 boolean indexSet = false; 94 for (PathIterator pi = oldGeneralShape.getPathIterator(null); !pi.isDone(); pi.next()) 95 { 96 if (index == pathSegmentIndex) 97 { 98 if (pathSegmentIndex == PathIterator.SEG_MOVETO) 99 generalShape.moveTo(location.x, location.y); 100 else 101 generalShape.lineTo(location.x, location.y); 102 103 index = -1; 104 indexSet = true; 105 continue; 106 } 107 int segType = pi.currentSegment(coords); 108 switch (segType) 109 { 110 case (PathIterator.SEG_MOVETO): 111 generalShape.moveTo(coords[0], coords[1]); 112 if (!indexSet) 113 index++; 114 break; 115 case (PathIterator.SEG_LINETO): 116 generalShape.lineTo(coords[0], coords[1]); 117 if (!indexSet) 118 index++; 119 break; 120 case (PathIterator.SEG_QUADTO): 121 generalShape.quadTo(coords[0], coords[1], coords[2], coords[3]); 122 if (!indexSet) 123 index++; 124 break; 125 case (PathIterator.SEG_CUBICTO): 126 generalShape.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]); 127 if (!indexSet) 128 index++; 129 break; 130 case (PathIterator.SEG_CLOSE): 131 generalShape.closePath(); 132 if (!indexSet) 133 index++; 134 break; 135 } 136 } 137 shape.setGeneralShape(generalShape); 138 } 139 140 public void undo() 141 { 142 shape.setGeneralShape(oldGeneralShape); 143 } 144 145 public void redo() 146 { 147 shape.setGeneralShape(generalShape); 148 } 149 } 150 | Popular Tags |