KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > caramel > util > ColumnList


1 /*
2 ** Caramel - Non-GUI Java Addons
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** Lesser General Public License as published by the Free Software Foundation.
9 ** Version 2.1 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package caramel.util;
24
25 import java.util.*;
26 import houston.*;
27
28 public class ColumnList
29 {
30    static Logger T = Logger.getLogger( ColumnList.class );
31
32    private List _list;
33
34    public ColumnList( List list )
35    {
36       _list = list;
37    }
38
39    public List getList()
40    {
41       return _list;
42       // return original list
43
}
44
45    public List[] getTableRows( int columns )
46    {
47       T.debug( "enter getList( " + columns + " )" );
48
49       // reorders elements so that they
50
// can easily be traversed using velocity-foreach
51
// example:
52
// - before
53
// 1 | 2 | 3
54
// 4 | 5 | 6
55
// 7 | 8 | 9
56
// 10 | 11 | 12
57
// 13 | 14
58
//
59
// after
60
// 1 | 6 | 11
61
// 2 | 7 | 12
62
// 3 | 8 | 13
63
// 4 | 9 | 14
64
// 5 | 10
65
// note: for every row one list is created
66

67       Object JavaDoc data[] = _list.toArray();
68
69       int size = data.length;
70       int columnSize = size / columns;
71       if( ( size % columns ) != 0 )
72          // don't cut off last partially filled row
73
columnSize += 1;
74
75       T.debug( "size=" + size );
76       T.debug( "columnSize=" + columnSize );
77
78       // note: columnSize = number of rows
79

80       List list[] = new List[columnSize];
81
82       for( int row = 0; row < columnSize; row++ )
83       {
84          list[row] = new ArrayList();
85
86          for( int col = 0; col < columns; col++ )
87          {
88             int index = ( col * columnSize ) + row;
89
90             if( index >= size )
91                continue;
92
93             // T.debug( "row=" + row + ", col=" + col );
94
// T.debug( "index=" + index );
95

96             list[row].add( data[index] );
97          }
98       }
99
100       T.debug( "leave getList()" );
101
102       return list;
103    }
104
105 }
106
Popular Tags