KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > report > core > RModel


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.report.core;
15
16 import java.io.*;
17 import java.util.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Report Model.
23  * Data is maintained in RModelData
24  *
25  * @author Jorg Janke
26  * @version $Id: RModel.java,v 1.7 2002/08/12 01:55:12 danb Exp $
27  */

28 public class RModel implements Serializable
29 {
30     /**
31      * Constructor. Creates RModelData
32      * @param TableName
33      */

34     public RModel (String JavaDoc TableName)
35     {
36         m_data = new RModelData (TableName);
37     } // RModel
38

39     /** Table Alias for SQL */
40     public static final String JavaDoc TABLE_ALIAS = "zz";
41     /** Function: Count */
42     public static final String JavaDoc FUNCTION_COUNT = "Count";
43     /** Function: Sum */
44     public static final String JavaDoc FUNCTION_SUM = "Sum";
45
46     /** Helper to retrieve Actual Data */
47     private RModelData m_data = null;
48     /** Is Content editable by user */
49     private boolean m_editable = false;
50
51
52     /*************************************************************************/
53
54     /**
55      * Get Column Display Name
56      * @param col
57      * @return RColumn
58      */

59     protected RColumn getRColumn (int col)
60     {
61         if (col < 0 || col > m_data.cols.size())
62             throw new java.lang.IllegalArgumentException JavaDoc("Column invalid");
63         return (RColumn)m_data.cols.get(col);
64     } // getRColumn
65

66     /*************************************************************************/
67
68     /**
69      * Add Column
70      * @param rc
71      */

72     public void addColumn (RColumn rc)
73     {
74         m_data.cols.add(rc);
75     } // addColumn
76

77     /**
78      * Add Column at Index
79      * @param rc
80      * @param index
81      */

82     public void addColumn (RColumn rc, int index)
83     {
84         m_data.cols.add(index, rc);
85     } // addColumn
86

87     /**
88      * Add Row
89      */

90     public void addRow ()
91     {
92         m_data.rows.add(new ArrayList());
93         m_data.rowsMeta.add(null);
94     } // addRow
95

96     /**
97      * Add Row at Index
98      * @param index
99      */

100     public void addRow (int index)
101     {
102         m_data.rows.add(index, new ArrayList());
103         m_data.rowsMeta.add(index, null);
104     } // addRow
105

106     /**
107      * Add Row
108      * @param l
109      */

110     public void addRow (ArrayList l)
111     {
112         m_data.rows.add(l);
113         m_data.rowsMeta.add(null);
114     } // addRow
115

116     /**
117      * Add Row at Index
118      * @param l
119      * @param index
120      */

121     public void addRow (ArrayList l, int index)
122     {
123         m_data.rows.add(index, l);
124         m_data.rowsMeta.add(index, null);
125     } // addRow
126

127     /**
128      * Find index for ColumnName
129      * @param columnName
130      * @return index or -1 if not found
131      */

132     public int getColumnIndex (String JavaDoc columnName)
133     {
134         if (columnName == null || columnName.length() == 0)
135             return -1;
136         //
137
for (int i = 0; i < m_data.cols.size(); i++)
138         {
139             RColumn rc = (RColumn)m_data.cols.get(i);
140         // Log.trace(Log.l6_Database, "Column " + i + " " + rc.getColSQL() + " ? " + columnName);
141
if (rc.getColSQL().startsWith(columnName))
142             {
143                 Log.trace(Log.l6_Database, "Column " + i + " " + rc.getColSQL() + " = " + columnName);
144                 return i;
145             }
146         }
147         return -1;
148     } // getColumnIndex
149

150     /*************************************************************************/
151
152     /**
153      * Query
154      * @param ctx
155      * @param whereClause
156      * @param orderClause
157      */

158     public void query (Properties ctx, String JavaDoc whereClause, String JavaDoc orderClause)
159     {
160         m_data.query (ctx, whereClause, orderClause);
161     } // query
162

163     /*************************************************************************/
164
165     /**
166      * Set a group column
167      * @param columnName
168      */

169     public void setGroup (String JavaDoc columnName)
170     {
171         setGroup(getColumnIndex(columnName));
172     } // setGroup
173

174     /**
175      * Set a group column -
176      * if the group value changes, a new row in inserted
177      * performing the calculations set in setGroupFunction().
178      * If you set multiple groups, start with the highest level
179      * (e.g. Country, Region, City)
180      * The data is assumed to be sorted to result in meaningful groups.
181      * <pre>
182      * A AA 1
183      * A AB 2
184      * B BA 3
185      * B BB 4
186      * after setGroup (0)
187      * A AA 1
188      * A AB 2
189      * A
190      * B BA 3
191      * B BB 4
192      * B
193      * </pre>
194      * @param col The Group BY Column
195      */

196     public void setGroup (int col)
197     {
198         Log.trace(Log.l4_Data, "RModel.setGroup col=" + col);
199         if (col < 0 || col >= m_data.cols.size())
200             return;
201         Integer JavaDoc ii = new Integer JavaDoc(col);
202         if (!m_data.groups.contains(ii))
203             m_data.groups.add(ii);
204     } // setGroup
205

206     /**
207      * Is Row a Group Row
208      * @param row index
209      * @return true if row is a group row
210      */

211     public boolean isGroupRow (int row)
212     {
213         return m_data.isGroupRow(row);
214     } // isGroupRow
215

216     /**
217      * Set Group Function
218      * @param columnName
219      * @param function
220      */

221     public void setFunction (String JavaDoc columnName, String JavaDoc function)
222     {
223         setFunction(getColumnIndex(columnName), function);
224     } // setFunction
225

226     /**
227      * Set Group Function -
228      * for the column, set a function like FUNCTION_SUM, FUNCTION_COUNT, ...
229      * @param col The column on which the function is performed
230      * @param function The function
231      */

232     public void setFunction (int col, String JavaDoc function)
233     {
234         Log.trace(Log.l4_Data, "RModel.setFunction col=" + col + " - " + function);
235         if (col < 0 || col >= m_data.cols.size())
236             return;
237         m_data.functions.put(new Integer JavaDoc(col), function);
238     } // setFunction
239

240     /*************************************************************************/
241     // TableModel interface
242

243     /**
244      * Return Total mumber of rows
245      * @return row count
246      */

247     public int getRowCount()
248     {
249         return m_data.rows.size();
250     } // getRowCount
251

252     /**
253      * Get total number of columns
254      * @return column count
255      */

256     public int getColumnCount()
257     {
258         return m_data.cols.size();
259     } // getColumnCount
260

261     /**
262      * Get Column Display Name
263      * @param col index
264      * @return ColumnName
265      */

266     public String JavaDoc getColumnName (int col)
267     {
268         if (col < 0 || col > m_data.cols.size())
269             throw new java.lang.IllegalArgumentException JavaDoc("Column invalid");
270         RColumn rc = (RColumn)m_data.cols.get(col);
271         if (rc != null)
272             return rc.getColHeader();
273         return null;
274     } // getColumnName
275

276     /**
277      * Get Column Class
278      * @param col index
279      * @return Column Class
280      */

281     public Class JavaDoc getColumnClass (int col)
282     {
283         if (col < 0 || col > m_data.cols.size())
284             throw new java.lang.IllegalArgumentException JavaDoc("Column invalid");
285         RColumn rc = (RColumn)m_data.cols.get(col);
286         if (rc != null)
287             return rc.getColClass();
288         return null;
289     } // getColumnC;ass
290

291     /**
292      * Is Cell Editable
293      * @param rowIndex
294      * @param columnIndex
295      * @return true, if editable
296      */

297     public boolean isCellEditable (int rowIndex, int columnIndex)
298     {
299         return m_editable;
300     } // isCellEditable
301

302     /**
303      * Get Value At
304      * @param row
305      * @param col
306      * @return value
307      */

308     public Object JavaDoc getValueAt(int row, int col)
309     {
310         // invalid row
311
if (row < 0 || row >= m_data.rows.size())
312             return null;
313     // throw new java.lang.IllegalArgumentException("Row invalid");
314
if (col < 0 || col >= m_data.cols.size())
315             return null;
316     // throw new java.lang.IllegalArgumentException("Column invalid");
317
//
318
ArrayList myRow = (ArrayList)m_data.rows.get(row);
319         // invalid column
320
if (myRow == null || col >= myRow.size())
321             return null;
322         // setValue
323
return myRow.get(col);
324     } // getValueAt
325

326     /**
327      * Set Value At
328      * @param aValue
329      * @param row
330      * @param col
331      * @throws IllegalArgumentException if row/column is invalid or cell is read only
332      */

333     public void setValueAt(Object JavaDoc aValue, int row, int col)
334     {
335         // invalid row
336
if (row < 0 || row >= m_data.rows.size())
337             throw new IllegalArgumentException JavaDoc("Row invalid");
338         if (col < 0 || col >= m_data.cols.size())
339             throw new IllegalArgumentException JavaDoc("Column invalid");
340         if (!isCellEditable(row, col))
341             throw new IllegalArgumentException JavaDoc("Cell is read only");
342         //
343
ArrayList myRow = (ArrayList)m_data.rows.get(row);
344         // invalid row
345
if (myRow == null)
346             throw new java.lang.IllegalArgumentException JavaDoc("Row not initialized");
347         // not enough columns - add nulls
348
if (col >= myRow.size())
349             while (myRow.size() < m_data.cols.size())
350                 myRow.add(null);
351         // setValue
352
myRow.set(col, aValue);
353     } // setValueAt
354

355     /**
356      * Move Row
357      * @param from index
358      * @param to index
359      */

360     public void moveRow (int from, int to)
361     {
362         m_data.moveRow(from,to);
363     } // moveRow
364

365
366     /*************************************************************************/
367
368 } // RModel
369
Popular Tags