Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    use of the FormItemInitHandler to init the DateItem to initiate FilterBuilder

    Hi I try to set date to NOW if the date is not pre-set for Date fields.
    Here's the code
    Code:
    final DataSource dataSource =  DeviceFieldFormatter.createDSwithPresetOptionsRemoved( dataSourceOld, 
    "field1", "field2");
    FilterBuilder filterBuilder = new FilterBuilder();
    filterBuilder.setDataSource( dataSource );
    
    public static DataSource createDSwithPresetOptionsRemoved(final DataSource dataSource, final String... fieldNames) {
    
    final DataSource newDataSource = new DataSource();
    		DataSourceField field;
    		DataSourceDateField dtField;
    		
    		
    		for(String fieldName : fieldNames) {	
    			//have all the datatime type fields already created
    			field = dataSource.getField( fieldName );
    			if ( field != null && field.getType() == FieldType.DATETIME ) {
    				dtField = new DataSourceDateField(field.getName(), field.getTitle());	
    				dtField.setHidden(field.getHidden());
    				
    				final DateItem editorType = new DateItem();
    				editorType.setUseTextField(true);
    				editorType.setInitHandler(new DateInit());
    				
    				
    				dtField.setEditorProperties(editorType);
    				newDataSource.setFields(dtField);
    			}
    		}
    		//must inherit only after the fields are already set
    	    newDataSource.setInheritsFrom(dataSource);
    	    newDataSource.setUseParentFieldOrder(true);
    
    		return newDataSource;
    	}
    	
    }
    
    private static class DateInit implements FormItemInitHandler {
    	
    		@Override
    		public void onInit(FormItem item) {
    			if(item.getDisplayValue().isEmpty()) {
    //HERE's the problem !
    				item.setValue(new Date());
    			}
    			
    			
    		}
    		
    	}
    I wanted it to set current date on the item if the value is empty.
    I can not use editorType.setDefaultValue(new Date()), because sometimes I use filterBuilder.setCriteria( existingAdvancedCriteria );
    and in this case instead of the values for dates from existingAdvancedCriteria it uses Default values.
    the exception which I get if I call item.setValue(new Date()) from the onInit():

    com.google.gwt.core.client.JavaScriptException: (TypeError) @com.google.gwt.core.client.impl.Impl::apply(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)([JavaScript object(50422), JavaScript object(53656), JavaScript object(53657)]): Unable to get property 'length' of undefined or null reference
    at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:249)
    at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:571)
    at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:279)
    at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
    at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
    at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:242)
    at sun.reflect.GeneratedMethodAccessor49.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
    at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
    at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
    at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:293)
    at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:547)
    at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:364)
    at java.lang.Thread.run(Unknown Source)

    What should I do to set the current date using the FormItemInitHandler ? I did read DataSourceField.setEditorProperties documentation but do not see how to use it in my case. I tried to provide editorType to the handler:
    Code:
    private static class DateInit implements FormItemInitHandler {
    	final DateItem editorType;
    public DateInit (DateItem editorType){
     this.editorType= editorType;
    }
    		@Override
    		public void onInit(FormItem item) {
    			if(item.getDisplayValue().isEmpty()) {
    				editorType.setValue(new Date());
    			}
    			
    			
    		}
    		
    	}
    Call it as
    Code:
    editorType.setInitHandler(new DateInit(editorType));
    It still does not work and throws exception.

    can you suggest how I can use FormItemInitHandler to set value if the current value is empty ?

    used version is: SmartClient Version: v9.0p_2014-04-23/PowerEdition Deployment (built 2014-04-23)

    #2
    The first thing we'd recommend is checking whether this issue exists on a more recent version of the product.
    If you can upgrade to 10.0p, we'd recommend you re-test there, but if this is not an option, please test against the latest 9.1p nightly build (available here).
    If the problem persists, let us know and we'll take a look.

    Regards
    Isomorphic Software

    Comment


      #3
      The issue is about support of one of our older versions of the product, we can not update the sgwt without complete re-testing of whole product , which is a very long process.
      My main issue here how to update the value of the item using the FormItemInitHandler.
      Why would item.setValue() throw exception...
      Code:
        
      private static class DateInit implements FormItemInitHandler {
      	
      		@Override
      		public void onInit(FormItem item) {
      			if(item.getDisplayValue().isEmpty()) {
      				//the set value throws the exception
      				//item.setValue(new Date());
      				System.out.println("EMPTY");
      			} else {
      				System.out.println("VALUE:"+item.getDisplayValue());
      			}
      			
      			
      		}
      		
      	}
      ///////////
      
      final DateItem editorType = new DateItem();
      				editorType.setUseTextField(true);
      				editorType.setInitHandler(new DateInit());	
      				dtField.setEditorProperties(editorType);

      Comment


        #4
        If upgrading to 9.1p or 10.0p is impossible for you we can probably address this in 9.0p if there is indeed a real framework bug here.
        This isn't yet clear from your description.
        (Note: If you are doing new development, you should always work with the latest version of the framework -- backporting fixes to older branches only really makes sense in the rare cases where you're already in production and need to resolve an issue there).

        We put together a small EntryPoint class based on your code in order to test this issue and are not able to reproduce the problem against the latest 9.0p build.

        Sample code follows:

        Code:
        package com.smartgwt.sample.client;
        
        import java.util.Date;
        
        import com.google.gwt.core.client.EntryPoint;
        import com.smartgwt.client.data.DataSource;
        import com.smartgwt.client.data.DataSourceField;
        import com.smartgwt.client.data.fields.DataSourceDateField;
        import com.smartgwt.client.types.FieldType;
        import com.smartgwt.client.widgets.form.FilterBuilder;
        import com.smartgwt.client.widgets.form.fields.DateItem;
        import com.smartgwt.client.widgets.form.fields.FormItem;
        import com.smartgwt.client.widgets.form.fields.events.FormItemInitHandler;
        
        public class DateItemInitHandler implements EntryPoint {
        
        	@Override
        	public void onModuleLoad() {
        		
        
        		DataSource dataSourceOld = new DataSource();
        		dataSourceOld.setClientOnly(true);
        		
        		DataSourceDateField field1 = new DataSourceDateField("field1", "Field 1");
        		DataSourceDateField field2 = new DataSourceDateField("field2", "Field 2");
        		
        		dataSourceOld.setFields(field1,field2);
        		
        		
        		final DataSource dataSource = createDSwithPresetOptionsRemoved( dataSourceOld, 
        				"field1", "field2");
        		FilterBuilder filterBuilder = new FilterBuilder();
        		filterBuilder.setDataSource( dataSource );
        		
        		filterBuilder.draw();
        
        	}
        	
        	public static DataSource createDSwithPresetOptionsRemoved(final DataSource dataSource, final String... fieldNames) {
        
        		final DataSource newDataSource = new DataSource();
        			DataSourceField field;
        			DataSourceDateField dtField;
        			
        			
        			for(String fieldName : fieldNames) {	
        				//have all the datatime type fields already created
        				field = dataSource.getField( fieldName );
        				if ( field != null && field.getType() == FieldType.DATETIME ) {
        					dtField = new DataSourceDateField(field.getName(), field.getTitle());	
        					dtField.setHidden(field.getHidden());
        					
        					final DateItem editorType = new DateItem();
        					editorType.setUseTextField(true);
        					editorType.setInitHandler(new DateInit());
        					
        					
        					dtField.setEditorProperties(editorType);
        					newDataSource.setFields(dtField);
        				}
        			}
        			//must inherit only after the fields are already set
        		    newDataSource.setInheritsFrom(dataSource);
        		    newDataSource.setUseParentFieldOrder(true);
        
        			return newDataSource;
        		}
        	
        	private static class DateInit implements FormItemInitHandler {
        	
        		@Override
        		public void onInit(FormItem item) {
        			if(item.getDisplayValue().isEmpty()) {
        //HERE's the problem !
        				item.setValue(new Date());
        			}
        			
        			
        		}
        		
        	}		
        	
        }
        If you'd like us to look further, we'd recommend you put together a complete test case we can actually run (possibly starting with the above EntryPoint) and (if necessary) give us steps to reproduce the problem.
        Please be sure to make the test case not be dependent on any resources we don't have (so we can actually run it), and as simple as possible.

        Thanks
        Isomorphic Software

        Comment

        Working...
        X