jdk/src/share/classes/javax/swing/text/html/FrameSetView.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.html;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Implements a FrameSetView, intended to support the HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <FRAMESET> tag.  Supports the ROWS and COLS attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author  Sunita Mani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *          Credit also to the hotjava browser engineers that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *          worked on making the allocation of space algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *          conform to the HTML 4.0 standard and also be netscape
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *          compatible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class FrameSetView extends javax.swing.text.BoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    String[] children;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int[] percentChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    int[] absoluteChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    int[] relativeChildren;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int percentTotals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int absoluteTotals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    int relativeTotals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs a FrameSetView for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param elem the element that this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public FrameSetView(Element elem, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        super(elem, axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        children = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Parses the ROW or COL attributes and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * an array of strings that represent the space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private String[] parseRowColSpec(HTML.Attribute key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        AttributeSet attributes = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        String spec = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (attributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (attributes.getAttribute(key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                spec = (String)attributes.getAttribute(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        StringTokenizer tokenizer = new StringTokenizer(spec, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        int nTokens = tokenizer.countTokens();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String[] items = new String[Math.max(nTokens, n)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (; i < nTokens; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            items[i] = tokenizer.nextToken().trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // As per the spec, 100% is the same as *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            // hence the mapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (items[i].equals("100%")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                items[i] = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // extend spec if we have more children than specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // in ROWS or COLS attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        for (; i < items.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            items[i] = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return items;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Initializes a number of internal state variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * that store information about space allocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * for the frames contained within the frameset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (getAxis() == View.Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            children = parseRowColSpec(HTML.Attribute.ROWS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            children = parseRowColSpec(HTML.Attribute.COLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        percentChildren = new int[children.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        relativeChildren = new int[children.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        absoluteChildren = new int[children.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (int i = 0; i < children.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            percentChildren[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            relativeChildren[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            absoluteChildren[i] = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (children[i].endsWith("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                if (children[i].length() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    relativeChildren[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        Integer.parseInt(children[i].substring(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                            0, children[i].length()-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    relativeTotals += relativeChildren[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    relativeChildren[i] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    relativeTotals += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            } else if (children[i].indexOf('%') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                percentChildren[i] = parseDigits(children[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                percentTotals += percentChildren[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                absoluteChildren[i] = Integer.parseInt(children[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (percentTotals > 100) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            for (int i = 0; i < percentChildren.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                if (percentChildren[i] > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    percentChildren[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        (percentChildren[i] * 100) / percentTotals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            percentTotals = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Perform layout for the major axis of the box (i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * axis that it represents).  The results of the layout should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * be placed in the given arrays which represent the allocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * to the children along the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *  whould be used to layout the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @param axis the axis being layed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *  each of the child views; this is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *  filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param spans the span of each child view; this is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *  value and is filled in by the implementation of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the offset and span for each child view in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *  offsets and spans parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                   int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (children == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        SizeRequirements.calculateTiledPositions(targetSpan, null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                                 getChildRequests(targetSpan,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                                                  axis),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                                 offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    protected SizeRequirements[] getChildRequests(int targetSpan, int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int span[] = new int[children.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        spread(targetSpan, span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        SizeRequirements[] reqs = new SizeRequirements[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        for (int i = 0, sIndex = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if ((v instanceof FrameView) || (v instanceof FrameSetView)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                reqs[i] = new SizeRequirements((int) v.getMinimumSpan(axis),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                               span[sIndex],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                               (int) v.getMaximumSpan(axis),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                               0.5f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                sIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                int min = (int) v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                int pref = (int) v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                int max = (int) v.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                float a = v.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                reqs[i] = new SizeRequirements(min, pref, max, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return reqs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * This method is responsible for returning in span[] the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * span for each child view along the major axis.  it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * computes this based on the information that extracted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * from the value of the ROW/COL attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void spread(int targetSpan, int span[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (targetSpan == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        int tempSpace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int remainingSpace = targetSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // allocate the absolute's first, they have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // precedence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        for (int i = 0; i < span.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (absoluteChildren[i] > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                span[i] = absoluteChildren[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                remainingSpace -= span[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        // then deal with percents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        tempSpace = remainingSpace;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (int i = 0; i < span.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (percentChildren[i] > 0 && tempSpace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                span[i] = (percentChildren[i] * tempSpace) / 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                remainingSpace -= span[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            } else if (percentChildren[i] > 0 && tempSpace <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                span[i] = targetSpan / span.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                remainingSpace -= span[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // allocate remainingSpace to relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (remainingSpace > 0 && relativeTotals > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            for (int i = 0; i < span.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (relativeChildren[i] > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    span[i] = (remainingSpace *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                relativeChildren[i]) / relativeTotals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } else if (remainingSpace > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            // There are no relative columns and the space has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            // under- or overallocated.  In this case, turn all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            // percentage and pixel specified columns to percentage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            // columns based on the ratio of their pixel count to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            // total "virtual" size. (In the case of percentage columns,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            // the pixel count would equal the specified percentage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // of the screen size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            // This action is in accordance with the HTML
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // 4.0 spec (see section 8.3, the end of the discussion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            // the FRAMESET tag).  The precedence of percentage and pixel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            // specified columns is unclear (spec seems to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // they share priority, however, unspecified what happens when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // overallocation occurs.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // addendum is that we behave similiar to netscape in that specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // widths have precedance over percentage widths...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            float vTotal = (float)(targetSpan - remainingSpace);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            float[] tempPercents = new float[span.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            remainingSpace = targetSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            for (int i = 0; i < span.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                // ok we know what our total space is, and we know how large each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                // column should be relative to each other... therefore we can use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // that relative information to deduce their percentages of a whole
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                // and then scale them appropriately for the correct size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                tempPercents[i] = ((float)span[i] / vTotal) * 100.00f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                span[i] = (int) ( ((float)targetSpan * tempPercents[i]) / 100.00f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                remainingSpace -= span[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // this is for just in case there is something left over.. if there is we just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // add it one pixel at a time to the frames in order.. We shouldn't really ever get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            // here and if we do it shouldn't be with more than 1 pixel, maybe two.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            while (remainingSpace != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                if (remainingSpace < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    span[i++]--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    remainingSpace++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    span[i++]++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    remainingSpace--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // just in case there are more pixels than frames...should never happen..
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (i == span.length)i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Users have been known to type things like "%25" and "25 %".  Deal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private int parseDigits(String mixedStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        for (int i = 0; i < mixedStr.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            char ch = mixedStr.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (Character.isDigit(ch)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                result = (result * 10) + Character.digit(ch, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
}