|
@@ -36,8 +36,7 @@ |
36 |
36 |
|
* all at their boundaries), return the undistributed width.</li> |
37 |
37 |
|
* </ol> |
38 |
38 |
|
*/ |
39 |
|
- |
class ColumnResizer |
40 |
|
- |
{ |
|
39 |
+ |
class ColumnResizer { |
41 |
40 |
|
/** |
42 |
41 |
|
* The resolution of the width of columns. The true target width of a column |
43 |
42 |
|
* is usually a decimal, but column widths can only be represented as integers |
|
@@ -53,8 +52,7 @@ |
53 |
52 |
|
/** |
54 |
53 |
|
* A class that contains the current and desired width of a column. |
55 |
54 |
|
*/ |
56 |
|
- |
static class ColumnWidthInfo |
57 |
|
- |
{ |
|
55 |
+ |
static class ColumnWidthInfo { |
58 |
56 |
|
private int minWidth; |
59 |
57 |
|
private int maxWidth; |
60 |
58 |
|
private int preferredWidth; |
|
@@ -78,71 +76,58 @@ |
78 |
76 |
|
* @param preferredWidth the preferred width of the column |
79 |
77 |
|
* @param curWidth the current width of the column |
80 |
78 |
|
*/ |
81 |
|
- |
public ColumnWidthInfo( int minWidth, int maxWidth, int preferredWidth, int curWidth ) |
82 |
|
- |
{ |
|
79 |
+ |
public ColumnWidthInfo( int minWidth, int maxWidth, int preferredWidth, int curWidth ) { |
83 |
80 |
|
this.minWidth = minWidth; |
84 |
81 |
|
this.maxWidth = maxWidth; |
85 |
82 |
|
this.preferredWidth = preferredWidth; |
86 |
83 |
|
this.curWidth = curWidth; |
87 |
84 |
|
} |
88 |
85 |
|
|
89 |
|
- |
public int getCurrentWidth() |
90 |
|
- |
{ |
|
86 |
+ |
public int getCurrentWidth() { |
91 |
87 |
|
return curWidth; |
92 |
88 |
|
} |
93 |
89 |
|
|
94 |
|
- |
public int getMaximumWidth() |
95 |
|
- |
{ |
|
90 |
+ |
public int getMaximumWidth() { |
96 |
91 |
|
// For calculation purposes, ensure maxWidth >= minWidth |
97 |
|
- |
if ( hasMaximumWidth() ) |
98 |
|
- |
{ |
|
92 |
+ |
if ( hasMaximumWidth() ) { |
99 |
93 |
|
return Math.max( maxWidth, minWidth ); |
100 |
94 |
|
} |
101 |
95 |
|
return maxWidth; |
102 |
96 |
|
} |
103 |
97 |
|
|
104 |
|
- |
public int getMinimumWidth() |
105 |
|
- |
{ |
|
98 |
+ |
public int getMinimumWidth() { |
106 |
99 |
|
return minWidth; |
107 |
100 |
|
} |
108 |
101 |
|
|
109 |
|
- |
public int getNewWidth() |
110 |
|
- |
{ |
|
102 |
+ |
public int getNewWidth() { |
111 |
103 |
|
return newWidth; |
112 |
104 |
|
} |
113 |
105 |
|
|
114 |
|
- |
public int getPreferredWidth() |
115 |
|
- |
{ |
|
106 |
+ |
public int getPreferredWidth() { |
116 |
107 |
|
return preferredWidth; |
117 |
108 |
|
} |
118 |
109 |
|
|
119 |
|
- |
public boolean hasMaximumWidth() |
120 |
|
- |
{ |
|
110 |
+ |
public boolean hasMaximumWidth() { |
121 |
111 |
|
return maxWidth >= 0; |
122 |
112 |
|
} |
123 |
113 |
|
|
124 |
|
- |
public boolean hasMinimumWidth() |
125 |
|
- |
{ |
|
114 |
+ |
public boolean hasMinimumWidth() { |
126 |
115 |
|
return minWidth >= 0; |
127 |
116 |
|
} |
128 |
117 |
|
|
129 |
|
- |
public void setCurrentWidth( int curWidth ) |
130 |
|
- |
{ |
|
118 |
+ |
public void setCurrentWidth( int curWidth ) { |
131 |
119 |
|
this.curWidth = curWidth; |
132 |
120 |
|
} |
133 |
121 |
|
|
134 |
|
- |
public void setMaximumWidth( int maxWidth ) |
135 |
|
- |
{ |
|
122 |
+ |
public void setMaximumWidth( int maxWidth ) { |
136 |
123 |
|
this.maxWidth = maxWidth; |
137 |
124 |
|
} |
138 |
125 |
|
|
139 |
|
- |
public void setMinimumWidth( int minWidth ) |
140 |
|
- |
{ |
|
126 |
+ |
public void setMinimumWidth( int minWidth ) { |
141 |
127 |
|
this.minWidth = minWidth; |
142 |
128 |
|
} |
143 |
129 |
|
|
144 |
|
- |
public void setPreferredWidth( int preferredWidth ) |
145 |
|
- |
{ |
|
130 |
+ |
public void setPreferredWidth( int preferredWidth ) { |
146 |
131 |
|
this.preferredWidth = preferredWidth; |
147 |
132 |
|
} |
148 |
133 |
|
|
|
@@ -154,23 +139,19 @@ |
154 |
139 |
|
* |
155 |
140 |
|
* @return the percentage difference |
156 |
141 |
|
*/ |
157 |
|
- |
double getPercentageDifference() |
158 |
|
- |
{ |
|
142 |
+ |
double getPercentageDifference() { |
159 |
143 |
|
return (newWidth - preferredWidth) / (double) preferredWidth; |
160 |
144 |
|
} |
161 |
145 |
|
|
162 |
|
- |
int getRequiredWidth() |
163 |
|
- |
{ |
|
146 |
+ |
int getRequiredWidth() { |
164 |
147 |
|
return requiredWidth; |
165 |
148 |
|
} |
166 |
149 |
|
|
167 |
|
- |
void setNewWidth( int newWidth ) |
168 |
|
- |
{ |
|
150 |
+ |
void setNewWidth( int newWidth ) { |
169 |
151 |
|
this.newWidth = newWidth; |
170 |
152 |
|
} |
171 |
153 |
|
|
172 |
|
- |
void setRequiredWidth( int requiredWidth ) |
173 |
|
- |
{ |
|
154 |
+ |
void setRequiredWidth( int requiredWidth ) { |
174 |
155 |
|
this.requiredWidth = requiredWidth; |
175 |
156 |
|
} |
176 |
157 |
|
} |
|
@@ -186,18 +167,13 @@ |
186 |
167 |
|
* |
187 |
168 |
|
* @return the width that could not be distributed |
188 |
169 |
|
*/ |
189 |
|
- |
public int distributeWidth( List<ColumnWidthInfo> columns, int width ) |
190 |
|
- |
{ |
|
170 |
+ |
public int distributeWidth( List<ColumnWidthInfo> columns, int width ) { |
191 |
171 |
|
// The new width defaults to the current width, within min/max range |
192 |
|
- |
for ( ColumnWidthInfo info : columns ) |
193 |
|
- |
{ |
|
172 |
+ |
for ( ColumnWidthInfo info : columns ) { |
194 |
173 |
|
int curWidth = info.getCurrentWidth(); |
195 |
|
- |
if ( info.hasMinimumWidth() && curWidth < info.getMinimumWidth() ) |
196 |
|
- |
{ |
|
174 |
+ |
if ( info.hasMinimumWidth() && curWidth < info.getMinimumWidth() ) { |
197 |
175 |
|
curWidth = info.getMinimumWidth(); |
198 |
|
- |
} |
199 |
|
- |
else if ( info.hasMaximumWidth() && curWidth > info.getMaximumWidth() ) |
200 |
|
- |
{ |
|
176 |
+ |
} else if ( info.hasMaximumWidth() && curWidth > info.getMaximumWidth() ) { |
201 |
177 |
|
curWidth = info.getMaximumWidth(); |
202 |
178 |
|
} |
203 |
179 |
|
width -= (curWidth - info.getCurrentWidth()); |
|
@@ -205,8 +181,7 @@ |
205 |
181 |
|
} |
206 |
182 |
|
|
207 |
183 |
|
// Do not modify widths if there is nothing to distribute |
208 |
|
- |
if ( width == 0 ) |
209 |
|
- |
{ |
|
184 |
+ |
if ( width == 0 ) { |
210 |
185 |
|
return 0; |
211 |
186 |
|
} |
212 |
187 |
|
|
|
@@ -214,50 +189,33 @@ |
214 |
189 |
|
List<ColumnWidthInfo> orderedColumns = new ArrayList<ColumnWidthInfo>( columns ); |
215 |
190 |
|
|
216 |
191 |
|
// Sort the list of columns |
217 |
|
- |
if ( width > 0 ) |
218 |
|
- |
{ |
|
192 |
+ |
if ( width > 0 ) { |
219 |
193 |
|
// Enlarge columns |
220 |
|
- |
Comparator<ColumnWidthInfo> comparator = new Comparator<ColumnWidthInfo>() |
221 |
|
- |
{ |
222 |
|
- |
public int compare( ColumnWidthInfo o1, ColumnWidthInfo o2 ) |
223 |
|
- |
{ |
|
194 |
+ |
Comparator<ColumnWidthInfo> comparator = new Comparator<ColumnWidthInfo>() { |
|
195 |
+ |
public int compare( ColumnWidthInfo o1, ColumnWidthInfo o2 ) { |
224 |
196 |
|
double diff1 = o1.getPercentageDifference(); |
225 |
197 |
|
double diff2 = o2.getPercentageDifference(); |
226 |
|
- |
if ( diff1 < diff2 ) |
227 |
|
- |
{ |
|
198 |
+ |
if ( diff1 < diff2 ) { |
228 |
199 |
|
return -1; |
229 |
|
- |
} |
230 |
|
- |
else if ( diff1 == diff2 ) |
231 |
|
- |
{ |
|
200 |
+ |
} else if ( diff1 == diff2 ) { |
232 |
201 |
|
return 0; |
233 |
|
- |
} |
234 |
|
- |
else |
235 |
|
- |
{ |
|
202 |
+ |
} else { |
236 |
203 |
|
return 1; |
237 |
204 |
|
} |
238 |
205 |
|
} |
239 |
206 |
|
}; |
240 |
207 |
|
Collections.sort( orderedColumns, comparator ); |
241 |
|
- |
} |
242 |
|
- |
else if ( width < 0 ) |
243 |
|
- |
{ |
|
208 |
+ |
} else if ( width < 0 ) { |
244 |
209 |
|
// Shrink columns |
245 |
|
- |
Comparator<ColumnWidthInfo> comparator = new Comparator<ColumnWidthInfo>() |
246 |
|
- |
{ |
247 |
|
- |
public int compare( ColumnWidthInfo o1, ColumnWidthInfo o2 ) |
248 |
|
- |
{ |
|
210 |
+ |
Comparator<ColumnWidthInfo> comparator = new Comparator<ColumnWidthInfo>() { |
|
211 |
+ |
public int compare( ColumnWidthInfo o1, ColumnWidthInfo o2 ) { |
249 |
212 |
|
double diff1 = o1.getPercentageDifference(); |
250 |
213 |
|
double diff2 = o2.getPercentageDifference(); |
251 |
|
- |
if ( diff1 > diff2 ) |
252 |
|
- |
{ |
|
214 |
+ |
if ( diff1 > diff2 ) { |
253 |
215 |
|
return -1; |
254 |
|
- |
} |
255 |
|
- |
else if ( diff1 == diff2 ) |
256 |
|
- |
{ |
|
216 |
+ |
} else if ( diff1 == diff2 ) { |
257 |
217 |
|
return 0; |
258 |
|
- |
} |
259 |
|
- |
else |
260 |
|
- |
{ |
|
218 |
+ |
} else { |
261 |
219 |
|
return 1; |
262 |
220 |
|
} |
263 |
221 |
|
} |
|
@@ -269,40 +227,32 @@ |
269 |
227 |
|
return distributeWidthImpl( orderedColumns, width ); |
270 |
228 |
|
} |
271 |
229 |
|
|
272 |
|
- |
private int distributeWidthImpl( List<ColumnWidthInfo> columns, int width ) |
273 |
|
- |
{ |
|
230 |
+ |
private int distributeWidthImpl( List<ColumnWidthInfo> columns, int width ) { |
274 |
231 |
|
// Iterate until width can not longer be distributed |
275 |
232 |
|
boolean growing = (width > 0); |
276 |
233 |
|
boolean fullySynced = false; |
277 |
234 |
|
int syncedColumns = 1; |
278 |
|
- |
while ( columns.size() > 0 && width != 0 ) |
279 |
|
- |
{ |
|
235 |
+ |
while ( columns.size() > 0 && width != 0 ) { |
280 |
236 |
|
// Calculate the target difference at the next level |
281 |
237 |
|
double targetDiff = getTargetDiff( columns, syncedColumns, width ); |
282 |
238 |
|
|
283 |
239 |
|
// Calculate the total required width to achieve the target difference |
284 |
240 |
|
int totalRequired = 0; |
285 |
|
- |
for ( int curIndex = 0; curIndex < syncedColumns; curIndex++ ) |
286 |
|
- |
{ |
|
241 |
+ |
for ( int curIndex = 0; curIndex < syncedColumns; curIndex++ ) { |
287 |
242 |
|
// Calculate the new width at the target diff |
288 |
243 |
|
ColumnWidthInfo curInfo = columns.get( curIndex ); |
289 |
244 |
|
int preferredWidth = curInfo.getPreferredWidth(); |
290 |
245 |
|
int newWidth = (int) (targetDiff * preferredWidth) + preferredWidth; |
291 |
246 |
|
|
292 |
247 |
|
// Compare the boundaries |
293 |
|
- |
if ( growing ) |
294 |
|
- |
{ |
|
248 |
+ |
if ( growing ) { |
295 |
249 |
|
newWidth = Math.max( newWidth, curInfo.getCurrentWidth() ); |
296 |
|
- |
if ( curInfo.hasMaximumWidth() ) |
297 |
|
- |
{ |
|
250 |
+ |
if ( curInfo.hasMaximumWidth() ) { |
298 |
251 |
|
newWidth = Math.min( newWidth, curInfo.getMaximumWidth() ); |
299 |
252 |
|
} |
300 |
|
- |
} |
301 |
|
- |
else |
302 |
|
- |
{ |
|
253 |
+ |
} else { |
303 |
254 |
|
newWidth = Math.min( newWidth, curInfo.getCurrentWidth() ); |
304 |
|
- |
if ( curInfo.hasMinimumWidth() ) |
305 |
|
- |
{ |
|
255 |
+ |
if ( curInfo.hasMinimumWidth() ) { |
306 |
256 |
|
newWidth = Math.max( newWidth, curInfo.getMinimumWidth() ); |
307 |
257 |
|
} |
308 |
258 |
|
} |
|
@@ -314,36 +264,27 @@ |
314 |
264 |
|
|
315 |
265 |
|
// Calculate the percent of the required width that is available |
316 |
266 |
|
double percentAvailable = 1.0; |
317 |
|
- |
if ( totalRequired != 0 ) |
318 |
|
- |
{ |
|
267 |
+ |
if ( totalRequired != 0 ) { |
319 |
268 |
|
percentAvailable = Math.min( 1.0, width / (double) totalRequired ); |
320 |
269 |
|
} |
321 |
|
- |
for ( int curIndex = 0; curIndex < syncedColumns; curIndex++ ) |
322 |
|
- |
{ |
|
270 |
+ |
for ( int curIndex = 0; curIndex < syncedColumns; curIndex++ ) { |
323 |
271 |
|
// Determine the true width to add to the column |
324 |
272 |
|
ColumnWidthInfo curInfo = columns.get( curIndex ); |
325 |
273 |
|
int required = (int) (percentAvailable * curInfo.getRequiredWidth()); |
326 |
274 |
|
|
327 |
275 |
|
// Make sure we get out of the loop by distributing at least 1 |
328 |
|
- |
if ( fullySynced ) |
329 |
|
- |
{ |
330 |
|
- |
if ( growing ) |
331 |
|
- |
{ |
|
276 |
+ |
if ( fullySynced ) { |
|
277 |
+ |
if ( growing ) { |
332 |
278 |
|
required = Math.max( RESOLUTION, required ); |
333 |
|
- |
} |
334 |
|
- |
else |
335 |
|
- |
{ |
|
279 |
+ |
} else { |
336 |
280 |
|
required = Math.min( -RESOLUTION, required ); |
337 |
281 |
|
} |
338 |
282 |
|
} |
339 |
283 |
|
|
340 |
284 |
|
// Don't distribute more than the available width |
341 |
|
- |
if ( growing && required > width ) |
342 |
|
- |
{ |
|
285 |
+ |
if ( growing && required > width ) { |
343 |
286 |
|
required = width; |
344 |
|
- |
} |
345 |
|
- |
else if ( !growing && required < width ) |
346 |
|
- |
{ |
|
287 |
+ |
} else if ( !growing && required < width ) { |
347 |
288 |
|
required = width; |
348 |
289 |
|
} |
349 |
290 |
|
|
|
@@ -353,16 +294,12 @@ |
353 |
294 |
|
|
354 |
295 |
|
// Remove the column if it has reached its maximum/minimum width |
355 |
296 |
|
boolean maxedOut = false; |
356 |
|
- |
if ( growing && curInfo.hasMaximumWidth() ) |
357 |
|
- |
{ |
|
297 |
+ |
if ( growing && curInfo.hasMaximumWidth() ) { |
358 |
298 |
|
maxedOut = (curInfo.getNewWidth() >= curInfo.getMaximumWidth()); |
359 |
|
- |
} |
360 |
|
- |
else if ( !growing && curInfo.hasMinimumWidth() ) |
361 |
|
- |
{ |
|
299 |
+ |
} else if ( !growing && curInfo.hasMinimumWidth() ) { |
362 |
300 |
|
maxedOut = (curInfo.getNewWidth() <= curInfo.getMinimumWidth()); |
363 |
301 |
|
} |
364 |
|
- |
if ( maxedOut ) |
365 |
|
- |
{ |
|
302 |
+ |
if ( maxedOut ) { |
366 |
303 |
|
columns.remove( curIndex ); |
367 |
304 |
|
curIndex--; |
368 |
305 |
|
syncedColumns--; |
|
@@ -370,12 +307,9 @@ |
370 |
307 |
|
} |
371 |
308 |
|
|
372 |
309 |
|
// Increment the number of synced column |
373 |
|
- |
if ( !fullySynced && syncedColumns < columns.size() ) |
374 |
|
- |
{ |
|
310 |
+ |
if ( !fullySynced && syncedColumns < columns.size() ) { |
375 |
311 |
|
syncedColumns++; |
376 |
|
- |
} |
377 |
|
- |
else |
378 |
|
- |
{ |
|
312 |
+ |
} else { |
379 |
313 |
|
fullySynced = true; |
380 |
314 |
|
} |
381 |
315 |
|
} |
|
@@ -391,20 +325,15 @@ |
391 |
325 |
|
* @param syncedColumns the number of synced columns |
392 |
326 |
|
* @param width the width to distribute |
393 |
327 |
|
*/ |
394 |
|
- |
private double getTargetDiff( List<ColumnWidthInfo> columns, int syncedColumns, int width ) |
395 |
|
- |
{ |
396 |
|
- |
if ( syncedColumns < columns.size() ) |
397 |
|
- |
{ |
|
328 |
+ |
private double getTargetDiff( List<ColumnWidthInfo> columns, int syncedColumns, int width ) { |
|
329 |
+ |
if ( syncedColumns < columns.size() ) { |
398 |
330 |
|
// Use the diff of the next un-synced column as the target |
399 |
331 |
|
return columns.get( syncedColumns ).getPercentageDifference(); |
400 |
|
- |
} |
401 |
|
- |
else |
402 |
|
- |
{ |
|
332 |
+ |
} else { |
403 |
333 |
|
// Calculate the total diff after all width has been distributed |
404 |
334 |
|
int totalNewWidth = width; |
405 |
335 |
|
int totalPreferredWidth = 0; |
406 |
|
- |
for ( ColumnWidthInfo info : columns ) |
407 |
|
- |
{ |
|
336 |
+ |
for ( ColumnWidthInfo info : columns ) { |
408 |
337 |
|
totalNewWidth += info.getNewWidth(); |
409 |
338 |
|
totalPreferredWidth += info.getPreferredWidth(); |
410 |
339 |
|
} |