KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > TableView


1 /*
2  * @(#)TableView.java 1.33 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text;
8
9 import java.awt.*;
10 import java.util.BitSet JavaDoc;
11 import java.util.Vector JavaDoc;
12 import javax.swing.SizeRequirements JavaDoc;
13 import javax.swing.event.DocumentEvent JavaDoc;
14
15 import javax.swing.text.html.HTML JavaDoc;
16
17 /**
18  * <p>
19  * Implements View interface for a table, that is composed of an
20  * element structure where the child elements of the element
21  * this view is responsible for represent rows and the child
22  * elements of the row elements are cells. The cell elements can
23  * have an arbitrary element structure under them, which will
24  * be built with the ViewFactory returned by the getViewFactory
25  * method.
26  * <pre>
27  *
28  * &nbsp; TABLE
29  * &nbsp; ROW
30  * &nbsp; CELL
31  * &nbsp; CELL
32  * &nbsp; ROW
33  * &nbsp; CELL
34  * &nbsp; CELL
35  *
36  * </pre>
37  * <p>
38  * This is implemented as a hierarchy of boxes, the table itself
39  * is a vertical box, the rows are horizontal boxes, and the cells
40  * are vertical boxes. The cells are allowed to span multiple
41  * columns and rows. By default, the table can be thought of as
42  * being formed over a grid (i.e. somewhat like one would find in
43  * gridbag layout), where table cells can request to span more
44  * than one grid cell. The default horizontal span of table cells
45  * will be based upon this grid, but can be changed by reimplementing
46  * the requested span of the cell (i.e. table cells can have independant
47  * spans if desired).
48  *
49  * @author Timothy Prinzing
50  * @version 1.33 05/18/04
51  * @see View
52  */

