/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* A view that arranges its children into a box shape by tiling
* its children along an axis. The box is somewhat like that
* found in TeX where there is alignment of the
* children, flexibility of the children is considered, etc.
* This is a building block that might be useful to represent
* things like a collection of lines, paragraphs,
* lists, columns, pages, etc. The axis along which the children are tiled is
* considered the major axis. The orthoginal axis is the minor axis.
* <p>
* Layout for each axis is handled separately by the methods
* <code>layoutMajorAxis</code> and <code>layoutMinorAxis</code>.
* Subclasses can change the layout algorithm by
* reimplementing these methods. These methods will be called
* as necessary depending upon whether or not there is cached
* layout information and the cache is considered
* valid. These methods are typically called if the given size
* along the axis changes, or if <code>layoutChanged</code> is
* called to force an updated layout. The <code>layoutChanged</code>
* method invalidates cached layout information, if there is any.
* The requirements published to the parent view are calculated by
* the methods <code>calculateMajorAxisRequirements</code>
* and <code>calculateMinorAxisRequirements</code>.
* If the layout algorithm is changed, these methods will
* likely need to be reimplemented.
*
* @author Timothy Prinzing
*/
/**
* Constructs a <code>BoxView</code>.
*
* @param elem the element this view is responsible for
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*/
super(elem);
majorOffsets = new int[0];
majorSpans = new int[0];
majorReqValid = false;
majorAllocValid = false;
minorOffsets = new int[0];
minorSpans = new int[0];
minorReqValid = false;
minorAllocValid = false;
}
/**
* Fetches the tile axis property. This is the axis along which
* the child views are tiled.
*
* @return the major axis of the box, either
* <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.3
*/
public int getAxis() {
return majorAxis;
}
/**
* Sets the tile axis property. This is the axis along which
* the child views are tiled.
*
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.3
*/
if (axisChanged) {
preferenceChanged(null, true, true);
}
}
/**
* Invalidates the layout along an axis. This happens
* automatically if the preferences have changed for
* any of the child views. In some cases the layout
* may need to be recalculated when the preferences
* have not changed. The layout can be marked as
* invalid by calling this method. The layout will
* be updated the next time the <code>setSize</code> method
* is called on this view (typically in paint).
*
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.3
*/
majorAllocValid = false;
} else {
minorAllocValid = false;
}
}
/**
* Determines if the layout is valid along the given axis.
*
* @param axis either <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
*
* @since 1.4
*/
return majorAllocValid;
} else {
return minorAllocValid;
}
}
/**
* Paints a child. By default
* that is all it does, but a subclass can use this to paint
* things relative to the child.
*
* @param g the graphics context
* @param alloc the allocated region to paint into
* @param index the child index, >= 0 && < getViewCount()
*/
}
// --- View methods ---------------------------------------------
/**
* Invalidates the layout and resizes the cache of
* requests/allocations. The child allocations can still
* be accessed for the old layout, but the new children
* will have an offset and span of 0.
*
* @param index the starting index into the child views to insert
* the new views; this should be a value >= 0 and <= getViewCount
* @param length the number of existing child views to remove;
* This should be a value >= 0 and <= (getViewCount() - offset)
* @param elems the child views to add; this value can be
* <code>null</code>to indicate no children are being added
* (useful to remove)
*/
// invalidate cache
majorReqValid = false;
majorAllocValid = false;
minorReqValid = false;
minorAllocValid = false;
}
/**
* Resizes the given layout array to match the new number of
* child views. The current number of child views are used to
* produce the new array. The contents of the old array are
* inserted into the new array at the appropriate places so that
* the old layout information is transferred to the new array.
*
* @param oldArray the original layout array
* @param offset location where new views will be inserted
* @param nInserted the number of child views being inserted;
* therefore the number of blank spaces to leave in the
* new array at location <code>offset</code>
* @return the new layout array
*/
int n = getViewCount();
int[] newArray = new int[n];
return newArray;
}
/**
* Forwards the given <code>DocumentEvent</code> to the child views
* that need to be notified of the change to the model.
* If a child changed its requirements and the allocation
* was valid prior to forwarding the portion of the box
* from the starting child to the end of the box will
* be repainted.
*
* @param ec changes to the element this view is responsible
* for (may be <code>null</code> if there were no changes)
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
* @since 1.3
*/
super.forwardUpdate(ec, e, a, f);
// determine if a repaint is needed
// Repaint is needed because one of the tiled children
// have changed their span along the major axis. If there
// is a hosting component and an allocated shape we repaint.
Component c = getContainer();
} else {
}
}
}
}
/**
* This is called by a child to indicate its
* preferred span has changed. This is implemented to
* throw away cached layout information so that new
* calculations will be done the next time the children
* need an allocation.
*
* @param child the child view
* @param width true if the width preference should change
* @param height true if the height preference should change
*/
if (majorChanged) {
majorReqValid = false;
majorAllocValid = false;
}
if (minorChanged) {
minorReqValid = false;
minorAllocValid = false;
}
}
/**
* Gets the resize weight. A value of 0 or less is not resizable.
*
* @param axis may be either <code>View.X_AXIS</code> or
* <code>View.Y_AXIS</code>
* @return the weight
* @exception IllegalArgumentException for an invalid axis
*/
return 1;
}
} else {
return 1;
}
}
return 0;
}
/**
* Sets the size of the view along an axis. This should cause
* layout of the view along the given axis.
*
* @param axis may be either <code>View.X_AXIS</code> or
* <code>View.Y_AXIS</code>
* @param span the span to layout to >= 0
*/
majorAllocValid = false;
}
if (! majorAllocValid) {
// layout the major axis
majorAllocValid = true;
// flush changes to the children
}
} else {
minorAllocValid = false;
}
if (! minorAllocValid) {
// layout the minor axis
minorAllocValid = true;
// flush changes to the children
}
}
}
/**
* Propagates the current allocations to the child views.
*/
void updateChildSizes() {
int n = getViewCount();
for (int i = 0; i < n; i++) {
}
} else {
for (int i = 0; i < n; i++) {
}
}
}
/**
* Returns the size of the view along an axis. This is implemented
* to return zero.
*
* @param axis may be either <code>View.X_AXIS</code> or
* <code>View.Y_AXIS</code>
* @return the current span of the view along the given axis, >= 0
*/
return majorSpan;
} else {
return minorSpan;
}
}
/**
* Sets the size of the view. This should cause
* layout of the view if the view caches any layout
* information. This is implemented to call the
* layout method with the sizes inside of the insets.
*
* @param width the width >= 0
* @param height the height >= 0
*/
}
/**
* Renders the <code>BoxView</code> using the given
* rendering surface and area
* on that surface. Only the children that intersect
* the clip bounds of the given <code>Graphics</code>
* will be rendered.
*
* @param g the rendering surface to use
* @param allocation the allocated region to render into
* @see View#paint
*/
int n = getViewCount();
int x = alloc.x + getLeftInset();
int y = alloc.y + getTopInset();
for (int i = 0; i < n; i++) {
// We should paint views that intersect with clipping region
// even if the intersection has no inside points (is a line).
// This is needed for supporting views that have zero width, like
// views that contain only combining marks.
paintChild(g, tempRect, i);
}
}
}
/**
* Fetches the allocation for the given child view.
* This enables finding out where various views
* are located. This is implemented to return
* <code>null</code> if the layout is invalid,
* otherwise the superclass behavior is executed.
*
* @param index the index of the child, >= 0 && < getViewCount()
* @param a the allocation to this view
* @return the allocation to the child; or <code>null</code>
* if <code>a</code> is <code>null</code>;
* or <code>null</code> if the layout is invalid
*/
if (a != null) {
// The child allocation may not have been set yet.
return null;
}
}
return ca;
}
return null;
}
/**
* Provides a mapping from the document model coordinate space
* to the coordinate space of the view mapped to it. This makes
* sure the allocation is valid before calling the superclass.
*
* @param pos the position to convert >= 0
* @param a the allocated region to render into
* @return the bounding box of the given position
* @exception BadLocationException if the given position does
* not represent a valid location in the associated document
* @see View#modelToView
*/
if (! isAllocationValid()) {
}
return super.modelToView(pos, a, b);
}
/**
* Provides a mapping from the view coordinate space to the logical
* coordinate space of the model.
*
* @param x x coordinate of the view location to convert >= 0
* @param y y coordinate of the view location to convert >= 0
* @param a the allocated region to render into
* @return the location within the model that best represents the
* given point in the view >= 0
* @see View#viewToModel
*/
if (! isAllocationValid()) {
}
return super.viewToModel(x, y, a, bias);
}
/**
* Determines the desired alignment for this view along an
* axis. This is implemented to give the total alignment
* needed to position the children with the alignment points
* lined up along the axis orthoginal to the axis that is
* being tiled. The axis being tiled will request to be
* centered (i.e. 0.5f).
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the desired alignment >= 0.0f && <= 1.0f; this should
* be a value between 0.0 and 1.0 where 0 indicates alignment at the
* origin and 1.0 indicates alignment to the full span
* away from the origin; an alignment of 0.5 would be the
* center of the view
* @exception IllegalArgumentException for an invalid axis
*/
return majorRequest.alignment;
} else {
return minorRequest.alignment;
}
}
/**
* Determines the preferred span for this view along an
* axis.
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the span the view would like to be rendered into >= 0;
* typically the view is told to render into the span
* that is returned, although there is no guarantee;
* the parent may choose to resize or break the view
* @exception IllegalArgumentException for an invalid axis type
*/
getTopInset() + getBottomInset();
} else {
}
}
/**
* Determines the minimum span for this view along an
* axis.
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the span the view would like to be rendered into >= 0;
* typically the view is told to render into the span
* that is returned, although there is no guarantee;
* the parent may choose to resize or break the view
* @exception IllegalArgumentException for an invalid axis type
*/
getTopInset() + getBottomInset();
} else {
}
}
/**
* Determines the maximum span for this view along an
* axis.
*
* @param axis may be either <code>View.X_AXIS</code>
* or <code>View.Y_AXIS</code>
* @return the span the view would like to be rendered into >= 0;
* typically the view is told to render into the span
* that is returned, although there is no guarantee;
* the parent may choose to resize or break the view
* @exception IllegalArgumentException for an invalid axis type
*/
getTopInset() + getBottomInset();
} else {
}
}
// --- local methods ----------------------------------------------------
/**
* Are the allocations for the children still
* valid?
*
* @return true if allocations still valid
*/
protected boolean isAllocationValid() {
return (majorAllocValid && minorAllocValid);
}
/**
* Determines if a point falls before an allocated region.
*
* @param x the X coordinate >= 0
* @param y the Y coordinate >= 0
* @param innerAlloc the allocated region; this is the area
* inside of the insets
* @return true if the point lies before the region else false
*/
return (x < innerAlloc.x);
} else {
return (y < innerAlloc.y);
}
}
/**
* Determines if a point falls after an allocated region.
*
* @param x the X coordinate >= 0
* @param y the Y coordinate >= 0
* @param innerAlloc the allocated region; this is the area
* inside of the insets
* @return true if the point lies after the region else false
*/
} else {
}
}
/**
* Fetches the child view at the given coordinates.
*
* @param x the X coordinate >= 0
* @param y the Y coordinate >= 0
* @param alloc the parents inner allocation on entry, which should
* be changed to the childs allocation on exit
* @return the view
*/
int n = getViewCount();
return getView(0);
}
for (int i = 0; i < n; i++) {
if (x < (alloc.x + majorOffsets[i])) {
return getView(i - 1);
}
}
return getView(n - 1);
} else {
return getView(0);
}
for (int i = 0; i < n; i++) {
if (y < (alloc.y + majorOffsets[i])) {
return getView(i - 1);
}
}
return getView(n - 1);
}
}
/**
* Allocates a region for a child view.
*
* @param index the index of the child view to
* allocate, >= 0 && < getViewCount()
* @param alloc the allocated region
*/
}
/**
* Perform layout on the box
*
* @param width the width (inside of the insets) >= 0
* @param height the height (inside of the insets) >= 0
*/
}
/**
* Returns the current width of the box. This is the width that
* it was last allocated.
* @return the current width of the box
*/
public int getWidth() {
int span;
} else {
}
return span;
}
/**
* Returns the current height of the box. This is the height that
* it was last allocated.
* @return the current height of the box
*/
public int getHeight() {
int span;
} else {
}
return span;
}
/**
* Performs layout for the major axis of the box (i.e. the
* axis that it represents). The results of the layout (the
* offset and span for each children) are placed in the given
* arrays which represent the allocations to the children
* along the major axis.
*
* @param targetSpan the total span given to the view, which
* would 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
*/
/*
* first pass, calculate the preferred sizes
* and the flexibility to adjust the sizes.
*/
long preferred = 0;
int n = getViewCount();
for (int i = 0; i < n; i++) {
}
/*
* Second pass, expand or contract by as much as possible to reach
* the target span.
*/
// determine the adjustment to be made
float adjustmentFactor = 0.0f;
if (desiredAdjustment != 0) {
long totalSpan = 0;
diffs = new int[n];
for (int i = 0; i < n; i++) {
int tmp;
if (desiredAdjustment < 0) {
} else {
}
}
}
// make the adjustments
int totalOffset = 0;
for (int i = 0; i < n; i++) {
offsets[i] = totalOffset;
if (desiredAdjustment != 0) {
}
}
}
/**
* Performs layout for the minor axis of the box (i.e. the
* axis orthoginal to the axis that it represents). The results
* of the layout (the offset and span for each children) are
* placed in the given arrays which represent the allocations to
* the children along the minor axis.
*
* @param targetSpan the total span given to the view, which
* would 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
*/
int n = getViewCount();
for (int i = 0; i < n; i++) {
if (max < targetSpan) {
// can't make the child this wide, align it
} else {
// make it the target width, or as small as it can get.
offsets[i] = 0;
}
}
}
/**
* Calculates the size requirements for the major axis
* <code>axis</code>.
*
* @param axis the axis being studied
* @param r the <code>SizeRequirements</code> object;
* if <code>null</code> one will be created
* @return the newly initialized <code>SizeRequirements</code> object
* @see javax.swing.SizeRequirements
*/
// calculate tiled request
float min = 0;
float pref = 0;
float max = 0;
int n = getViewCount();
for (int i = 0; i < n; i++) {
}
if (r == null) {
r = new SizeRequirements();
}
r.alignment = 0.5f;
return r;
}
/**
* Calculates the size requirements for the minor axis
* <code>axis</code>.
*
* @param axis the axis being studied
* @param r the <code>SizeRequirements</code> object;
* if <code>null</code> one will be created
* @return the newly initialized <code>SizeRequirements</code> object
* @see javax.swing.SizeRequirements
*/
int min = 0;
long pref = 0;
int n = getViewCount();
for (int i = 0; i < n; i++) {
}
if (r == null) {
r = new SizeRequirements();
r.alignment = 0.5f;
}
return r;
}
/**
* Checks the request cache and update if needed.
* @param axis the axis being studied
* @exception IllegalArgumentException if <code>axis</code> is
* neither <code>View.X_AXIS</code> nor <code>View.Y_AXIS</code>
*/
}
if (!majorReqValid) {
majorReqValid = true;
}
} else if (! minorReqValid) {
minorReqValid = true;
}
}
/**
* Computes the location and extent of each child view
* in this <code>BoxView</code> given the <code>targetSpan</code>,
* which is the width (or height) of the region we have to
* work with.
*
* @param targetSpan the total span given to the view, which
* would be used to layout the children
* @param axis the axis being studied, either
* <code>View.X_AXIS</code> or <code>View.Y_AXIS</code>
* @param offsets an empty array filled by this method with
* values specifying the location of each child view
* @param spans an empty array filled by this method with
* values specifying the extent of each child view
*/
int n = getViewCount();
for (int i = 0; i < n; i++) {
float viewSpan;
// if resizable then resize to the best fit
// the smallest span possible
// the largest span possible
if (align == 0.0f) {
// if the alignment is 0 then we need to fit into the descent
} else if (align == 1.0f) {
// if the alignment is 1 then we need to fit into the ascent
} else {
// figure out the span that we must fit into
// fit into the calculated span
}
} else {
// otherwise use the preferred spans
}
}
}
/**
* Calculates the size requirements for this <code>BoxView</code>
* by examining the size of each child view.
*
* @param axis the axis being studied
* @param r the <code>SizeRequirements</code> object;
* if <code>null</code> one will be created
* @return the newly initialized <code>SizeRequirements</code> object
*/
if (r == null) {
r = new SizeRequirements();
}
r.alignment = 0.5f;
int n = getViewCount();
// loop through all children calculating the max of all their ascents and
// descents at minimum, preferred, and maximum sizes
for (int i = 0; i < n; i++) {
float span;
int ascent;
int descent;
// find the maximum of the preferred ascents and descents
// if the view is resizable then do the same for the minimum and
// maximum ascents and descents
} else {
// otherwise use the preferred
}
}
// we now have an overall preferred, minimum, and maximum ascent and descent
// calculate the preferred span as the sum of the preferred ascent and preferred descent
// calculate the preferred alignment as the preferred ascent divided by the preferred span
if (r.preferred > 0) {
}
if (r.alignment == 0.0f) {
// if the preferred alignment is 0 then the minimum and maximum spans are simply
// the minimum and maximum descents since there's nothing above the baseline
} else if (r.alignment == 1.0f) {
// if the preferred alignment is 1 then the minimum and maximum spans are simply
// the minimum and maximum ascents since there's nothing below the baseline
} else {
// we want to honor the preferred alignment so we calculate two possible minimum
// span values using 1) the minimum ascent and the alignment, and 2) the minimum
// descent and the alignment. We'll choose the larger of these two numbers.
// a similar calculation is made for the maximum but we choose the smaller number.
}
return r;
}
/**
* Fetches the offset of a particular child's current layout.
* @param axis the axis being studied
* @param childIndex the index of the requested child
* @return the offset (location) for the specified child
*/
return offsets[childIndex];
}
/**
* Fetches the span of a particular childs current layout.
* @param axis the axis being studied
* @param childIndex the index of the requested child
* @return the span (width or height) of the specified child
*/
return spans[childIndex];
}
/**
* Determines in which direction the next view lays.
* Consider the View at index n. Typically the <code>View</code>s
* are layed out from left to right, so that the <code>View</code>
* to the EAST will be at index n + 1, and the <code>View</code>
* to the WEST will be at index n - 1. In certain situations,
* such as with bidirectional text, it is possible
* that the <code>View</code> to EAST is not at index n + 1,
* but rather at index n - 1, or that the <code>View</code>
* to the WEST is not at index n - 1, but index n + 1.
* In this case this method would return true,
* indicating the <code>View</code>s are layed out in
* descending order. Otherwise the method would return false
* indicating the <code>View</code>s are layed out in ascending order.
* <p>
* If the receiver is laying its <code>View</code>s along the
* <code>Y_AXIS</code>, this will will return the value from
* invoking the same method on the <code>View</code>
* responsible for rendering <code>position</code> and
* <code>bias</code>. Otherwise this will return false.
*
* @param position position into the model
* @param bias either <code>Position.Bias.Forward</code> or
* <code>Position.Bias.Backward</code>
* @return true if the <code>View</code>s surrounding the
* <code>View</code> responding for rendering
* <code>position</code> and <code>bias</code>
* are layed out in descending order; otherwise false
*/
if(index != -1) {
if(v != null && v instanceof CompositeView) {
bias);
}
}
}
return false;
}
// --- variables ------------------------------------------------
int majorAxis;
int majorSpan;
int minorSpan;
/*
* Request cache
*/
boolean majorReqValid;
boolean minorReqValid;
/*
* Allocation cache
*/
boolean majorAllocValid;
int[] majorOffsets;
int[] majorSpans;
boolean minorAllocValid;
int[] minorOffsets;
int[] minorSpans;
/** used in paint. */
}