1 16 package swingwtx.swing; 17 18 public class SizeRequirements { 19 20 public int minimum; 21 public int preferred; 22 public int maximum; 23 public float alignment; 24 25 public SizeRequirements() { 26 minimum = 0; 27 preferred = 0; 28 maximum = 0; 29 alignment = (float) .5; 30 } 31 32 public SizeRequirements(int min, int pref, int max, float a) { 33 minimum = min; 34 preferred = pref; 35 maximum = max; 36 if (a > 1) 37 alignment = 1; 38 else if (a < 0) 39 alignment = 0; 40 else 41 alignment = a; 42 } 43 44 45 public static SizeRequirements getTiledSizeRequirements(SizeRequirements[] children) { 46 SizeRequirements total = new SizeRequirements(); 47 for (int i = 0; i < children.length; i++) { 48 SizeRequirements req = children[i]; 49 total.minimum = (int) Math.min((long) total.minimum + (long) req.minimum, Integer.MAX_VALUE); 50 total.preferred = (int) Math.min((long) total.preferred + (long) req.preferred, Integer.MAX_VALUE); 51 total.maximum = (int) Math.min((long) total.maximum + (long) req.maximum, Integer.MAX_VALUE); 52 } 53 return total; 54 } 55 public static SizeRequirements getAlignedSizeRequirements(SizeRequirements[] children) { 56 SizeRequirements totalAscent = new SizeRequirements(); 57 SizeRequirements totalDescent = new SizeRequirements(); 58 for (int i = 0; i < children.length; i++) { 59 SizeRequirements req = children[i]; 60 61 int ascent = (int) (req.alignment * req.minimum); 62 int descent = req.minimum - ascent; 63 totalAscent.minimum = Math.max(ascent, totalAscent.minimum); 64 totalDescent.minimum = Math.max(descent, totalDescent.minimum); 65 66 ascent = (int) (req.alignment * req.preferred); 67 descent = req.preferred - ascent; 68 totalAscent.preferred = Math.max(ascent, totalAscent.preferred); 69 totalDescent.preferred = Math.max(descent, totalDescent.preferred); 70 71 ascent = (int) (req.alignment * req.maximum); 72 descent = req.maximum - ascent; 73 totalAscent.maximum = Math.max(ascent, totalAscent.maximum); 74 totalDescent.maximum = Math.max(descent, totalDescent.maximum); 75 } 76 int min = (int) Math.min((long) totalAscent.minimum + (long) totalDescent.minimum, Integer.MAX_VALUE); 77 int pref = (int) Math.min((long) totalAscent.preferred + (long) totalDescent.preferred, Integer.MAX_VALUE); 78 int max = (int) Math.min((long) totalAscent.maximum + (long) totalDescent.maximum, Integer.MAX_VALUE); 79 float alignment = 0.0f; 80 if (min > 0) { 81 alignment = (float) totalAscent.minimum / min; 82 alignment = alignment > 1 ? 1 : alignment < 0 ? 0 : alignment; 83 } 84 return new SizeRequirements(min, pref, max, alignment); 85 } 86 public static void calculateTiledPositions(int allocated, 87 SizeRequirements total, 88 SizeRequirements[] children, 89 int[] offsets, 90 int[] spans) { 91 calculateTiledPositions(allocated, total, children, offsets, spans, true); 92 } 93 public static void calculateTiledPositions(int allocated, 94 SizeRequirements total, 95 SizeRequirements[] children, 96 int[] offsets, 97 int[] spans, 98 boolean forward) { 99 long min = 0; 100 long pref = 0; 101 long max = 0; 102 for (int i = 0; i < children.length; i++) { 103 min += children[i].minimum; 104 pref += children[i].preferred; 105 max += children[i].maximum; 106 } 107 if (allocated >= pref) { 108 109 float totalPlay = Math.min(allocated - pref, max - pref); 110 float factor = (max - pref == 0) ? 0 : totalPlay / (max - pref); 111 int totalOffset; 112 113 if( forward ) { 114 totalOffset = 0; 115 for (int i = 0; i < spans.length; i++) { 116 offsets[i] = totalOffset; 117 SizeRequirements req = children[i]; 118 int play = (int)(factor * (req.maximum - req.preferred)); 119 spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE); 120 totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE); 121 } 122 } else { 123 totalOffset = allocated; 124 for (int i = 0; i < spans.length; i++) { 125 SizeRequirements req = children[i]; 126 int play = (int)(factor * (req.maximum - req.preferred)); 127 spans[i] = (int) Math.min((long) req.preferred + (long) play, Integer.MAX_VALUE); 128 offsets[i] = totalOffset - spans[i]; 129 totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0); 130 } 131 } 132 133 134 } else { 135 136 float totalPlay = Math.min(pref - allocated, pref - min); 137 float factor = (pref - min == 0) ? 0 : totalPlay / (pref - min); 138 int totalOffset; 139 if( forward ) { 140 totalOffset = 0; 141 for (int i = 0; i < spans.length; i++) { 142 offsets[i] = totalOffset; 143 SizeRequirements req = children[i]; 144 float play = factor * (req.preferred - req.minimum); 145 spans[i] = (int)(req.preferred - play); 146 totalOffset = (int) Math.min((long) totalOffset + (long) spans[i], Integer.MAX_VALUE); 147 } 148 } else { 149 totalOffset = allocated; 150 for (int i = 0; i < spans.length; i++) { 151 SizeRequirements req = children[i]; 152 float play = factor * (req.preferred - req.minimum); 153 spans[i] = (int)(req.preferred - play); 154 offsets[i] = totalOffset - spans[i]; 155 totalOffset = (int) Math.max((long) totalOffset - (long) spans[i], 0); 156 } 157 } 158 159 } 160 } 161 162 public static void calculateAlignedPositions(int allocated, 163 SizeRequirements total, 164 SizeRequirements[] children, 165 int[] offsets, 166 int[] spans) { 167 calculateAlignedPositions( allocated, total, children, offsets, spans, true ); 168 } 169 170 public static void calculateAlignedPositions(int allocated, 171 SizeRequirements total, 172 SizeRequirements[] children, 173 int[] offsets, 174 int[] spans, 175 boolean normal) { 176 177 float totalAlignment = normal ? total.alignment : 1 - total.alignment; 178 179 int totalAscent = (int)(allocated * totalAlignment); 180 int totalDescent = allocated - totalAscent; 181 for (int i = 0; i < children.length; i++) { 182 SizeRequirements req = children[i]; 183 float alignment = normal ? req.alignment : 1 - req.alignment; 184 int maxAscent = (int)(req.maximum * alignment); 185 int maxDescent = req.maximum - maxAscent; 186 int ascent = Math.min(totalAscent, maxAscent); 187 int descent = Math.min(totalDescent, maxDescent); 188 189 offsets[i] = totalAscent - ascent; 190 spans[i] = (int) Math.min((long) ascent + (long) descent, Integer.MAX_VALUE); 191 } 192 } 193 194 public static int[] adjustSizes(int delta, SizeRequirements[] children) { 195 return new int[0]; 196 } 197 198 199 public String toString() { 200 return "[" + minimum + "," + preferred + "," + maximum + "]@" + alignment; 201 } 202 } | Popular Tags |