4 Replies Latest reply: Apr 10, 2012 1:37 AM by 812215 RSS

    如何使InputRangeSlide同时支持Double和Integer?

    812215
      开发环境ADF11.1.1.6.0,使用InputRangeSlide调用TestBean中的NumberRange。希望InputRangeSlide返回double值。

      页面:

      <af:inputRangeSlider label="Label 1" id="irs1"
      contentStyle="width:300px;" minimum="8.0"
      maximum="18.0" minimumIncrement="0.5"
      minorIncrement="0.5" value="#{testBean.nr}"
      autoSubmit="true" immediate="true"
      majorIncrement="1.0" />


      TestBean:

      private NumberRange nr = new NumberRange(8, 12);


      1. 初始化的时候是整型,在InputRangeSlide中拖拉至9,可以。但是拖拉到9.5就报错:java.lang.Long cannot be cast to java.lang.Double。

      private NumberRange<Double> nr = new NumberRange<Double>(8.5, 12.5);

      2. 初始化的时候是Double,在InputRangeSlide中拖拉至9.5,可以。但是拖拉到10就报错:java.lang.Double cannot be cast to java.lang.Long。


      如何使InputRangeSlide对应的NumberRange同时支持Double和Integer呢?
        • 1. Re: 如何使InputRangeSlide同时支持Double和Integer?
          857279
          Hi, Copy一段可以work的例子:

          #MrgBean:

          public class DemoInputBean implements Serializable{

          private Double _doubleSlider1 = 4.0;

          public Double getDoubleSlider1()
          {
          return _doubleSlider1;
          }

          public void setDoubleSlider1(Double doubleSlider1)
          {
          this._doubleSlider1 = doubleSlider1;
          }

          }

          #JSF:
          <af:inputNumberSlider label="bound to a double, step size=0.1" minorIncrement=".5" majorIncrement="1"
          minimumIncrement=".1" minimum="-5" maximum="5"
          orientation="horizontal" value="#{demoInput.doubleSlider1}" contentStyle="width: 50em"
          id="boundToDouble">
          <af:convertNumber maxFractionDigits="2"/>
          </af:inputNumberSlider>

          其中demoInput配置为指向DemoInputBean。
          • 2. Re: 如何使InputRangeSlide同时支持Double和Integer?
            812215
            InputNumberSlide是没有问题的,它可以对应Double或者Integer。。。

            有问题的是*InputRangeSlide*,此组件对应的是NumberRange类,在转换过程中会默认初始化的类型。如果初始化是Double,就无法读取Integer数值,转换出错!
            • 3. Re: 如何使InputRangeSlide同时支持Double和Integer?
              857279
              不好意思没有看清楚你的问题。我在11.1.2版本中都是没有问题的,在11.1.1.6里面你是否可以尝试用Custom Converter来转化这些值? 下面是以前搞过的一个例子,我稍微修改了下,还未测试,你先看看,可能是蛮麻烦的一件事情。

              #Convert Class

              package view;

              import java.util.Collection;
              import java.util.Collections;

              import javax.faces.component.UIComponent;
              import javax.faces.context.FacesContext;
              import javax.faces.convert.Converter;

              import oracle.adf.view.rich.model.NumberRange;

              import org.apache.myfaces.trinidad.convert.ClientConverter;


              public class StringToDoubleConverter implements Converter, ClientConverter {
              public StringToDoubleConverter() {
              super();
              }

              public Object getAsObject(FacesContext facesContext,
              UIComponent uiComponent, String string) {




              NumberRange d = null;

              if (string != null){

              int pos = string.indexOf(" ");

              String first = string.substring(0,pos);
              System.err.println(first);
              String second = string.substring(pos+1);
              System.err.println(second);

              d = new NumberRange(new Double(first),new Double(second));

              }
              return d;
              }

              public String getAsString(FacesContext facesContext,
              UIComponent uiComponent, Object object) {

              NumberRange dou = null;
              if (object instanceof NumberRange){

              dou = ((NumberRange)object);
              double a = dou.getMinimum().doubleValue();
              double b = dou.getMaximum().doubleValue();
              return a+" "+b;
              }
              else
              return object.toString();

              }

              public String getClientLibrarySource(FacesContext facesContext) {
              return facesContext.getExternalContext().getRequestContextPath() +
              "/jsLibs/converter.js";

              // return null;
              }

              public Collection<String> getClientImportNames() {
              return null;
              }

              public String getClientScript(FacesContext facesContext,
              UIComponent uiComponent) {
              return null;
              }

              public String getClientConversion(FacesContext facesContext,
              UIComponent uiComponent) {
              return ("new SimpleStringToDoubleConverter()");
              // return null;
              }
              }


              #Client Converter
              /** Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. */
              function SimpleStringToDoubleConverter()
              {
              // for debugging
              this._class = "SimpleStringToDoubleConverter";

              }

              SimpleStringToDoubleConverter.prototype = new TrConverter();

              SimpleStringToDoubleConverter.prototype.getFormatHint = function()
              {
                   return null;
              }

              SimpleStringToDoubleConverter.prototype.getAsString = function(
              number,
              label
              )
              {     
              alert('converted');
                   return ""+number;
              }

              SimpleStringToDoubleConverter.prototype.getAsObject = function(
              numberString,
              label
              )
              {
              alert('as object '+ numberString);      
              // TODO throw TrConverterException when
              // numberString is not correct...
                   return parseFloat(numberString);
              }
              • 4. Re: 如何使InputRangeSlide同时支持Double和Integer?
                812215
                谢谢建议,现在初始Slide为Integer,也就是80到180,然后使用Customer Converter后在页面显示成Double就可以了。