KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > icefaces > samples > showcase > components > table > ColumnsBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.icefaces.samples.showcase.components.table;
35
36 import javax.faces.event.ValueChangeEvent;
37 import javax.faces.model.DataModel;
38 import javax.faces.model.ListDataModel;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  * The ColumnsBean object generates ASCII data for the table columns example.
43  *
44  * @since 1.5
45  */

46 public class ColumnsBean {
47     // row columna data map
48
private DataModel columnDataModel;
49     private DataModel rowDataModel;
50
51     // ascii index
52
public static final int ASCII_START = 33;
53     public static final int ASCII_END = 126;
54     public static final int ASCII_RANGE = ASCII_END - ASCII_START;
55     private static final AsciiData[] asciiData = new AsciiData[ASCII_RANGE];
56
57     // we only need to initialized the ascii array once.
58
private static boolean isInit;
59
60     // default column and row values
61
private int columns = 5;
62     private int rows = 0; // ASCII_RANGE / 5 columns.
63

64     //2D array to save the table values to.
65
private String JavaDoc table[][];
66     
67     private static final int ROW_CONSTANT = 13;
68     
69     public ColumnsBean() {
70
71         // calulate rows
72
calculateRows();
73
74         // generate some default data.
75
init();
76         updateTableColumns(null);
77     }
78
79     /**
80      * Initialize an array of ASCII values which we will display differently
81      * depending on the number of columns specified by the user.
82      */

83     private synchronized void init() {
84         if (isInit) {
85             return;
86         }
87         isInit = true;
88
89         // build the asic data set
90
AsciiData tmp;
91         int index;
92         for (int i = 0; i < ASCII_RANGE ; i++) {
93             tmp = new AsciiData();
94             index = ASCII_START + i;
95             tmp.setIndex(index);
96             tmp.setIndexChar((char) index);
97             tmp.setIndexHex(Integer.toHexString(index));
98             asciiData[i] = tmp;
99         }
100     }
101
102     public int getColumns() {
103         return columns;
104     }
105
106     public int getRows() {
107         return rows;
108     }
109
110     public void setColumns(int columns) {
111         this.columns = columns;
112     }
113
114     public DataModel getRowDataModel() {
115         return rowDataModel;
116     }
117
118     public DataModel getColumnDataModel() {
119         return columnDataModel;
120     }
121
122     /**
123      * Called by the table interator. This method reads the column and row data
124      * models and displays the correct cell value.
125      *
126      * @return data which should be displayed for the given model state.
127      */

128     public String JavaDoc getCellValue() {
129         if (rowDataModel.isRowAvailable() &&
130             columnDataModel.isRowAvailable()) {
131
132             // get the index of the row and column that this method is being
133
// called for
134
int row = rowDataModel.getRowIndex();
135             int col = columnDataModel.getRowIndex();
136            
137             //return the element at the specified column and row
138
return table[col][row];
139         }
140         // empty field.
141
return "-";
142     }
143     
144     
145
146     private void calculateRows(){
147         // calculate the number of columns.
148
rows = ASCII_END / columns;
149
150         // make an extra row if there is a modulus
151
if ((ASCII_END % columns) != 0) {
152             rows += 1;
153
154         }
155     }
156
157     /**
158      * Updates the table model data.
159      *
160      * @param event property change event which specifies whether or not the the
161      * column count has changed.
162      */

163     public void updateTableColumns(ValueChangeEvent event) {
164         if (event != null && event.getNewValue() != null &&
165             event.getNewValue() instanceof Integer JavaDoc) {
166             // get the new column count
167
columns = ((Integer JavaDoc) event.getNewValue()).intValue();
168         }
169         int numberOfRows=26;
170         if( columns > 1 ){
171             numberOfRows = columns * ROW_CONSTANT;
172         }
173               
174         ArrayList JavaDoc columnList = new ArrayList JavaDoc();
175         ArrayList JavaDoc rowList = new ArrayList JavaDoc();
176         
177         table = new String JavaDoc[columns][numberOfRows];
178         String JavaDoc r;
179         for( int i=0; i < columns; i++ ){
180             for( int j=0; j<numberOfRows; j++ ){
181                 r = getChar(j);
182                 // add row data.
183
table[i][j] = r;
184                 // add numeric row header
185
rowList.add(r);
186              }
187             columnList.add(String.valueOf(i + 1));
188             
189         }
190         
191         rowDataModel = new ListDataModel(rowList);
192         columnDataModel = new ListDataModel(columnList);
193         
194     }
195
196    private String JavaDoc getChar(int i){
197         i += 65;
198         return String.valueOf((char)i);
199     }
200
201     /**
202      * Utility class to store index, char and hex data. Used to store
203      * table cell data for this example.
204      */

205     public class AsciiData {
206         private int index;
207         private char indexChar;
208         private String JavaDoc indexHex = " ";
209
210
211         public int getIndex() {
212             return index;
213         }
214
215         public void setIndex(int index) {
216             this.index = index;
217         }
218
219         public char getIndexChar() {
220             return indexChar;
221         }
222
223         public char getIndexCharString() {
224             // only show ASCII values after char 32 (space).
225
if (indexChar > 32)
226                 return indexChar;
227             else {
228                 return ' ';
229             }
230         }
231
232         public void setIndexChar(char indexChar) {
233             this.indexChar = indexChar;
234         }
235
236         public String JavaDoc getIndexHex() {
237             return indexHex;
238         }
239
240         public void setIndexHex(String JavaDoc indexHex) {
241             this.indexHex = indexHex;
242         }
243
244         public String JavaDoc toString() {
245             return index + " " + indexChar + " " + indexHex;
246         }
247     }
248
249 }
250
251
Popular Tags