jdk/src/share/classes/javax/swing/text/TableView.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 7959 2e05332a8f5c
child 20158 1c5d22e5b898
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7959
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
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.BitSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.SizeRequirements;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.html.HTML;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Implements View interface for a table, that is composed of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * element structure where the child elements of the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * this view is responsible for represent rows and the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * elements of the row elements are cells.  The cell elements can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * have an arbitrary element structure under them, which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * be built with the ViewFactory returned by the getViewFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * &nbsp;  TABLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * &nbsp;    ROW
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * &nbsp;      CELL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * &nbsp;      CELL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * &nbsp;    ROW
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * &nbsp;      CELL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * &nbsp;      CELL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This is implemented as a hierarchy of boxes, the table itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * is a vertical box, the rows are horizontal boxes, and the cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * are vertical boxes.  The cells are allowed to span multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * columns and rows.  By default, the table can be thought of as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * being formed over a grid (i.e. somewhat like one would find in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * gridbag layout), where table cells can request to span more
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * than one grid cell.  The default horizontal span of table cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * will be based upon this grid, but can be changed by reimplementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the requested span of the cell (i.e. table cells can have independant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * spans if desired).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @see     View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public abstract class TableView extends BoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Constructs a TableView for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param elem the element that this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public TableView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(elem, View.Y_AXIS);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    79
        rows = new Vector<TableRow>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        gridValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Creates a new table row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param elem an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @return the row
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected TableRow createTableRow(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return new TableRow(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @deprecated Table cells can now be any arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * View implementation and should be produced by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * ViewFactory rather than the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param elem an element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return the cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    protected TableCell createTableCell(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return new TableCell(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The number of columns in the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return columnSpans.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Fetches the span (width) of the given column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * This is used by the nested cells to query the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * sizes of grid locations outside of themselves.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    int getColumnSpan(int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return columnSpans[col];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * The number of rows in the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return rows.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Fetches the span (height) of the given row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    int getRowSpan(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        View rv = getRow(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (rv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return (int) rv.getPreferredSpan(Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    TableRow getRow(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (row < rows.size()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   142
            return rows.elementAt(row);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Determines the number of columns occupied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * the table cell represented by given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /*protected*/ int getColumnsOccupied(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        // PENDING(prinz) this code should be in the html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // paragraph, but we can't add api to enable it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        AttributeSet a = v.getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        String s = (String) a.getAttribute(HTML.Attribute.COLSPAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                return Integer.parseInt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                // fall through to one column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Determines the number of rows occupied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * the table cell represented by given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /*protected*/ int getRowsOccupied(View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // PENDING(prinz) this code should be in the html
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        // paragraph, but we can't add api to enable it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        AttributeSet a = v.getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        String s = (String) a.getAttribute(HTML.Attribute.ROWSPAN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                return Integer.parseInt(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                // fall through to one row
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*protected*/ void invalidateGrid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        gridValid = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected void forwardUpdate(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                     DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        super.forwardUpdate(ec, e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // A change in any of the table cells usually effects the whole table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // so redraw it all!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                   a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * Change the child views.  This is implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * provide the superclass behavior and invalidate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * grid so that rows and columns will be recalculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void replace(int offset, int length, View[] views) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        super.replace(offset, length, views);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        invalidateGrid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Fill in the grid locations that are placeholders
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * for multi-column, multi-row, and missing grid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * locations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    void updateGrid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (! gridValid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // determine which views are table rows and clear out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            // grid points marked filled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            rows.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (v instanceof TableRow) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   230
                    rows.addElement((TableRow) v);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    TableRow rv = (TableRow) v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    rv.clearFilledColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    rv.setRow(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            int maxColumns = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int nrows = rows.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            for (int row = 0; row < nrows; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                TableRow rv = getRow(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                for (int cell = 0; cell < rv.getViewCount(); cell++, col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    View cv = rv.getView(cell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    // advance to a free column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    for (; rv.isFilled(col); col++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    int rowSpan = getRowsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    int colSpan = getColumnsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    if ((colSpan > 1) || (rowSpan > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        // fill in the overflow entries for this cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        int rowLimit = row + rowSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        int colLimit = col + colSpan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        for (int i = row; i < rowLimit; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                            for (int j = col; j < colLimit; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                if (i != row || j != col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                    addFill(i, j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        if (colSpan > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                            col += colSpan - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                maxColumns = Math.max(maxColumns, col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            // setup the column layout/requirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            columnSpans = new int[maxColumns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            columnOffsets = new int[maxColumns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            columnRequirements = new SizeRequirements[maxColumns];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            for (int i = 0; i < maxColumns; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                columnRequirements[i] = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            gridValid = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Mark a grid location as filled in for a cells overflow.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    void addFill(int row, int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        TableRow rv = getRow(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (rv != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            rv.fillColumn(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Lays out the columns to fit within the given target span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Returns the results through {@code offsets} and {@code spans}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param targetSpan the given span for total of all the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *  columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param reqs the requirements desired for each column.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *  is the column maximum of the cells minimum, preferred, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *  maximum requested span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param spans the return value of how much to allocated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *  each column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param offsets the return value of the offset from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *  origin for each column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    protected void layoutColumns(int targetSpan, int[] offsets, int[] spans,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                 SizeRequirements[] reqs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        // allocate using the convenience method on SizeRequirements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        SizeRequirements.calculateTiledPositions(targetSpan, null, reqs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                                 offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Perform layout for the minor axis of the box (i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * axis orthoginal to the axis that it represents).  The results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * of the layout should be placed in the given arrays which represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * the allocations to the children along the minor axis.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * is called by the superclass whenever the layout needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * updated along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * This is implemented to call the
7959
2e05332a8f5c 6589952: Swing: dead links in API documentation
rupashka
parents: 5506
diff changeset
   318
     * {@link #layoutColumns layoutColumns} method, and then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * forward to the superclass to actually carry out the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * of the tables rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *  whould be used to layout the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @param axis the axis being layed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *  each of the child views.  This is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *  filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @param spans the span of each child view.  This is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *  value and is filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        // make grid is properly represented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        updateGrid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // all of the row layouts are invalid, so mark them that way
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        int n = getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            TableRow row = getRow(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            row.layoutChanged(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        // calculate column spans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        layoutColumns(targetSpan, columnOffsets, columnSpans, columnRequirements);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        // continue normal layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        super.layoutMinorAxis(targetSpan, axis, offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * Calculate the requirements for the minor axis.  This is called by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * the superclass whenever the requirements need to be updated (i.e.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * a preferenceChanged was messaged through this view).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * This is implemented to calculate the requirements as the sum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * requirements of the columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        updateGrid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        // calculate column requirements for each column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        calculateColumnRequirements(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        // the requirements are the sum of the columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (r == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            r = new SizeRequirements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        long min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        long pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        long max = 0;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   371
        for (SizeRequirements req : columnRequirements) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            min += req.minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            pref += req.preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            max += req.maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        r.minimum = (int) min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        r.preferred = (int) pref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        r.maximum = (int) max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        r.alignment = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    boolean shouldTrace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        AttributeSet a = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        Object o = a.getAttribute(HTML.Attribute.ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if ((o != null) && o.equals("debug")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Calculate the requirements for each column.  The calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * is done as two passes over the table.  The table cells that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * occupy a single column are scanned first to determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * maximum of minimum, preferred, and maximum spans along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * give axis.  Table cells that span multiple columns are excluded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * from the first pass.  A second pass is made to determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the cells that span multiple columns are satisfied.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * column requirements are not satisified, the needs of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * multi-column cell is mixed into the existing column requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * The calculation of the multi-column distribution is based upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * the proportions of the existing column requirements and taking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * into consideration any constraining maximums.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    void calculateColumnRequirements(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // pass 1 - single column cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        boolean hasMultiColumn = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        int nrows = getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        for (int i = 0; i < nrows; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            TableRow row = getRow(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            int ncells = row.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            for (int cell = 0; cell < ncells; cell++, col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                View cv = row.getView(cell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                for (; row.isFilled(col); col++); // advance to a free column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                int rowSpan = getRowsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                int colSpan = getColumnsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (colSpan == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    checkSingleColumnCell(axis, col, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    hasMultiColumn = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    col += colSpan - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        // pass 2 - multi-column cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        if (hasMultiColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            for (int i = 0; i < nrows; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                TableRow row = getRow(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                int ncells = row.getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                for (int cell = 0; cell < ncells; cell++, col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    View cv = row.getView(cell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    for (; row.isFilled(col); col++); // advance to a free column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    int colSpan = getColumnsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                    if (colSpan > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                        checkMultiColumnCell(axis, col, colSpan, cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        col += colSpan - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if (shouldTrace()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            System.err.println("calc:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            for (int i = 0; i < columnRequirements.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                System.err.println(" " + i + ": " + columnRequirements[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * check the requirements of a table cell that spans a single column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    void checkSingleColumnCell(int axis, int col, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        SizeRequirements req = columnRequirements[col];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        req.minimum = Math.max((int) v.getMinimumSpan(axis), req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        req.preferred = Math.max((int) v.getPreferredSpan(axis), req.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        req.maximum = Math.max((int) v.getMaximumSpan(axis), req.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * check the requirements of a table cell that spans multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    void checkMultiColumnCell(int axis, int col, int ncols, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        // calculate the totals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        long min = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        long pref = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        long max = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        for (int i = 0; i < ncols; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            SizeRequirements req = columnRequirements[col + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            min += req.minimum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            pref += req.preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            max += req.maximum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        // check if the minimum size needs adjustment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        int cmin = (int) v.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (cmin > min) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
             * the columns that this cell spans need adjustment to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
             * this table cell.... calculate the adjustments.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
             * maximum for each cell is the maximum of the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
             * maximum or the amount needed by the cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            SizeRequirements[] reqs = new SizeRequirements[ncols];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            for (int i = 0; i < ncols; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                SizeRequirements r = reqs[i] = columnRequirements[col + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                r.maximum = Math.max(r.maximum, (int) v.getMaximumSpan(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            int[] spans = new int[ncols];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            int[] offsets = new int[ncols];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            SizeRequirements.calculateTiledPositions(cmin, null, reqs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                                                     offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            // apply the adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            for (int i = 0; i < ncols; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                SizeRequirements req = reqs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                req.minimum = Math.max(spans[i], req.minimum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                req.preferred = Math.max(req.minimum, req.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                req.maximum = Math.max(req.preferred, req.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        // check if the preferred size needs adjustment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        int cpref = (int) v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        if (cpref > pref) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             * the columns that this cell spans need adjustment to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             * this table cell.... calculate the adjustments.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             * maximum for each cell is the maximum of the existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
             * maximum or the amount needed by the cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            SizeRequirements[] reqs = new SizeRequirements[ncols];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            for (int i = 0; i < ncols; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                SizeRequirements r = reqs[i] = columnRequirements[col + i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            int[] spans = new int[ncols];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            int[] offsets = new int[ncols];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            SizeRequirements.calculateTiledPositions(cpref, null, reqs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                                     offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            // apply the adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            for (int i = 0; i < ncols; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                SizeRequirements req = reqs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                req.preferred = Math.max(spans[i], req.preferred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                req.maximum = Math.max(req.preferred, req.maximum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * Fetches the child view that represents the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * the model.  This is implemented to walk through the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * looking for a range that contains the given position.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * view the children do not necessarily have a one to one mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * with the child elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param pos  the search position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param a  the allocation to the table on entry, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *   allocation of the view containing the position on exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * @return  the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *   <code>null</code> if there isn't one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    protected View getViewAtPosition(int pos, Rectangle a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            int p0 = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            int p1 = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if ((pos >= p0) && (pos < p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                // it's in this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    childAllocation(i, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        if (pos == getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            View v = getView(n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                this.childAllocation(n - 1, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    // ---- variables ----------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    int[] columnSpans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    int[] columnOffsets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    SizeRequirements[] columnRequirements;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   580
    Vector<TableRow> rows;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    boolean gridValid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    static final private BitSet EMPTY = new BitSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * View of a row in a row-centric table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    public class TableRow extends BoxView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * Constructs a TableView for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * @param elem the element that this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        public TableRow(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            super(elem, View.X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            fillColumns = new BitSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        void clearFilledColumns() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            fillColumns.and(EMPTY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        void fillColumn(int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            fillColumns.set(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        boolean isFilled(int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            return fillColumns.get(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /** get location in the overall set of rows */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        int getRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * set location in the overall set of rows, this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         * set by the TableView.updateGrid() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        void setRow(int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            this.row = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
         * The number of columns present in this row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            int nfill = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            int n = fillColumns.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                if (fillColumns.get(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    nfill ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            return getViewCount() + nfill;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
         * Change the child views.  This is implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
         * provide the superclass behavior and invalidate the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
         * grid so that rows and columns will be recalculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        public void replace(int offset, int length, View[] views) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            super.replace(offset, length, views);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            invalidateGrid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
         * Perform layout for the major axis of the box (i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
         * axis that it represents).  The results of the layout should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
         * be placed in the given arrays which represent the allocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
         * to the children along the major axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
         * This is re-implemented to give each child the span of the column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
         * width for the table, and to give cells that span multiple columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
         * the multi-column span.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
         *  whould be used to layout the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
         * @param axis the axis being layed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
         * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
         *  each of the child views.  This is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
         *  filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
         * @param spans the span of each child view.  This is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
         *  value and is filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            int ncells = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            for (int cell = 0; cell < ncells; cell++, col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                View cv = getView(cell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                for (; isFilled(col); col++); // advance to a free column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                int colSpan = getColumnsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                spans[cell] = columnSpans[col];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                offsets[cell] = columnOffsets[col];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                if (colSpan > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    int n = columnSpans.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                    for (int j = 1; j < colSpan; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        // Because the table may be only partially formed, some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        // of the columns may not yet exist.  Therefore we check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        // the bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        if ((col+j) < n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                            spans[cell] += columnSpans[col+j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                    col += colSpan - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
         * Perform layout for the minor axis of the box (i.e. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         * axis orthoginal to the axis that it represents).  The results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         * of the layout should be placed in the given arrays which represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
         * the allocations to the children along the minor axis.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
         * is called by the superclass whenever the layout needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
         * updated along the minor axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
         * This is implemented to delegate to the superclass, then adjust
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
         * the span for any cell that spans multiple rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
         * @param targetSpan the total span given to the view, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
         *  whould be used to layout the children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
         * @param axis the axis being layed out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
         * @param offsets the offsets from the origin of the view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
         *  each of the child views.  This is a return value and is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
         *  filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
         * @param spans the span of each child view.  This is a return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
         *  value and is filled in by the implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            super.layoutMinorAxis(targetSpan, axis, offsets, spans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            int col = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            int ncells = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            for (int cell = 0; cell < ncells; cell++, col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                View cv = getView(cell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                for (; isFilled(col); col++); // advance to a free column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                int colSpan = getColumnsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                int rowSpan = getRowsOccupied(cv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                if (rowSpan > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    for (int j = 1; j < rowSpan; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                        // test bounds of each row because it may not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                        // either because of error or because the table isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                        // fully loaded yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                        int row = getRow() + j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                        if (row < TableView.this.getViewCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                            int span = TableView.this.getSpan(Y_AXIS, getRow()+j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                            spans[cell] += span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                if (colSpan > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    col += colSpan - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         * Determines the resizability of the view along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * given axis.  A value of 0 or less is not resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
         * @return the resize weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
         * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         * Fetches the child view that represents the given position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * the model.  This is implemented to walk through the children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         * looking for a range that contains the given position.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * view the children do not necessarily have a one to one mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * with the child elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * @param pos  the search position >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         * @param a  the allocation to the table on entry, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
         *   allocation of the view containing the position on exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
         * @return  the view representing the given position, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
         *   <code>null</code> if there isn't one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        protected View getViewAtPosition(int pos, Rectangle a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            int n = getViewCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                View v = getView(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                int p0 = v.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                int p1 = v.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                if ((pos >= p0) && (pos < p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                    // it's in this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        childAllocation(i, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                    return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            if (pos == getEndOffset()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                View v = getView(n - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    this.childAllocation(n - 1, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        /** columns filled by multi-column or multi-row cells */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        BitSet fillColumns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        /** the row within the overall grid */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        int row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @deprecated  A table cell can now be any View implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    public class TableCell extends BoxView implements GridCell {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
         * Constructs a TableCell for the given element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
         * @param elem the element that this view is responsible for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
         * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        public TableCell(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            super(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        // --- GridCell methods -------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
         * Gets the number of columns this cell spans (e.g. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
         * grid width).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * @return the number of columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        public int getColumnCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * Gets the number of rows this cell spans (that is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         * grid height).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * @return the number of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        public int getRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
         * Sets the grid location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
         * @param row the row >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
         * @param col the column >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        public void setGridLocation(int row, int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            this.row = row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            this.col = col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
         * Gets the row of the grid location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        public int getGridRow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
         * Gets the column of the grid location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        public int getGridColumn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            return col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        int row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        int col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * <em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * THIS IS NO LONGER USED, AND WILL BE REMOVED IN THE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * NEXT RELEASE.  THE JCK SIGNATURE TEST THINKS THIS INTERFACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * SHOULD EXIST
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * </em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    interface GridCell {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         * Sets the grid location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         * @param row the row >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
         * @param col the column >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        public void setGridLocation(int row, int col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         * Gets the row of the grid location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        public int getGridRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
         * Gets the column of the grid location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        public int getGridColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         * Gets the number of columns this cell spans (e.g. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
         * grid width).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
         * @return the number of columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        public int getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
         * Gets the number of rows this cell spans (that is, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
         * grid height).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
         * @return the number of rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        public int getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
}