KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.geom.PathIterator JavaDoc;
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     /** The ShapeDrawComponent to edit */
57     protected ShapeDrawComponent shape;
58     public void setShapeDrawComponent(ShapeDrawComponent sdc) {
59       shape = sdc;
60     }
61       
62   /**
63    *
64    */

65   public EditShapeCommand()
66   {
67     super();
68     setLabel(EditorPlugin.getResourceString("command.edit.shape"));
69   }
70
71   /**
72    * @param label
73    */

74   public EditShapeCommand(String JavaDoc label)
75   {
76     super(label);
77   }
78     
79   public void execute()
80   {
81     oldGeneralShape = (GeneralShape) shape.getGeneralShape().clone();
82 // generalShape = (GeneralShape) shape.getGeneralShape();
83
// float[] coords = new float[] {location.x, location.y};
84
// if (pathSegmentIndex == 0) {
85
// generalShape.setPathSegment(PathIterator.SEG_MOVETO, pathSegmentIndex, coords);
86
// } else {
87
// generalShape.setPathSegment(PathIterator.SEG_LINETO, pathSegmentIndex, coords);
88
// }
89

90     generalShape = new GeneralShape();
91     float[] coords = new float[6];
92     int index = 0;
93     boolean indexSet = false;
94     for (PathIterator JavaDoc 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