1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.components.barbecue;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import net.sf.jasperreports.engine.JRComponentElement;
30 import net.sf.jasperreports.engine.JRPrintElement;
31 import net.sf.jasperreports.engine.base.JRBasePrintImage;
32 import net.sf.jasperreports.engine.component.ComponentDesignConverter;
33 import net.sf.jasperreports.engine.convert.ReportConverter;
34 import net.sf.jasperreports.engine.type.ScaleImageEnum;
35 import net.sf.jasperreports.engine.util.JRExpressionUtil;
36 import net.sourceforge.barbecue.Barcode;
37
38 /**
39  * 
40  * @author Lucian Chirita (lucianc@users.sourceforge.net)
41  */

42 public class BarbecueDesignConverter implements ComponentDesignConverter
43 {
44     
45     private static final Log log = LogFactory.getLog(BarbecueDesignConverter.class);
46     
47     private static final String DEFAULT_PREVIEW_CODE = "01234567890";
48
49     @Override
50     public JRPrintElement convert(ReportConverter reportConverter,
51             JRComponentElement element)
52     {
53         BarbecueComponent component = (BarbecueComponent) element.getComponent();
54         if (component == null || component.getType() == null)
55         {
56             return null;
57         }
58         
59         try
60         {
61             JRBasePrintImage image = new JRBasePrintImage(
62                     reportConverter.getDefaultStyleProvider());
63             reportConverter.copyBaseAttributes(element, image);
64             image.setScaleImage(ScaleImageEnum.RETAIN_SHAPE);
65             
66             String code = null;
67             if (component.getCodeExpression() != null)
68             {
69                 code = JRExpressionUtil.getSimpleExpressionText(
70                         component.getCodeExpression());
71             }
72             if (code == null)
73             {
74                 //TODO custom default code
75                 code = DEFAULT_PREVIEW_CODE;
76             }
77             
78             String applicationIdentifier = null;
79             if (component.getApplicationIdentifierExpression() != null)
80             {
81                 applicationIdentifier = JRExpressionUtil.getSimpleExpressionText(
82                         component.getApplicationIdentifierExpression());
83             }
84             //TODO custom default app id
85             
86             BarcodeInfo barcodeInfo = new BarcodeInfo();
87             barcodeInfo.setType(component.getType());
88             barcodeInfo.setCode(code);
89             barcodeInfo.setApplicationIdentifier(applicationIdentifier);
90             barcodeInfo.setDrawText(component.isDrawText());
91             barcodeInfo.setRequiresChecksum(component.isChecksumRequired());
92             barcodeInfo.setBarWidth(component.getBarWidth());
93             barcodeInfo.setBarHeight(component.getBarHeight());
94             
95             Barcode barcode = BarcodeProviders.createBarcode(barcodeInfo);
96             BarbecueRendererImpl renderer = new BarbecueRendererImpl(barcode);
97             renderer.setRotation(BarbecueStyleResolver.getRotationValue(element));
98             image.setRenderer(renderer);
99             return image;
100         }
101         catch (Exception e)
102         {
103             if (log.isDebugEnabled())
104             {
105                 log.debug("Error while previewing barcode", e);
106             }
107             
108             return null;
109         }
110         
111     }
112
113 }
114