KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > library > objects > TotalObject


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * TotalObject.java
28  *
29  * Created on 17 settembre 2004, 19.15
30  *
31  */

32
33 package it.businesslogic.ireport.gui.library.objects;
34 import it.businesslogic.ireport.gui.library.*;
35 import it.businesslogic.ireport.*;
36 /**
37  *
38  * @author Administrator
39  */

40 public class TotalObject extends AbstractLibraryObject {
41     
42     private static javax.swing.ImageIcon JavaDoc defaultIcon;
43     
44     static {
45         
46         defaultIcon = new javax.swing.ImageIcon JavaDoc(AbstractLibraryObject.class.getResource("/it/businesslogic/ireport/icons/library/total.png"));
47     }
48     
49     /** Creates a new instance of PageNumberObject */
50     public TotalObject() {
51     }
52     
53     public String JavaDoc getName()
54     {
55         return it.businesslogic.ireport.util.I18n.getString("gui.library.objects.total","Total");
56     }
57
58     public void drop(java.awt.dnd.DropTargetDropEvent JavaDoc dtde) {
59         
60         TotalObjectDialog tod = new TotalObjectDialog(getReportFrame().getMainFrame(), true );
61         tod.setJrf( getReportFrame() );
62         tod.setVisible(true);
63         if (tod.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
64         {
65           Object JavaDoc obj = tod.getSelectedObject();
66           if (obj == null) return;
67           // 1. We must create the variable....
68
String JavaDoc clazz = "java.lang.Integer";
69           String JavaDoc expression = "";
70           String JavaDoc tot_name ="";
71           if (obj instanceof JRField)
72           {
73               clazz = ((JRField)obj).getClassType();
74               expression = "$F{" + obj + "}";
75               tot_name += obj +"_";
76           }
77           else if (obj instanceof JRParameter)
78           {
79               clazz = ((JRParameter)obj).getClassType();
80               expression = "$P{" + obj + "}";
81               tot_name += obj +"_";
82           }
83           else if (obj instanceof JRVariable)
84           {
85               clazz = ((JRVariable)obj).getClassType();
86               expression = "$V{" + obj + "}";
87               tot_name += obj +"_";
88           }
89           else
90           {
91               expression = "" + obj;
92           }
93           
94           String JavaDoc var_name = "SUM_" + tot_name;
95           String JavaDoc time = "Report";
96           String JavaDoc tmp_name = var_name;
97           // 1. Find the first free var name...
98
java.util.Vector JavaDoc variables = getReportFrame().getReport().getVariables();
99           for (int i=1; ; ++i)
100           {
101               var_name = tmp_name + i;
102               boolean found = false;
103               for (int k=0; k<variables.size(); ++k)
104               {
105                   if ( ((JRVariable)variables.get(k)).getName().equals(var_name))
106                   {
107                       found = true;
108                       break;
109                   }
110               }
111               if (!found) break;
112           }
113           
114           // 2. Find the future band...
115
Band b = getReportFrame().getBandAt(dtde.getLocation());
116           JRVariable jrv = new JRVariable(var_name,false);
117           jrv.setClassType( clazz );
118           jrv.setExpression( expression );
119           
120           if (b.getName().equals("pageFooter"))
121           {
122               time = "Page";
123           }
124           else if (b.getName().equals("lastPageFooter"))
125           {
126               time = "Report";
127           }
128           else if (b.getName().equals("columnFooter"))
129           {
130               time = "Column";
131           }
132           else if (b.getName().endsWith("Footer"))
133           {
134               time = "Group";
135               jrv.setResetGroup( b.getName().substring(0, b.getName().length() - "Footer".length() ));
136           }
137           
138           jrv.setResetType( time );
139           jrv.setCalculation("Sum");
140           getReportFrame().getReport().addVariable( jrv );
141           
142           getReportFrame().dropNewTextField( dtde.getLocation(), "$V{" + var_name + "}", clazz, "Now" );
143         }
144     }
145     
146      public javax.swing.ImageIcon JavaDoc getIcon()
147     {
148         return defaultIcon;
149     }
150     
151 }
152
Popular Tags