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: * PercentageObject.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: import it.businesslogic.ireport.gui.event.ReportElementChangedEvent;
038: import java.util.Vector;
039:
040: /**
041: *
042: * @author Administrator
043: */
044: public class PercentageObject extends AbstractLibraryObject {
045:
046: private static javax.swing.ImageIcon defaultIcon;
047:
048: static {
049:
050: defaultIcon = new javax.swing.ImageIcon(
051: AbstractLibraryObject.class
052: .getResource("/it/businesslogic/ireport/icons/library/percentage.png"));
053: }
054:
055: /** Creates a new instance of PageNumberObject */
056: public PercentageObject() {
057: }
058:
059: public String getName() {
060: return it.businesslogic.ireport.util.I18n.getString(
061: "gui.library.objects.percentage", "Percentage");
062: }
063:
064: public void drop(java.awt.dnd.DropTargetDropEvent dtde) {
065:
066: // We are in a band?
067:
068: PercentageObjectDialog tod = new PercentageObjectDialog(
069: getReportFrame().getMainFrame(), true);
070: tod.setFields(getReportFrame().getReport().getFields());
071: Vector resetTypes = new Vector();
072: resetTypes.add("Report");
073: resetTypes.add("Page");
074: resetTypes.add("Column");
075: resetTypes.addAll(getReportFrame().getReport().getGroups());
076: tod.setResetTypes(resetTypes);
077:
078: tod.setVisible(true);
079: if (tod.getDialogResult() == javax.swing.JOptionPane.OK_OPTION) {
080: JRField obj = tod.getField();
081:
082: String clazz = "java.lang.Integer";
083: String expression = "";
084: String tot_name = "";
085:
086: clazz = obj.getClassType();
087: expression = "$F{" + obj + "}";
088: tot_name += obj + "_";
089:
090: String var_name = "SUM_" + tot_name;
091: String time = "Report";
092: String tmp_name = var_name;
093:
094: // 1. Find the first free var name...
095: java.util.Vector variables = getReportFrame().getReport()
096: .getVariables();
097: for (int i = 1;; ++i) {
098: var_name = tmp_name + i;
099: boolean found = false;
100: for (int k = 0; k < variables.size(); ++k) {
101: if (((JRVariable) variables.get(k)).getName()
102: .equals(var_name)) {
103: found = true;
104: break;
105: }
106: }
107: if (!found)
108: break;
109: }
110:
111: // 2. Find the future band...
112: Band b = getReportFrame().getBandAt(dtde.getLocation());
113: JRVariable jrv = new JRVariable(var_name, false);
114: jrv.setClassType(clazz);
115: jrv.setExpression(expression);
116:
117: time = tod.getResetType();
118: if (!time.equals("Report") && !time.equals("Page")
119: && !time.equals("Column")) {
120: time = "Group";
121: jrv.setResetGroup(tod.getResetType());
122: }
123: jrv.setResetType(time);
124: jrv.setCalculation("Sum");
125: getReportFrame().getReport().addVariable(jrv);
126:
127: String field_exp = "$F{" + obj + "}.doubleValue()";
128: String var_name_exp = "$V{" + var_name + "}.doubleValue()";
129:
130: TextFieldReportElement re = getReportFrame()
131: .dropNewTextField(
132: dtde.getLocation(),
133: "new Double(" + field_exp + " / "
134: + var_name_exp + ")",
135: "java.lang.Double", "Auto");
136: re.setPattern("#,##0.00 %");
137: getReportFrame().fireReportListenerReportElementsChanged(
138: new ReportElementChangedEvent(getReportFrame(), re,
139: ReportElementChangedEvent.CHANGED));
140: }
141: }
142:
143: public javax.swing.ImageIcon getIcon() {
144: return defaultIcon;
145: }
146:
147: }
|