1 7 package javax.swing.text.html; 8 9 import java.awt.*; 10 import java.util.*; 11 import javax.swing.*; 12 import javax.swing.text.*; 13 import javax.swing.event.*; 14 15 28 29 class FrameSetView extends javax.swing.text.BoxView { 30 31 String [] children; 32 int[] percentChildren; 33 int[] absoluteChildren; 34 int[] relativeChildren; 35 int percentTotals; 36 int absoluteTotals; 37 int relativeTotals; 38 39 44 public FrameSetView(Element elem, int axis) { 45 super(elem, axis); 46 children = null; 47 } 48 49 55 private String [] parseRowColSpec(HTML.Attribute key) { 56 57 AttributeSet attributes = getElement().getAttributes(); 58 String spec = "*"; 59 if (attributes != null) { 60 if (attributes.getAttribute(key) != null) { 61 spec = (String )attributes.getAttribute(key); 62 } 63 } 64 65 StringTokenizer tokenizer = new StringTokenizer(spec, ","); 66 int nTokens = tokenizer.countTokens(); 67 int n = getViewCount(); 68 String [] items = new String [Math.max(nTokens, n)]; 69 int i = 0; 70 for (; i < nTokens; i++) { 71 items[i] = tokenizer.nextToken().trim(); 72 if (items[i].equals("100%")) { 76 items[i] = "*"; 77 } 78 } 79 for (; i < items.length; i++) { 82 items[i] = "*"; 83 } 84 return items; 85 } 86 87 88 93 private void init() { 94 if (getAxis() == View.Y_AXIS) { 95 children = parseRowColSpec(HTML.Attribute.ROWS); 96 } else { 97 children = parseRowColSpec(HTML.Attribute.COLS); 98 } 99 percentChildren = new int[children.length]; 100 relativeChildren = new int[children.length]; 101 absoluteChildren = new int[children.length]; 102 103 for (int i = 0; i < children.length; i++) { 104 percentChildren[i] = -1; 105 relativeChildren[i] = -1; 106 absoluteChildren[i] = -1; 107 108 if (children[i].endsWith("*")) { 109 if (children[i].length() > 1) { 110 relativeChildren[i] = 111 Integer.parseInt(children[i].substring( 112 0, children[i].length()-1)); 113 relativeTotals += relativeChildren[i]; 114 } else { 115 relativeChildren[i] = 1; 116 relativeTotals += 1; 117 } 118 } else if (children[i].indexOf('%') != -1) { 119 percentChildren[i] = parseDigits(children[i]); 120 percentTotals += percentChildren[i]; 121 } else { 122 absoluteChildren[i] = Integer.parseInt(children[i]); 123 } 124 } 125 if (percentTotals > 100) { 126 for (int i = 0; i < percentChildren.length; i++) { 127 if (percentChildren[i] > 0) { 128 percentChildren[i] = 129 (percentChildren[i] * 100) / percentTotals; 130 } 131 } 132 percentTotals = 100; 133 } 134 } 135 136 153 protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, 154 int[] spans) { 155 if (children == null) { 156 init(); 157 } 158 SizeRequirements.calculateTiledPositions(targetSpan, null, 159 getChildRequests(targetSpan, 160 axis), 161 offsets, spans); 162 } 163 164 protected SizeRequirements[] getChildRequests(int targetSpan, int axis) { 165 166 int span[] = new int[children.length]; 167 168 spread(targetSpan, span); 169 int n = getViewCount(); 170 SizeRequirements[] reqs = new SizeRequirements[n]; 171 for (int i = 0, sIndex = 0; i < n; i++) { 172 View v = getView(i); 173 if ((v instanceof FrameView ) || (v instanceof FrameSetView )) { 174 reqs[i] = new SizeRequirements((int) v.getMinimumSpan(axis), 175 span[sIndex], 176 (int) v.getMaximumSpan(axis), 177 0.5f); 178 sIndex++; 179 } else { 180 int min = (int) v.getMinimumSpan(axis); 181 int pref = (int) v.getPreferredSpan(axis); 182 int max = (int) v.getMaximumSpan(axis); 183 float a = v.getAlignment(axis); 184 reqs[i] = new SizeRequirements(min, pref, max, a); 185 } 186 } 187 return reqs; 188 } 189 190 191 197 private void spread(int targetSpan, int span[]) { 198 199 if (targetSpan == 0) { 200 return; 201 } 202 203 int tempSpace = 0; 204 int remainingSpace = targetSpan; 205 206 for (int i = 0; i < span.length; i++) { 210 if (absoluteChildren[i] > 0) { 211 span[i] = absoluteChildren[i]; 212 remainingSpace -= span[i]; 213 } 214 } 215 216 tempSpace = remainingSpace; 219 for (int i = 0; i < span.length; i++) { 220 if (percentChildren[i] > 0 && tempSpace > 0) { 221 span[i] = (percentChildren[i] * tempSpace) / 100; 222 remainingSpace -= span[i]; 223 } else if (percentChildren[i] > 0 && tempSpace <= 0) { 224 span[i] = targetSpan / span.length; 225 remainingSpace -= span[i]; 226 } 227 } 228 229 if (remainingSpace > 0 && relativeTotals > 0) { 231 for (int i = 0; i < span.length; i++) { 232 if (relativeChildren[i] > 0) { 233 span[i] = (remainingSpace * 234 relativeChildren[i]) / relativeTotals; 235 } 236 } 237 } else if (remainingSpace > 0) { 238 246 253 256 float vTotal = (float)(targetSpan - remainingSpace); 257 float[] tempPercents = new float[span.length]; 258 remainingSpace = targetSpan; 259 for (int i = 0; i < span.length; i++) { 260 tempPercents[i] = ((float)span[i] / vTotal) * 100.00f; 265 span[i] = (int) ( ((float)targetSpan * tempPercents[i]) / 100.00f); 266 remainingSpace -= span[i]; 267 } 268 269 270 int i = 0; 274 while (remainingSpace != 0) { 275 if (remainingSpace < 0) { 276 span[i++]--; 277 remainingSpace++; 278 } 279 else { 280 span[i++]++; 281 remainingSpace--; 282 } 283 284 if (i == span.length)i = 0; 286 } 287 } 288 } 289 290 294 private int parseDigits(String mixedStr) { 295 int result = 0; 296 for (int i = 0; i < mixedStr.length(); i++) { 297 char ch = mixedStr.charAt(i); 298 if (Character.isDigit(ch)) { 299 result = (result * 10) + Character.digit(ch, 10); 300 } 301 } 302 return result; 303 } 304 305 } 306 307 | Popular Tags |