Announcement

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

    Do not display relative values for "datetime" in the FilterBuilder

    I use the FilterBuilder :
    Code:
     FilterBuilder filterBuilder = new FilterBuilder();
    filterBuilder.setDataSource(myDataSource);
    The myDataSource has different types of variables, part of them are "number", part are "ntext" and part are "datetime".
    As by default the "datetime" variables display SelectItem with the following pre-configured values "Today, Yestarday, Tomorrow,.. N month ago, N month from now" (see attached picture) and calendar picker.
    I would like to display the datetime type WITHOUT these pre-configured values. Do I have a way to clean the pre-configured values from the "datetime" field.

    I use SmartClient Version v9.0p_2014-04-23/PowerEdition
    Attached Files
    Last edited by aafros; 4 Feb 2015, 08:00.

    #2
    RelativeDateItem.presetOptions is how you configure these. You can set them system-wide via RelativeDateItem.setDefaultProperties().

    Comment


      #3
      here's the implementation:
      Code:
      public static void removePresetOptionsFromDateTime(final DataSource dataSource, final String... fieldNames) {
      		
      		final LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(0);
      		final RelativeDateItem editorType = new RelativeDateItem();		
      		editorType.setPresetOptions(options);
      		editorType.setShowPastOptions(false);
      		editorType.setShowFutureOptions(false);
      		
      		DataSourceField field;
      		
      		for(String fieldName : fieldNames) {
      			field = dataSource.getField( fieldName );
      			if ( field != null && field.getType() == FieldType.DATETIME ) {
      				//field.setEditorType(editorType);
      				field.setEditorProperties(editorType);
      			}
      		}
      
      	}
      Thanks for your help.

      Comment


        #4
        there's other problem with this solution. Once I changed the DataSource - I should change it back if I want other screens which use this DataSource still have preset options for datatime types.
        I see only 2 solutions:
        1. Every time I close the window reset the setting to the default:
        Code:
         
        public static void resetPresetOptionsToDateTime(final DataSource dataSource, final String... fieldNames) {
        		
        		final RelativeDateItem editorType = new RelativeDateItem();			
        		editorType.setShowPastOptions(true);
        		editorType.setShowFutureOptions(true);
        		
        		DataSourceField field;
        		
        		for(String fieldName : fieldNames) {
        			field = dataSource.getField( fieldName );
        			if ( field != null && field.getType() == FieldType.DATETIME ) {
        				field.setEditorProperties(editorType);
        			}
        		}
        
        	}
        2. every time I use the DataSource for the removePresetOptionsFromDateTime I need to clone the original DataSource, but it did not work for me:
        Code:
        DataSource ds = new DataSource(originalDataSource.getDataURL());
        or
        Code:
        DataSource ds = new DataSource(originalDataSource.getJsObj());
        they both did not create copy or cloned version of the originalDataSource.

        My questions are :
        Can I clone the DataSource ?
        Can I delete the preset options from the FilterBuilder only for current window without changing "global" DataSource object ?

        Comment


          #5
          The easiest thing, if you are trying to tweak how fields are edited just for this context, is to create a DataSource that inheritsFrom the original DataSource, then selectively override field definitions.

          See the docs for DataSource.inheritsFrom for details and related properties.

          Comment


            #6
            I try to change the field in the inherited DataSource
            Code:
             
            public static DataSource removePresetOptionsFromDateTime(final DataSource dataSource, final String... fieldNames) {
            		
            		final LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(0);
            		final RelativeDateItem editorType = new RelativeDateItem();		
            		editorType.setPresetOptions(options);
            		editorType.setShowPastOptions(false);
            		editorType.setShowFutureOptions(false);
            		
            
            		DataSource newDataSource = new DataSource();
            		newDataSource.setInheritsFrom(dataSource);
            		
            		DataSourceField field;
            		
            		for(String fieldName : fieldNames) {
            			field = newDataSource.getField( fieldName );
            			if ( field != null && field.getType() == FieldType.DATETIME ) {
            				field.setEditorProperties(editorType);
            			}
            		}
            		return newDataSource;
            
            	}
            unfortunately I got the same result trying to use the "newDataSource"
            I checked the newly created data source and found that it uses the same fields as the original one, I mean it does NOT copy the fielda, but uses the same objects as the original:
            Code:
             
            DataSource newDataSource = new DataSource();
            newDataSource.setInheritsFrom(dataSource);
            		
            DataSourceField field, newField;
            		
            for(String fieldName : fieldNames) {
            		field = newDataSource.getField( fieldName );
                             newField= dataSource.getField( fieldName );
            }
            i checked in debugger both 'field' and 'newField' have the same address, and inner jsObj in both point obviously onto the same object.
            This way then I change newField it's the same as change the field itself.

            Do I use the setInheritsFrom() right ? I wanted to create the separate DataSource object, not the shallow copy of the original object.
            how should I use the setInheritsFrom functionality to create a separate DataSource object?

            Comment


              #7
              It looks like you didn't read the docs for inheritsFrom - it's very important to read this. As the docs tell you, you should be providing new field definitions to the inheriting DataSource. These then act as overrides of the field definitions from the original DataSource - the docs describe the details.

              Comment


                #8
                Code:
                public static DataSource createDSwithPresetOptionsRemoved(final DataSource dataSource, final String... fieldNames) {
                		
                		final LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(0);
                		final RelativeDateItem editorType = new RelativeDateItem();		
                		editorType.setPresetOptions(options);
                		editorType.setShowPastOptions(false);
                		editorType.setShowFutureOptions(false);
                		
                		DataSourceField field;
                		DataSourceDateTimeField dtField;
                		DataSource newDataSource = new DataSource();
                		for(String fieldName : fieldNames) {
                			
                			field = dataSource.getField( fieldName );
                			if ( field != null && field.getType() == FieldType.DATETIME ) {
                				dtField = new DataSourceDateTimeField(field.getName(), field.getTitle());	
                				dtField.setHidden(field.getHidden());
                				dtField.setEditorProperties(editorType);
                				newDataSource.setFields(dtField);
                			}
                		}
                	    newDataSource.setInheritsFrom(dataSource);
                	    newDataSource.setUseParentFieldOrder(true);
                
                		return newDataSource;
                	}
                This one seems to work and create the copy of the DataSource but without preset options on the 'fieldNames' fields
                Last edited by aafros; 6 Feb 2015, 05:54.

                Comment


                  #9
                  There's no "DataSet" being copied, probably you meant DataSource.

                  As far as your description of what's going on - that's what you wanted, right?

                  Comment


                    #10
                    Yes, I wanted create the new DataSource witch can be used in the filterBuilder.
                    This is the use of the function above:
                    Code:
                    FilterBuilder filterBuilder = new FilterBuilder();
                    final DataSource dataSource =  createDSwithPresetOptionsRemoved( globalDataSource, "field1_name", "field2_name");
                    filterBuilder.setDataSource( dataSource );
                    Now filterBuilder uses the newly created 'local copy' of the global dataSource with overwritten preset options for the "field1_name", "field2_name".
                    All the rest of the windows which use the same globalDataSource for creating their own FilterBuilder are not affected and still display preset options for these fields (today, yesterday, tomorrow and so on).

                    Thanks for your help.

                    Comment

                    Working...
                    X