soham Posted December 11, 2008 Share Posted December 11, 2008 Hello, I have a requirement to display currency(a BigDecimal) in red with parenthesis when the respective amount is negative. I am able to achieve it by creating a conditional style and applying the pattern as $#,##0.00 ;$ (#,##0.00). However, I have several Fields(of amount/currency type) for which I have to this conditional style. As the example specified in the code section, for each field, I am creating a conditional style. Now, I have more than 10 fields for which I have to create 10 conditional styles. For all the fields, the requirement is same(red in parenthesis if negative). And, each of them is a BigDecimal. Also, for each field I have a corresponding total field in the pagefooter section. For instance a $V{TotalAmount1} for $F{Amount1}. So, I created a new conditional style for the variable. Altogether, there will be 20 conditinal styles for 10 fields and their respective 10 totals. Is there any way I could create one style(where I do not have to refer the field name in the condition like $F{Amount1}.intValue <0 ) and apply that style to all the 10 Fields? In our application we display the report in HTML and export it to several formats such as PDF, XLS and CSV. I would to like display this red amounts in PDF, XLS and HTML. Could anybody suggest if there is any alternative way of doing this apart from creating conditinal style for each field.Thanks,Naveen Code: Post Edited by Naveen K at 12/11/08 19:35Post Edited by Naveen K at 12/11/08 19:56 Link to comment Share on other sites More sharing options...
C-Box Posted December 11, 2008 Share Posted December 11, 2008 this is indeed very annoying - we also would like a feature like this.new Boolean((BigDecimal)$X{CurrentField4Style}.compareTo(BigDecimal.ZERO) < 0) where $X{CurrentField4Style} is just a reference/placeholder to the field the expressions is actually called by. Of course it should be just an java.lang.Object but with casting you could do the needed expressions. (like my sample) So what do you think Teodor or Lucian, is this feasible ??? regards from GermanyC-Box Link to comment Share on other sites More sharing options...
lucianc Posted December 12, 2008 Share Posted December 12, 2008 I think that would be feasible, and it would surely help reusing conditional styles in a report. The enhancement has been requested some time ago (see this), hopefully we will find the time to implement this soon.As a note, the feature does pose some issues that are not straightforward to settle. For instance, the Java type of the $X{CurrentField} placeholder needs to be somehow determined at compile time.Regards,Lucian Link to comment Share on other sites More sharing options...
C-Box Posted December 12, 2008 Share Posted December 12, 2008 Why not just use java.lang.Object and cast it within the expression to BigDecimal or String if needed ... the worst that could happen would be a ClassCastException at runtime when the field has a wrong type for that conditional style. C-Box Link to comment Share on other sites More sharing options...
soham Posted December 12, 2008 Author Share Posted December 12, 2008 So, I was doing a wild goose chase then. Anyways, CBox and Lucian, appreciate your responses. It would be nice if we could have that feature at the earliest. Not sure if it helps anybody checking this post, I was able to fix my problem while exporting to XLS(only) by applying the pattern as $#,##0.00 ;[Red]$ (#,##0.00) and setting the custom element property(using IReport), net.sf.jasperreports.print.keep.full.text, of the amount field to true. This solution was posted by someone in the forum. Thanks,Naveen Link to comment Share on other sites More sharing options...
pinnau Posted November 24, 2009 Share Posted November 24, 2009 Is there any progress concerning this issue? I have exactly the same problem.As a workaround I implemented an own MarkupProcessor. I use the <style> pattern attribute for the precision formatting and the <textElement> markup attribute to use an additional MarkupProcessor. The processor just looks for a starting "-" and surrounds the value with <font color="red"></font>.Works perfect. Maybe someone will find this useful.Code:<textField> <reportElement x="0" y="0" width="45" height="15" style="summe"/> <textElement markup="sc" /> <textFieldExpression class="java.math.BigDecimal">$V{IST_ERLOS</textFieldExpression></textField>public class BigDecimalMarkupFactory implements MarkupProcessorFactory { @Override public MarkupProcessor createMarkupProcessor() { return new MarkupProcessor() { @Override public String convert(String arg0) { if (arg0 != null && arg0.startsWith("-")) return "<font color=\"red\">" + arg0 + "</font>"; return arg0; } }; }} Link to comment Share on other sites More sharing options...
luebke Posted January 5, 2010 Share Posted January 5, 2010 UPDATE:All what was missing is a line in jasperreport.properties:net.sf.jasperreports.markup.processor.factory.simple=name.of.SimpleMarkupProcessorFactoryRegards Stefan Hi Pinnau! I'm trying to wrote my own MarkupProcessorFactory, but i don't know how to make it available to the report engine. I've implemented the class, but when i'm trying to use it, i get an error:Exception in thread "main" java.lang.Error: net.sf.jasperreports.engine.JRRuntimeException: No markup processor factory specifyed for 'simple' markup.Can you please give me a hint what do do next?Regards StefanMy report snipped:<textElement verticalAlignment="Middle" markup="simple"><font size="8"/></textElement> My Class public class SimpleMarkupProcessorFactory implements MarkupProcessorFactory { public static final String PROPERTY_MARKUP_PROCESSOR_FACTORY_PREFIX = "simple"; @Override public MarkupProcessor createMarkupProcessor() { return new MarkupProcessor() { @Override public String convert(String arg0) { return "hello world!"; }; }; } } pinnauWrote: Is there any progress concerning this issue? I have exactly the same problem. As a workaround I implemented an own MarkupProcessor. I use the <style> pattern attribute for the precision formatting and the <textElement> markup attribute to use an additional MarkupProcessor. The processor just looks for a starting "-" and surrounds the value with <font color="red"></font>. Works perfect. Maybe someone will find this useful. Code: Post Edited by luebke at 01/05/2010 12:54Post Edited by luebke at 01/05/2010 12:55 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now