1 26 27 package org.nightlabs.editor2d.util; 28 29 import java.awt.geom.AffineTransform ; 30 import java.awt.geom.Point2D ; 31 import java.util.Comparator ; 32 import java.util.List ; 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 ("Param alignment = "+alignment+" is not valid!"); 54 } 55 56 public static void alignDrawComponents(List drawComponents, double distance, int alignment) 57 { 58 checkAlignment(alignment); 59 if (drawComponents != null) 60 { 61 DrawComponent firstDC = null; 62 DrawComponent lastDC = null; 63 Point2D firstPoint = null; 64 Point2D 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 (firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY()); 70 lastPoint = new Point2D.Double (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 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 (firstDC.getBounds().getMinX(), firstDC.getBounds().getMinY()); 83 lastPoint = new Point2D.Double (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 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 } 95 } 96 } 97 98 protected static double[] matrix = new double[6]; 99 public static AffineTransform checkAffineTransform(AffineTransform 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 (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 148 return degreesToRotate; 149 } 150 165 173 public static java.awt.Point getLeftTopCenterLocation(java.awt.Rectangle r1, java.awt.Rectangle 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 (diffX + diffWidth, diffY + diffHeight); 200 } 201 202 public static Comparator idComparator = new Comparator () 203 { 204 public int compare(Object arg0, Object 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 |