boxview.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 974 行 · 第 1/3 页

JAVA
974
字号
/* 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;import javax.swing.event.DocumentEvent;/** * 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.   */  private int myAxis;  /**   * Indicates if the layout is valid along X_AXIS or Y_AXIS.   */  private boolean[] layoutValid = new boolean[2];  /**   * The spans along the X_AXIS and Y_AXIS.   */  private int[][] spans = new int[2][];  /**   * The offsets of the children along the X_AXIS and Y_AXIS.   */  private int[][] offsets = new int[2][];  /**   * The size requirements along the X_AXIS and Y_AXIS.   */  private SizeRequirements[] requirements = new SizeRequirements[2];  /**   * The current span along X_AXIS or Y_AXIS.   */  private int[] span = new int[2];  /**   * The SizeRequirements of the child views along the X_AXIS and Y_AXIS.   */  private SizeRequirements[][] childReqs = new SizeRequirements[2][];  /**   * 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;    layoutValid[0] = false;    layoutValid[1] = false;    span[0] = 0;    span[1] = 0;    requirements[0] = new SizeRequirements();    requirements[1] = new SizeRequirements();    // Initialize the cache arrays.    spans[0] = new int[0];    spans[1] = new int[0];    offsets[0] = new int[0];    offsets[1] = new int[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   *   * @since 1.3   */  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   *   * @since 1.3   */  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   *   * @since 1.3   */  public void layoutChanged(int axis)  {    if (axis != X_AXIS && axis != Y_AXIS)      throw new IllegalArgumentException("Invalid axis parameter.");    layoutValid[axis] = false;  }  /**   * 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   *   * @since 1.4   */  protected boolean isLayoutValid(int axis)  {    if (axis != X_AXIS && axis != Y_AXIS)      throw new IllegalArgumentException("Invalid axis parameter.");    return layoutValid[axis];  }  /**   * 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)  {    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 + numViews];    System.arraycopy(spans[X_AXIS], 0, newSpansX, 0, offset);    System.arraycopy(spans[X_AXIS], offset + length, newSpansX,                     offset + numViews,                     oldSize - (offset + length));    spans[X_AXIS] = newSpansX;    // The spansY cache.    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));    spans[Y_AXIS] = newSpansY;    // The offsetsX cache.    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));    offsets[X_AXIS] = newOffsetsX;    // The offsetsY cache.    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));    offsets[Y_AXIS] = 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)  {    Rectangle inside = getInsideAllocation(a);    // TODO: Used for debugging.    //g.drawRect(inside.x, inside.y, inside.width, inside.height);    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)  {    updateRequirements(axis);    return requirements[axis].preferred;  }  /**   * Returns the maximum span of this view along the specified axis.   * This returns <code>Integer.MAX_VALUE</code> for the minor axis   * and the preferred span for the major axis.   *   * @param axis the axis   *   * @return the maximum span of this view along the specified axis   */  public float getMaximumSpan(int axis)  {    float max;    if (axis == myAxis)      max = getPreferredSpan(axis);    else

⌨️ 快捷键说明

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