1 |
772 |
jeremybenn |
/* SizeSequence.java --
|
2 |
|
|
Copyright (C) 2002, 2006, Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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.
|
20 |
|
|
|
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.
|
25 |
|
|
|
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. */
|
37 |
|
|
|
38 |
|
|
package javax.swing;
|
39 |
|
|
|
40 |
|
|
import java.util.Arrays;
|
41 |
|
|
|
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).
|
45 |
|
|
*
|
46 |
|
|
* @author Andrew Selkirk
|
47 |
|
|
*/
|
48 |
|
|
public class SizeSequence
|
49 |
|
|
{
|
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.
|
53 |
|
|
|
54 |
|
|
/** Storage for the element sizes. */
|
55 |
|
|
private int[] sizes;
|
56 |
|
|
|
57 |
|
|
/**
|
58 |
|
|
* Creates a new empty <code>SizeSequence</code> instance.
|
59 |
|
|
*/
|
60 |
|
|
public SizeSequence()
|
61 |
|
|
{
|
62 |
|
|
sizes = new int[0];
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
/**
|
66 |
|
|
* Creates a new <code>SizeSequence</code> instance with the specified number
|
67 |
|
|
* of elements, each having a size of 0.
|
68 |
|
|
*
|
69 |
|
|
* @param numEntries the number of elements.
|
70 |
|
|
*/
|
71 |
|
|
public SizeSequence(int numEntries)
|
72 |
|
|
{
|
73 |
|
|
this(numEntries, 0);
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
/**
|
77 |
|
|
* Creates a new <code>SizeSequence</code> instance with the specified number
|
78 |
|
|
* of elements all having the same size (<code>value</code>).
|
79 |
|
|
*
|
80 |
|
|
* @param numEntries the number of elements.
|
81 |
|
|
* @param value the value for each element.
|
82 |
|
|
*/
|
83 |
|
|
public SizeSequence(int numEntries, int value)
|
84 |
|
|
{
|
85 |
|
|
sizes = new int[numEntries];
|
86 |
|
|
Arrays.fill(sizes, value);
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/**
|
90 |
|
|
* Creates a new <code>SizeSequence</code> instance using the specified
|
91 |
|
|
* element sizes.
|
92 |
|
|
*
|
93 |
|
|
* @param sizes the element sizes (<code>null</code> not permitted).
|
94 |
|
|
*/
|
95 |
|
|
public SizeSequence(int[] sizes)
|
96 |
|
|
{
|
97 |
|
|
this.sizes = (int[]) sizes.clone();
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* Sets the size of the element at the specified index.
|
102 |
|
|
*
|
103 |
|
|
* @param index the index.
|
104 |
|
|
* @param size the size.
|
105 |
|
|
*/
|
106 |
|
|
public void setSize(int index, int size)
|
107 |
|
|
{
|
108 |
|
|
if (index >= 0 && index < sizes.length)
|
109 |
|
|
sizes[index] = size;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* Returns the index of the element that contains the specified position.
|
114 |
|
|
*
|
115 |
|
|
* @param position the position.
|
116 |
|
|
*
|
117 |
|
|
* @return The index of the element that contains the specified position.
|
118 |
|
|
*/
|
119 |
|
|
public int getIndex(int position)
|
120 |
|
|
{
|
121 |
|
|
int i = 0;
|
122 |
|
|
int runningTotal = 0;
|
123 |
|
|
while (i < sizes.length && position >= runningTotal + sizes[i])
|
124 |
|
|
{
|
125 |
|
|
runningTotal += sizes[i];
|
126 |
|
|
i++;
|
127 |
|
|
}
|
128 |
|
|
return i;
|
129 |
|
|
}
|
130 |
|
|
|
131 |
|
|
/**
|
132 |
|
|
* Returns the size of the specified element, or 0 if the element index is
|
133 |
|
|
* outside the defined range.
|
134 |
|
|
*
|
135 |
|
|
* @param index the element index.
|
136 |
|
|
*
|
137 |
|
|
* @return The size of the specified element, or 0 if the element index is
|
138 |
|
|
* outside the defined range.
|
139 |
|
|
*/
|
140 |
|
|
public int getSize(int index)
|
141 |
|
|
{
|
142 |
|
|
if (index < 0 || index >= sizes.length)
|
143 |
|
|
return 0;
|
144 |
|
|
return sizes[index];
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
/**
|
148 |
|
|
* Sets the sizes for the elements in the sequence.
|
149 |
|
|
*
|
150 |
|
|
* @param sizes the element sizes (<code>null</code> not permitted).
|
151 |
|
|
*/
|
152 |
|
|
public void setSizes(int[] sizes)
|
153 |
|
|
{
|
154 |
|
|
this.sizes = (int[]) sizes.clone();
|
155 |
|
|
}
|
156 |
|
|
|
157 |
|
|
/**
|
158 |
|
|
* Returns an array containing the sizes for all the elements in the sequence.
|
159 |
|
|
*
|
160 |
|
|
* @return The element sizes.
|
161 |
|
|
*/
|
162 |
|
|
public int[] getSizes()
|
163 |
|
|
{
|
164 |
|
|
return (int[]) sizes.clone();
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
/**
|
168 |
|
|
* Returns the position of the specified element.
|
169 |
|
|
*
|
170 |
|
|
* @param index the element index.
|
171 |
|
|
*
|
172 |
|
|
* @return The position.
|
173 |
|
|
*/
|
174 |
|
|
public int getPosition(int index)
|
175 |
|
|
{
|
176 |
|
|
int position;
|
177 |
|
|
int loop;
|
178 |
|
|
position = 0;
|
179 |
|
|
for (loop = 0; loop < index; loop++)
|
180 |
|
|
position += sizes[loop];
|
181 |
|
|
return position;
|
182 |
|
|
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
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>.
|
189 |
|
|
*
|
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.
|
193 |
|
|
*/
|
194 |
|
|
public void insertEntries(int start, int length, int value)
|
195 |
|
|
{
|
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;
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
/**
|
206 |
|
|
* Removes the element(s) at index <code>start</code> (the number of elements
|
207 |
|
|
* removed is <code>length</code>).
|
208 |
|
|
*
|
209 |
|
|
* @param start the index of the first element to remove.
|
210 |
|
|
* @param length the number of elements to remove.
|
211 |
|
|
*/
|
212 |
|
|
public void removeEntries(int start, int length)
|
213 |
|
|
{
|
214 |
|
|
// Sanity check.
|
215 |
|
|
if ((start + length) > sizes.length)
|
216 |
|
|
throw new IllegalArgumentException("Specified start/length that "
|
217 |
|
|
+ "is greater than available sizes");
|
218 |
|
|
|
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;
|
224 |
|
|
}
|
225 |
|
|
}
|