Quantcast
Channel: SmartClient Forums
Viewing all articles
Browse latest Browse all 4756

use of the FormItemInitHandler to init the DateItem to initiate FilterBuilder

$
0
0
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)

Viewing all articles
Browse latest Browse all 4756

Trending Articles