⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tableview.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	}        protected StyleSheet getStyleSheet() {	    HTMLDocument doc = (HTMLDocument) getDocument();	    return doc.getStyleSheet();	}	/**	 * This is called by a child to indicate its 	 * preferred span has changed.  This is implemented to	 * execute the superclass behavior and well as try to	 * determine if a row with a multi-row cell hangs across	 * this row.  If a multi-row cell covers this row it also	 * needs to propagate a preferenceChanged so that it will	 * recalculate the multi-row cell.	 *	 * @param child the child view	 * @param width true if the width preference should change	 * @param height true if the height preference should change	 */        public void preferenceChanged(View child, boolean width, boolean height) {	    super.preferenceChanged(child, width, height);	    if (TableView.this.multiRowCells && height) {		for (int i = rowIndex  - 1; i >= 0; i--) {		    RowView rv = TableView.this.getRow(i);		    if (rv.multiRowCells) {			rv.preferenceChanged(null, false, true);			break;		    }		}	    }	}        // The major axis requirements for a row are dictated by the column        // requirements. These methods use the value calculated by        // TableView.        protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) {            SizeRequirements req = new SizeRequirements();            req.minimum = totalColumnRequirements.minimum;            req.maximum = totalColumnRequirements.maximum;            req.preferred = totalColumnRequirements.preferred;            req.alignment = 0f;            return req;        }        public float getMinimumSpan(int axis) {            float value;            if (axis == View.X_AXIS) {                value = totalColumnRequirements.minimum + getLeftInset() +                        getRightInset();            }            else {                value = super.getMinimumSpan(axis);            }            return value;        }        public float getMaximumSpan(int axis) {            float value;            if (axis == View.X_AXIS) {                // We're flexible.                value = (float)Integer.MAX_VALUE;            }            else {                value = super.getMaximumSpan(axis);            }            return value;        }        public float getPreferredSpan(int axis) {            float value;            if (axis == View.X_AXIS) {                value = totalColumnRequirements.preferred + getLeftInset() +                        getRightInset();            }            else {                value = super.getPreferredSpan(axis);            }            return value;        }	public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {	    super.changedUpdate(e, a, f);	    int pos = e.getOffset();	    if (pos <= getStartOffset() && (pos + e.getLength()) >=		getEndOffset()) {		RowView.this.setPropertiesFromAttributes();	    }	}		/**	 * Renders using the given rendering surface and area on that	 * surface.  This is implemented to delegate to the css box	 * painter to paint the border and background prior to the 	 * interior.	 *	 * @param g the rendering surface to use	 * @param allocation the allocated region to render into	 * @see View#paint	 */	public void paint(Graphics g, Shape allocation) {	    Rectangle a = (Rectangle) allocation;	    painter.paint(g, a.x, a.y, a.width, a.height, this);	    super.paint(g, a);	}	/**	 * Change the child views.  This is implemented to	 * provide the superclass behavior and invalidate the	 * grid so that rows and columns will be recalculated.	 */        public void replace(int offset, int length, View[] views) {	    super.replace(offset, length, views);	    invalidateGrid();	}	/**	 * Calculate the height requirements of the table row.  The	 * requirements of multi-row cells are not considered for this	 * calculation.  The table itself will check and adjust the row	 * requirements for all the rows that have multi-row cells spanning	 * them.  This method updates the multi-row flag that indicates that	 * this row and rows below need additional consideration.	 */        protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {//	    return super.calculateMinorAxisRequirements(axis, r);	    long min = 0;	    long pref = 0;	    long max = 0;	    multiRowCells = false;	    int n = getViewCount();	    for (int i = 0; i < n; i++) {		View v = getView(i);		if (getRowsOccupied(v) > 1) {		    multiRowCells = true;		    max = Math.max((int) v.getMaximumSpan(axis), max);		} else {		    min = Math.max((int) v.getMinimumSpan(axis), min);		    pref = Math.max((int) v.getPreferredSpan(axis), pref);		    max = Math.max((int) v.getMaximumSpan(axis), max);		}	    }	    if (r == null) {		r = new SizeRequirements();		r.alignment = 0.5f;	    }	    r.preferred = (int) pref;	    r.minimum = (int) min;	    r.maximum = (int) max;	    return r;	}	/**	 * Perform layout for the major axis of the box (i.e. the	 * axis that it represents).  The results of the layout should	 * be placed in the given arrays which represent the allocations	 * to the children along the major axis.  	 * <p>	 * This is re-implemented to give each child the span of the column 	 * width for the table, and to give cells that span multiple columns 	 * the multi-column span.	 *	 * @param targetSpan the total span given to the view, which	 *  whould be used to layout the children	 * @param axis the axis being layed out	 * @param offsets the offsets from the origin of the view for	 *  each of the child views; this is a return value and is	 *  filled in by the implementation of this method	 * @param spans the span of each child view; this is a return	 *  value and is filled in by the implementation of this method	 * @return the offset and span for each child view in the	 *  offsets and spans parameters	 */        protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {	    int col = 0;	    int ncells = getViewCount();	    for (int cell = 0; cell < ncells; cell++) {		View cv = getView(cell);		if (skipComments && !(cv instanceof CellView)) {		    continue;		}		for (; isFilled(col); col++); // advance to a free column		int colSpan = getColumnsOccupied(cv);		spans[cell] = columnSpans[col];		offsets[cell] = columnOffsets[col];		if (colSpan > 1) {		    int n = columnSpans.length;		    for (int j = 1; j < colSpan; j++) {			// Because the table may be only partially formed, some			// of the columns may not yet exist.  Therefore we check			// the bounds.			if ((col+j) < n) {			    spans[cell] += columnSpans[col+j];			    spans[cell] += cellSpacing;			}		    }		    col += colSpan - 1;		}		col++;	    }	}	/**	 * Perform layout for the minor axis of the box (i.e. the	 * axis orthoginal to the axis that it represents).  The results 	 * of the layout should be placed in the given arrays which represent 	 * the allocations to the children along the minor axis.  This 	 * is called by the superclass whenever the layout needs to be 	 * updated along the minor axis.	 * <p>	 * This is implemented to delegate to the superclass, then adjust	 * the span for any cell that spans multiple rows.	 *	 * @param targetSpan the total span given to the view, which	 *  whould be used to layout the children	 * @param axis the axis being layed out	 * @param offsets the offsets from the origin of the view for	 *  each of the child views; this is a return value and is	 *  filled in by the implementation of this method	 * @param spans the span of each child view; this is a return	 *  value and is filled in by the implementation of this method	 * @return the offset and span for each child view in the	 *  offsets and spans parameters	 */        protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {	    super.layoutMinorAxis(targetSpan, axis, offsets, spans);	    int col = 0;	    int ncells = getViewCount();	    for (int cell = 0; cell < ncells; cell++, col++) {		View cv = getView(cell);		for (; isFilled(col); col++); // advance to a free column		int colSpan = getColumnsOccupied(cv);		int rowSpan = getRowsOccupied(cv);		if (rowSpan > 1) {		    		    int row0 = rowIndex;		    int row1 = Math.min(rowIndex + rowSpan - 1, getRowCount()-1);		    spans[cell] = getMultiRowSpan(row0, row1);		}		if (colSpan > 1) {		    col += colSpan - 1;		}	    }	}	/**	 * Determines the resizability of the view along the	 * given axis.  A value of 0 or less is not resizable.	 *	 * @param axis may be either View.X_AXIS or View.Y_AXIS	 * @return the resize weight	 * @exception IllegalArgumentException for an invalid axis	 */        public int getResizeWeight(int axis) {	    return 1;	}	/**	 * Fetches the child view that represents the given position in	 * the model.  This is implemented to walk through the children	 * looking for a range that contains the given position.  In this	 * view the children do not necessarily have a one to one mapping 	 * with the child elements.	 *	 * @param pos  the search position >= 0	 * @param a  the allocation to the table on entry, and the	 *   allocation of the view containing the position on exit	 * @return  the view representing the given position, or 	 *   null if there isn't one	 */        protected View getViewAtPosition(int pos, Rectangle a) {	    int n = getViewCount();	    for (int i = 0; i < n; i++) {		View v = getView(i);		int p0 = v.getStartOffset();		int p1 = v.getEndOffset();		if ((pos >= p0) && (pos < p1)) {		    // it's in this view.		    if (a != null) {			childAllocation(i, a);		    }		    return v;		}	    }	    if (pos == getEndOffset()) {		View v = getView(n - 1);		if (a != null) {		    this.childAllocation(n - 1, a);		}		return v;	    }	    return null;	}	/**	 * Update any cached values that come from attributes.	 */	void setPropertiesFromAttributes() {	    StyleSheet sheet = getStyleSheet();	    attr = sheet.getViewAttributes(this);	    painter = sheet.getBoxPainter(attr);	}	private StyleSheet.BoxPainter painter;        private AttributeSet attr;	/** columns filled by multi-column or multi-row cells */	BitSet fillColumns;	/** 	 * The row index within the overall grid 	 */	int rowIndex;	/**	 * The view index (for row index to view index conversion).	 * This is set by the updateGrid method.	 */	int viewIndex;	/**	 * Does this table row have cells that span multiple rows?	 */	boolean multiRowCells;    }    /**     * Default view of an html table cell.  This needs to be moved     * somewhere else.     */    class CellView extends BlockView {	/**	 * Constructs a TableCell for the given element.	 *	 * @param elem the element that this view is responsible for	 */        public CellView(Element elem) {	    super(elem, Y_AXIS);	}	/**	 * Perform layout for the major axis of the box (i.e. the	 * axis that it represents).  The results of the layout should	 * be placed in the given arrays which represent the allocations	 * to the children along the major axis.  This is called by the	 * superclass to recalculate the positions of the child views	 * when the layout might have changed.	 * <p>	 * This is implemented to delegate to the superclass to	 * tile the children.  If the target span is greater than	 * was needed, the offsets are adjusted to align the children	 * (i.e. position according to the html valign attribute).	 *	 * @param targetSpan the total span given to the view, which	 *  whould be used to layout the children	 * @param axis the axis being layed out	 * @param offsets the offsets from the origin of the view for	 *  each of the child views; this is a return value and is	 *  filled in by the implementation of this method	 * @param spans the span of each child view; this is a return	 *  value and is filled in by the implementation of this method	 * @return the offset and span for each child view in the	 *  offsets and spans parameters	 */        protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {	    super.layoutMajorAxis(targetSpan, axis, offsets, spans);	    // calculate usage	    int used = 0;	    int n = spans.length;	    for (int i = 0; i < n; i++) {		used += spans[i];	    }	    // calculate adjustments	    int adjust = 0;	    if (used < targetSpan) {		// PENDING(prinz) change to use the css alignment.		String valign = (String) getElement().getAttributes().getAttribute(		    HTML.Attribute.VALIGN);		if (valign == null) {		    AttributeSet rowAttr = getElement().getParentElement().getAttributes();		    valign = (String) rowAttr.getAttribute(HTML.Attribute.VALIGN);		}		if ((valign == null) || valign.equals("middle")) {		    adjust = (targetSpan - used) / 2;		} else if (valign.equals("bottom")) {		    adjust = targetSpan - used;		}	    }	    // make adjustments.	    if (adjust != 0) {		for (int i = 0; i < n; i++) {		    offsets[i] += adjust;		}	    }	}	/**	 * Calculate the requirements needed along the major axis.	 * This is called by the superclass whenever the requirements 	 * need to be updated (i.e. a preferenceChanged was messaged 	 * through this view).  	 * <p>	 * This is implemented to delegate to the superclass, but	 * indicate the maximum size is very large (i.e. the cell 	 * is willing to expend to occupy the full height of the row).	 * 	 * @param axis the axis being layed out.	 * @param r the requirements to fill in.  If null, a new one	 *  should be allocated.	 */        protected SizeRequirements calculateMajorAxisRequirements(int axis, 								  SizeRequirements r) {	    SizeRequirements req = super.calculateMajorAxisRequirements(axis, r);	    req.maximum = Integer.MAX_VALUE;	    return req;	}        @Override        protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {            SizeRequirements rv = super.calculateMinorAxisRequirements(axis, r);            //for the cell the minimum should be derived from the child views             //the parent behaviour is to use CSS for that            int n = getViewCount();            int min = 0;            for (int i = 0; i < n; i++) {                View v = getView(i);                min = Math.max((int) v.getMinimumSpan(axis), min);                            }                        rv.minimum = Math.min(rv.minimum, min);            return rv;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -