You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Pranav kacholia <pr...@gmail.com> on 2011/09/22 22:45:22 UTC

DecimalFormatLabel (proposed)

I have foound many places where i need to format a number and display it in a
label. Can we have a simple child of Label as follows which can be used to
do the same? 

------------------------------------------------------------
package ;

import java.text.DecimalFormat;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 *
 * @author pkacholia
 */
public class DecimalFormatLabel extends Label {

    final private DecimalFormat formatter;
    
    public DecimalFormatLabel(String id, IModel<? extends Number> model,
DecimalFormat format) {
        super(id, model);
        formatter = format;
    }

    public DecimalFormatLabel(String id, Number obj, DecimalFormat format) {
        this(id, new Model<Number>(obj), format);
    }

    public DecimalFormatLabel(String id, DecimalFormat format) {
        super(id);
        formatter = format;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onComponentTagBody(final MarkupStream markupStream, final
ComponentTag openTag) {
        String response;
        try {
        response = formatter.format(getDefaultModelObject());
        } catch (IllegalArgumentException ex) {
            error(getString("NaN"));
            response = "";
        } catch (NullPointerException ex) {
            error(getString("NullFormatter"));
            response = "";
        } 
        replaceComponentTagBody(markupStream, openTag, response);
    }
}





--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3834813.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Igor Vaynberg <ig...@gmail.com>.
yeah, transient fields are ok...

-igor


On Thu, Sep 22, 2011 at 2:20 PM, Pranav kacholia
<pr...@gmail.com> wrote:
> Looking over the sourcecode of DecimalFormat (Java 6) there are a few fields
> which do not implements Serializable.
> Those are all of type DigitList and FieldPosition.
>
> however, in all the cases they are marked as Transient, so they shouldnt
> interfere with the serialization?
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3834903.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
Looking over the sourcecode of DecimalFormat (Java 6) there are a few fields
which do not implements Serializable. 
Those are all of type DigitList and FieldPosition. 

however, in all the cases they are marked as Transient, so they shouldnt
interfere with the serialization?

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3834903.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
I have been using it since earlier today. Havent seen any exceptions thrown. 
Im relatively new to wicket , so what should i look out for in case it isnt
actually serializable? 

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3834847.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Igor Vaynberg <ig...@gmail.com>.
not sure DecimalFormat is really serializable. Format is, but
DecimalFormat's DigitList is not....

-igor


On Thu, Sep 22, 2011 at 1:45 PM, Pranav kacholia
<pr...@gmail.com> wrote:
> I have foound many places where i need to format a number and display it in a
> label. Can we have a simple child of Label as follows which can be used to
> do the same?
>
> ------------------------------------------------------------
> package ;
>
> import java.text.DecimalFormat;
> import org.apache.wicket.markup.ComponentTag;
> import org.apache.wicket.markup.MarkupStream;
> import org.apache.wicket.markup.html.basic.Label;
> import org.apache.wicket.model.IModel;
> import org.apache.wicket.model.Model;
>
> /**
>  *
>  * @author pkacholia
>  */
> public class DecimalFormatLabel extends Label {
>
>    final private DecimalFormat formatter;
>
>    public DecimalFormatLabel(String id, IModel<? extends Number> model,
> DecimalFormat format) {
>        super(id, model);
>        formatter = format;
>    }
>
>    public DecimalFormatLabel(String id, Number obj, DecimalFormat format) {
>        this(id, new Model<Number>(obj), format);
>    }
>
>    public DecimalFormatLabel(String id, DecimalFormat format) {
>        super(id);
>        formatter = format;
>    }
>
>    /**
>     * {@inheritDoc}
>     */
>    @Override
>    public void onComponentTagBody(final MarkupStream markupStream, final
> ComponentTag openTag) {
>        String response;
>        try {
>        response = formatter.format(getDefaultModelObject());
>        } catch (IllegalArgumentException ex) {
>            error(getString("NaN"));
>            response = "";
>        } catch (NullPointerException ex) {
>            error(getString("NullFormatter"));
>            response = "";
>        }
>        replaceComponentTagBody(markupStream, openTag, response);
>    }
> }
>
>
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3834813.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
Updated the patch and resubmitted.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3847952.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Igor Vaynberg <ig...@gmail.com>.
wrap it in a wicket runtime exception

-igor


On Tue, Sep 27, 2011 at 6:32 AM, Pranav kacholia
<pr...@gmail.com> wrote:
> I have submitted a patch  https://issues.apache.org/jira/browse/WICKET-4085
> Wicket-4085 . for a formatted label.
>
> I cant figure what would be the best way to handle an
> IllegalArgumentException which is thrown when the supplied format object in
> not valid for the underlying data object.
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3847356.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
I have submitted a patch  https://issues.apache.org/jira/browse/WICKET-4085
Wicket-4085 . for a formatted label. 

I cant figure what would be the best way to handle an
IllegalArgumentException which is thrown when the supplied format object in
not valid for the underlying data object. 


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3847356.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Yves-Marie LAINÉ <ym...@gmail.com>.
I agree with you.

Yves-Marie

2011/9/25 Pranav kacholia <pr...@gmail.com>

> One way or the other, i think Wicket needs a label of that sort.
> There are too many places where i have to display currency. It should have
> something inbuilt that allows us to display formatted strings and numbers
> (a
> label)
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3840944.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Yves-Marie LAINÉ

Re: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
One way or the other, i think Wicket needs a label of that sort. 
There are too many places where i have to display currency. It should have
something inbuilt that allows us to display formatted strings and numbers (a
label)


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3840944.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Yves-Marie LAINÉ <ym...@gmail.com>.
In my opinion, it could be more usefull to nest  a StringResourceModel
directly in a "ResourceLabel", as formating number is often application wide
and sometimes  Locale dependent. And it covers more use case. It's just my
point of view.

Maybe like this :
    protected void onComponentTagBody(final MarkupStream markupStream, final
ComponentTag openTag)
    {
        replaceComponentTagBody(markupStream, openTag, new
StringResourceModel(formatKey, this, getDefaultModel(), new
Object[]{getDefaultModelObject()}, defaultValue));
    }



2011/9/23 Pranav kacholia <pr...@gmail.com>

> Or we go one step further back. I think this would handle all use cases. If
> we want to put in a NumberFormat or a DecimalFormat , in both cases it
> should work.
>
>
> public class FormattedLabel extends Label{
>
>    final private Format formatter;
>
>    public FormattedLabel(String id, IModel<? extends Number> model, Format
> format) {
>        super(id, model);
>        formatter = format;
>    }
>
>     public FormattedLabel(String id, Number obj, Format format) {
>         this(id, new Model<Number>(obj), format);
>    }
>
>     public FormattedLabel(String id, Format format) {
>         super(id);
>        formatter = format;
>    }
>
>    /**
>     * {@inheritDoc}
>     */
>    @Override
>    public void onComponentTagBody(final MarkupStream markupStream, final
> ComponentTag openTag) {
>        String response;
>        try {
>        response = formatter.format(getDefaultModelObject());
>        } catch (IllegalArgumentException ex) {
>             error(getString("Format"));
>             response = "";
>        } catch (NullPointerException ex) {
>            error(getString("NullFormatter"));
>            response = "";
>        }
>        replaceComponentTagBody(markupStream, openTag, response);
>    }
> }
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837336.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Yves-Marie LAINÉ

Re: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
Or we go one step further back. I think this would handle all use cases. If
we want to put in a NumberFormat or a DecimalFormat , in both cases it
should work.


public class FormattedLabel extends Label{
    
