KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > util > EditorModelUtil


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.util;
28
29 import java.awt.geom.AffineTransform JavaDoc;
30 import java.awt.geom.Point2D JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.nightlabs.editor2d.DrawComponent;
35 import org.nightlabs.editor2d.PositionConstants;
36 import org.nightlabs.math.MathUtil;
37
38 public class EditorModelUtil
39 {
40     public EditorModelUtil() {
41         super();
42     }
43
44     public static boolean checkAlignment(int alignment)
45     {
46         if (alignment == PositionConstants.ALIGNMENT_LEFT ||
47                 alignment == PositionConstants.ALIGNMENT_CENTER ||
48                 alignment == PositionConstants.ALIGNMENT_RIGHT)
49         {
50             return true;
51         }
52         else
53             throw new IllegalArgumentException JavaDoc("Param alignment = "+alignment+" is not valid!");
54     }
55     
56     public static void alignDrawComponents(List JavaDoc drawComponents, double distance, int alignment)
57     {
58         checkAlignment(alignment);
59         if (drawComponents != null)
60         {
61             DrawComponent firstDC = null;
62             DrawComponent lastDC = null;
63             Point2D JavaDoc firstPoint = null;
64             Point2D JavaDoc lastPoint = null;
65             if (alignment == PositionConstants.ALIGNMENT_LEFT)
66             {
67                 firstDC = (DrawComponent) drawComponents.get(drawComponents.size()-1);
68                 lastDC = (DrawComponent) drawComponents.get(0);
69                 firstPoint = new Point2D.Double JavaDoc(firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY());
70                 lastPoint = new Point2D.Double JavaDoc(lastDC.getBounds().getMinX(), lastDC.getBounds().getMinY());
71                 for (int i=drawComponents.size()-1; i>=0; i--)
72                 {
73                     DrawComponent dc = (DrawComponent) drawComponents.get(i);
74                     Point2D JavaDoc p = MathUtil.getPointOnLineWithDistance(firstPoint, lastPoint, distance * i);
75                     dc.setLocation((int)p.getX(), (int)p.getY());
76                 }
77             }
78             else if (alignment == PositionConstants.ALIGNMENT_RIGHT)
79             {
80                 firstDC = (DrawComponent) drawComponents.get(0);
81                 lastDC = (DrawComponent) drawComponents.get(drawComponents.size()-1);
82                 firstPoint = new Point2D.Double JavaDoc(firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY());
83                 lastPoint = new Point2D.Double JavaDoc(lastDC.getBounds().getMinX(), lastDC.getBounds().getMinY());
84                 for (int i=drawComponents.size()-1; i>=0; i--)
85                 {
86                     DrawComponent dc = (DrawComponent) drawComponents.get(i);
87                     Point2D JavaDoc p = MathUtil.getPointOnLineWithDistance(firstPoint, lastPoint, distance * i);
88                     dc.setLocation((int)p.getX(), (int)p.getY());
89                 }
90             }
91             else if (alignment == PositionConstants.ALIGNMENT_CENTER)
92             {
93                 // TODO: implement this
94
}
95         }
96     }
97     
98     protected static double[] matrix = new double[6];
99     public static AffineTransform JavaDoc checkAffineTransform(AffineTransform JavaDoc at)
100     {
101         at.getMatrix(matrix);
102         for (int i=0; i<matrix.length; i++) {
103             matrix[i] = checkFactor(matrix[i]);
104         }
105         return new AffineTransform JavaDoc(matrix);
106     }
107     
108   public static double checkFactor(double factor)
109   {
110     if (Double.isInfinite(factor) || Double.isNaN(factor))
111       factor = 1d;
112     
113     return factor;
114   }
115     
116   public static float checkFactor(float factor)
117   {
118     if (Float.isInfinite(factor) || Float.isNaN(factor))
119       factor = 1f;
120     
121     return factor;
122   }
123   
124   public static double getConstrainedValue(double value, double limit)
125   {
126     if (value > limit || value < -limit) {
127       return value%limit;
128     }
129     return value;
130   }
131   
132   public static double calcDiffRotation(double newRotation, double oldRotation)
133   {
134     double degreesToRotate = 0;
135     newRotation = getConstrainedValue(newRotation, 360.0d);
136         
137     if (newRotation == 0)
138         degreesToRotate = -oldRotation;
139     else if (newRotation <= oldRotation)
140         degreesToRotate = -(oldRotation - newRotation);
141     else
142         degreesToRotate = newRotation - oldRotation;
143     
144 // LOGGER.debug("oldRotation : "+oldRotation);
145
// LOGGER.debug("newRotation : "+newRotation);
146
// LOGGER.debug("degreeesToRotate : "+degreesToRotate);
147

148     return degreesToRotate;
149   }
150 // public static double calcDiffRotation(double newRotation, double oldRotation)
151
// {
152
// double degreesToRotate = 0;
153
// newRotation = getConstrainedValue(newRotation, 360.0d);
154
//
155
// if (newRotation == 0)
156
// degreesToRotate = oldRotation;
157
// else if (newRotation <= oldRotation)
158
// degreesToRotate = (oldRotation - newRotation);
159
// else
160
// degreesToRotate = -(newRotation - oldRotation);
161
//
162
// return degreesToRotate;
163
// }
164

165   /**
166    * calcs the location for r2, so that it is placed in the middle of r1
167    *
168    * @param r1 The Rectangle which is the reference for the location of param r2
169    * @param r2 The Rectangle which should be placed in the middle of r1
170    * @return the Location of the second param r2, which is then located in the middle of
171    * bounds of param r2
172    */

173   public static java.awt.Point JavaDoc getLeftTopCenterLocation(java.awt.Rectangle JavaDoc r1, java.awt.Rectangle JavaDoc r2)
174   {
175     int diffWidth;
176     if (r1.width > r2.width)
177       diffWidth = (r1.width - r2.width) / 2;
178     else
179       diffWidth = (r2.width - r1.width) / 2;
180     
181     int diffHeight;
182     if (r1.height > r2.height)
183       diffHeight = (r1.height - r2.height) / 2;
184     else
185       diffHeight = (r2.height - r1.height) / 2;
186     
187     int diffX;
188     if (r1.x > r2.x)
189       diffX = r1.x - r2.x;
190     else
191       diffX = r2.x - r1.x;
192
193     int diffY;
194     if (r1.y > r2.y)
195       diffY = r1.y - r2.y;
196     else
197       diffY = r2.y - r1.y;
198       
199     return new java.awt.Point JavaDoc(diffX + diffWidth, diffY + diffHeight);
200   }
201   
202   public static Comparator JavaDoc idComparator = new Comparator JavaDoc()
203   {
204         public int compare(Object JavaDoc arg0, Object JavaDoc arg1)
205         {
206             if (arg0 instanceof DrawComponent && arg1 instanceof DrawComponent)
207             {
208                 DrawComponent dc1 = (DrawComponent) arg0;
209                 DrawComponent dc2 = (DrawComponent) arg1;
210                 if (dc1.getId() < dc2.getId())
211                     return -1;
212                 if (dc1.getId() > dc2.getId())
213                     return 1;
214             }
215             return 0;
216         }
217     };
218 }
219
Popular Tags