KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PercentageObject.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 import it.businesslogic.ireport.gui.event.ReportElementChangedEvent;
37 import java.util.Vector JavaDoc;
38 /**
39  *
40  * @author Administrator
41  */

42 public class PercentageObject extends AbstractLibraryObject {
43     
44     private static javax.swing.ImageIcon JavaDoc defaultIcon;
45     
46     static {
47         
48         defaultIcon = new javax.swing.ImageIcon JavaDoc(AbstractLibraryObject.class.getResource("/it/businesslogic/ireport/icons/library/percentage.png"));
49     }
50     
51     /** Creates a new instance of PageNumberObject */
52     public PercentageObject() {
53     }
54     
55     public String JavaDoc getName()
56     {
57         return it.businesslogic.ireport.util.I18n.getString("gui.library.objects.percentage","Percentage");
58     }
59
60     public void drop(java.awt.dnd.DropTargetDropEvent JavaDoc dtde) {
61         
62         
63         // We are in a band?
64

65         PercentageObjectDialog tod = new PercentageObjectDialog(getReportFrame().getMainFrame(), true );
66         tod.setFields( getReportFrame().getReport().getFields() );
67         Vector JavaDoc resetTypes = new Vector JavaDoc();
68         resetTypes.add("Report");
69         resetTypes.add("Page");
70         resetTypes.add("Column");
71         resetTypes.addAll(getReportFrame().getReport().getGroups() );
72         tod.setResetTypes(resetTypes);
73         
74         tod.setVisible(true);
75         if (tod.getDialogResult() == javax.swing.JOptionPane.OK_OPTION)
76         {
77           JRField obj = tod.getField();
78           
79           String JavaDoc clazz = "java.lang.Integer";
80           String JavaDoc expression = "";
81           String JavaDoc tot_name ="";
82           
83           clazz = obj.getClassType();
84           expression = "$F{" + obj + "}";
85           tot_name += obj +"_";
86           
87           String JavaDoc var_name = "SUM_" + tot_name;
88           String JavaDoc time = "Report";
89           String JavaDoc tmp_name = var_name;
90           
91            // 1. Find the first free var name...
92
java.util.Vector JavaDoc variables = getReportFrame().getReport().getVariables();
93           for (int i=1; ; ++i)
94           {
95               var_name = tmp_name + i;
96               boolean found = false;
97               for (int k=0; k<variables.size(); ++k)
98               {
99                   if ( ((JRVariable)variables.get(k)).getName().equals(var_name))
100                   {
101                       found = true;
102                       break;
103                   }
104               }
105               if (!found) break;
106           }
107           
108           // 2. Find the future band...
109
Band b = getReportFrame().getBandAt(dtde.getLocation());
110           JRVariable jrv = new JRVariable(var_name,false);
111           jrv.setClassType( clazz );
112           jrv.setExpression( expression );
113           
114           time = tod.getResetType();
115           if (!time.equals("Report") &&
116               !time.equals("Page") &&
117               !time.equals("Column"))
118           {
119             time = "Group";
120             jrv.setResetGroup( tod.getResetType() );
121           }
122           jrv.setResetType( time );
123           jrv.setCalculation("Sum");
124           getReportFrame().getReport().addVariable( jrv );
125           
126           String JavaDoc field_exp = "$F{" + obj + "}.doubleValue()";
127           String JavaDoc var_name_exp = "$V{" + var_name + "}.doubleValue()";
128           
129           TextFieldReportElement re = getReportFrame().dropNewTextField( dtde.getLocation(), "new Double(" + field_exp + " / " + var_name_exp + ")", "java.lang.Double", "Auto" );
130           re.setPattern("#,##0.00 %");
131           getReportFrame().fireReportListenerReportElementsChanged(new ReportElementChangedEvent(getReportFrame(), re, ReportElementChangedEvent.CHANGED));
132         }
133     }
134     
135      public javax.swing.ImageIcon JavaDoc getIcon()
136     {
137         return defaultIcon;
138     }
139     
140 }
141
Popular Tags