    final private Format formatter;
    
    public FormattedLabel(String id, IModel<? extends Number> model, Format
format) {
        super(id, model);
        formatter = format;
    }

    public FormattedLabel(String id, Number obj, Format format) {
        this(id, new Model<Number>(obj), format);
    }

    public FormattedLabel(String id, Format format) {
        super(id);
        formatter = format;
    }
    
    /**
     * {@inheritDoc}
     */
    @Override
    public void onComponentTagBody(final MarkupStream markupStream, final
ComponentTag openTag) {
        String response;
        try {
        response = formatter.format(getDefaultModelObject());
        } catch (IllegalArgumentException ex) {
            error(getString("Format"));
            response = "";
        } catch (NullPointerException ex) {
            error(getString("NullFormatter"));
            response = "";
        } 
        replaceComponentTagBody(markupStream, openTag, response);
    }
}


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837336.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Yves-Marie LAINÉ <ym...@gmail.com>.
Yes, you're right, but this way fit 90% of my needs (It's a Label...)

Otherwise, it's possible to use a CustomConverter to do it, and then
override getConverter on Component. So, it also work on TextField (but i
agree, it's not exactly your use case. ) :

    @Override
    public IConverter getConverter(Class<?> type) {

        return PercentConverter.INSTANCE_2DEC;
    }

public class PercentConverter extends AbstractNumberConverter {

    public static final PercentConverter INSTANCE = new PercentConverter();

    public static final PercentConverter INSTANCE_2DEC = new
PercentConverter() {
        public NumberFormat getNumberFormat(Locale locale) {
            NumberFormat nf = DecimalFormat.getPercentInstance(locale);
            nf.setMinimumFractionDigits(2);
            nf.setMaximumFractionDigits(2);
            return nf;
        };
    };

    public static final PercentConverter INSTANCE_4DEC = new
PercentConverter() {
        public NumberFormat getNumberFormat(Locale locale) {
            NumberFormat nf = DecimalFormat.getPercentInstance(locale);
            nf.setMinimumFractionDigits(4);
            nf.setMaximumFractionDigits(4);
            return nf;
        };
    };

    public Object convertToObject(String value, Locale locale) {
        if (value == null)
            return null;

        value = value.trim();
        if (value.trim().equals(""))
        {
            return null;
        }

        Number n = null;
        try {
            n =  getNumberFormat(locale).parse(value);
        } catch (ParseException e) {
            throw newConversionException(e.getMessage(), value,
locale).setFormat(
                    getNumberFormat(locale));

        }

        return n.floatValue();
    }

    @Override
    protected Class<?> getTargetType() {
        return Float.class;
    }

    @Override
    public NumberFormat getNumberFormat(Locale locale) {
        return NumberFormat.getPercentInstance(locale);
    }

}






2011/9/23 Pranav kacholia <pr...@gmail.com>

> Suppose someone wants to call formatlLabel.getModelObject()  ...
>
> I don’t see why they would, but hypothetically.
>
> In this case you would get the formatted string. Since the label is merely
> a view, should it be changing the model even by formatting it?
>
>
>
> From: Yves-Marie LAINÉ [via Apache Wicket] [mailto:
> ml-node+s1842946n3837008h89@n4.nabble.com]
> Sent: 23 September 2011 19:57
> To: Pranav kacholia
> Subject: Re: DecimalFormatLabel (proposed)
>
>
>
> Hi all,
>
> Personnaly I did it like that (simpler for i18n, i think) :
>
> public class FormatLabel extends Label {
>
>    public FormatLabel(String id,final String formatKey, final IModel<?
> extends Serializable> model) {
>        super (id);
>        setDefaultModel(new AbstractReadOnlyModel<String>() {
>            @Override
>            public String getObject() {
>
>                Serializable o = model.getObject();
>
>                if ( o != null) {
>                    return new StringResourceModel(formatKey,
> FormatLabel.this, model, new Object[]{o}).getObject();
>                }
>
>                return getString(FormatLabel.this.getId() + ".null");
>            }
>        });
>    }
> }
>
>
> Yves-Marie
>
>
>
> 2011/9/23 Pranav kacholia <[hidden email]>
>
>
> > Perhaps we can keep it as a NumberFormatLabel for greater flexibility
> >
> > import java.text.NumberFormat;
> > import org.apache.wicket.markup.ComponentTag;
> > import org.apache.wicket.markup.MarkupStream;
> > import org.apache.wicket.markup.html.basic.Label;
> > import org.apache.wicket.model.IModel;
> > import org.apache.wicket.model.Model;
> >
> > /**
> >  *
> >  * @author pkacholia
> >  */
> > public class NumberFormatLabel extends Label {
> >
> >    final private NumberFormat formatter;
> >
> >    public NumberFormatLabel(String id, IModel<? extends Number> model,
> > NumberFormat format) {
> >         super(id, model);
> >        formatter = format;
> >    }
> >
> >     public NumberFormatLabel(String id, Number obj, NumberFormat format)
> {
> >         this(id, new Model<Number>(obj), format);
> >    }
> >
> >     public NumberFormatLabel(String id, NumberFormat format) {
> >         super(id);
> >        formatter = format;
> >    }
> >
> >    /**
> >     * {@inheritDoc}
> >     */
> >    @Override
> >    public void onComponentTagBody(final MarkupStream markupStream, final
> > ComponentTag openTag) {
> >        String response;
> >        try {
> >        response = formatter.format(getDefaultModelObject());
> >        } catch (IllegalArgumentException ex) {
> >            error(getString("NaN"));
> >            response = "";
> >        } catch (NullPointerException ex) {
> >            error(getString("NullFormatter"));
> >            response = "";
> >        }
> >        replaceComponentTagBody(markupStream, openTag, response);
> >    }
> > }
> >
> > --
> > View this message in context:
> >
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3836001.html
> > Sent from the Users forum mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>
>
>
> --
> Yves-Marie LAINÉ
>
>
>
>  _____
>
> If you reply to this email, your message will be added to the discussion
> below:
>
>
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837008.html
>
> To unsubscribe from DecimalFormatLabel (proposed), click <
> http://apache-wicket.1842946.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3834813&code=cHJhbmF2LmthY2hvbGlhQGdtYWlsLmNvbXwzODM0ODEzfC0yMDAxMDYyNzM1>
>  here.
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837115.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Yves-Marie LAINÉ

RE: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
Suppose someone wants to call formatlLabel.getModelObject()  ... 

I don’t see why they would, but hypothetically. 

In this case you would get the formatted string. Since the label is merely a view, should it be changing the model even by formatting it? 

 

From: Yves-Marie LAINÉ [via Apache Wicket] [mailto:ml-node+s1842946n3837008h89@n4.nabble.com] 
Sent: 23 September 2011 19:57
To: Pranav kacholia
Subject: Re: DecimalFormatLabel (proposed)

 

Hi all, 

Personnaly I did it like that (simpler for i18n, i think) : 

public class FormatLabel extends Label { 

    public FormatLabel(String id,final String formatKey, final IModel<? 
extends Serializable> model) { 
        super (id); 
        setDefaultModel(new AbstractReadOnlyModel<String>() { 
            @Override 
            public String getObject() { 

                Serializable o = model.getObject(); 

                if ( o != null) { 
                    return new StringResourceModel(formatKey, 
FormatLabel.this, model, new Object[]{o}).getObject(); 
                } 

                return getString(FormatLabel.this.getId() + ".null"); 
            } 
        }); 
    } 
} 


Yves-Marie 



2011/9/23 Pranav kacholia <[hidden email]> 


> Perhaps we can keep it as a NumberFormatLabel for greater flexibility 
> 
> import java.text.NumberFormat; 
> import org.apache.wicket.markup.ComponentTag; 
> import org.apache.wicket.markup.MarkupStream; 
> import org.apache.wicket.markup.html.basic.Label; 
> import org.apache.wicket.model.IModel; 
> import org.apache.wicket.model.Model; 
> 
> /** 
>  * 
>  * @author pkacholia 
>  */ 
> public class NumberFormatLabel extends Label { 
> 
>    final private NumberFormat formatter; 
> 
>    public NumberFormatLabel(String id, IModel<? extends Number> model, 
> NumberFormat format) { 
>         super(id, model); 
>        formatter = format; 
>    } 
> 
>     public NumberFormatLabel(String id, Number obj, NumberFormat format) { 
>         this(id, new Model<Number>(obj), format); 
>    } 
> 
>     public NumberFormatLabel(String id, NumberFormat format) { 
>         super(id); 
>        formatter = format; 
>    } 
> 
>    /** 
>     * {@inheritDoc} 
>     */ 
>    @Override 
>    public void onComponentTagBody(final MarkupStream markupStream, final 
> ComponentTag openTag) { 
>        String response; 
>        try { 
>        response = formatter.format(getDefaultModelObject()); 
>        } catch (IllegalArgumentException ex) { 
>            error(getString("NaN")); 
>            response = ""; 
>        } catch (NullPointerException ex) { 
>            error(getString("NullFormatter")); 
>            response = ""; 
>        } 
>        replaceComponentTagBody(markupStream, openTag, response); 
>    } 
> } 
> 
> -- 
> View this message in context: 
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3836001.html
> Sent from the Users forum mailing list archive at Nabble.com. 
> 
> --------------------------------------------------------------------- 
> To unsubscribe, e-mail: [hidden email] 
> For additional commands, e-mail: [hidden email] 
> 
> 



-- 
Yves-Marie LAINÉ 



  _____  

If you reply to this email, your message will be added to the discussion below:

http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837008.html 

To unsubscribe from DecimalFormatLabel (proposed), click <http://apache-wicket.1842946.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3834813&code=cHJhbmF2LmthY2hvbGlhQGdtYWlsLmNvbXwzODM0ODEzfC0yMDAxMDYyNzM1>  here. 



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3837115.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Yves-Marie LAINÉ <ym...@gmail.com>.
Hi all,

Personnaly I did it like that (simpler for i18n, i think) :

public class FormatLabel extends Label {

    public FormatLabel(String id,final String formatKey, final IModel<?
extends Serializable> model) {
        super (id);
        setDefaultModel(new AbstractReadOnlyModel<String>() {
            @Override
            public String getObject() {

                Serializable o = model.getObject();

                if ( o != null) {
                    return new StringResourceModel(formatKey,
FormatLabel.this, model, new Object[]{o}).getObject();
                }

                return getString(FormatLabel.this.getId() + ".null");
            }
        });
    }
}


Yves-Marie



2011/9/23 Pranav kacholia <pr...@gmail.com>

> Perhaps we can keep it as a NumberFormatLabel for greater flexibility
>
> import java.text.NumberFormat;
> import org.apache.wicket.markup.ComponentTag;
> import org.apache.wicket.markup.MarkupStream;
> import org.apache.wicket.markup.html.basic.Label;
> import org.apache.wicket.model.IModel;
> import org.apache.wicket.model.Model;
>
> /**
>  *
>  * @author pkacholia
>  */
> public class NumberFormatLabel extends Label {
>
>    final private NumberFormat formatter;
>
>    public NumberFormatLabel(String id, IModel<? extends Number> model,
> NumberFormat format) {
>         super(id, model);
>        formatter = format;
>    }
>
>     public NumberFormatLabel(String id, Number obj, NumberFormat format) {
>         this(id, new Model<Number>(obj), format);
>    }
>
>     public NumberFormatLabel(String id, NumberFormat format) {
>         super(id);
>        formatter = format;
>    }
>
>    /**
>     * {@inheritDoc}
>     */
>    @Override
>    public void onComponentTagBody(final MarkupStream markupStream, final
> ComponentTag openTag) {
>        String response;
>        try {
>        response = formatter.format(getDefaultModelObject());
>        } catch (IllegalArgumentException ex) {
>            error(getString("NaN"));
>            response = "";
>        } catch (NullPointerException ex) {
>            error(getString("NullFormatter"));
>            response = "";
>        }
>        replaceComponentTagBody(markupStream, openTag, response);
>    }
> }
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3836001.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Yves-Marie LAINÉ

RE: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
Perhaps we can keep it as a NumberFormatLabel for greater flexibility

import java.text.NumberFormat;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 *
 * @author pkacholia
 */
public class NumberFormatLabel extends Label {

    final private NumberFormat formatter;
    
    public NumberFormatLabel(String id, IModel<? extends Number> model,
NumberFormat format) {
        super(id, model);
        formatter = format;
    }

    public NumberFormatLabel(String id, Number obj, NumberFormat format) {
        this(id, new Model<Number>(obj), format);
    }

    public NumberFormatLabel(String id, NumberFormat format) {
        super(id);
        formatter = format;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onComponentTagBody(final MarkupStream markupStream, final
ComponentTag openTag) {
        String response;
        try {
        response = formatter.format(getDefaultModelObject());
        } catch (IllegalArgumentException ex) {
            error(getString("NaN"));
            response = "";
        } catch (NullPointerException ex) {
            error(getString("NullFormatter"));
            response = "";
        } 
        replaceComponentTagBody(markupStream, openTag, response);
    }
}

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3836001.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


RE: DecimalFormatLabel (proposed)

Posted by Pranav kacholia <pr...@gmail.com>.
I figured this would work best if we use things like CompoundPropertyModel. 

 

Imagine you have a property of type int called myInt  in POJO called myPojo

 

Then the code becomes extremely simple 

 

WebMarkupContainer container = new WebMarkupContainer("container", new
CompoundPropertyModel(myPojo));

container.add(new DecimalFormatLabel("myInt", new DecimalFormat());

 

 

 

From: uiron [via Apache Wicket]
[mailto:ml-node+s1842946n3835944h5@n4.nabble.com] 
Sent: 23 September 2011 12:17
To: Pranav kacholia
Subject: Re: DecimalFormatLabel (proposed)

 

Wouldn't it be better to create a model of String type which is 
constructed with a Number and NumberFormat as arguments, and use it as 
model for standard label? 

On 2011.09.22 23:45, Pranav kacholia wrote: 


> I have foound many places where i need to format a number and display it
in a 
> label. Can we have a simple child of Label as follows which can be used to

> do the same? 
> 
> ------------------------------------------------------------ 
> package ; 
> 
> import java.text.DecimalFormat; 
> import org.apache.wicket.markup.ComponentTag; 
> import org.apache.wicket.markup.MarkupStream; 
> import org.apache.wicket.markup.html.basic.Label; 
> import org.apache.wicket.model.IModel; 
> import org.apache.wicket.model.Model; 
> 
> /** 
>   * 
>   * @author pkacholia 
>   */ 
> public class DecimalFormatLabel extends Label { 
> 
>      final private DecimalFormat formatter; 
> 
>      public DecimalFormatLabel(String id, IModel<? extends Number>  model,

> DecimalFormat format) { 
>          super(id, model); 
>          formatter = format; 
>      } 
> 
>      public DecimalFormatLabel(String id, Number obj, DecimalFormat
format) { 
>          this(id, new Model<Number>(obj), format); 
>      } 
> 
>      public DecimalFormatLabel(String id, DecimalFormat format) { 
>          super(id); 
>          formatter = format; 
>      } 
> 
>      /** 
>       * {@inheritDoc} 
>       */ 
>      @Override 
>      public void onComponentTagBody(final MarkupStream markupStream, final

> ComponentTag openTag) { 
>          String response; 
>          try { 
>          response = formatter.format(getDefaultModelObject()); 
>          } catch (IllegalArgumentException ex) { 
>              error(getString("NaN")); 
>              response = ""; 
>          } catch (NullPointerException ex) { 
>              error(getString("NullFormatter")); 
>              response = ""; 
>          } 
>          replaceComponentTagBody(markupStream, openTag, response); 
>      } 
> } 
> 
> 
> 
> 
> 
> -- 
> View this message in context:
http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp383
4813p3834813.html
> Sent from the Users forum mailing list archive at Nabble.com. 
> 
> --------------------------------------------------------------------- 
> To unsubscribe, e-mail: [hidden email] 
> For additional commands, e-mail: [hidden email] 
> 



--------------------------------------------------------------------- 
To unsubscribe, e-mail: [hidden email] 
For additional commands, e-mail: [hidden email] 




  _____  

If you reply to this email, your message will be added to the discussion
below:

http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp383
4813p3835944.html 

To unsubscribe from DecimalFormatLabel (proposed), click
<http://apache-wicket.1842946.n4.nabble.com/template/NamlServlet.jtp?macro=u
nsubscribe_by_code&node=3834813&code=cHJhbmF2LmthY2hvbGlhQGdtYWlsLmNvbXwzODM
0ODEzfC0yMDAxMDYyNzM1>  here. 



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3835958.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DecimalFormatLabel (proposed)

Posted by Viktoras <it...@gmail.com>.
Wouldn't it be better to create a model of String type which is 
constructed with a Number and NumberFormat as arguments, and use it as 
model for standard label?

On 2011.09.22 23:45, Pranav kacholia wrote:
> I have foound many places where i need to format a number and display it in a
> label. Can we have a simple child of Label as follows which can be used to
> do the same?
>
> ------------------------------------------------------------
> package ;
>
> import java.text.DecimalFormat;
> import org.apache.wicket.markup.ComponentTag;
> import org.apache.wicket.markup.MarkupStream;
> import org.apache.wicket.markup.html.basic.Label;
> import org.apache.wicket.model.IModel;
> import org.apache.wicket.model.Model;
>
> /**
>   *
>   * @author pkacholia
>   */
> public class DecimalFormatLabel extends Label {
>
>      final private DecimalFormat formatter;
>
>      public DecimalFormatLabel(String id, IModel<? extends Number>  model,
> DecimalFormat format) {
>          super(id, model);
>          formatter = format;
>      }
>
>      public DecimalFormatLabel(String id, Number obj, DecimalFormat format) {
>          this(id, new Model<Number>(obj), format);
>      }
>
>      public DecimalFormatLabel(String id, DecimalFormat format) {
>          super(id);
>          formatter = format;
>      }
>
>      /**
>       * {@inheritDoc}
>       */
>      @Override
>      public void onComponentTagBody(final MarkupStream markupStream, final
> ComponentTag openTag) {
>          String response;
>          try {
>          response = formatter.format(getDefaultModelObject());
>          } catch (IllegalArgumentException ex) {
>              error(getString("NaN"));
>              response = "";
>          } catch (NullPointerException ex) {
>              error(getString("NullFormatter"));
>              response = "";
>          }
>          replaceComponentTagBody(markupStream, openTag, response);
>      }
> }
>
>
>
>
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/DecimalFormatLabel-proposed-tp3834813p3834813.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org