How to share code between Server and Client with DateTimeFormat

591 views
Skip to first unread message

Jogi

unread,
May 25, 2010, 8:16:29 AM5/25/10
to Google Web Toolkit
Hi,

I have a little problem and I hope someone can help me to solve it. I
have a class which used on client and server side, mainly to parse
Strings into Dates...

So I use on client side the com.google.gwt.i18n.client.DateTimeFormat
and on server side the java.text.SimpleDateFormat. The code of the
class got more and more and I don't want to have the same class (the
only different is really this one formatting part) on client and
server side... Is there a way to do a switch like:

Date parsed = null;
if (GWT.isScript()) {
com.google.gwt.i18n.client.DateTimeFormat formatter =
com.google.gwt.i18n.client.DateTimeFormat.getFormat(this.getFormat());
parsed = formatter.parse(value);
} else {
java.text.SimpleDateFormat formatter = new
java.text.SimpleDateFormat(this.getFormat());
parsed = formatter.parse(value);
}

This way does not work, cause the GWT Compiler does not see that it
will always be true and skip the second part... on server side this
would work fine (I guess) cause isScript would be false, so the
annoying GWT.create() error would not be raised... but is there some
way to get such a switch?

Thanks!

kozura

unread,
May 26, 2010, 10:20:04 AM5/26/10
to Google Web Toolkit
Make a generic interface for the methods you need and a factory; on
the client side set the factory to create a DateTimeFormat version of
the interface, and on the server a SimpleDateFormat version, in the
initialization code for the client (onModuleLoad) and server
(HttpServlet).

Mauro

unread,
Apr 24, 2011, 12:06:36 AM4/24/11
to google-we...@googlegroups.com
Hi, would you give me a sample on where the initialization code for server goes? Thanks

Adligo

unread,
Apr 24, 2011, 2:29:35 PM4/24/11
to Google Web Toolkit
Hi I,

use this class
http://cvs.adligo.org/viewvc/i_util/src/org/adligo/i/util/client/CommonTime.java?view=markup

Then when in JSE (on the Server) it delegates to
http://cvs.adligo.org/viewvc/jse_util/src/org/adligo/jse/util/JSETextFormatter.java?view=markup

And in GWT it delegates to
http://cvs.adligo.org/viewvc/gwt_util/src/org/adligo/gwt/util/client/GwtTextFormatter.java?view=markup


So to use it do this somewhere on the server;
static {
46 try {
47 JSEPlatform.init();
48 } catch (Exception x) {
49 x.printStackTrace();
50 }
51 }
For instance in ;
http://cvs.adligo.org/viewvc/adi_server_config/src/org/adligo/i/adi/server_config/ExternalConfigListener.java?view=markup
...
http://cvs.adligo.org/viewvc/adi_server_config/src/org/adligo/i/adi/server_config/ServerConfig.java?view=markup

And on the client do;
try {
39 GwtPlatform.init();
40 GwtLogFactory.init();
41 } catch (Exception x) {
42 x.printStackTrace();
43 log.error(x.getMessage(), x);
44 }
as in this example;
http://cvs.adligo.org/viewvc/gwt_util_demo/src/org/adligo/gwt/util/demo/client/DemoEntryPoint.java?view=markup

Then simply use the static methods in CommonTime.java and constants to
get the same date formatted on client or server.

Cheers,
Scott

Mauro

unread,
Apr 28, 2011, 1:40:06 PM4/28/11
to google-we...@googlegroups.com
Thank you very much for the help. I wonder if is there a central point where I can put once the static {...} code on the server-side or if I need to put this initialization on each piece of source-code on the server-side?

Best regards,
Mauro
Reply all
Reply to author
Forward
0 new messages