2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / libjava / classpath / javax / swing / SizeSequence.java
blob69c7366be42c37b76fb375267636d7caf7011d2b
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 import java.util.Arrays;
42 /**
43 * A sequence of values that represent the dimensions (widths or heights) of
44 * some collection of items (for example, the widths of the columns in a table).
46 * @author Andrew Selkirk
48 public class SizeSequence
50 // TODO: Sun's API specification for this class contains an implementation
51 // note regarding the encoding for the element sizes. We currently use the
52 // simple size encoding but we should look at improving this.
54 /** Storage for the element sizes. */
55 private int[] sizes;
57 /**
58 * Creates a new empty <code>SizeSequence</code> instance.
60 public SizeSequence()
62 sizes = new int[0];
65 /**
66 * Creates a new <code>SizeSequence</code> instance with the specified number
67 * of elements, each having a size of 0.
69 * @param numEntries the number of elements.
71 public SizeSequence(int numEntries)
73 this(numEntries, 0);
76 /**
77 * Creates a new <code>SizeSequence</code> instance with the specified number
78 * of elements all having the same size (<code>value</code>).
80 * @param numEntries the number of elements.
81 * @param value the value for each element.
83 public SizeSequence(int numEntries, int value)
85 sizes = new int[numEntries];
86 Arrays.fill(sizes, value);
89 /**
90 * Creates a new <code>SizeSequence</code> instance using the specified
91 * element sizes.
93 * @param sizes the element sizes (<code>null</code> not permitted).
95 public SizeSequence(int[] sizes)
97 this.sizes = (int[]) sizes.clone();
101 * Sets the size of the element at the specified index.
103 * @param index the index.
104 * @param size the size.
106 public void setSize(int index, int size)
108 if (index >= 0 && index < sizes.length)
109 sizes[index] = size;
113 * Returns the index of the element that contains the specified position.
115 * @param position the position.
117 * @return The index of the element that contains the specified position.
119 public int getIndex(int position)
121 int i = 0;
122 int runningTotal = 0;
123 while (i < sizes.length && position >= runningTotal + sizes[i])
125 runningTotal += sizes[i];
126 i++;
128 return i;
132 * Returns the size of the specified element, or 0 if the element index is
133 * outside the defined range.
135 * @param index the element index.
137 * @return The size of the specified element, or 0 if the element index is
138 * outside the defined range.
140 public int getSize(int index)
142 if (index < 0 || index >= sizes.length)
143 return 0;
144 return sizes[index];
148 * Sets the sizes for the elements in the sequence.
150 * @param sizes the element sizes (<code>null</code> not permitted).
152 public void setSizes(int[] sizes)
154 this.sizes = (int[]) sizes.clone();
158 * Returns an array containing the sizes for all the elements in the sequence.
160 * @return The element sizes.
162 public int[] getSizes()
164 return (int[]) sizes.clone();
168 * Returns the position of the specified element.
170 * @param index the element index.
172 * @return The position.
174 public int getPosition(int index)
176 int position;
177 int loop;
178 position = 0;
179 for (loop = 0; loop < index; loop++)
180 position += sizes[loop];
181 return position;
186 * Inserts new entries into the sequence at the <code>start</code> position.
187 * There are <code>length</code> new entries each having the specified
188 * <code>value</code>.
190 * @param start the start element.
191 * @param length the number of elements to insert.
192 * @param value the size for each of the new elements.
194 public void insertEntries(int start, int length, int value)
196 int[] newSizes = new int[sizes.length + length];
197 System.arraycopy(sizes, 0, newSizes, 0, start);
198 for (int i = start; i < start + length; i++)
199 newSizes[i] = value;
200 System.arraycopy(sizes, start, newSizes, start + length,
201 sizes.length - start);
202 sizes = newSizes;
206 * Removes the element(s) at index <code>start</code> (the number of elements
207 * removed is <code>length</code>).
209 * @param start the index of the first element to remove.
210 * @param length the number of elements to remove.
212 public void removeEntries(int start, int length)
214 // Sanity check.
215 if ((start + length) > sizes.length)
216 throw new IllegalArgumentException("Specified start/length that "
217 + "is greater than available sizes");
219 int[] newSizes = new int[sizes.length - length];
220 System.arraycopy(sizes, 0, newSizes, 0, start);
221 System.arraycopy(sizes, start + length, newSizes, start,
222 sizes.length - start - length);
223 sizes = newSizes;