KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > crosstable > DataBean


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.examples.crosstable;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.model.DataModel;
25 import javax.faces.model.ListDataModel;
26
27 import org.apache.myfaces.examples.listexample.SimpleCountry;
28 import org.apache.myfaces.examples.listexample.SimpleCountryList;
29
30 /**
31  * @author Mathias Broekelmann (latest modification by $Author: matzew $)
32  * @version $Revision: 1.1 $ $Date: 2005/03/29 11:40:50 $
33  */

34 public class DataBean extends SimpleCountryList
35 {
36   private DataModel mColumns;
37   private DataModel mCountryDataModel;
38   private Map JavaDoc mValueMap = new HashMap JavaDoc();
39   private boolean mEditValues;
40   private String JavaDoc mColumnLabel;
41
42   /**
43    *
44    */

45   public DataBean()
46   {
47     super();
48   }
49
50   public boolean isEditValues()
51   {
52     return mEditValues;
53   }
54
55   public String JavaDoc editValues()
56   {
57     mEditValues = true;
58     return null;
59   }
60
61   public String JavaDoc saveValues()
62   {
63     mEditValues = false;
64     return null;
65   }
66
67   public String JavaDoc addColumn()
68   {
69     if (mColumnLabel != null)
70     {
71       List JavaDoc columns = (List JavaDoc) getColumnDataModel().getWrappedData();
72       columns.add(mColumnLabel);
73     }
74     return null;
75   }
76
77   public String JavaDoc removeColumn()
78   {
79     if (mColumns != null && mColumns.isRowAvailable())
80     {
81       Object JavaDoc column = mColumns.getRowData();
82       List JavaDoc columns = (List JavaDoc) getColumnDataModel().getWrappedData();
83       columns.remove(column);
84     }
85     return null;
86   }
87
88   public String JavaDoc getColumnLabel()
89   {
90     return mColumnLabel;
91   }
92
93   public void setColumnLabel(String JavaDoc label)
94   {
95     mColumnLabel = label;
96   }
97
98   public DataModel getCountryDataModel()
99   {
100     if (mCountryDataModel == null)
101     {
102       mCountryDataModel = new ListDataModel(getCountries());
103     }
104     return mCountryDataModel;
105   }
106
107   public DataModel getColumnDataModel()
108   {
109     if (mColumns == null)
110     {
111       String JavaDoc[] result = new String JavaDoc[] {"2002", "2003", "2004"};
112       mColumns = new ListDataModel(new ArrayList JavaDoc(Arrays.asList(result)));
113     }
114     return mColumns;
115   }
116
117   public String JavaDoc getColumnValue()
118   {
119     DataModel countryDataModel = getCountryDataModel();
120     if (countryDataModel.isRowAvailable())
121     {
122       SimpleCountry row = (SimpleCountry) countryDataModel.getRowData();
123       DataModel columnDataModel = getColumnDataModel();
124       if (columnDataModel.isRowAvailable())
125       {
126         Object JavaDoc column = columnDataModel.getRowData();
127         Object JavaDoc key = new RowColumnKey(new Long JavaDoc(row.getId()), column);
128         if (!mValueMap.containsKey(key))
129         {
130           // initialize with random value
131
String JavaDoc randomValue = String.valueOf((int) (Math.random() * 5000) + 5000);
132           mValueMap.put(key, randomValue);
133         }
134         return (String JavaDoc) mValueMap.get(key);
135       }
136     }
137     return null;
138   }
139
140   public void setColumnValue(String JavaDoc value)
141   {
142     DataModel countryDataModel = getCountryDataModel();
143     if (countryDataModel.isRowAvailable())
144     {
145       SimpleCountry row = (SimpleCountry) countryDataModel.getRowData();
146       DataModel columnDataModel = getColumnDataModel();
147       if (columnDataModel.isRowAvailable())
148       {
149         Object JavaDoc column = columnDataModel.getRowData();
150         Object JavaDoc key = new RowColumnKey(new Long JavaDoc(row.getId()), column);
151         mValueMap.put(key, value);
152       }
153     }
154   }
155
156   private class RowColumnKey
157   {
158     private final Object JavaDoc mRow;
159     private final Object JavaDoc mColumn;
160
161     /**
162      * @param row
163      * @param column
164      */

165     public RowColumnKey(Object JavaDoc row, Object JavaDoc column)
166     {
167       mRow = row;
168       mColumn = column;
169     }
170
171     /**
172      * @see java.lang.Object#equals(java.lang.Object)
173      */

174     public boolean equals(Object JavaDoc obj)
175     {
176       if (obj == null)
177       {
178         return false;
179       }
180       if (obj == this)
181       {
182         return true;
183       }
184       if (obj instanceof RowColumnKey)
185       {
186         RowColumnKey other = (RowColumnKey) obj;
187         return other.mRow.equals(mRow) && other.mColumn.equals(mColumn);
188       }
189       return super.equals(obj);
190     }
191
192     /**
193      * @see java.lang.Object#hashCode()
194      */

195     public int hashCode()
196     {
197       return (37 * 3 + mRow.hashCode()) * (37 * 3 + mColumn.hashCode());
198     }
199
200     /**
201      * @see java.lang.Object#toString()
202      */

203     public String JavaDoc toString()
204     {
205       return mRow.toString() + "," + mColumn.toString();
206     }
207   }
208 }
209
210 /**
211  * $Log: DataBean.java,v $
212  * Revision 1.1 2005/03/29 11:40:50 matzew
213  * added new crosstable component (x:columns). Contributed by Mathias Broekelmann
214  *
215  */

216
Popular Tags