KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > BarcodeReportElement


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  * BarcodeReportElement.java
28  *
29  * Created on 15. April 2004, 13:21 by Heiko Wenzel
30  *
31  */

32
33 package it.businesslogic.ireport;
34 import java.awt.*;
35 import it.businesslogic.ireport.util.*;
36
37 import java.util.*;
38
39 /**
40  *
41  * @author Administrator
42  */

43 public class BarcodeReportElement extends it.businesslogic.ireport.ImageReportElement {
44
45     private String JavaDoc title = "Sample barcode";
46     private boolean legend = false;
47     private boolean showText = false;
48     private String JavaDoc text = "\"0815\"";
49     private int type = 13;
50     private boolean checkSum = false;
51     private String JavaDoc lastError = null;
52         private static java.awt.Image JavaDoc barcodeError = null;
53         private int imageHeight = 0;
54         private int imageWidth = 0;
55         private String JavaDoc applicationIdentifier = "null";
56
57     public BarcodeReportElement(int x, int y, int width, int height) {
58         // do not allow barcodes with a width or height == 0
59
super(x, y, ( width == 0 ? 1 : Math.abs(width)) , (height == 0 ? 1 : Math.abs(height)) );
60         
61                 if (barcodeError == null)
62                 {
63                     barcodeError = Misc.loadImageFromResources("it/businesslogic/ireport/icons/barcodeerror.png");
64                 }
65                 
66         //setGraphicElementPen("Thin");
67
setBarCodeImg(type, text, showText, checkSum);
68         setImageClass("java.awt.Image");
69         setScaleImage("RetainShape");
70         setGraphicElementPen("None");
71         setHyperlinkType("None");
72         setAnchorNameExpression("");
73         setImgDef(null);
74                 setKey("barcode");
75     }
76     
77     public void setBarCodeImg(int type, String JavaDoc text, boolean showText, boolean checkSum){
78         
79         StringBuffer JavaDoc bcCall = new StringBuffer JavaDoc("it.businesslogic.ireport.barcode.BcImage.getBarcodeImage(");
80         //boolean isFormula = text.trim().startsWith("$");
81
bcCall.append(type);
82         
83         //if (isFormula) {
84
bcCall.append(",");
85             bcCall.append(text);
86             bcCall.append(",");
87         //} else {
88
// bcCall.append(",\"");
89
// bcCall.append(text);
90
// bcCall.append("\",");
91
//}
92

93         bcCall.append(showText);
94         bcCall.append(",");
95         bcCall.append(checkSum);
96                 bcCall.append(",");
97                 bcCall.append( applicationIdentifier);
98                 bcCall.append(",");
99                 bcCall.append( getImageWidth() + "," + getImageHeight());
100         bcCall.append(")");
101         
102         super.setImageExpression(bcCall.toString());
103
104         try{
105             
106             setImg(it.businesslogic.ireport.barcode.BcImage.getBarcodeImage(type, text, showText, checkSum,getApplicationIdentifier(),getImageWidth(),getImageHeight()));
107             lastError = null;
108             
109         } catch (RuntimeException JavaDoc e) {
110             
111             //reset image
112
setImg(barcodeError);
113                         
114             
115             //save last error message
116
lastError = e.getMessage();
117         }
118     }
119     
120         /*
121     public void setImageExpression(java.lang.String imageExpression) {
122         super.setImageExpression(imageExpression);
123         
124         String iE = imageExpression.substring(imageExpression.indexOf("(") + 1,
125                         imageExpression.lastIndexOf(")"));
126         String[] params = iE.split(",");
127         type = new Integer(params[0]).intValue();
128         
129         //if (params[1].startsWith("$")) {
130                 text = params[1];
131                 
132         //} else {
133         // text = params[1].substring(1, params[1].length() - 1);
134         //}
135         
136         showText = new Boolean(params[2]).booleanValue();
137         checkSum = new Boolean(params[3]).booleanValue();
138
139                 if (params.length > 4)
140                 {
141                     this.setApplicationIdentifier( params[4] );
142                     this.setImageWidth( Integer.parseInt( params[5]) );
143                     this.setImageHeight( Integer.parseInt( params[6]) );
144                 }
145                 
146         update();
147     }*/

148         public void setImageExpression(java.lang.String JavaDoc imageExpression) {
149           super.setImageExpression(imageExpression);
150           final int numberOfParams = 7;
151           //We must get our params from the end of the imageExpression by
152
//using comma delimiters because the actual expression
153
//may contain commas itself (e.g. String.format("%s", xx)
154

155           String JavaDoc iE = imageExpression.substring(imageExpression.indexOf("(") + 1, imageExpression.lastIndexOf(")"));
156           String JavaDoc[] params = iE.split(",");
157           int paramCount = params.length;
158
159           type = new Integer JavaDoc(params[0]).intValue(); //params[0] will always be type
160

161           String JavaDoc text = "";
162           for (int i=0; i <= paramCount - numberOfParams; i++)
163           {
164             text += params[i+1] + ",";
165           }
166           text = text.substring(0, text.length() - 1);
167
168           setText(text);
169
170           showText = new Boolean JavaDoc(params[2 + (paramCount - numberOfParams)]).booleanValue();
171           checkSum = new Boolean JavaDoc(params[3 + (paramCount - numberOfParams)]).booleanValue();
172
173           if (params.length > 4)
174           {
175             this.setApplicationIdentifier( params[4 + (paramCount - numberOfParams)] );
176             this.setImageWidth( Integer.parseInt( params[5 + (paramCount - numberOfParams)]) );
177             this.setImageHeight( Integer.parseInt( params[6 + (paramCount - numberOfParams)]) );
178           }
179
180           update();
181         }
182
183     
184     public void setShowText(boolean showText) {
185         this.showText = showText;
186         update();
187     }
188     
189     public boolean isShowText() {
190         return this.showText;
191     }
192     
193     public void update() {
194         
195         setBarCodeImg(type, text, showText, checkSum);
196         
197         /*
198                  boolean isFormula = text.trim().startsWith("$");
199                 
200         if (! isFormula) {
201             this.width = it.businesslogic.ireport.barcode.BcImage.getBarcode().getMinimumSize().width;
202             this.height = it.businesslogic.ireport.barcode.BcImage.getBarcode().getMinimumSize().height;
203         }
204         */

205         this.updateBounds();
206     }
207     
208     public ReportElement cloneMe() {
209         BarcodeReportElement newReportElement = new BarcodeReportElement(position.x, position.y, width, height);
210         copyBaseReportElement(newReportElement, this);
211         newReportElement.setImageHeight( this.getImageHeight());
212                 newReportElement.setImageWidth( this.getImageWidth());
213                 newReportElement.setApplicationIdentifier( this.getApplicationIdentifier());
214                 return newReportElement;
215     }
216     
217     public java.lang.String JavaDoc getTitle() {
218         return title;
219     }
220     
221     public void setTitle(java.lang.String JavaDoc title) {
222         this.title = title;
223         this.setImg(null);
224     }
225     
226     public String JavaDoc getText() {
227         return this.text;
228     }
229     
230     public void setText(String JavaDoc text) {
231         this.text = text;
232         update();
233     }
234     
235     public int getType() {
236         return this.type;
237     }
238     
239     public void setType(int type) {
240         this.type = type;
241     }
242     
243     public boolean isCheckSum() {
244         return this.checkSum;
245     }
246     
247     public void setCheckSum(boolean checkSum) {
248         this.checkSum = checkSum;
249         update();
250     }
251     
252     /**
253      * Last error message.
254      * @return Returns null if ok.
255      */

256     public String JavaDoc getLastError() {
257         return lastError;
258     }
259
260
261
262     public int getImageHeight() {
263         return imageHeight;
264     }
265
266     public void setImageHeight(int imageHeight) {
267         this.imageHeight = imageHeight;
268         update();
269     }
270
271     public String JavaDoc getApplicationIdentifier() {
272         if (applicationIdentifier == null || applicationIdentifier.equals("null")) return "";
273         return applicationIdentifier;
274     }
275
276     public void setApplicationIdentifier(String JavaDoc applicationIdentifier) {
277         if (applicationIdentifier == null || applicationIdentifier.equals("")) applicationIdentifier = "null";
278         this.applicationIdentifier = applicationIdentifier;
279         update();
280     }
281
282     public int getImageWidth() {
283         return imageWidth;
284     }
285
286     public void setImageWidth(int imageWidth) {
287         this.imageWidth = imageWidth;
288         update();
289     }
290
291 }
292
Popular Tags