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: * BarcodeReportElement.java
028: *
029: * Created on 15. April 2004, 13:21 by Heiko Wenzel
030: *
031: */
032:
033: package it.businesslogic.ireport;
034:
035: import java.awt.*;
036: import it.businesslogic.ireport.util.*;
037:
038: import java.util.*;
039:
040: /**
041: *
042: * @author Administrator
043: */
044: public class BarcodeReportElement extends
045: it.businesslogic.ireport.ImageReportElement {
046:
047: private String title = "Sample barcode";
048: private boolean legend = false;
049: private boolean showText = false;
050: private String text = "\"0815\"";
051: private int type = 13;
052: private boolean checkSum = false;
053: private String lastError = null;
054: private static java.awt.Image barcodeError = null;
055: private int imageHeight = 0;
056: private int imageWidth = 0;
057: private String applicationIdentifier = "null";
058:
059: public BarcodeReportElement(int x, int y, int width, int height) {
060: // do not allow barcodes with a width or height == 0
061: super (x, y, (width == 0 ? 1 : Math.abs(width)),
062: (height == 0 ? 1 : Math.abs(height)));
063:
064: if (barcodeError == null) {
065: barcodeError = Misc
066: .loadImageFromResources("it/businesslogic/ireport/icons/barcodeerror.png");
067: }
068:
069: //setGraphicElementPen("Thin");
070: setBarCodeImg(type, text, showText, checkSum);
071: setImageClass("java.awt.Image");
072: setScaleImage("RetainShape");
073: setGraphicElementPen("None");
074: setHyperlinkType("None");
075: setAnchorNameExpression("");
076: setImgDef(null);
077: setKey("barcode");
078: }
079:
080: public void setBarCodeImg(int type, String text, boolean showText,
081: boolean checkSum) {
082:
083: StringBuffer bcCall = new StringBuffer(
084: "it.businesslogic.ireport.barcode.BcImage.getBarcodeImage(");
085: //boolean isFormula = text.trim().startsWith("$");
086: bcCall.append(type);
087:
088: //if (isFormula) {
089: bcCall.append(",");
090: bcCall.append(text);
091: bcCall.append(",");
092: //} else {
093: // bcCall.append(",\"");
094: // bcCall.append(text);
095: // bcCall.append("\",");
096: //}
097:
098: bcCall.append(showText);
099: bcCall.append(",");
100: bcCall.append(checkSum);
101: bcCall.append(",");
102: bcCall.append(applicationIdentifier);
103: bcCall.append(",");
104: bcCall.append(getImageWidth() + "," + getImageHeight());
105: bcCall.append(")");
106:
107: super .setImageExpression(bcCall.toString());
108:
109: try {
110:
111: setImg(it.businesslogic.ireport.barcode.BcImage
112: .getBarcodeImage(type, text, showText, checkSum,
113: getApplicationIdentifier(),
114: getImageWidth(), getImageHeight()));
115: lastError = null;
116:
117: } catch (RuntimeException e) {
118:
119: //reset image
120: setImg(barcodeError);
121:
122: //save last error message
123: lastError = e.getMessage();
124: }
125: }
126:
127: /*
128: public void setImageExpression(java.lang.String imageExpression) {
129: super.setImageExpression(imageExpression);
130:
131: String iE = imageExpression.substring(imageExpression.indexOf("(") + 1,
132: imageExpression.lastIndexOf(")"));
133: String[] params = iE.split(",");
134: type = new Integer(params[0]).intValue();
135:
136: //if (params[1].startsWith("$")) {
137: text = params[1];
138:
139: //} else {
140: // text = params[1].substring(1, params[1].length() - 1);
141: //}
142:
143: showText = new Boolean(params[2]).booleanValue();
144: checkSum = new Boolean(params[3]).booleanValue();
145:
146: if (params.length > 4)
147: {
148: this.setApplicationIdentifier( params[4] );
149: this.setImageWidth( Integer.parseInt( params[5]) );
150: this.setImageHeight( Integer.parseInt( params[6]) );
151: }
152:
153: update();
154: }*/
155: public void setImageExpression(java.lang.String imageExpression) {
156: super .setImageExpression(imageExpression);
157: final int numberOfParams = 7;
158: //We must get our params from the end of the imageExpression by
159: //using comma delimiters because the actual expression
160: //may contain commas itself (e.g. String.format("%s", xx)
161:
162: String iE = imageExpression.substring(imageExpression
163: .indexOf("(") + 1, imageExpression.lastIndexOf(")"));
164: String[] params = iE.split(",");
165: int paramCount = params.length;
166:
167: type = new Integer(params[0]).intValue(); //params[0] will always be type
168:
169: String text = "";
170: for (int i = 0; i <= paramCount - numberOfParams; i++) {
171: text += params[i + 1] + ",";
172: }
173: text = text.substring(0, text.length() - 1);
174:
175: setText(text);
176:
177: showText = new Boolean(
178: params[2 + (paramCount - numberOfParams)])
179: .booleanValue();
180: checkSum = new Boolean(
181: params[3 + (paramCount - numberOfParams)])
182: .booleanValue();
183:
184: if (params.length > 4) {
185: this
186: .setApplicationIdentifier(params[4 + (paramCount - numberOfParams)]);
187: this
188: .setImageWidth(Integer
189: .parseInt(params[5 + (paramCount - numberOfParams)]));
190: this
191: .setImageHeight(Integer
192: .parseInt(params[6 + (paramCount - numberOfParams)]));
193: }
194:
195: update();
196: }
197:
198: public void setShowText(boolean showText) {
199: this .showText = showText;
200: update();
201: }
202:
203: public boolean isShowText() {
204: return this .showText;
205: }
206:
207: public void update() {
208:
209: setBarCodeImg(type, text, showText, checkSum);
210:
211: /*
212: boolean isFormula = text.trim().startsWith("$");
213:
214: if (! isFormula) {
215: this.width = it.businesslogic.ireport.barcode.BcImage.getBarcode().getMinimumSize().width;
216: this.height = it.businesslogic.ireport.barcode.BcImage.getBarcode().getMinimumSize().height;
217: }
218: */
219: this .updateBounds();
220: }
221:
222: public ReportElement cloneMe() {
223: BarcodeReportElement newReportElement = new BarcodeReportElement(
224: position.x, position.y, width, height);
225: copyBaseReportElement(newReportElement, this );
226: newReportElement.setImageHeight(this .getImageHeight());
227: newReportElement.setImageWidth(this .getImageWidth());
228: newReportElement.setApplicationIdentifier(this
229: .getApplicationIdentifier());
230: return newReportElement;
231: }
232:
233: public java.lang.String getTitle() {
234: return title;
235: }
236:
237: public void setTitle(java.lang.String title) {
238: this .title = title;
239: this .setImg(null);
240: }
241:
242: public String getText() {
243: return this .text;
244: }
245:
246: public void setText(String text) {
247: this .text = text;
248: update();
249: }
250:
251: public int getType() {
252: return this .type;
253: }
254:
255: public void setType(int type) {
256: this .type = type;
257: }
258:
259: public boolean isCheckSum() {
260: return this .checkSum;
261: }
262:
263: public void setCheckSum(boolean checkSum) {
264: this .checkSum = checkSum;
265: update();
266: }
267:
268: /**
269: * Last error message.
270: * @return Returns null if ok.
271: */
272: public String getLastError() {
273: return lastError;
274: }
275:
276: public int getImageHeight() {
277: return imageHeight;
278: }
279:
280: public void setImageHeight(int imageHeight) {
281: this .imageHeight = imageHeight;
282: update();
283: }
284:
285: public String getApplicationIdentifier() {
286: if (applicationIdentifier == null
287: || applicationIdentifier.equals("null"))
288: return "";
289: return applicationIdentifier;
290: }
291:
292: public void setApplicationIdentifier(String applicationIdentifier) {
293: if (applicationIdentifier == null
294: || applicationIdentifier.equals(""))
295: applicationIdentifier = "null";
296: this .applicationIdentifier = applicationIdentifier;
297: update();
298: }
299:
300: public int getImageWidth() {
301: return imageWidth;
302: }
303:
304: public void setImageWidth(int imageWidth) {
305: this.imageWidth = imageWidth;
306: update();
307: }
308:
309: }
|