📄 boxview.java
字号:
* * @param axis the axis that is examined * @param sr the <code>SizeRequirements</code> object to hold the result, * if <code>null</code>, a new one is created * * @return the size requirements for this <code>BoxView</code> along * the specified axis */ protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements sr) { SizeRequirements[] childReqs = getChildRequirements(axis); return SizeRequirements.getTiledSizeRequirements(childReqs); } /** * Calculates the size requirements of this <code>BoxView</code> along * its minor axis, that is the axis opposite to the axis specified in the * constructor. * * @param axis the axis that is examined * @param sr the <code>SizeRequirements</code> object to hold the result, * if <code>null</code>, a new one is created * * @return the size requirements for this <code>BoxView</code> along * the specified axis */ protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements sr) { SizeRequirements[] childReqs = getChildRequirements(axis); return SizeRequirements.getAlignedSizeRequirements(childReqs); } /** * Returns <code>true</code> if the specified point lies before the * given <code>Rectangle</code>, <code>false</code> otherwise. * * "Before" is typically defined as being to the left or above. * * @param x the X coordinate of the point * @param y the Y coordinate of the point * @param r the rectangle to test the point against * * @return <code>true</code> if the specified point lies before the * given <code>Rectangle</code>, <code>false</code> otherwise */ protected boolean isBefore(int x, int y, Rectangle r) { boolean result = false; if (myAxis == X_AXIS) result = x < r.x; else result = y < r.y; return result; } /** * Returns <code>true</code> if the specified point lies after the * given <code>Rectangle</code>, <code>false</code> otherwise. * * "After" is typically defined as being to the right or below. * * @param x the X coordinate of the point * @param y the Y coordinate of the point * @param r the rectangle to test the point against * * @return <code>true</code> if the specified point lies after the * given <code>Rectangle</code>, <code>false</code> otherwise */ protected boolean isAfter(int x, int y, Rectangle r) { boolean result = false; if (myAxis == X_AXIS) result = x > r.x; else result = y > r.y; return result; } /** * Returns the child <code>View</code> at the specified location. * * @param x the X coordinate * @param y the Y coordinate * @param r the inner allocation of this <code>BoxView</code> on entry, * the allocation of the found child on exit * * @return the child <code>View</code> at the specified location */ protected View getViewAtPoint(int x, int y, Rectangle r) { View result = null; int count = getViewCount(); Rectangle copy = new Rectangle(r); for (int i = 0; i < count; ++i) { copy.setBounds(r); childAllocation(i, r); if (copy.contains(x, y)) { result = getView(i); break; } } if (result == null && count > 0) return getView(count - 1); return result; } /** * Computes the allocation for a child <code>View</code>. The parameter * <code>a</code> stores the allocation of this <code>CompositeView</code> * and is then adjusted to hold the allocation of the child view. * * @param index * the index of the child <code>View</code> * @param a * the allocation of this <code>CompositeView</code> before the * call, the allocation of the child on exit */ protected void childAllocation(int index, Rectangle a) { if (! isAllocationValid()) layout(a.width, a.height); a.x += offsetsX[index]; a.y += offsetsY[index]; a.width = spansX[index]; a.height = spansY[index]; } /** * Lays out the children of this <code>BoxView</code> with the specified * bounds. * * @param width the width of the allocated region for the children (that * is the inner allocation of this <code>BoxView</code> * @param height the height of the allocated region for the children (that * is the inner allocation of this <code>BoxView</code> */ protected void layout(int width, int height) { baselineLayout(width, X_AXIS, offsetsX, spansX); baselineLayout(height, Y_AXIS, offsetsY, spansY); } /** * Performs the layout along the major axis of a <code>BoxView</code>. * * @param targetSpan the (inner) span of the <code>BoxView</code> in which * to layout the children * @param axis the axis along which the layout is performed * @param offsets the array that holds the offsets of the children on exit * @param spans the array that holds the spans of the children on exit */ protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets, int[] spans) { SizeRequirements[] childReqs = getChildRequirements(axis); // Calculate the spans and offsets using the SizeRequirements uility // methods. SizeRequirements.calculateTiledPositions(targetSpan, null, childReqs, offsets, spans); validateLayout(axis); } /** * Performs the layout along the minor axis of a <code>BoxView</code>. * * @param targetSpan the (inner) span of the <code>BoxView</code> in which * to layout the children * @param axis the axis along which the layout is performed * @param offsets the array that holds the offsets of the children on exit * @param spans the array that holds the spans of the children on exit */ protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) { SizeRequirements[] childReqs = getChildRequirements(axis); // Calculate the spans and offsets using the SizeRequirements uility // methods. // TODO: This might be an opportunity for performance optimization. Here // we could use a cached instance of SizeRequirements instead of passing // null to baselineRequirements. However, this would involve rewriting // the baselineRequirements() method to not use the SizeRequirements // utility method, since they cannot reuse a cached instance. SizeRequirements total = baselineRequirements(axis, null); SizeRequirements.calculateAlignedPositions(targetSpan, total, childReqs, offsets, spans); validateLayout(axis); } /** * Returns <code>true</code> if the cached allocations for the children * are still valid, <code>false</code> otherwise. * * @return <code>true</code> if the cached allocations for the children * are still valid, <code>false</code> otherwise */ protected boolean isAllocationValid() { return isLayoutValid(X_AXIS) && isLayoutValid(Y_AXIS); } /** * Return the current width of the box. This is the last allocated width. * * @return the current width of the box */ public int getWidth() { return width; } /** * Return the current height of the box. This is the last allocated height. * * @return the current height of the box */ public int getHeight() { return height; } /** * Sets the size of the view. If the actual size has changed, the layout * is updated accordingly. * * @param width the new width * @param height the new height */ public void setSize(float width, float height) { if (this.width != (int) width) layoutChanged(X_AXIS); if (this.height != (int) height) layoutChanged(Y_AXIS); this.width = (int) width; this.height = (int) height; Rectangle outside = new Rectangle(0, 0, this.width, this.height); Rectangle inside = getInsideAllocation(outside); if (!isAllocationValid()) layout(inside.width, inside.height); } /** * Sets the layout to valid for a specific axis. * * @param axis the axis for which to validate the layout */ void validateLayout(int axis) { if (axis == X_AXIS) xLayoutValid = true; if (axis == Y_AXIS) yLayoutValid = true; } /** * Returns the size requirements of this view's children for the major * axis. * * @return the size requirements of this view's children for the major * axis */ SizeRequirements[] getChildRequirements(int axis) { // Allocate SizeRequirements for each child view. int count = getViewCount(); SizeRequirements[] childReqs = new SizeRequirements[count]; for (int i = 0; i < count; ++i) { View view = getView(i); childReqs[i] = new SizeRequirements((int) view.getMinimumSpan(axis), (int) view.getPreferredSpan(axis), (int) view.getMaximumSpan(axis), view.getAlignment(axis)); } return childReqs; } /** * Returns the span for the child view with the given index for the specified * axis. * * @param axis the axis to examine, either <code>X_AXIS</code> or * <code>Y_AXIS</code> * @param childIndex the index of the child for for which to return the span * * @return the span for the child view with the given index for the specified * axis */ protected int getSpan(int axis, int childIndex) { if (axis == X_AXIS) return spansX[childIndex]; else return spansY[childIndex]; } /** * Returns the offset for the child view with the given index for the * specified axis. * * @param axis the axis to examine, either <code>X_AXIS</code> or * <code>Y_AXIS</code> * @param childIndex the index of the child for for which to return the span * * @return the offset for the child view with the given index for the * specified axis */ protected int getOffset(int axis, int childIndex) { if (axis == X_AXIS) return offsetsX[childIndex]; else return offsetsY[childIndex]; } /** * Returns the alignment for this box view for the specified axis. The * axis that is tiled (the major axis) will be requested to be aligned * centered (0.5F). The minor axis alignment depends on the child view's * total alignment. * * @param axis the axis which is examined * * @return the alignment for this box view for the specified axis */ public float getAlignment(int axis) { if (axis == myAxis) return 0.5F; else return baselineRequirements(axis, null).alignment; } /** * Called by a child View when its preferred span has changed. * * @param width indicates that the preferred width of the child changed. * @param height indicates that the preferred height of the child changed. * @param child the child View. */ public void preferenceChanged (View child, boolean width, boolean height) { if (width) xLayoutValid = false; if (height) yLayoutValid = false; super.preferenceChanged(child, width, height); } /** * Maps the document model position <code>pos</code> to a Shape * in the view coordinate space. This method overrides CompositeView's * method to make sure the children are allocated properly before * calling the super's behaviour. */ public Shape modelToView(int pos, Shape a, Position.Bias bias) throws BadLocationException { // Make sure everything is allocated properly and then call super if (!isAllocationValid()) { Rectangle bounds = a.getBounds(); setSize(bounds.width, bounds.height); } return super.modelToView(pos, a, bias); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -