Announcement

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

    SpinnerItem showing percent

    Hi,

    I would like to use a SpinnerItem to edit a value between 0 and 1 by step of 0.01. The value should be displayed and edited in %.

    I would like it to be possible to edit either with the up/down arrows or typing the value (with a %) directly in the text box.

    With the code below, editing by typing works well, however it does not work using the arrows. Not using the parser (using only Formatter) makes it work with arrows but not direct edition. Any advice? thanks!

    Code:
    	private static final boolean NEED_GWT_4670_FIX;
    	static {
    		NumberFormat nf = NumberFormat.getFormat("#%");
    		NEED_GWT_4670_FIX = nf.parse(nf.format(1d)) == 100d;
    	}
    	
    	public void quickTest(){
    		
    		DynamicForm form = new DynamicForm();
    
    		SpinnerItem field = new SpinnerItem("test");
    		field.setCanEdit(true);
    		field.setValue(0.5);
    		field.setMin(0.0);
    		field.setMax(1.0);
    		field.setStep(0.01);
    		
    		field.setEditorValueFormatter(new FormItemValueFormatter(){
    
    			@Override
    			public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
    	            if(value == null) return null;  
    	            String val = null;  
    	            try {  
    	            	NumberFormat df = NumberFormat.getFormat("#,##0%");
    	                val = df.format(((Number) value).doubleValue());  
    	            } catch (Exception e) {  
    	                return value.toString();  
    	            }  
    	            return val;  			
    			}
    		});
    		
    		field.setEditorValueParser(new FormItemValueParser(){
    
    			@Override
    			public Object parseValue(String value, DynamicForm form, FormItem item) {
    	            if(value == null) return null;  
    	            Double val = null;  
    	            try {  
    					NumberFormat df = NumberFormat.getFormat("#,##0%");
    					val = df.parse(value);
    					if (NEED_GWT_4670_FIX) val = val / 100d; //This is a workaround for a known GWT bug
    	            } catch (Exception e) {  
    	                return null;  
    	            }  
    
    	            return val;  			
    			}			
    		});
    		
    		form.setItems(field);
    
    		form.draw();	
    		
    	}

    #2
    Any feedback on this please ?

    Thanks!

    Comment


      #3
      I'm trying to do the same thing and getting the same behavior.

      Using 2.5p with Firefox.

      Comment


        #4
        It looks like in the original code, any value that does not end in a percent is rejected (parser returns null). Instead, return a numeric value even if the user has not entered a percent char (%).

        Comment


          #5
          Actually, I didn't notice that in the OP's code. My code just multiplies the value by 100 to get it to show a value between 0 and 100 instead of a value between 0 and 1. When I type in a number and click outside the spinner item, the value changes correctly. But when I click the spinner up or down icons, the value does not change at all.

          Comment


            #6
            You'll need to show your code so we can analyze it.

            Comment


              #7
              Below is a test case I wrote. With this code, when I type a number and press tab, the value is not changed, which I would expect because the parse and formatter cancel each other out. However, when I click the up arrow, the parser is only called, which makes the value smaller.

              Code:
              FormItemValueFormatter newFormItemValueFormatter(final String name) {
                  return new FormItemValueFormatter() {
                      @Override
                      public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
                          if (value == null) value = 0d;
                          Log.info(name + " formatter: " + value);
                          return Double.toString((Float)value * 100);
                      }
                  };
              }
              
              @Override
              public void onModuleLoad() {
                  SpinnerItem item = new SpinnerItem();
                  item.setEditorValueFormatter(newFormItemValueFormatter("editor");
                  item.setValueFormatter(newFormItemValueFormatter("value");
                  item.setEditorValueParser(new FormItemValueParser() {
                      @Override
                      public Object parseValue(String value, DynamicForm form, FormItem item) {
                          if (value == null || value.isEmpty()) return 0;
                          Log.info("Parser: value=" + value + " item value=" + item.getValue());
                          return Double.parseDouble(value) / 100;
                      }
                  }
                  DynamicForm form = new DyanmicForm();
                  form.setItems(item);
              
                  VLayout layout = new VLayout();
                  layout.setMembers(form);
                  layout.draw();
              }

              Comment


                #8
                There is a framework issue here, which we have now resolved in 4.0d and 3.1p.
                The fix will be present in the next nightly build

                Regards
                Isomorphic Software

                Comment


                  #9
                  Is there any way a patch could be provided for 2.5p (SC_SNAPSHOT-2012-01-06_v8.1p/PowerEdition Deployment)? We won't be able to upgrade any time soon :(

                  Comment


                    #10
                    In this case the change is simple enough that we can (and have) ported it across. Please try the next nightly build!

                    Please do be aware that in some cases backporting fixes to 2.5p is going to be difficult - we've had 3 releases since then and some areas of the framework have undergone significant changes, so we'd definitely recommend you consider an upgrade at some point.

                    Regards
                    Isomorphic Software

                    Comment


                      #11
                      Thanks for the reply. Our approval process makes it difficult to even upgrade to nightly builds. However, we can include patches if it's possible.

                      Comment


                        #12
                        See smartclient.com/builds - the build we are suggesting you use includes patches only. The "nightly" aspect means only that it is built at night.

                        Comment


                          #13
                          Sorry I should've been more clear--since it doesn't require that much regression testing, we can include specific patches in the form of .js files being loaded from our html. I know you've done that in the past for us and we would be very grateful if you could again. Thanks!

                          Comment


                            #14
                            Used patched builds is easier for you (no separate file to load), easier for us, and avoids you running into other already-patched issues. In addition, it's lower risk than a set of separate patches, which have never been tested together.

                            So we'd strongly suggest just switching over to patch builds now. Regression testing for a patch build should be similar to installing an individual patch - it's not a new version, just a "service pack", except it contains even less risky changes than are typically covered by that terminology.

                            Comment

                            Working...
                            X