diff options
author | Roman Kennke <roman@kennke.org> | 2006-02-09 14:28:49 +0000 |
---|---|---|
committer | Roman Kennke <roman@kennke.org> | 2006-02-09 14:28:49 +0000 |
commit | 40ac5688039396b966d1c7e663bb2d0ff6d41169 (patch) | |
tree | 39bd121c43bfec46054eb61d4c9bd437379b8c84 /javax/swing/text/BoxView.java | |
parent | b66ff2508143bc2d27bb7771cbb40b09a7542497 (diff) | |
download | classpath-40ac5688039396b966d1c7e663bb2d0ff6d41169.tar.gz |
2006-02-09 Roman Kennke <kennke@aicas.com>
* javax/swing/text/BoxView.java
(myAxis): Made field private.
(xLayoutValid): Replaced by layoutValid array.
(yLayoutValid): Replaced by layoutValid array.
(layoutValid): New field.
(spansX): Replaced by spans array.
(spansY): Replaced by spans array.
(spans): New field.
(offsetsX): Replaced by offsets array.
(offsetsY): Replaced by offsets array.
(offsets): New field.
(requirements): New field.
(BoxView): Initialize new fields.
(layoutChanged): Rewritten to use the layoutValid array.
(isLayoutValid): Rewritten to use the layoutValid array.
(replace): Use the new arrays.
(getPreferredSpan): Rewritten to call calculateXXXRequirements
instead of baselineRequirements.
(baselineRequirements): Rewritten to calculate baseline requirements.
(baselineLayout): Rewritten to calculate baseline layout.
(childAllocation): Use new arrays.
(layout): Rewritten. Only update the layout if necessary.
(layoutMajorAxis): Directly set layoutValid.
(layoutMinorAxis): Directly set layoutValid. Use cached size
requirements.
(getWidth): Use new span array.
(getHeight): Likewise.
(setSize): Rewritten to simply call layout().
(validateLayout): Removed unneeded method.
(getSpan): Use new arrays.
(getOffset): Use new arrays.
(getAlignment): Use cached requirements if possible.
(preferenceChanged): Use new arrays.
* javax/swing/text/FlowView.java
(FlowStrategy.insertUpdate): Do nothing here.
(FlowStrategy.removeUpdate): Do nothing here.
(FlowStrategy.changedUpdate): Do nothing here.
(FlowStrategy.layoutRow): Rewritten.
(FlowStrategy.createView): Rewritten.
(FlowStrategy.adjustRow): New method.
(LogicalView.getViewIndex): Fixed condition for finding child
view.
(layoutDirty): New field indicating the state of the layout.
(FlowView): Initialize new field.
(loadChildren): Set parent on logical view so that preferenceChanges
get propagated upwards.
(layout): Rewritten to match the specs.
(insertUpdate): Set layout to dirty.
(removeUpdate): Set layout to dirty.
(changedUpdate): Set layout to dirty.
* javax/swing/text/GlyphView.java
(getBreakWeight): Rewritten to use the Utilities class. Commented
out though because that is broken.
(insertUpdate): Call preferenceChanged on this object instead of
parent.
* javax/swing/text/ParagraphView.java
(Row.loadChildren): Overridden to be a noop to prevent initial
creation of child views. This is carried out by the flow layout.
* javax/swing/text/View.java
(getPreferredSpan): Added API docs.
(getResizeWeight): Added API docs.
(getMaximumSpan): Added API docs. Rewritten to only have one exit
point.
(getMinimumSpan): Added API docs. Rewritten to return 0 when
resizable instead of Integer.MAX_VALUE.
(getAlignment): Added API docs.
(replace): Added API docs.
(forwardUpdate): Rewritten to only notify child views that need to
be notified.
Diffstat (limited to 'javax/swing/text/BoxView.java')
-rw-r--r-- | javax/swing/text/BoxView.java | 283 |
1 files changed, 131 insertions, 152 deletions
diff --git a/javax/swing/text/BoxView.java b/javax/swing/text/BoxView.java index 5c9587dfe..0dc429bcc 100644 --- a/javax/swing/text/BoxView.java +++ b/javax/swing/text/BoxView.java @@ -58,49 +58,32 @@ public class BoxView /** * The axis along which this <code>BoxView</code> is laid out. */ - int myAxis; + private int myAxis; /** - * Indicates wether the layout in X_AXIS is valid. + * Indicates if the layout is valid along X_AXIS or Y_AXIS. */ - boolean xLayoutValid; + private boolean[] layoutValid = new boolean[2]; /** - * Indicates whether the layout in Y_AXIS is valid. + * The spans along the X_AXIS and Y_AXIS. */ - boolean yLayoutValid; + private int[][] spans = new int[2][]; /** - * The spans in X direction of the children. + * The offsets of the children along the X_AXIS and Y_AXIS. */ - int[] spansX; + private int[][] offsets = new int[2][]; /** - * The spans in Y direction of the children. + * The size requirements along the X_AXIS and Y_AXIS. */ - int[] spansY; + private SizeRequirements[] requirements = new SizeRequirements[2]; /** - * The offsets of the children in X direction relative to this BoxView's - * inner bounds. + * The current span along X_AXIS or Y_AXIS. */ - int[] offsetsX; - - /** - * The offsets of the children in Y direction relative to this BoxView's - * inner bounds. - */ - int[] offsetsY; - - /** - * The current width. - */ - int width; - - /** - * The current height. - */ - int height; + private int[] span = new int[2]; /** * Creates a new <code>BoxView</code> for the given @@ -114,17 +97,18 @@ public class BoxView { super(element); myAxis = axis; - xLayoutValid = false; - yLayoutValid = false; + layoutValid[0] = false; + layoutValid[1] = false; + span[0] = 0; + span[1] = 0; + requirements[0] = new SizeRequirements(); + requirements[1] = new SizeRequirements(); // Initialize the cache arrays. - spansX = new int[0]; - spansY = new int[0]; - offsetsX = new int[0]; - offsetsY = new int[0]; - - width = 0; - height = 0; + spans[0] = new int[0]; + spans[1] = new int[0]; + offsets[0] = new int[0]; + offsets[1] = new int[0]; } /** @@ -166,17 +150,9 @@ public class BoxView */ public void layoutChanged(int axis) { - switch (axis) - { - case X_AXIS: - xLayoutValid = false; - break; - case Y_AXIS: - yLayoutValid = false; - break; - default: - throw new IllegalArgumentException("Invalid axis parameter."); - } + if (axis != X_AXIS && axis != Y_AXIS) + throw new IllegalArgumentException("Invalid axis parameter."); + layoutValid[axis] = false; } /** @@ -193,19 +169,9 @@ public class BoxView */ protected boolean isLayoutValid(int axis) { - boolean valid = false; - switch (axis) - { - case X_AXIS: - valid = xLayoutValid; - break; - case Y_AXIS: - valid = yLayoutValid; - break; - default: - throw new IllegalArgumentException("Invalid axis parameter."); - } - return valid; + if (axis != X_AXIS && axis != Y_AXIS) + throw new IllegalArgumentException("Invalid axis parameter."); + return layoutValid[axis]; } /** @@ -242,40 +208,44 @@ public class BoxView */ public void replace(int offset, int length, View[] views) { + int numViews = 0; + if (views != null) + numViews = views.length; + // Resize and copy data for cache arrays. // The spansX cache. int oldSize = getViewCount(); - int[] newSpansX = new int[oldSize - length + views.length]; - System.arraycopy(spansX, 0, newSpansX, 0, offset); - System.arraycopy(spansX, offset + length, newSpansX, - offset + views.length, + int[] newSpansX = new int[oldSize - length + numViews]; + System.arraycopy(spans[X_AXIS], 0, newSpansX, 0, offset); + System.arraycopy(spans[X_AXIS], offset + length, newSpansX, + offset + numViews, oldSize - (offset + length)); - spansX = newSpansX; + spans[X_AXIS] = newSpansX; // The spansY cache. - int[] newSpansY = new int[oldSize - length + views.length]; - System.arraycopy(spansY, 0, newSpansY, 0, offset); - System.arraycopy(spansY, offset + length, newSpansY, - offset + views.length, + int[] newSpansY = new int[oldSize - length + numViews]; + System.arraycopy(spans[Y_AXIS], 0, newSpansY, 0, offset); + System.arraycopy(spans[Y_AXIS], offset + length, newSpansY, + offset + numViews, oldSize - (offset + length)); - spansY = newSpansY; + spans[Y_AXIS] = newSpansY; // The offsetsX cache. - int[] newOffsetsX = new int[oldSize - length + views.length]; - System.arraycopy(offsetsX, 0, newOffsetsX, 0, offset); - System.arraycopy(offsetsX, offset + length, newOffsetsX, - offset + views.length, + int[] newOffsetsX = new int[oldSize - length + numViews]; + System.arraycopy(offsets[X_AXIS], 0, newOffsetsX, 0, offset); + System.arraycopy(offsets[X_AXIS], offset + length, newOffsetsX, + offset + numViews, oldSize - (offset + length)); - offsetsX = newOffsetsX; + offsets[X_AXIS] = newOffsetsX; // The offsetsY cache. - int[] newOffsetsY = new int[oldSize - length + views.length]; - System.arraycopy(offsetsY, 0, newOffsetsY, 0, offset); - System.arraycopy(offsetsY, offset + length, newOffsetsY, - offset + views.length, + int[] newOffsetsY = new int[oldSize - length + numViews]; + System.arraycopy(offsets[Y_AXIS], 0, newOffsetsY, 0, offset); + System.arraycopy(offsets[Y_AXIS], offset + length, newOffsetsY, + offset + numViews, oldSize - (offset + length)); - offsetsY = newOffsetsY; + offsets[Y_AXIS] = newOffsetsY; // Actually perform the replace. super.replace(offset, length, views); @@ -323,9 +293,16 @@ public class BoxView */ public float getPreferredSpan(int axis) { - SizeRequirements sr = new SizeRequirements(); - int pref = baselineRequirements(axis, sr).preferred; - return (float) pref; + if (!isLayoutValid(axis)) + { + if (axis == myAxis) + requirements[axis] = calculateMajorAxisRequirements(axis, + requirements[axis]); + else + requirements[axis] = calculateMinorAxisRequirements(axis, + requirements[axis]); + } + return requirements[axis].preferred; } public float getMaximumSpan(int axis) @@ -337,8 +314,9 @@ public class BoxView } /** - * Calculates the size requirements for this <code>BoxView</code> along - * the specified axis. + * This method is obsolete and no longer in use. It is replaced by + * {@link #calculateMajorAxisRequirements(int, SizeRequirements)} and + * {@link #calculateMinorAxisRequirements(int, SizeRequirements)}. * * @param axis the axis that is examined * @param sr the <code>SizeRequirements</code> object to hold the result, @@ -350,12 +328,8 @@ public class BoxView protected SizeRequirements baselineRequirements(int axis, SizeRequirements sr) { - SizeRequirements result; - if (axis == myAxis) - result = calculateMajorAxisRequirements(axis, sr); - else - result = calculateMinorAxisRequirements(axis, sr); - return result; + SizeRequirements[] childReqs = getChildRequirements(axis); + return SizeRequirements.getAlignedSizeRequirements(childReqs); } /** @@ -370,10 +344,12 @@ public class BoxView protected void baselineLayout(int span, int axis, int[] offsets, int[] spans) { - if (axis == myAxis) - layoutMajorAxis(span, axis, offsets, spans); - else - layoutMinorAxis(span, axis, offsets, spans); + SizeRequirements[] childReqs = getChildRequirements(axis); + // Calculate the spans and offsets using the SizeRequirements uility + // methods. + SizeRequirements.calculateAlignedPositions(span, requirements[axis], + childReqs, offsets, spans); + layoutValid[axis] = true; } /** @@ -412,6 +388,7 @@ public class BoxView SizeRequirements[] childReqs = getChildRequirements(axis); return SizeRequirements.getAlignedSizeRequirements(childReqs); } + /** * Returns <code>true</code> if the specified point lies before the @@ -511,10 +488,10 @@ public class BoxView if (! isAllocationValid()) layout(a.width, a.height); - a.x += offsetsX[index]; - a.y += offsetsY[index]; - a.width = spansX[index]; - a.height = spansY[index]; + a.x += offsets[X_AXIS][index]; + a.y += offsets[Y_AXIS][index]; + a.width = spans[X_AXIS][index]; + a.height = spans[Y_AXIS][index]; } /** @@ -528,8 +505,35 @@ public class BoxView */ protected void layout(int width, int height) { - baselineLayout(width, X_AXIS, offsetsX, spansX); - baselineLayout(height, Y_AXIS, offsetsY, spansY); + int[] newSpan = new int[]{ width, height }; + + // Update major axis as appropriate. + if (! isLayoutValid(myAxis) || newSpan[myAxis] != span[myAxis]) + { + requirements[myAxis] = calculateMajorAxisRequirements(myAxis, + requirements[myAxis]); + layoutMajorAxis(newSpan[myAxis], myAxis, offsets[myAxis], + spans[myAxis]); + span[myAxis] = newSpan[myAxis]; + } + + // Update minor axis as appropriate. + int minorAxis = myAxis == X_AXIS ? Y_AXIS : X_AXIS; + if (! isLayoutValid(minorAxis)) + { + requirements[minorAxis] = calculateMinorAxisRequirements(minorAxis, + requirements[minorAxis]); + layoutMinorAxis(newSpan[minorAxis], minorAxis, offsets[minorAxis], + spans[minorAxis]); + span[myAxis] = newSpan[myAxis]; + } + + // Update the child view's sizes. + int count = getViewCount(); + for (int i = 0; i < count; ++i) + { + getView(i).setSize(spans[X_AXIS][i], spans[Y_AXIS][i]); + } } /** @@ -549,7 +553,7 @@ public class BoxView // methods. SizeRequirements.calculateTiledPositions(targetSpan, null, childReqs, offsets, spans); - validateLayout(axis); + layoutValid[axis] = true; } /** @@ -567,15 +571,9 @@ public class BoxView 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); + SizeRequirements.calculateAlignedPositions(targetSpan, requirements[axis], + childReqs, offsets, spans); + layoutValid[axis] = true; } /** @@ -597,7 +595,7 @@ public class BoxView */ public int getWidth() { - return width; + return span[X_AXIS]; } /** @@ -607,7 +605,7 @@ public class BoxView */ public int getHeight() { - return height; + return span[Y_AXIS]; } /** @@ -619,31 +617,7 @@ public class BoxView */ 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; + layout((int) width, (int) height); } /** @@ -653,7 +627,7 @@ public class BoxView * @return the size requirements of this view's children for the major * axis */ - SizeRequirements[] getChildRequirements(int axis) + private SizeRequirements[] getChildRequirements(int axis) { // Allocate SizeRequirements for each child view. int count = getViewCount(); @@ -682,10 +656,9 @@ public class BoxView */ protected int getSpan(int axis, int childIndex) { - if (axis == X_AXIS) - return spansX[childIndex]; - else - return spansY[childIndex]; + if (axis != X_AXIS && axis != Y_AXIS) + throw new IllegalArgumentException("Illegal axis argument"); + return spans[axis][childIndex]; } /** @@ -701,10 +674,9 @@ public class BoxView */ protected int getOffset(int axis, int childIndex) { - if (axis == X_AXIS) - return offsetsX[childIndex]; - else - return offsetsY[childIndex]; + if (axis != X_AXIS && axis != Y_AXIS) + throw new IllegalArgumentException("Illegal axis argument"); + return offsets[axis][childIndex]; } /** @@ -719,10 +691,17 @@ public class BoxView */ public float getAlignment(int axis) { + float align; if (axis == myAxis) - return 0.5F; + align = 0.5F; else - return baselineRequirements(axis, null).alignment; + { + if (! isLayoutValid(axis)) + requirements[axis] = calculateMinorAxisRequirements(axis, + requirements[axis]); + align = requirements[axis].alignment; + } + return align; } /** @@ -735,9 +714,9 @@ public class BoxView public void preferenceChanged (View child, boolean width, boolean height) { if (width) - xLayoutValid = false; + layoutValid[X_AXIS] = false; if (height) - yLayoutValid = false; + layoutValid[Y_AXIS] = false; super.preferenceChanged(child, width, height); } |