53 public abstract class TableView extends BoxView JavaDoc {
54
55     /**
56      * Constructs a TableView for the given element.
57      *
58      * @param elem the element that this view is responsible for
59      */

60     public TableView(Element JavaDoc elem) {
61     super(elem, View.Y_AXIS);
62     rows = new Vector JavaDoc();
63     gridValid = false;
64     }
65
66     /**
67      * Creates a new table row.
68      *
69      * @param elem an element
70      * @return the row
71      */

72     protected TableRow createTableRow(Element JavaDoc elem) {
73     return new TableRow(elem);
74     }
75
76     /**
77      * @deprecated Table cells can now be any arbitrary
78      * View implementation and should be produced by the
79      * ViewFactory rather than the table.
80      *
81      * @param elem an element
82      * @return the cell
83      */

84     @Deprecated JavaDoc
85     protected TableCell createTableCell(Element JavaDoc elem) {
86     return new TableCell(elem);
87     }
88     
89     /**
90      * The number of columns in the table.
91      */

92     int getColumnCount() {
93     return columnSpans.length;
94     }
95
96     /**
97      * Fetches the span (width) of the given column.
98      * This is used by the nested cells to query the
99      * sizes of grid locations outside of themselves.
100      */

101     int getColumnSpan(int col) {
102     return columnSpans[col];
103     }
104
105     /**
106      * The number of rows in the table.
107      */

108     int getRowCount() {
109     return rows.size();
110     }
111
112     /**
113      * Fetches the span (height) of the given row.
114      */

115     int getRowSpan(int row) {
116     View JavaDoc rv = getRow(row);
117     if (rv != null) {
118         return (int) rv.getPreferredSpan(Y_AXIS);
119     }
120     return 0;
121     }
122
123     TableRow getRow(int row) {
124     if (row < rows.size()) {
125         return (TableRow) rows.elementAt(row);
126     }
127     return null;
128     }
129
130     /**
131      * Determines the number of columns occupied by
132      * the table cell represented by given element.
133      */

134     /*protected*/ int getColumnsOccupied(View JavaDoc v) {
135     // PENDING(prinz) this code should be in the html
136
// paragraph, but we can't add api to enable it.
137
AttributeSet JavaDoc a = v.getElement().getAttributes();
138     String JavaDoc s = (String JavaDoc) a.getAttribute(HTML.Attribute.COLSPAN);
139     if (s != null) {
140         try {
141         return Integer.parseInt(s);
142         } catch (NumberFormatException JavaDoc nfe) {
143         // fall through to one column
144
}
145     }
146
147     return 1;
148     }
149
150     /**
151      * Determines the number of rows occupied by
152      * the table cell represented by given element.
153      */

154     /*protected*/ int getRowsOccupied(View JavaDoc v) {
155     // PENDING(prinz) this code should be in the html
156
// paragraph, but we can't add api to enable it.
157
AttributeSet JavaDoc a = v.getElement().getAttributes();
158     String JavaDoc s = (String JavaDoc) a.getAttribute(HTML.Attribute.ROWSPAN);
159     if (s != null) {
160         try {
161         return Integer.parseInt(s);
162         } catch (NumberFormatException JavaDoc nfe) {
163         // fall through to one row
164
}
165     }
166
167     return 1;
168     }
169
170     /*protected*/ void invalidateGrid() {
171     gridValid = false;
172     }
173
174     protected void forwardUpdate(DocumentEvent.ElementChange JavaDoc ec,
175                      DocumentEvent JavaDoc e, Shape a, ViewFactory JavaDoc f) {
176     super.forwardUpdate(ec, e, a, f);
177     // A change in any of the table cells usually effects the whole table,
178
// so redraw it all!
179
if (a != null) {
180         Component c = getContainer();
181         if (c != null) {
182         Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
183                            a.getBounds();
184         c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
185         }
186     }
187     }
188
189     /**
190      * Change the child views. This is implemented to
191      * provide the superclass behavior and invalidate the
192      * grid so that rows and columns will be recalculated.
193      */

194     public void replace(int offset, int length, View JavaDoc[] views) {
195     super.replace(offset, length, views);
196     invalidateGrid();
197     }
198
199     /**
200      * Fill in the grid locations that are placeholders
201      * for multi-column, multi-row, and missing grid
202      * locations.
203      */

204     void updateGrid() {
205     if (! gridValid) {
206         // determine which views are table rows and clear out
207
// grid points marked filled.
208
rows.removeAllElements();
209         int n = getViewCount();
210         for (int i = 0; i < n; i++) {
211         View JavaDoc v = getView(i);
212         if (v instanceof TableRow) {
213             rows.addElement(v);
214             TableRow rv = (TableRow) v;
215             rv.clearFilledColumns();
216             rv.setRow(i);
217         }
218         }
219
220         int maxColumns = 0;
221         int nrows = rows.size();
222         for (int row = 0; row < nrows; row++) {
223         TableRow rv = getRow(row);
224         int col = 0;
225         for (int cell = 0; cell < rv.getViewCount(); cell++, col++) {
226             View JavaDoc cv = rv.getView(cell);
227             // advance to a free column
228
for (; rv.isFilled(col); col++);
229             int rowSpan = getRowsOccupied(cv);
230             int colSpan = getColumnsOccupied(cv);
231             if ((colSpan > 1) || (rowSpan > 1)) {
232             // fill in the overflow entries for this cell
233
int rowLimit = row + rowSpan;
234             int colLimit = col + colSpan;
235             for (int i = row; i < rowLimit; i++) {
236                 for (int j = col; j < colLimit; j++) {
237                 if (i != row || j != col) {
238                     addFill(i, j);
239                 }
240                 }
241             }
242             if (colSpan > 1) {
243                 col += colSpan - 1;
244             }
245             }
246         }
247         maxColumns = Math.max(maxColumns, col);
248         }
249
250         // setup the column layout/requirements
251
columnSpans = new int[maxColumns];
252         columnOffsets = new int[maxColumns];
253         columnRequirements = new SizeRequirements JavaDoc[maxColumns];
254         for (int i = 0; i < maxColumns; i++) {
255         columnRequirements[i] = new SizeRequirements JavaDoc();
256         }
257         gridValid = true;
258     }
259     }
260
261     /**
262      * Mark a grid location as filled in for a cells overflow.
263      */

264     void addFill(int row, int col) {
265     TableRow rv = getRow(row);
266     if (rv != null) {
267         rv.fillColumn(col);
268     }
269     }
270
271     /**
272      * Layout the columns to fit within the given target span.
273      *
274      * @param targetSpan the given span for total of all the table
275      * columns.
276      * @param reqs the requirements desired for each column. This
277      * is the column maximum of the cells minimum, preferred, and
278      * maximum requested span.
279      * @param spans the return value of how much to allocated to
280      * each column.
281      * @param offsets the return value of the offset from the
282      * origin for each column.
283      * @return the offset from the origin and the span for each column
284      * in the offsets and spans parameters
285      */

286     protected void layoutColumns(int targetSpan, int[] offsets, int[] spans,
287                  SizeRequirements JavaDoc[] reqs) {
288     // allocate using the convenience method on SizeRequirements
289
SizeRequirements.calculateTiledPositions(targetSpan, null, reqs,
290                          offsets, spans);
291     }
292
293     /**
294      * Perform layout for the minor axis of the box (i.e. the
295      * axis orthoginal to the axis that it represents). The results
296      * of the layout should be placed in the given arrays which represent
297      * the allocations to the children along the minor axis. This
298      * is called by the superclass whenever the layout needs to be
299      * updated along the minor axis.
300      * <p>
301      * This is implemented to call the
302      * <a HREF="#layoutColumns">layoutColumns</a> method, and then
303      * forward to the superclass to actually carry out the layout
304      * of the tables rows.
305      *
306      * @param targetSpan the total span given to the view, which
307      * whould be used to layout the children.
308      * @param axis the axis being layed out.
309      * @param offsets the offsets from the origin of the view for
310      * each of the child views. This is a return value and is
311      * filled in by the implementation of this method.
312      * @param spans the span of each child view. This is a return
313      * value and is filled in by the implementation of this method.
314      * @return the offset and span for each child view in the
315      * offsets and spans parameters
316      */

317     protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
318     // make grid is properly represented
319
updateGrid();
320
321     // all of the row layouts are invalid, so mark them that way
322
int n = getRowCount();
323     for (int i = 0; i < n; i++) {
324         TableRow row = getRow(i);
325         row.layoutChanged(axis);
326     }
327
328     // calculate column spans
329
layoutColumns(targetSpan, columnOffsets, columnSpans, columnRequirements);
330
331     // continue normal layout
332
super.layoutMinorAxis(targetSpan, axis, offsets, spans);
333     }
334
335     /**
336      * Calculate the requirements for the minor axis. This is called by
337      * the superclass whenever the requirements need to be updated (i.e.
338      * a preferenceChanged was messaged through this view).
339      * <p>
340      * This is implemented to calculate the requirements as the sum of the
341      * requirements of the columns.
342      */

343     protected SizeRequirements JavaDoc calculateMinorAxisRequirements(int axis, SizeRequirements JavaDoc r) {
344     updateGrid();
345     
346     // calculate column requirements for each column
347
calculateColumnRequirements(axis);
348
349
350     // the requirements are the sum of the columns.
351
if (r == null) {
352         r = new SizeRequirements JavaDoc();
353     }
354     long min = 0;
355     long pref = 0;
356     long max = 0;
357     for (int i = 0; i < columnRequirements.length; i++) {
358         SizeRequirements JavaDoc req = columnRequirements[i];
359         min += req.minimum;
360         pref += req.preferred;
361         max += req.maximum;
362     }
363     r.minimum = (int) min;
364     r.preferred = (int) pref;
365     r.maximum = (int) max;
366     r.alignment = 0;
367     return r;
368     }
369
370     /*
371     boolean shouldTrace() {
372     AttributeSet a = getElement().getAttributes();
373     Object o = a.getAttribute(HTML.Attribute.ID);
374     if ((o != null) && o.equals("debug")) {
375         return true;
376     }
377     return false;
378     }
379     */

380
381     /**
382      * Calculate the requirements for each column. The calculation
383      * is done as two passes over the table. The table cells that
384      * occupy a single column are scanned first to determine the
385      * maximum of minimum, preferred, and maximum spans along the
386      * give axis. Table cells that span multiple columns are excluded
387      * from the first pass. A second pass is made to determine if
388      * the cells that span multiple columns are satisfied. If the
389      * column requirements are not satisified, the needs of the
390      * multi-column cell is mixed into the existing column requirements.
391      * The calculation of the multi-column distribution is based upon
392      * the proportions of the existing column requirements and taking
393      * into consideration any constraining maximums.
394      */

395     void calculateColumnRequirements(int axis) {
396     // pass 1 - single column cells
397
boolean hasMultiColumn = false;
398     int nrows = getRowCount();
399     for (int i = 0; i < nrows; i++) {
400         TableRow row = getRow(i);
401         int col = 0;
402         int ncells = row.getViewCount();
403         for (int cell = 0; cell < ncells; cell++, col++) {
404         View JavaDoc cv = row.getView(cell);
405         for (; row.isFilled(col); col++); // advance to a free column
406
int rowSpan = getRowsOccupied(cv);
407         int colSpan = getColumnsOccupied(cv);
408         if (colSpan == 1) {
409             checkSingleColumnCell(axis, col, cv);
410         } else {
411             hasMultiColumn = true;
412             col += colSpan - 1;
413         }
414         }
415     }
416
417     // pass 2 - multi-column cells
418
if (hasMultiColumn) {
419         for (int i = 0; i < nrows; i++) {
420         TableRow row = getRow(i);
421         int col = 0;
422         int ncells = row.getViewCount();
423         for (int cell = 0; cell < ncells; cell++, col++) {
424             View JavaDoc cv = row.getView(cell);
425             for (; row.isFilled(col); col++); // advance to a free column
426
int colSpan = getColumnsOccupied(cv);
427             if (colSpan > 1) {
428             checkMultiColumnCell(axis, col, colSpan, cv);
429             col += colSpan - 1;
430             }
431         }
432         }
433     }
434
435     /*
436     if (shouldTrace()) {
437         System.err.println("calc:");
438         for (int i = 0; i < columnRequirements.length; i++) {
439         System.err.println(" " + i + ": " + columnRequirements[i]);
440         }
441     }
442     */

443     }
444
445     /**
446      * check the requirements of a table cell that spans a single column.
447      */

448     void checkSingleColumnCell(int axis, int col, View JavaDoc v) {
449     SizeRequirements JavaDoc req = columnRequirements[col];
450     req.minimum = Math.max((int) v.getMinimumSpan(axis), req.minimum);
451     req.preferred = Math.max((int) v.getPreferredSpan(axis), req.preferred);
452     req.maximum = Math.max((int) v.getMaximumSpan(axis), req.maximum);
453     }
454
455     /**
456      * check the requirements of a table cell that spans multiple
457      * columns.
458      */

459     void checkMultiColumnCell(int axis, int col, int ncols, View JavaDoc v) {
460     // calculate the totals
461
long min = 0;
462     long pref = 0;
463     long max = 0;
464     for (int i = 0; i < ncols; i++) {
465         SizeRequirements JavaDoc req = columnRequirements[col + i];
466         min += req.minimum;
467         pref += req.preferred;
468         max += req.maximum;
469     }
470
471     // check if the minimum size needs adjustment.
472
int cmin = (int) v.getMinimumSpan(axis);
473     if (cmin > min) {
474         /*
475          * the columns that this cell spans need adjustment to fit
476          * this table cell.... calculate the adjustments. The
477          * maximum for each cell is the maximum of the existing
478          * maximum or the amount needed by the cell.
479          */

480         SizeRequirements JavaDoc[] reqs = new SizeRequirements JavaDoc[ncols];
481         for (int i = 0; i < ncols; i++) {
482         SizeRequirements JavaDoc r = reqs[i] = columnRequirements[col + i];
483         r.maximum = Math.max(r.maximum, (int) v.getMaximumSpan(axis));
484         }
485         int[] spans = new int[ncols];
486         int[] offsets = new int[ncols];
487         SizeRequirements.calculateTiledPositions(cmin, null, reqs,
488                              offsets, spans);
489         // apply the adjustments
490
for (int i = 0; i < ncols; i++) {
491         SizeRequirements JavaDoc req = reqs[i];
492         req.minimum = Math.max(spans[i], req.minimum);
493         req.preferred = Math.max(req.minimum, req.preferred);
494         req.maximum = Math.max(req.preferred, req.maximum);
495         }
496     }
497
498     // check if the preferred size needs adjustment.
499
int cpref = (int) v.getPreferredSpan(axis);
500     if (cpref > pref) {
501         /*
502          * the columns that this cell spans need adjustment to fit
503          * this table cell.... calculate the adjustments. The
504          * maximum for each cell is the maximum of the existing
505          * maximum or the amount needed by the cell.
506          */

507         SizeRequirements JavaDoc[] reqs = new SizeRequirements JavaDoc[ncols];
508         for (int i = 0; i < ncols; i++) {
509         SizeRequirements JavaDoc r = reqs[i] = columnRequirements[col + i];
510         }
511         int[] spans = new int[ncols];
512         int[] offsets = new int[ncols];
513         SizeRequirements.calculateTiledPositions(cpref, null, reqs,
514                              offsets, spans);
515         // apply the adjustments
516
for (int i = 0; i < ncols; i++) {
517         SizeRequirements JavaDoc req = reqs[i];
518         req.preferred = Math.max(spans[i], req.preferred);
519         req.maximum = Math.max(req.preferred, req.maximum);
520         }
521     }
522
523     }
524
525     /**
526      * Fetches the child view that represents the given position in
527      * the model. This is implemented to walk through the children
528      * looking for a range that contains the given position. In this
529      * view the children do not necessarily have a one to one mapping
530      * with the child elements.
531      *
532      * @param pos the search position >= 0
533      * @param a the allocation to the table on entry, and the
534      * allocation of the view containing the position on exit
535      * @return the view representing the given position, or
536      * <code>null</code> if there isn't one
537      */

538     protected View JavaDoc getViewAtPosition(int pos, Rectangle a) {
539         int n = getViewCount();
540         for (int i = 0; i < n; i++) {
541             View JavaDoc v = getView(i);
542             int p0 = v.getStartOffset();
543             int p1 = v.getEndOffset();
544             if ((pos >= p0) && (pos < p1)) {
545                 // it's in this view.
546
if (a != null) {
547             childAllocation(i, a);
548         }
549                 return v;
550             }
551         }
552     if (pos == getEndOffset()) {
553         View JavaDoc v = getView(n - 1);
554         if (a != null) {
555         this.childAllocation(n - 1, a);
556         }
557         return v;
558     }
559         return null;
560     }
561
562     // ---- variables ----------------------------------------------------
563

564     int[] columnSpans;
565     int[] columnOffsets;
566     SizeRequirements JavaDoc[] columnRequirements;
567     Vector JavaDoc rows;
568     boolean gridValid;
569     static final private BitSet JavaDoc EMPTY = new BitSet JavaDoc();
570
571     /**
572      * View of a row in a row-centric table.
573      */

574     public class TableRow extends BoxView JavaDoc {
575
576     /**
577      * Constructs a TableView for the given element.
578      *
579      * @param elem the element that this view is responsible for
580      */

581         public TableRow(Element JavaDoc elem) {
582         super(elem, View.X_AXIS);
583         fillColumns = new BitSet JavaDoc();
584     }
585
586     void clearFilledColumns() {
587         fillColumns.and(EMPTY);
588     }
589
590     void fillColumn(int col) {
591         fillColumns.set(col);
592     }
593
594     boolean isFilled(int col) {
595         return fillColumns.get(col);
596     }
597
598     /** get location in the overall set of rows */
599     int getRow() {
600         return row;
601     }
602
603     /**
604      * set location in the overall set of rows, this is
605      * set by the TableView.updateGrid() method.
606      */

607     void setRow(int row) {
608         this.row = row;
609     }
610
611     /**
612      * The number of columns present in this row.
613      */

614     int getColumnCount() {
615         int nfill = 0;
616         int n = fillColumns.size();
617         for (int i = 0; i < n; i++) {
618         if (fillColumns.get(i)) {
619             nfill ++;
620         }
621         }
622         return getViewCount() + nfill;
623     }
624
625     /**
626      * Change the child views. This is implemented to
627      * provide the superclass behavior and invalidate the
628      * grid so that rows and columns will be recalculated.
629      */

630         public void replace(int offset, int length, View JavaDoc[] views) {
631         super.replace(offset, length, views);
632         invalidateGrid();
633     }
634
635     /**
636      * Perform layout for the major axis of the box (i.e. the
637      * axis that it represents). The results of the layout should
638      * be placed in the given arrays which represent the allocations
639      * to the children along the major axis.
640      * <p>
641      * This is re-implemented to give each child the span of the column
642      * width for the table, and to give cells that span multiple columns
643      * the multi-column span.
644      *
645      * @param targetSpan the total span given to the view, which
646      * whould be used to layout the children.
647      * @param axis the axis being layed out.
648      * @param offsets the offsets from the origin of the view for
649      * each of the child views. This is a return value and is
650      * filled in by the implementation of this method.
651      * @param spans the span of each child view. This is a return
652      * value and is filled in by the implementation of this method.
653      * @return the offset and span for each child view in the
654      * offsets and spans parameters
655      */

656         protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
657         int col = 0;
658         int ncells = getViewCount();
659         for (int cell = 0; cell < ncells; cell++, col++) {
660         View JavaDoc cv = getView(cell);
661         for (; isFilled(col); col++); // advance to a free column
662
int colSpan = getColumnsOccupied(cv);
663         spans[cell] = columnSpans[col];
664         offsets[cell] = columnOffsets[col];
665         if (colSpan > 1) {
666             int n = columnSpans.length;
667             for (int j = 1; j < colSpan; j++) {
668             // Because the table may be only partially formed, some
669
// of the columns may not yet exist. Therefore we check
670
// the bounds.
671
if ((col+j) < n) {
672                 spans[cell] += columnSpans[col+j];
673             }
674             }
675             col += colSpan - 1;
676         }
677         }
678     }
679
680     /**
681      * Perform layout for the minor axis of the box (i.e. the
682      * axis orthoginal to the axis that it represents). The results
683      * of the layout should be placed in the given arrays which represent
684      * the allocations to the children along the minor axis. This
685      * is called by the superclass whenever the layout needs to be
686      * updated along the minor axis.
687      * <p>
688      * This is implemented to delegate to the superclass, then adjust
689      * the span for any cell that spans multiple rows.
690      *
691      * @param targetSpan the total span given to the view, which
692      * whould be used to layout the children.
693      * @param axis the axis being layed out.
694      * @param offsets the offsets from the origin of the view for
695      * each of the child views. This is a return value and is
696      * filled in by the implementation of this method.
697      * @param spans the span of each child view. This is a return
698      * value and is filled in by the implementation of this method.
699      * @return the offset and span for each child view in the
700      * offsets and spans parameters
701      */

702         protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
703         super.layoutMinorAxis(targetSpan, axis, offsets, spans);
704         int col = 0;
705         int ncells = getViewCount();
706         for (int cell = 0; cell < ncells; cell++, col++) {
707         View JavaDoc cv = getView(cell);
708         for (; isFilled(col); col++); // advance to a free column
709
int colSpan = getColumnsOccupied(cv);
710         int rowSpan = getRowsOccupied(cv);
711         if (rowSpan > 1) {
712             for (int j = 1; j < rowSpan; j++) {
713             // test bounds of each row because it may not exist
714
// either because of error or because the table isn't
715
// fully loaded yet.
716
int row = getRow() + j;
717             if (row < TableView.this.getViewCount()) {
718                 int span = TableView.this.getSpan(Y_AXIS, getRow()+j);
719                 spans[cell] += span;
720             }
721             }
722         }
723         if (colSpan > 1) {
724             col += colSpan - 1;
725         }
726         }
727     }
728
729     /**
730      * Determines the resizability of the view along the
731      * given axis. A value of 0 or less is not resizable.
732      *
733      * @param axis may be either View.X_AXIS or View.Y_AXIS
734      * @return the resize weight
735      * @exception IllegalArgumentException for an invalid axis
736      */

