1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
54: public class FrameSetView
55: extends BoxView
56: {
57:
58:
61: private class FrameSetRow
62: extends BoxView
63: {
64: private int row;
65: FrameSetRow(Element el, int r)
66: {
67: super(el, X_AXIS);
68: row = r;
69: }
70:
71: protected void loadChildren(ViewFactory f)
72: {
73:
74: Element el = getElement();
75: View[] columns = new View[numViews[X_AXIS]];
76: int offset = row * numViews[X_AXIS];
77: for (int c = 0; c < numViews[X_AXIS]; c++)
78: {
79: Element child = el.getElement(offset + c);
80: columns[c] = f.create(child);
81: }
82: replace(0, 0, columns);
83: }
84:
85: protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets,
86: int[] spans)
87: {
88: int numRows = numViews[X_AXIS];
89: int[] abs = absolute[X_AXIS];
90: int[] rel = relative[X_AXIS];
91: int[] perc = percent[X_AXIS];
92: layoutViews(targetSpan, axis, offsets, spans, numRows, abs, rel, perc);
93: }
94: }
95:
96:
102: int[][] absolute;
103:
104:
110: int[][] relative;
111:
112:
120: int[][] percent;
121:
122:
125: int[] numViews;
126:
127: FrameSetView(Element el)
128: {
129: super(el, Y_AXIS);
130: numViews = new int[2];
131: absolute = new int[2][];
132: relative = new int[2][];
133: percent = new int[2][];
134: }
135:
136:
139: protected void loadChildren(ViewFactory f)
140: {
141: parseRowsCols();
142:
143: View[] rows = new View[numViews[Y_AXIS]];
144: for (int r = 0; r < numViews[Y_AXIS]; r++)
145: {
146: rows[r] = new FrameSetRow(getElement(), r);
147: }
148: replace(0, 0, rows);
149: }
150:
151:
154: private void parseRowsCols()
155: {
156: Element el = getElement();
157: AttributeSet atts = el.getAttributes();
158: String cols = (String) atts.getAttribute(HTML.Attribute.COLS);
159: if (cols == null)
160: cols = "100%";
161: parseLayout(cols, X_AXIS);
162: String rows = (String) atts.getAttribute(HTML.Attribute.ROWS);
163: if (rows == null)
164: rows = "100%";
165: parseLayout(rows, Y_AXIS);
166: }
167:
168:
175: private void parseLayout(String att, int axis)
176: {
177: StringTokenizer tokens = new StringTokenizer(att, ",");
178: numViews[axis] = tokens.countTokens();
179: absolute[axis] = new int[numViews[axis]];
180: relative[axis] = new int[numViews[axis]];
181: percent[axis] = new int[numViews[axis]];
182: for (int index = 0; tokens.hasMoreTokens(); index++)
183: {
184: String token = tokens.nextToken();
185: int p = token.indexOf('%');
186: int s = token.indexOf('*');
187: if (p != -1)
188: {
189:
190: String number = token.substring(0, p);
191: try
192: {
193: percent[axis][index] = Integer.parseInt(number);
194: }
195: catch (NumberFormatException ex)
196: {
197:
198: }
199: }
200: else if (s != -1)
201: {
202:
203: String number = token.substring(0, s);
204: try
205: {
206: relative[axis][index] = Integer.parseInt(number);
207: }
208: catch (NumberFormatException ex)
209: {
210:
211: }
212: }
213: else
214: {
215:
216: try
217: {
218: absolute[axis][index] = Integer.parseInt(token);
219: }
220: catch (NumberFormatException ex)
221: {
222:
223: }
224: }
225: }
226: }
227:
228: protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets,
229: int[] spans)
230: {
231: int numRows = numViews[Y_AXIS];
232: int[] abs = absolute[Y_AXIS];
233: int[] rel = relative[Y_AXIS];
234: int[] perc = percent[Y_AXIS];
235: layoutViews(targetSpan, axis, offsets, spans, numRows, abs, rel, perc);
236: }
237:
238: void layoutViews(int targetSpan, int axis, int[] offsets, int[] spans,
239: int numViews, int[] abs, int[] rel, int[] perc)
240: {
241:
242:
243:
244: int total = 0;
245: int relTotal = 0;
246: for (int i = 0; i < numViews; i++)
247: {
248: if (abs[i] > 0)
249: {
250: spans[i] = abs[i];
251: total += spans[i];
252: }
253: else if (perc[i] > 0)
254: {
255: spans[i] = (targetSpan * perc[i]) / 100;
256: total += spans[i];
257: }
258: else if (rel[i] > 0)
259: {
260: relTotal += rel[i];
261: }
262: }
263: int offs = 0;
264: for (int i = 0; i < numViews; i++)
265: {
266: if (relTotal > 0 && rel[i] > 0)
267: {
268: spans[i] = targetSpan * (rel[i] / relTotal);
269: }
270: offsets[i] = offs;
271: offs += spans[i];
272: }
273: }
274: }