<s> """<STR_LIT>""" <EOL> from pyjamas . ui . TextBox import TextBox <EOL> from pyjamas . ui . Grid import Grid <EOL> from pyjamas . ui . FormPanel import FormPanel <EOL> from pyjamas . log import writebr <EOL> from pyjamas . ui . TextBox import TextBox <EOL> class CharField ( TextBox ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> TextBox . __init__ ( self ) <EOL> self . max_length = kwargs . get ( '<STR_LIT:max_length>' , None ) <EOL> self . min_length = kwargs . get ( '<STR_LIT>' , None ) <EOL> self . required = kwargs . get ( '<STR_LIT>' , None ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> self . setValue ( kwargs [ '<STR_LIT>' ] ) <EOL> def setValue ( self , val ) : <EOL> if val is None : <EOL> val = '<STR_LIT>' <EOL> self . setText ( val ) <EOL> def getValue ( self ) : <EOL> return self . getText ( ) <EOL> class IntegerField ( TextBox ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> TextBox . __init__ ( self ) <EOL> self . required = kwargs . get ( '<STR_LIT>' , None ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> self . setValue ( kwargs [ '<STR_LIT>' ] ) <EOL> def setValue ( self , val ) : <EOL> if val is None : <EOL> val = '<STR_LIT>' <EOL> self . setText ( val ) <EOL> def getValue ( self ) : <EOL> writebr ( '<STR_LIT>' % self . getText ( ) ) <EOL> return int ( self . getText ( ) ) <EOL> class FloatField ( TextBox ) : <EOL> def __init__ ( self , ** kwargs ) : <EOL> TextBox . __init__ ( self ) <EOL> self . max_length = kwargs . get ( '<STR_LIT:max_length>' , None ) <EOL> self . min_length = kwargs . get ( '<STR_LIT>' , None ) <EOL> self . required = kwargs . get ( '<STR_LIT>' , None ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> self . setValue ( kwargs [ '<STR_LIT>' ] ) <EOL> def setValue ( self , val ) : <EOL> if val is None : <EOL> val = '<STR_LIT>' <EOL> self . setText ( val ) <EOL> def getValue ( self ) : <EOL> return self . getText ( ) <EOL> widget_factory = { '<STR_LIT>' : CharField , <EOL> '<STR_LIT>' : IntegerField , <EOL> '<STR_LIT>' : FloatField <EOL> } <EOL> class FormSaveGrid : <EOL> def __init__ ( self , sink ) : <EOL> self . sink = sink <EOL> def onRemoteResponse ( self , response , request_info ) : <EOL> method = request_info . method <EOL> writebr ( repr ( response ) ) <EOL> writebr ( "<STR_LIT>" % len ( response ) ) <EOL> writebr ( "<STR_LIT:%s>" % repr ( response . keys ( ) ) ) <EOL> self . sink . save_respond ( response ) <EOL> class FormGetGrid : <EOL> def __init__ ( self , sink ) : <EOL> self . sink = sink <EOL> def onRemoteResponse ( self , response , request_info ) : <EOL> method = request_info . method <EOL> writebr ( method ) <EOL> writebr ( repr ( response ) ) <EOL> writebr ( "<STR_LIT>" % len ( response ) ) <EOL> writebr ( "<STR_LIT:%s>" % repr ( response . keys ( ) ) ) <EOL> self . sink . do_get ( response ) <EOL> def onRemoteError ( self , code , message , request_info ) : <EOL> writebr ( "<STR_LIT>" % code + "<STR_LIT>" + message + '<STR_LIT>' + request_info . method ) <EOL> class FormDescribeGrid : <EOL> def __init__ ( self , sink ) : <EOL> self . sink = sink <EOL> def onRemoteResponse ( self , response , request_info ) : <EOL> method = request_info . method <EOL> self . sink . do_describe ( response ) <EOL> def onRemoteError ( self , code , message , request_info ) : <EOL> writebr ( "<STR_LIT>" % code + "<STR_LIT>" + message + '<STR_LIT>' + request_info . method ) <EOL> class Form ( FormPanel ) : <EOL> def __init__ ( self , svc , ** kwargs ) : <EOL> self . describe_listeners = [ ] <EOL> if kwargs . has_key ( '<STR_LIT>' ) : <EOL> listener = kwargs . pop ( '<STR_LIT>' ) <EOL> self . addDescribeListener ( listener ) <EOL> if kwargs . has_key ( '<STR_LIT:data>' ) : <EOL> data = kwargs . pop ( '<STR_LIT:data>' ) <EOL> else : <EOL> data = None <EOL> FormPanel . __init__ ( self , ** kwargs ) <EOL> self . svc = svc <EOL> self . grid = Grid ( ) <EOL> self . grid . resize ( <NUM_LIT:0> , <NUM_LIT:3> ) <EOL> self . add ( self . grid ) <EOL> self . describer = FormDescribeGrid ( self ) <EOL> self . saver = FormSaveGrid ( self ) <EOL> self . getter = FormGetGrid ( self ) <EOL> self . formsetup ( data ) <EOL> def addDescribeListener ( self , l ) : <EOL> self . describe_listeners . append ( l ) <EOL> def add_widget ( self , description , widget ) : <EOL> """<STR_LIT>""" <EOL> num_rows = self . grid . getRowCount ( ) <EOL> self . grid . resize ( ( num_rows + <NUM_LIT:1> ) , <NUM_LIT:3> ) <EOL> self . grid . setHTML ( num_rows , <NUM_LIT:0> , description ) <EOL> self . grid . setWidget ( num_rows , <NUM_LIT:1> , widget ) <EOL> def get ( self , ** kwargs ) : <EOL> writebr ( repr ( kwargs ) ) <EOL> self . svc ( { } , { '<STR_LIT>' : kwargs } , self . getter ) <EOL> def save ( self , data = None ) : <EOL> self . clear_errors ( ) <EOL> if data is None : <EOL> data = self . getValue ( ) <EOL> self . data = data <EOL> writebr ( repr ( self . data ) ) <EOL> self . svc ( data , { '<STR_LIT>' : None } , self . saver ) <EOL> def save_respond ( self , response ) : <EOL> if not response [ '<STR_LIT:success>' ] : <EOL> errors = response [ '<STR_LIT>' ] <EOL> self . set_errors ( errors ) <EOL> for l in self . describe_listeners : <EOL> l . onErrors ( self , errors ) <EOL> return <EOL> for l in self . describe_listeners : <EOL> l . onSaveDone ( self , response ) <EOL> def formsetup ( self , data = None ) : <EOL> if data is None : <EOL> data = { } <EOL> self . data = data <EOL> self . svc ( data , { '<STR_LIT>' : None } , self . describer ) <EOL> def clear_errors ( self ) : <EOL> for idx , fname in enumerate ( self . fields ) : <EOL> self . grid . setHTML ( idx , <NUM_LIT:2> , None ) <EOL> def set_errors ( self , errors ) : <EOL> offsets = { } <EOL> for idx , fname in enumerate ( self . fields ) : <EOL> offsets [ fname ] = idx <EOL> for k , err in errors . items ( ) : <EOL> err = "<STR_LIT>" . join ( err ) <EOL> idx = offsets [ k ] <EOL> self . grid . setHTML ( idx , <NUM_LIT:2> , err ) <EOL> def update_values ( self , data = None ) : <EOL> if data is not None : <EOL> self . data = data <EOL> for idx , fname in enumerate ( self . fields ) : <EOL> val = None <EOL> if self . data . has_key ( fname ) : <EOL> val = self . data [ fname ] <EOL> w = self . grid . getWidget ( idx , <NUM_LIT:1> ) <EOL> w . setValue ( val ) <EOL> def do_get ( self , response ) : <EOL> fields = response . get ( '<STR_LIT>' , None ) <EOL> if fields : <EOL> self . update_values ( fields ) <EOL> for l in self . describe_listeners : <EOL> l . onRetrieveDone ( self , fields ) <EOL> def do_describe ( self , fields ) : <EOL> self . fields = fields . keys ( ) <EOL> for idx , fname in enumerate ( self . fields ) : <EOL> field = fields [ fname ] <EOL> if self . data and self . data . has_key ( fname ) : <EOL> field [ '<STR_LIT>' ] = self . data [ fname ] <EOL> field_type = field [ '<STR_LIT:type>' ] <EOL> widget_kls = widget_factory . get ( field_type , CharField ) <EOL> fv = { } <EOL> for ( k , v ) in field . items ( ) : <EOL> fv [ str ( k ) ] = v <EOL> w = widget_kls ( ** fv ) <EOL> self . add_widget ( field [ '<STR_LIT:label>' ] , w ) <EOL> for l in self . describe_listeners : <EOL> l . onDescribeDone ( self ) <EOL> def getValue ( self ) : <EOL> res = { } <EOL> for idx , fname in enumerate ( self . fields ) : <EOL> w = self . grid . getWidget ( idx , <NUM_LIT:1> ) <EOL> val = w . getValue ( ) <EOL> res [ fname ] = val <EOL> self . data [ fname ] = val <EOL> return res </s>
