0N/A/*
3909N/A * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/A/**
0N/A * A <code>SizeSequence</code> object
0N/A * efficiently maintains an ordered list
0N/A * of sizes and corresponding positions.
0N/A * One situation for which <code>SizeSequence</code>
0N/A * might be appropriate is in a component
0N/A * that displays multiple rows of unequal size.
0N/A * In this case, a single <code>SizeSequence</code>
0N/A * object could be used to track the heights
0N/A * and Y positions of all rows.
0N/A * <p>
0N/A * Another example would be a multi-column component,
0N/A * such as a <code>JTable</code>,
0N/A * in which the column sizes are not all equal.
0N/A * The <code>JTable</code> might use a single
0N/A * <code>SizeSequence</code> object
0N/A * to store the widths and X positions of all the columns.
0N/A * The <code>JTable</code> could then use the
0N/A * <code>SizeSequence</code> object
0N/A * to find the column corresponding to a certain position.
0N/A * The <code>JTable</code> could update the
0N/A * <code>SizeSequence</code> object
0N/A * whenever one or more column sizes changed.
0N/A *
0N/A * <p>
0N/A * The following figure shows the relationship between size and position data
0N/A * for a multi-column component.
0N/A * <p>
0N/A * <center>
0N/A * <img src="doc-files/SizeSequence-1.gif" width=384 height = 100
0N/A * alt="The first item begins at position 0, the second at the position equal
0N/A to the size of the previous item, and so on.">
0N/A * </center>
0N/A * <p>
0N/A * In the figure, the first index (0) corresponds to the first column,
0N/A * the second index (1) to the second column, and so on.
0N/A * The first column's position starts at 0,
0N/A * and the column occupies <em>size<sub>0</sub></em> pixels,
0N/A * where <em>size<sub>0</sub></em> is the value returned by
0N/A * <code>getSize(0)</code>.
0N/A * Thus, the first column ends at <em>size<sub>0</sub></em> - 1.
0N/A * The second column then begins at
0N/A * the position <em>size<sub>0</sub></em>
0N/A * and occupies <em>size<sub>1</sub></em> (<code>getSize(1)</code>) pixels.
0N/A * <p>
0N/A * Note that a <code>SizeSequence</code> object simply represents intervals
0N/A * along an axis.
0N/A * In our examples, the intervals represent height or width in pixels.
0N/A * However, any other unit of measure (for example, time in days)
0N/A * could be just as valid.
0N/A *
0N/A * <p>
0N/A *
0N/A * <h4>Implementation Notes</h4>
0N/A *
0N/A * Normally when storing the size and position of entries,
0N/A * one would choose between
0N/A * storing the sizes or storing their positions
0N/A * instead. The two common operations that are needed during
0N/A * rendering are: <code>getIndex(position)</code>
0N/A * and <code>setSize(index, size)</code>.
0N/A * Whichever choice of internal format is made one of these
0N/A * operations is costly when the number of entries becomes large.
0N/A * If sizes are stored, finding the index of the entry
0N/A * that encloses a particular position is linear in the
0N/A * number of entries. If positions are stored instead, setting
0N/A * the size of an entry at a particular index requires updating
0N/A * the positions of the affected entries, which is also a linear
0N/A * calculation.
0N/A * <p>
0N/A * Like the above techniques this class holds an array of N integers
0N/A * internally but uses a hybrid encoding, which is halfway
0N/A * between the size-based and positional-based approaches.
0N/A * The result is a data structure that takes the same space to store
0N/A * the information but can perform most operations in Log(N) time
0N/A * instead of O(N), where N is the number of entries in the list.
0N/A * <p>
0N/A * Two operations that remain O(N) in the number of entries are
0N/A * the <code>insertEntries</code>
0N/A * and <code>removeEntries</code> methods, both
0N/A * of which are implemented by converting the internal array to
0N/A * a set of integer sizes, copying it into the new array, and then
0N/A * reforming the hybrid representation in place.
0N/A *
0N/A * @author Philip Milne
0N/A * @since 1.3
0N/A */
0N/A
0N/A/*
0N/A * Each method is implemented by taking the minimum and
0N/A * maximum of the range of integers that need to be operated
0N/A * upon. All the algorithms work by dividing this range
0N/A * into two smaller ranges and recursing. The recursion
0N/A * is terminated when the upper and lower bounds are equal.
0N/A */
0N/A
0N/Apublic class SizeSequence {
0N/A
0N/A private static int[] emptyArray = new int[0];
0N/A private int a[];
0N/A
0N/A /**
0N/A * Creates a new <code>SizeSequence</code> object
0N/A * that contains no entries. To add entries, you
0N/A * can use <code>insertEntries</code> or <code>setSizes</code>.
0N/A *
0N/A * @see #insertEntries
3370N/A * @see #setSizes(int[])
0N/A */
0N/A public SizeSequence() {
0N/A a = emptyArray;
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>SizeSequence</code> object
0N/A * that contains the specified number of entries,
0N/A * all initialized to have size 0.
0N/A *
0N/A * @param numEntries the number of sizes to track
0N/A * @exception NegativeArraySizeException if
0N/A * <code>numEntries < 0</code>
0N/A */
0N/A public SizeSequence(int numEntries) {
0N/A this(numEntries, 0);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>SizeSequence</code> object
0N/A * that contains the specified number of entries,
0N/A * all initialized to have size <code>value</code>.
0N/A *
0N/A * @param numEntries the number of sizes to track
0N/A * @param value the initial value of each size
0N/A */
0N/A public SizeSequence(int numEntries, int value) {
0N/A this();
0N/A insertEntries(0, numEntries, value);
0N/A }
0N/A
0N/A /**
0N/A * Creates a new <code>SizeSequence</code> object
0N/A * that contains the specified sizes.
0N/A *
0N/A * @param sizes the array of sizes to be contained in
0N/A * the <code>SizeSequence</code>
0N/A */
0N/A public SizeSequence(int[] sizes) {
0N/A this();
0N/A setSizes(sizes);
0N/A }
0N/A
0N/A /**
0N/A * Resets the size sequence to contain <code>length</code> items
0N/A * all with a size of <code>size</code>.
0N/A */
0N/A void setSizes(int length, int size) {
0N/A if (a.length != length) {
0N/A a = new int[length];
0N/A }
0N/A setSizes(0, length, size);
0N/A }
0N/A
0N/A private int setSizes(int from, int to, int size) {
0N/A if (to <= from) {
0N/A return 0;
0N/A }
0N/A int m = (from + to)/2;
0N/A a[m] = size + setSizes(from, m, size);
0N/A return a[m] + setSizes(m + 1, to, size);
0N/A }
0N/A
0N/A /**
0N/A * Resets this <code>SizeSequence</code> object,
0N/A * using the data in the <code>sizes</code> argument.
0N/A * This method reinitializes this object so that it
0N/A * contains as many entries as the <code>sizes</code> array.
0N/A * Each entry's size is initialized to the value of the
0N/A * corresponding item in <code>sizes</code>.
0N/A *
0N/A * @param sizes the array of sizes to be contained in
0N/A * this <code>SizeSequence</code>
0N/A */
0N/A public void setSizes(int[] sizes) {
0N/A if (a.length != sizes.length) {
0N/A a = new int[sizes.length];
0N/A }
0N/A setSizes(0, a.length, sizes);
0N/A }
0N/A
0N/A private int setSizes(int from, int to, int[] sizes) {
0N/A if (to <= from) {
0N/A return 0;
0N/A }
0N/A int m = (from + to)/2;
0N/A a[m] = sizes[m] + setSizes(from, m, sizes);
0N/A return a[m] + setSizes(m + 1, to, sizes);
0N/A }
0N/A
0N/A /**
0N/A * Returns the size of all entries.
0N/A *
0N/A * @return a new array containing the sizes in this object
0N/A */
0N/A public int[] getSizes() {
0N/A int n = a.length;
0N/A int[] sizes = new int[n];
0N/A getSizes(0, n, sizes);
0N/A return sizes;
0N/A }
0N/A
0N/A private int getSizes(int from, int to, int[] sizes) {
0N/A if (to <= from) {
0N/A return 0;
0N/A }
0N/A int m = (from + to)/2;
0N/A sizes[m] = a[m] - getSizes(from, m, sizes);
0N/A return a[m] + getSizes(m + 1, to, sizes);
0N/A }
0N/A
0N/A /**
0N/A * Returns the start position for the specified entry.
0N/A * For example, <code>getPosition(0)</code> returns 0,
0N/A * <code>getPosition(1)</code> is equal to
0N/A * <code>getSize(0)</code>,
0N/A * <code>getPosition(2)</code> is equal to
0N/A * <code>getSize(0)</code> + <code>getSize(1)</code>,
0N/A * and so on.
0N/A * <p>Note that if <code>index</code> is greater than
0N/A * <code>length</code> the value returned may
0N/A * be meaningless.
0N/A *
0N/A * @param index the index of the entry whose position is desired
0N/A * @return the starting position of the specified entry
0N/A */
0N/A public int getPosition(int index) {
0N/A return getPosition(0, a.length, index);
0N/A }
0N/A
0N/A private int getPosition(int from, int to, int index) {
0N/A if (to <= from) {
0N/A return 0;
0N/A }
0N/A int m = (from + to)/2;
0N/A if (index <= m) {
0N/A return getPosition(from, m, index);
0N/A }
0N/A else {
0N/A return a[m] + getPosition(m + 1, to, index);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the entry
0N/A * that corresponds to the specified position.
0N/A * For example, <code>getIndex(0)</code> is 0,
0N/A * since the first entry always starts at position 0.
0N/A *
0N/A * @param position the position of the entry
0N/A * @return the index of the entry that occupies the specified position
0N/A */
0N/A public int getIndex(int position) {
0N/A return getIndex(0, a.length, position);
0N/A }
0N/A
0N/A private int getIndex(int from, int to, int position) {
0N/A if (to <= from) {
0N/A return from;
0N/A }
0N/A int m = (from + to)/2;
0N/A int pivot = a[m];
0N/A if (position < pivot) {
0N/A return getIndex(from, m, position);
0N/A }
0N/A else {
0N/A return getIndex(m + 1, to, position - pivot);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the size of the specified entry.
0N/A * If <code>index</code> is out of the range
0N/A * <code>(0 <= index < getSizes().length)</code>
0N/A * the behavior is unspecified.
0N/A *
0N/A * @param index the index corresponding to the entry
0N/A * @return the size of the entry
0N/A */
0N/A public int getSize(int index) {
0N/A return getPosition(index + 1) - getPosition(index);
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of the specified entry.
0N/A * Note that if the value of <code>index</code>
0N/A * does not fall in the range:
0N/A * <code>(0 <= index < getSizes().length)</code>
0N/A * the behavior is unspecified.
0N/A *
0N/A * @param index the index corresponding to the entry
0N/A * @param size the size of the entry
0N/A */
0N/A public void setSize(int index, int size) {
0N/A changeSize(0, a.length, index, size - getSize(index));
0N/A }
0N/A
0N/A private void changeSize(int from, int to, int index, int delta) {
0N/A if (to <= from) {
0N/A return;
0N/A }
0N/A int m = (from + to)/2;
0N/A if (index <= m) {
0N/A a[m] += delta;
0N/A changeSize(from, m, index, delta);
0N/A }
0N/A else {
0N/A changeSize(m + 1, to, index, delta);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Adds a contiguous group of entries to this <code>SizeSequence</code>.
0N/A * Note that the values of <code>start</code> and
0N/A * <code>length</code> must satisfy the following
0N/A * conditions: <code>(0 <= start < getSizes().length)
0N/A * AND (length >= 0)</code>. If these conditions are
0N/A * not met, the behavior is unspecified and an exception
0N/A * may be thrown.
0N/A *
0N/A * @param start the index to be assigned to the first entry
0N/A * in the group
0N/A * @param length the number of entries in the group
0N/A * @param value the size to be assigned to each new entry
0N/A * @exception ArrayIndexOutOfBoundsException if the parameters
0N/A * are outside of the range:
0N/A * (<code>0 <= start < (getSizes().length)) AND (length >= 0)</code>
0N/A */
0N/A public void insertEntries(int start, int length, int value) {
0N/A int sizes[] = getSizes();
0N/A int end = start + length;
0N/A int n = a.length + length;
0N/A a = new int[n];
0N/A for (int i = 0; i < start; i++) {
0N/A a[i] = sizes[i] ;
0N/A }
0N/A for (int i = start; i < end; i++) {
0N/A a[i] = value ;
0N/A }
0N/A for (int i = end; i < n; i++) {
0N/A a[i] = sizes[i-length] ;
0N/A }
0N/A setSizes(a);
0N/A }
0N/A
0N/A /**
0N/A * Removes a contiguous group of entries
0N/A * from this <code>SizeSequence</code>.
0N/A * Note that the values of <code>start</code> and
0N/A * <code>length</code> must satisfy the following
0N/A * conditions: <code>(0 <= start < getSizes().length)
0N/A * AND (length >= 0)</code>. If these conditions are
0N/A * not met, the behavior is unspecified and an exception
0N/A * may be thrown.
0N/A *
0N/A * @param start the index of the first entry to be removed
0N/A * @param length the number of entries to be removed
0N/A */
0N/A public void removeEntries(int start, int length) {
0N/A int sizes[] = getSizes();
0N/A int end = start + length;
0N/A int n = a.length - length;
0N/A a = new int[n];
0N/A for (int i = 0; i < start; i++) {
0N/A a[i] = sizes[i] ;
0N/A }
0N/A for (int i = start; i < n; i++) {
0N/A a[i] = sizes[i+length] ;
0N/A }
0N/A setSizes(a);
0N/A }
0N/A}