001: /*
002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
003: * http://www.jaspersoft.com.
004: *
005: * Unless you have purchased a commercial license agreement from JasperSoft,
006: * the following license terms apply:
007: *
008: * This program is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License version 2 as published by
010: * the Free Software Foundation.
011: *
012: * This program is distributed WITHOUT ANY WARRANTY; and without the
013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
014: * See the GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
018: * or write to:
019: *
020: * Free Software Foundation, Inc.,
021: * 59 Temple Place - Suite 330,
022: * Boston, MA USA 02111-1307
023: *
024: *
025: *
026: *
027: * TotalObject.java
028: *
029: * Created on 17 settembre 2004, 19.15
030: *
031: */
032:
033: package it.businesslogic.ireport.gui.library.objects;
034:
035: import it.businesslogic.ireport.gui.library.*;
036: import it.businesslogic.ireport.*;
037:
038: /**
039: *
040: * @author Administrator
041: */
042: public class TotalObject extends AbstractLibraryObject {
043:
044: private static javax.swing.ImageIcon defaultIcon;
045:
046: static {
047:
048: defaultIcon = new javax.swing.ImageIcon(
049: AbstractLibraryObject.class
050: .getResource("/it/businesslogic/ireport/icons/library/total.png"));
051: }
052:
053: /** Creates a new instance of PageNumberObject */
054: public TotalObject() {
055: }
056:
057: public String getName() {
058: return it.businesslogic.ireport.util.I18n.getString(
059: "gui.library.objects.total", "Total");
060: }
061:
062: public void drop(java.awt.dnd.DropTargetDropEvent dtde) {
063:
064: TotalObjectDialog tod = new TotalObjectDialog(getReportFrame()
065: .getMainFrame(), true);
066: tod.setJrf(getReportFrame());
067: tod.setVisible(true);
068: if (tod.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
069: Object obj = tod.getSelectedObject();
070: if (obj == null)
071: return;
072: // 1. We must create the variable....
073: String clazz = "java.lang.Integer";
074: String expression = "";
075: String tot_name = "";
076: if (obj instanceof JRField) {
077: clazz = ((JRField) obj).getClassType();
078: expression = "$F{" + obj + "}";
079: tot_name += obj + "_";
080: } else if (obj instanceof JRParameter) {
081: clazz = ((JRParameter) obj).getClassType();
082: expression = "$P{" + obj + "}";
083: tot_name += obj + "_";
084: } else if (obj instanceof JRVariable) {
085: clazz = ((JRVariable) obj).getClassType();
086: expression = "$V{" + obj + "}";
087: tot_name += obj + "_";
088: } else {
089: expression = "" + obj;
090: }
091:
092: String var_name = "SUM_" + tot_name;
093: String time = "Report";
094: String tmp_name = var_name;
095: // 1. Find the first free var name...
096: java.util.Vector variables = getReportFrame().getReport()
097: .getVariables();
098: for (int i = 1;; ++i) {
099: var_name = tmp_name + i;
100: boolean found = false;
101: for (int k = 0; k < variables.size(); ++k) {
102: if (((JRVariable) variables.get(k)).getName()
103: .equals(var_name)) {
104: found = true;
105: break;
106: }
107: }
108: if (!found)
109: break;
110: }
111:
112: // 2. Find the future band...
113: Band b = getReportFrame().getBandAt(dtde.getLocation());
114: JRVariable jrv = new JRVariable(var_name, false);
115: jrv.setClassType(clazz);
116: jrv.setExpression(expression);
117:
118: if (b.getName().equals("pageFooter")) {
119: time = "Page";
120: } else if (b.getName().equals("lastPageFooter")) {
121: time = "Report";
122: } else if (b.getName().equals("columnFooter")) {
123: time = "Column";
124: } else if (b.getName().endsWith("Footer")) {
125: time = "Group";
126: jrv.setResetGroup(b.getName().substring(0,
127: b.getName().length() - "Footer".length()));
128: }
129:
130: jrv.setResetType(time);
131: jrv.setCalculation("Sum");
132: getReportFrame().getReport().addVariable(jrv);
133:
134: getReportFrame().dropNewTextField(dtde.getLocation(),
135: "$V{" + var_name + "}", clazz, "Now");
136: }
137: }
138:
139: public javax.swing.ImageIcon getIcon() {
140: return defaultIcon;
141: }
142:
143: }
|