737         public int getResizeWeight(int axis) {
738         return 1;
739     }
740
741     /**
742      * Fetches the child view that represents the given position in
743      * the model. This is implemented to walk through the children
744      * looking for a range that contains the given position. In this
745      * view the children do not necessarily have a one to one mapping
746      * with the child elements.
747      *
748      * @param pos the search position >= 0
749      * @param a the allocation to the table on entry, and the
750      * allocation of the view containing the position on exit
751      * @return the view representing the given position, or
752      * <code>null</code> if there isn't one
753      */

754         protected View JavaDoc getViewAtPosition(int pos, Rectangle a) {
755         int n = getViewCount();
756         for (int i = 0; i < n; i++) {
757         View JavaDoc v = getView(i);
758         int p0 = v.getStartOffset();
759         int p1 = v.getEndOffset();
760         if ((pos >= p0) && (pos < p1)) {
761             // it's in this view.
762
if (a != null) {
763             childAllocation(i, a);
764             }
765             return v;
766         }
767         }
768         if (pos == getEndOffset()) {
769         View JavaDoc v = getView(n - 1);
770         if (a != null) {
771             this.childAllocation(n - 1, a);
772         }
773         return v;
774         }
775         return null;
776     }
777
778     /** columns filled by multi-column or multi-row cells */
779     BitSet JavaDoc fillColumns;
780     /** the row within the overall grid */
781     int row;
782     }
783
784     /**
785      * @deprecated A table cell can now be any View implementation.
786      */

787     @Deprecated JavaDoc
788     public class TableCell extends BoxView JavaDoc implements GridCell {
789
790     /**
791      * Constructs a TableCell for the given element.
792      *
793      * @param elem the element that this view is responsible for
794      */

795         public TableCell(Element JavaDoc elem) {
796         super(elem, View.Y_AXIS);
797     }
798     
799     // --- GridCell methods -------------------------------------
800

801     /**
802      * Gets the number of columns this cell spans (e.g. the
803      * grid width).
804          *
805          * @return the number of columns
806      */

807     public int getColumnCount() {
808         return 1;
809     }
810
811     /**
812      * Gets the number of rows this cell spans (that is, the
813      * grid height).
814          *
815          * @return the number of rows
816      */

817     public int getRowCount() {
818         return 1;
819     }
820
821
822         /**
823          * Sets the grid location.
824          *
825          * @param row the row >= 0
826          * @param col the column >= 0
827          */

828         public void setGridLocation(int row, int col) {
829             this.row = row;
830             this.col = col;
831     }
832
833     /**
834      * Gets the row of the grid location
835      */

836         public int getGridRow() {
837         return row;
838     }
839
840     /**
841      * Gets the column of the grid location
842      */

843         public int getGridColumn() {
844         return col;
845     }
846
847     int row;
848     int col;
849     }
850
851     /**
852      * <em>
853      * THIS IS NO LONGER USED, AND WILL BE REMOVED IN THE
854      * NEXT RELEASE. THE JCK SIGNATURE TEST THINKS THIS INTERFACE
855      * SHOULD EXIST
856      * </em>
857      */

858     interface GridCell {
859
860         /**
861          * Sets the grid location.
862          *
863          * @param row the row >= 0
864          * @param col the column >= 0
865          */

866         public void setGridLocation(int row, int col);
867
868     /**
869      * Gets the row of the grid location
870      */

871     public int getGridRow();
872
873     /**
874      * Gets the column of the grid location
875      */

876     public int getGridColumn();
877
878     /**
879      * Gets the number of columns this cell spans (e.g. the
880      * grid width).
881          *
882          * @return the number of columns
883      */

884     public int getColumnCount();
885
886     /**
887      * Gets the number of rows this cell spans (that is, the
888      * grid height).
889          *
890          * @return the number of rows
891      */

892     public int getRowCount();
893
894     }
895
896 }
897
Popular Tags