US and Worldwide: +1 (866) 660-7555
Results 1 to 8 of 8

Thread: Rendering objects

  1. #1
    Join Date
    Jul 2003
    Posts
    15

    Default Rendering objects

    I have a column in my table showing timezones.
    I wrote my own a tablecellrenderer TimeZoneRenderer to nicely display e.g. "GMT" instead of the default toString() output
    "sun.util.Calendar.ZoneInfo [id="GMT", offset=0, dstSavings=0, .....]"

    Now when I print my table using JFreeReport (fed with the table's model)
    I don't get "GMT" but the default toString() output.

    In general, is there some way to ask JFreeReport to use a specific renderer?

    Thanks for any help.

    Code:
    table.setDefaultRenderer(TimeZone.class,  new TimeZoneRenderer());
    with
    Code:
    public class TimeZoneRenderer extends DefaultTableCellRenderer {
          
        /** Creates a new instance of TimeZoneRenderer */
        public TimeZoneRenderer() {
        }
        
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, 
                                               boolean hasFocus, int row, int column) {
            if (value == null)
                return super.getTableCellRendererComponent(
                    table, value, isSelected, hasFocus, row, column);
            String displayString = ((TimeZone) value).getID();
            return super.getTableCellRendererComponent(
                    table, displayString, isSelected, hasFocus, row, column);
        }
    }

  2. #2
    Join Date
    Mar 2003
    Posts
    7,557

    Default

    Hi,

    TableCell renderer are part of the JTable-API. Although we use the tablemodel interface, we dont use the JTable in any way. If you want to transform an object to string in a controlled way, then either write an expression to do that work or just put strings into the tablemodel.

    To make such an expression, you can use the BSH Expression:

    <expression name="TimeZone-toString" class="com.jrefinery.report.functions.BSHExpression">
    <properties>
    <property name="expression">
    import java.util.TimeZone;

    Object getValue()
    {
    if (dataRow == null) return null; // pass initialization
    TimeZone tz = dataRow.get ("column-name");
    if (tz == null) return null;
    return tz.getID();
    }
    </property>
    </properties>
    </expression>

    As the SwingRenderer create JComponents, we are never able to use or adapt these renderers in any useable way. Our system needs raw data whereever possible.

    Have more fun,
    said Thomas

  3. #3
    Join Date
    Jul 2003
    Posts
    15

    Default

    Hi Taqua,

    It looks very easy. But I'm working via the API not via XML.
    Could you tell me how to translate that in "API-language"?
    I can't find how to do it. ops:

    Or where can I find examples on how to use expressions via the API?
    I find the documentation rather obscure concerning that.

    Thanks.

  4. #4
    Join Date
    Mar 2003
    Posts
    7,557

    Default

    Hi,

    the simplest approach: Don't use the beanshell if you can use the real compiler:

    Code:
    report.addExpression &#40;new AbstractExpression&#40;&#41; &#123;
     public Object getValue&#40;&#41;
    &#123;
    TimeZone tz = getDataRow&#40;&#41;.get &#40;"column-name"&#41;;
    if &#40;tz == null&#41; return null;
    return tz.getID&#40;&#41;;
    &#125;;
    &#125;&#41;;
    You know, the documentation is slightly outdated and does not contain a full description of the functions/expressions topic. We will improve that soon ...
    Until then there is an first article in the tips'n'docs forum about writing and unsing functions and expression.

    Have more fun,
    said Thomas

  5. #5
    Join Date
    Jul 2003
    Posts
    15

    Default

    Hi Taqua,

    Thanks for the quick response, but...
    It compiles, but when I run it I get a ClassCastException in LevelledExpressionList.initializeFunctions() at the line:

    Code:
    // Explicit cast to Function to test all contained elements to be Functions!
    Function f = &#40;Function&#41; functionCollection.getExpression&#40;i&#41;;
    Another thing I wonder: shouldn't my expression by given a name? Otherwise I can't use it, do I?
    Something like:
    Code:
            AbstractExpression exp = new AbstractExpression&#40;&#41; &#123;
                public Object getValue&#40;&#41; 
                &#123;
                    ...
                &#125;; 
            &#125;;
            exp.setName&#40;"tweaked_date"&#41;;
            report.addExpression&#40;exp&#41;;
    Can you provide some sample code on how to use it now?

    Is something like the following OK? (can't try it yet because of the initialization error)

    Code:
                  items.addElement&#40;
                    ItemFactory.createStringElement&#40;
                        "Element"+i,
                        new Rectangle2D.Float&#40;startX, startY, columnWidth , lineHeight&#41;,
                        Color.black,
                        ElementAlignment.LEFT.getOldAlignment&#40;&#41;,
                        ElementAlignment.MIDDLE.getOldAlignment&#40;&#41;,
                        null, // font
                        "-", // null string
                        "tweaked_date"
                    &#41;
                  &#41;;

  6. #6
    Join Date
    Jul 2003
    Posts
    15

    Default

    Hi Taqua,

    I finally got it working. 8)
    Thanks for your time.

    For the people who read this:

    + The ClassCastException was due to the fact that I defined an Expression but set this as a Function to the report. Wrong of course.
    + All the rest turned out to be OK. (yes, you have to set a name to be able to use it afterwards)

  7. #7
    Join Date
    Mar 2003
    Posts
    7,557

    Default

    Hi,

    arhg! I forgot the name ... sometimes I'm sure and sometimes just wrong

    Btw. I have to get rid of that stupid separation between expressions and functions - function extends expression and that code is 100% historical

    Well, the function interface needs to be changed next version anyway - lets cut that away.

    Have more fun,
    said Thomas

  8. #8
    Join Date
    Apr 2003
    Posts
    19

    Default Update for Version 0.8.5

    I've just tested the code with 0.8.5. It still works, but you need to change the expression class to "org.jfree.report.modules.misc.beanshell.BSHExpression".

    Klaus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •