📄 boxview.java
字号:
/* BoxView.java -- An composite view Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.Shape;import javax.swing.SizeRequirements;/** * An implementation of {@link CompositeView} that arranges its children in * a box along one axis. This is comparable to how the <code>BoxLayout</code> * works, but for <code>View</code> children. * * @author Roman Kennke (roman@kennke.org) */public class BoxView extends CompositeView{ /** * The axis along which this <code>BoxView</code> is laid out. */ int myAxis; /** * Indicates wether the layout in X_AXIS is valid. */ boolean xLayoutValid; /** * Indicates whether the layout in Y_AXIS is valid. */ boolean yLayoutValid; /** * The spans in X direction of the children. */ int[] spansX; /** * The spans in Y direction of the children. */ int[] spansY; /** * The offsets of the children in X direction relative to this BoxView's * inner bounds. */ 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; /** * Creates a new <code>BoxView</code> for the given * <code>Element</code> and axis. Valid values for the axis are * {@link View#X_AXIS} and {@link View#Y_AXIS}. * * @param element the element that is rendered by this BoxView * @param axis the axis along which the box is laid out */ public BoxView(Element element, int axis) { super(element); myAxis = axis; xLayoutValid = false; yLayoutValid = false; // Initialize the cache arrays. spansX = new int[0]; spansY = new int[0]; offsetsX = new int[0]; offsetsY = new int[0]; width = 0; height = 0; } /** * Returns the axis along which this <code>BoxView</code> is laid out. * * @return the axis along which this <code>BoxView</code> is laid out */ public int getAxis() { return myAxis; } /** * Sets the axis along which this <code>BoxView</code> is laid out. * * Valid values for the axis are {@link View#X_AXIS} and * {@link View#Y_AXIS}. * * @param axis the axis along which this <code>BoxView</code> is laid out */ public void setAxis(int axis) { myAxis = axis; } /** * Marks the layout along the specified axis as invalid. This is triggered * automatically when any of the child view changes its preferences * via {@link #preferenceChanged(View, boolean, boolean)}. * * The layout will be updated the next time when * {@link #setSize(float, float)} is called, typically from within the * {@link #paint(Graphics, Shape)} method. * * Valid values for the axis are {@link View#X_AXIS} and * {@link View#Y_AXIS}. * * @param axis an <code>int</code> value */ 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."); } } /** * Returns <code>true</code> if the layout along the specified * <code>axis</code> is valid, <code>false</code> otherwise. * * Valid values for the axis are {@link View#X_AXIS} and * {@link View#Y_AXIS}. * * @param axis the axis * * @return <code>true</code> if the layout along the specified * <code>axis</code> is valid, <code>false</code> otherwise */ 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; } /** * Paints the child <code>View</code> at the specified <code>index</code>. * This method modifies the actual values in <code>alloc</code> so make * sure you have a copy of the original values if you need them. * * @param g the <code>Graphics</code> context to paint to * @param alloc the allocated region for the child to paint into * @param index the index of the child to be painted * * @see #childAllocation(int, Rectangle) */ protected void paintChild(Graphics g, Rectangle alloc, int index) { View child = getView(index); child.paint(g, alloc); } /** * Replaces child views by some other child views. If there are no views to * remove (<code>length == 0</code>), the result is a simple insert, if * there are no children to add (<code>view == null</code>) the result * is a simple removal. * * In addition this invalidates the layout and resizes the internal cache * for the child allocations. The old children's cached allocations can * still be accessed (although they are not guaranteed to be valid), and * the new children will have an initial offset and span of 0. * * @param offset the start offset from where to remove children * @param length the number of children to remove * @param views the views that replace the removed children */ public void replace(int offset, int length, View[] views) { // 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, oldSize - (offset + length)); spansX = 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, oldSize - (offset + length)); spansY = 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, oldSize - (offset + length)); offsetsX = 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, oldSize - (offset + length)); offsetsY = newOffsetsY; // Actually perform the replace. super.replace(offset, length, views); // Invalidate layout information. layoutChanged(X_AXIS); layoutChanged(Y_AXIS); } /** * Renders the <code>Element</code> that is associated with this * <code>View</code>. * * @param g the <code>Graphics</code> context to render to * @param a the allocated region for the <code>Element</code> */ public void paint(Graphics g, Shape a) { // Adjust size if the size is changed. Rectangle bounds = a.getBounds(); if (bounds.width != getWidth() || bounds.height != getHeight()) setSize(bounds.width, bounds.height); Rectangle inside = getInsideAllocation(a); Rectangle copy = new Rectangle(inside); int count = getViewCount(); for (int i = 0; i < count; ++i) { copy.setBounds(inside); childAllocation(i, copy); if (!copy.isEmpty() && g.hitClip(copy.x, copy.y, copy.width, copy.height)) paintChild(g, copy, i); } } /** * Returns the preferred span of the content managed by this * <code>View</code> along the specified <code>axis</code>. * * @param axis the axis * * @return the preferred span of this <code>View</code>. */ public float getPreferredSpan(int axis) { SizeRequirements sr = new SizeRequirements(); int pref = baselineRequirements(axis, sr).preferred; return (float) pref; } public float getMaximumSpan(int axis) { if (axis == getAxis()) return getPreferredSpan(axis); else return Integer.MAX_VALUE; } /** * Calculates the size requirements for this <code>BoxView</code> along * the specified axis. * * @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 baselineRequirements(int axis, SizeRequirements sr) { SizeRequirements result; if (axis == myAxis) result = calculateMajorAxisRequirements(axis, sr); else result = calculateMinorAxisRequirements(axis, sr); return result; } /** * Calculates the layout of the children of this <code>BoxView</code> along * the specified axis. * * @param span the target span * @param axis the axis that is examined * @param offsets an empty array, filled with the offsets of the children * @param spans an empty array, filled with the spans of the children */ 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); } /** * Calculates the size requirements of this <code>BoxView</code> along * its major axis, that is the axis specified in the constructor.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -