javadatabaseerpjvx

Get display value of cell editor in JVx


I have a simple work-screen (JVx application framework) which shows records of a database table. The table contains number and formatted date values. The number and date formats are configured with cell editors as usual. It works without problems in the table and bound editor controls. So the table shows a formatted number/date and the editor as well.

My problem is that I want to show a label with the same number or date. The raw values (datarow.getValue("DATECOL"), datarow.getValue("NUMBERCOL")) are java.sql.Timestamp or java.math.BigDecimal objects. If I try to get a string (datarow.getValueAsString("DATECOL"), datarow.getValueAsString("NUMBERCOL")`) it's an unformatted string - clear.

So, is it possible to get the text, formatted with the same settings as defined for cell editors?

I found that the framework contains a class LabelControl class which can be bound to a column. But in my case, I need the text for a specific report and not for the UI.

Any help appreciated!

Is there an API for e.g. Util.getText(datarow, "NUMBERCOL")?


Solution

  • You're right, the LabelControl is a good option, but not in your case.

    So, simply use the cell editor and format the value:

    Object value = dataRow.getValue(columnName);
        
    String text;
        
    if (value instanceof String)
    {
        text = (String)value;
    }
    else
    {
        ICellEditor cellEditor = UICellEditor.getCellEditor(dataRow, columnName);
        
        if (value instanceof Number && cellEditor instanceof INumberCellEditor)
        {
            if (numberUtil == null)
            {
                numberUtil = new NumberUtil();
            }
     
            numberUtil.setNumberPattern(
                         ((INumberCellEditor)cellEditor).getNumberFormat());
            
            text = numberUtil.format((Number)value);
        }
        else if (value instanceof Date && cellEditor instanceof IDateCellEditor)
        {
            if (dateUtil == null)
            {
                dateUtil = new DateUtil();
            }
            
            dateUtil.setDatePattern(
                       ((IDateCellEditor)cellEditor).getDateFormat());
            
            text = dateUtil.format((Date)value);
        }
        else
        {
            text = dataRow.getValueAsString(columnName);
        }
    }
    

    It's important to create new NumberUtil and DateUtil, because the format can be different for every date or number column. There's also a utility class for formatting.

    text = DataBookUtil.getValueAsString(dataRow, columnName);