Wednesday, May 27, 2009

Converting java (hex) string to java.awt.Color

Here is a simple method that you can use it to convert hex string into java.awt.Color objects.
private static Color parseStringtoColor(String hexstring){ Integer i = Integer.parseInt(hexstring.substring(1),16); Color color = new Color(i); return color; }

Monday, May 25, 2009

Multiple form submission due to background attribute in "td" tag

Our team faced an issue with a particular module. The form was getting submitted more than once. We tried hard to figure out a solution for the problem. Finally we found that the problem was due to a normal "td" tag in the .jsp file. We had by mistake given an attribute “background” to that "td". The attribute “background” works absolutely fine within a td as long as the value to the attribute is a path to an image file. But incase you give a color code (say #FFFFFF) it begins to behave abnormally. Example: background="c:/example.gif” td tag This works fine Whereas background="#FFFFFF" in td tag doesn’t work If you want to change the background color of your "td" use “bgcolor” attribute. The attribute can take color codes as value. Note: background="red" also works fine in IE. This could be because the browser corrects the programmer’s mistake and converts it into bgcolor=”red” internally.

Friday, May 22, 2009

JSF tips and Tricks

If you are using JSF in your application, then you should probably read through. This post covers few of the problems that I faced while using Apache Trinidad Implementation of JSF and the solutions to those problems.

RTL Language Switching: If your application has both LTR ( languages like English) and RTL(languages like Arabic, Hebrew) support, then be extra careful while creating your jspx pages. Make sure your jspx page doesn't contain any free text between tags. It can mess with your page swap functionality. Make sure that the jspx is a has proper XML structure. If the XML structure is not proper, no error will be thrown but most of the JSF functionality will not work.

Verbatim Tag: Use verbatim tag ()if you want to write html code or free text inside jspx page. The verbatim tag is standard tag supported by all JSF implementations. This tag displays the enclosed content as is and Expression within the tag will NOT be evaluated.

Jasper Report Features

Off late I had been closely working with jasper reports (an open source java reporting tool developed by JasperForge). It's one of the coolest reporting tools I have come across. It has too many exciting features... Here are few of them that might excite you.

Right to Left Reporting:
Do you have a scenario where you need to build both left-to-right and right-to-left(used in languages like arabic) reports? People generally end up creating two different layouts - that looks like mirror images of each other. This would cause confusion, maintenance issue and it's not cost effective. You can instead use the jasper reports API to get this done.

Use the following two simple methods to get this working. You could add to this method for more effective mirroring:






public static void mirrorLayout(JasperDesign design){
int pageWidth = design.getPageWidth();
int rightMargin =
design.getRightMargin();
int leftMargin = design.getLeftMargin();
JRBand titleBand = design.getTitle();
JRElement[] titleBandElements =
titleBand.getElements();
mirrorDesign(titleBandElements,(pageWidth -
leftMargin - rightMargin));

design.setRightMargin(leftMargin);
design.setLeftMargin(rightMargin);

}

And the method mirrorDesign does the actual mirroring:


protected static void mirrorDesign(JRElement[] elements, int totalWidth) {
for (int i = 0; i < elements.length; i++) {

JRElement element = (JRElement) elements[i];
int mirrorX = totalWidth - element.getX() - element.getWidth();
element.setX(mirrorX);
if (element instanceof JRFrame)
{
JRFrame frame = (JRFrame) element;
mirrorDesign(frame.getElements(), frame.getWidth());
}


Zipping the exported Reports:
Though JasperReports doesn't provide zipping option directly (because JasperReports is a Reporting tool, and Zipping the report doesn't lie within the project scope), a little tweek to your code can give you the desired output. Here's sample to zip the output that you get while exporting the report. Note: The same can be used for any type of export (Excel, PDF, RTF, etc). The code snippet shows zipping for PDF export:

response.setContentType("text/xml;charset=utf-8");
response.setHeader("content-type", "text/pdf; charset=UTF-8");
response.setHeader("Content-Disposition","attachment;
filename=\""+ zipFileName.toString() + ".zip\"");

JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRPdfExporterParameter.JASPER_PRINT,jasperPrint);
pdfExporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE);
pdfExporter.setParameter(JRPdfExporterParameter.IS_COMPRESSED,Boolean.FALSE);
pdfExporter.setParameter(JRPdfExporterParameter.FORCE_SVG_SHAPES,
Boolean.TRUE);

pdfExporter.setParameter(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY, Boolean.TRUE);
pdfExporter.setParameter(JRPdfExporterParameter.IGNORE_PAGE_MARGINS,Boolean.FALSE);
ZipOutputStream zos = new ZipOutputStream(out);
pdfExporter.setParameter( JRXlsExporterParameter.OUTPUT_STREAM, zos);
ZipEntry ze = new ZipEntry(fileName + ".pdf");
zos.putNextEntry(ze);
pdfExporter.exportReport();
zos.closeEntry();
zos.close();
out.close();