Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / SizeSequence.java
blob26099a1546116b520137dcc382a37d50eaf43c1d
1 /* SizeSequence.java --
2 Copyright (C) 2002, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 /**
41 * A sequence of values that represent the dimensions (widths or heights) of
42 * some collection of items (for example, the widths of the columns in a table).
44 * @author Andrew Selkirk
46 public class SizeSequence
48 // TODO: Sun's API specification for this class contains an implementation
49 // note regarding the encoding for the element sizes. We currently use the
50 // simple size encoding but we should look at improving this.
52 /** Storage for the element sizes. */
53 private int[] sizes;
55 /**
56 * Creates a new empty <code>SizeSequence</code> instance.
58 public SizeSequence()
60 sizes = new int[0];
63 /**
64 * Creates a new <code>SizeSequence</code> instance with the specified number
65 * of elements, each having a size of 0.
67 * @param numEntries the number of elements.
69 public SizeSequence(int numEntries)
71 this(numEntries, 0);
74 /**
75 * Creates a new <code>SizeSequence</code> instance with the specified number
76 * of elements all having the same size (<code>value</code>).
78 * @param numEntries the number of elements.
79 * @param value the value for each element.
81 public SizeSequence(int numEntries, int value)
83 sizes = new int[0];
84 insertEntries(0, numEntries, value);
87 /**
88 * Creates a new <code>SizeSequence</code> instance using the specified
89 * element sizes.
91 * @param sizes the element sizes (<code>null</code> not permitted).
93 public SizeSequence(int[] sizes)
95 this.sizes = (int[]) sizes.clone();
98 /**
99 * Sets the size of the element at the specified index.
101 * @param index the index.
102 * @param size the size.
104 public void setSize(int index, int size)
106 if (index >= 0 && index < sizes.length)
107 sizes[index] = size;
111 * Returns the index of the element that contains the specified position.
113 * @param position the position.
115 * @return The index of the element that contains the specified position.
117 public int getIndex(int position)
119 int i = 0;
120 int runningTotal = 0;
121 while (i < sizes.length && position >= runningTotal + sizes[i])
123 runningTotal += sizes[i];
124 i++;
126 return i;
130 * Returns the size of the specified element.
132 * @param index the element index.
134 * @return The size of the specified element.
136 public int getSize(int index)
138 return sizes[index];
142 * Sets the sizes for the elements in the sequence.
144 * @param sizes the element sizes (<code>null</code> not permitted).
146 public void setSizes(int[] sizes)
148 this.sizes = (int[]) sizes.clone();
152 * Returns an array containing the sizes for all the elements in the sequence.
154 * @return The element sizes.
156 public int[] getSizes()
158 return (int[]) sizes.clone();
162 * Returns the position of the specified element.
164 * @param index the element index.
166 * @return The position.
168 public int getPosition(int index)
170 int position;
171 int loop;
172 position = 0;
173 for (loop = 0; loop < index; loop++)
174 position += sizes[loop];
175 return position;
180 * Inserts new entries into the sequence at the <code>start</code> position.
181 * There are <code>length</code> new entries each having the specified
182 * <code>value</code>.
184 * @param start the start element.
185 * @param length the number of elements to insert.
186 * @param value the size for each of the new elements.
188 public void insertEntries(int start, int length, int value)
190 int[] newSizes = new int[sizes.length + length];
191 System.arraycopy(sizes, 0, newSizes, 0, start);
192 for (int i = start; i < start + length; i++)
193 newSizes[i] = value;
194 System.arraycopy(sizes, start, newSizes, start + length,
195 sizes.length - start);
196 sizes = newSizes;
200 * Removes the element(s) at index <code>start</code> (the number of elements
201 * removed is <code>length</code>).
203 * @param start the index of the first element to remove.
204 * @param length the number of elements to remove.
206 public void removeEntries(int start, int length)
208 // Sanity check.
209 if ((start + length) > sizes.length)
210 throw new IllegalArgumentException("Specified start/length that "
211 + "is greater than available sizes");
213 int[] newSizes = new int[sizes.length - length];
214 System.arraycopy(sizes, 0, newSizes, 0, start);
215 System.arraycopy(sizes, start + length, newSizes, start,
216 sizes.length - start - length);
217 sizes = newSizes;