0

label component format

asked 2007-08-09 17:07:34 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4456991

By: nalin1279

Hi all

i want to put Date value in a label, for a read only view.
But when i set the label value with the date, the all timestamp is setted.
I'd like to override the label component and create a DateLabel with format properties...

Can u tell, me on wich event should i use a DateFormat to translate my date in a specific format ?

thx


delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2007-08-09 17:19:49 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4457011

By: nalin1279

ok it's work, i solve my help request :
i give my source code for those we need :

---------------------------------JAVA
FILE--------------------------------------------
package com.zul;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.zkoss.zk.ui.event.CreateEvent;
import org.zkoss.zul.Label;


public class DateLabel extends Label{

private static final long serialVersionUID = 1L;

private String format;
private Date date;

public void onCreate(CreateEvent evt){
System.out.println("format : " + format);
System.out.println("date : " + date);
DateFormat df = new SimpleDateFormat(getFormat());
super.setValue(df.format(getDate()));
}

public String getFormat() {
return format;
}

public void setFormat(String format) {
this.format = format;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}


---------------------ZULL FILE ---------------------

<label date="${quote.firstQuote}" format="dd/MM/yyyy" style="font-size:10px"
use="com.zul.DateLabel"/>


link publish delete flag offensive edit

answered 2007-08-09 20:14:48 +0800

admin gravatar image admin
18691 1 10 130
ZK Team


Orignial message at:
https://sourceforge.net/forum/message.php?msg_id=4457317

By: bakoma

Extending a component is expensive and not versatile in other occasions. I would rather implement a tag function like this:

public String format(String originalString, String formatString);

And use it this way:

<label value="${t:format(quote.firstQuote,"dd/MM/yyyy")}"/>

or other formats as needed. This is also available for:

<treecell label="${t:format(quote.firstQuote,"dd/MM/yyyy")}"/>
<button label="${t:format(quote.firstQuote,"dd/MM/yyyy")}"/>


etc...

link publish delete flag offensive edit

answered 2008-08-19 17:05:47 +0800

Luc gravatar image Luc
6

Hi all,

I also want to format my Date objects for labels but I use data binding with ZUML Annotations. I tried to implement a tag function according to bakoma but I think it's not possinble to do this:

<listcell label="${z:formatDate(@{akce.zacatek}, 'dd.MM.yy')}"/>

I worked with JSF before and there are converters for this purposes. I am a new one so any help appreciated.

thx

link publish delete flag offensive edit

answered 2008-08-19 18:40:05 +0800

robertpic71 gravatar image robertpic71
1275 1

updated 2008-08-19 18:44:24 +0800

Hi Luc,

you can work with converters in ZK. Take a look to my examples.

I extract one example:

<listcell label="@{person.birthdate, converter='org.zkforge.converters.DateUser'}"/>

Here is the convertersource from DateUser:

package org.zkforge.converters;
...
..
public class DateUser implements TypeConverter {

  /* (non-Javadoc)
   * @see org.zkoss.zkplus.databind.TypeConverter#coerceToUi(java.lang.Object, org.zkoss.zk.ui.Component)
   */
  public Object coerceToUi(Object arg0, Component arg1) {
    if (arg0 == null) {
      return null;
    }
    SimpleDateFormat sdf = new SimpleDateFormat(getDefaultFormat());
      Date date = (Date) arg0;
      return sdf.format(date);
  }
  /**
     * Returns the default format, which is used when outputing 
     * the date
     */
    protected String getDefaultFormat() {
      DateFormat df = DateFormat.getDateInstance(
          DateFormat.DEFAULT, Locales.getCurrent());
      if (df instanceof SimpleDateFormat) {
        String fmt = ((SimpleDateFormat)df).toPattern();
        if (fmt != null && !"M/d/yy h:mm a".equals(fmt))
          return fmt; //note: JVM use "M/d/yy h:mm a" if not found!
      }
      return "yyyy/MM/dd";
    }
}

Note: DateUser retrieves the Clientsettings (=Browerlanguage --> Default Dateformat), there are noDatu Converteroptions. Check the examples for the whole sourcecode.

If you want a customformat it should looks like this:

<listcell label="@{person.birthdate, converter='org.zkforge.converters.DateCustom'}"
    <custom-attributes format="dd.MM.yy"/></listcell>
w

Sorry my smalltalk about databinding is behind time, but i'm really busy most time. I want to finish the converters before a launch my next Utilities (direct SQL-Databinding things like <combobox model=${JDBCBinding.getSQL('select * from status')}/>).

/Robert

link publish delete flag offensive edit

answered 2008-08-19 18:43:59 +0800

robertpic71 gravatar image robertpic71
1275 1

Here is the sourcecode for the DateCustom Converter:

package org.zkforge.converters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.zkoss.util.Locales;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.databind.TypeConverter;

/**
 * @author Robert Pichelhofer
 * 
 * Databindingconverter
 * Direction: Output only
 * 
 * Source: Date, Timestamp
 * Destination: Label, Listcell 
 * 
 * Dateformat is retrieved from the customattribute format
 * 
 * TODO: more Examples
 * 
 * <listcell label="@{person.birthdate, converter='org.zkforge.converters.DateCustom'}"
  *  <custom-attributes format="dd.mm.yy"/></listcell>
 *
 * TODO: override the serverdefault via zk.xml
 */

public class DateCustom implements TypeConverter {

    String serverDefaultFormat;

    public DateCustom() {
	serverDefaultFormat = getDefaultFormat();
    }
    /* (non-Javadoc)
     * @see org.zkoss.zkplus.databind.TypeConverter#coerceToBean(java.lang.Object, org.zkoss.zk.ui.Component)
     */
    public Object coerceToBean(Object arg0, Component arg1) {
	// this is an output converter, no action here
	return null;
    }

    /* (non-Javadoc)
     * @see org.zkoss.zkplus.databind.TypeConverter#coerceToUi(java.lang.Object, org.zkoss.zk.ui.Component)
     */
    public Object coerceToUi(Object arg0, Component arg1) {
	if (arg0 == null) {
	    return null;
	}
	String format = (String) arg1.getAttribute("format");
        if (format == null) {
            format = serverDefaultFormat;
        }
	SimpleDateFormat sdf = new SimpleDateFormat(format);
	Date date = (Date) arg0;
	return sdf.format(date);
    }
    /**
     * Returns the default format, which is used when outputing 
     * the date
     * <p>The default format ({@link #getFormat}) depends on JVM's setting
     * and the current user's locale. That is,
     * <code>DateFormat.getDateInstance(DateFormat,DEFAULT, Locales.getCurrent).</code>
     *
     * <p>You might override this method to provide your own default format.
     */
    protected String getDefaultFormat() {
	DateFormat df = DateFormat.getDateInstance(
		DateFormat.DEFAULT, Locales.getCurrent());
	if (df instanceof SimpleDateFormat) {
	    final String fmt = ((SimpleDateFormat)df).toPattern();
	    if (fmt != null && !"M/d/yy h:mm a".equals(fmt))
		return fmt; //note: JVM use "M/d/yy h:mm a" if not found!
	}
	return "yyyy/MM/dd";
    }
}

/Robert

link publish delete flag offensive edit

answered 2008-08-19 19:25:30 +0800

Luc gravatar image Luc
6

Hi Robert,

thank you for your quick answer. I'll try the converter and examples on your website.

Again thanks for your help.

Luc

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2007-08-09 17:07:34 +0800

Seen: 1,494 times

Last updated: Aug 19 '08

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More