KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * <p> Project: com.nightlabs.editor2d.model </p>
3  * <p> Copyright: Copyright (c) 2005 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 10.08.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.util;
9
10 import java.awt.geom.Point2D JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
13
14 import com.nightlabs.editor2d.DrawComponent;
15 import com.nightlabs.editor2d.PositionConstants;
16 import com.nightlabs.math.MathUtil;
17
18 public class EditorModelUtil
19 {
20     public EditorModelUtil() {
21         super();
22     }
23
24     public static boolean checkAlignment(int alignment)
25     {
26         if (alignment == PositionConstants.ALIGNMENT_LEFT ||
27                 alignment == PositionConstants.ALIGNMENT_CENTER ||
28                 alignment == PositionConstants.ALIGNMENT_RIGHT)
29         {
30             return true;
31         }
32         else
33             throw new IllegalArgumentException JavaDoc("Param alignment = "+alignment+" is not valid!");
34     }
35     
36     public static void alignDrawComponents(List JavaDoc drawComponents, double distance, int alignment)
37     {
38         checkAlignment(alignment);
39         if (drawComponents != null)
40         {
41             DrawComponent firstDC = null;
42             DrawComponent lastDC = null;
43             Point2D JavaDoc firstPoint = null;
44             Point2D JavaDoc lastPoint = null;
45             if (alignment == PositionConstants.ALIGNMENT_LEFT)
46             {
47                 firstDC = (DrawComponent) drawComponents.get(drawComponents.size()-1);
48                 lastDC = (DrawComponent) drawComponents.get(0);
49                 firstPoint = new Point2D.Double JavaDoc(firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY());
50                 lastPoint = new Point2D.Double JavaDoc(lastDC.getBounds().getMinX(), lastDC.getBounds().getMinY());
51 // for (int i=0; i<drawComponents.size(); i++)
52
for (int i=drawComponents.size()-1; i>=0; i--)
53                 {
54                     DrawComponent dc = (DrawComponent) drawComponents.get(i);
55                     Point2D JavaDoc p = MathUtil.getPointOnLineWithDistance(firstPoint, lastPoint, distance * i);
56 // Point2D p = MathUtil.getPointOnLineWithDistance(lastPoint, firstPoint, distance * i);
57
dc.setLocation((int)p.getX(), (int)p.getY());
58                 }
59             }
60             else if (alignment == PositionConstants.ALIGNMENT_RIGHT)
61             {
62 // firstDC = (DrawComponent) drawComponents.get(drawComponents.size()-1);
63
// lastDC = (DrawComponent) drawComponents.get(0);
64
firstDC = (DrawComponent) drawComponents.get(0);
65                 lastDC = (DrawComponent) drawComponents.get(drawComponents.size()-1);
66                 firstPoint = new Point2D.Double JavaDoc(firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY());
67                 lastPoint = new Point2D.Double JavaDoc(lastDC.getBounds().getMinX(), lastDC.getBounds().getMinY());
68 // for (int i=0; i<drawComponents.size(); i++)
69
for (int i=drawComponents.size()-1; i>=0; i--)
70                 {
71                     DrawComponent dc = (DrawComponent) drawComponents.get(i);
72                     Point2D JavaDoc p = MathUtil.getPointOnLineWithDistance(firstPoint, lastPoint, distance * i);
73 // Point2D p = MathUtil.getPointOnLineWithDistance(lastPoint, firstPoint, distance * i);
74
dc.setLocation((int)p.getX(), (int)p.getY());
75                 }
76             }
77             else if (alignment == PositionConstants.ALIGNMENT_CENTER)
78             {
79                 // TODO: implement this
80
}
81         }
82     }
83     
84   public static float checkFactor(float factor)
85   {
86     if (factor == Float.NEGATIVE_INFINITY || factor == Float.POSITIVE_INFINITY || factor == Float.NaN)
87       factor = 1;
88     
89     return factor;
90   }
91   
92   public static double getConstrainedValue(double value, double limit)
93   {
94     if (value > limit || value < -limit) {
95       return value%limit;
96     }
97     return value;
98   }
99   
100   public static double calcDiffRotation(double newRotation, double oldRotation)
101   {
102     double degreesToRotate = 0;
103     newRotation = getConstrainedValue(newRotation, 360.0d);
104         
105     if (newRotation == 0)
106         degreesToRotate = -oldRotation;
107     else if (newRotation <= oldRotation)
108         degreesToRotate = -(oldRotation - newRotation);
109     else
110         degreesToRotate = newRotation - oldRotation;
111     
112 // LOGGER.debug("oldRotation : "+oldRotation);
113
// LOGGER.debug("newRotation : "+newRotation);
114
// LOGGER.debug("degreeesToRotate : "+degreesToRotate);
115

116     return degreesToRotate;
117   }
118   
119   /**
120    * calcs the location for r2, so that it is placed in the middle of r1
121    *
122    * @param r1 The Rectangle which is the reference for the location of param r2
123    * @param r2 The Rectangle which should be placed in the middle of r1
124    * @return the Location of the second param r2, which is then located in the middle of
125    * bounds of param r2
126    */

127   public static java.awt.Point JavaDoc getLeftTopCenterLocation(java.awt.Rectangle JavaDoc r1, java.awt.Rectangle JavaDoc r2)
128   {
129     int diffWidth;
130     if (r1.width > r2.width)
131       diffWidth = (r1.width - r2.width) / 2;
132     else
133       diffWidth = (r2.width - r1.width) / 2;
134     
135     int diffHeight;
136     if (r1.height > r2.height)
137       diffHeight = (r1.height - r2.height) / 2;
138     else
139       diffHeight = (r2.height - r1.height) / 2;
140     
141     int diffX;
142     if (r1.x > r2.x)
143       diffX = r1.x - r2.x;
144     else
145       diffX = r2.x - r1.x;
146
147     int diffY;
148     if (r1.y > r2.y)
149       diffY = r1.y - r2.y;
150     else
151       diffY = r2.y - r1.y;
152       
153     return new java.awt.Point JavaDoc(diffX + diffWidth, diffY + diffHeight);
154   }
155 }
156
Popular Tags