示例#1
0
    public int compare(Object o1, Object o2) {
      PropertyDescriptor p1 = (PropertyDescriptor) o1;
      PropertyDescriptor p2 = (PropertyDescriptor) o2;

      Integer i1 = (Integer) p1.getValue(SORT_ORDER);
      Integer i2 = (Integer) p2.getValue(SORT_ORDER);

      if (i1 == null && i2 == null) {
        return p1.getName().compareTo(p2.getName());
      } else if (i1 != null) {
        return i1.compareTo(i2);
      } else {
        return i2.compareTo(i1) * -1;
      }
    }
示例#2
0
 @Override
 public Class getPropertyType(String name) {
   PropertyDescriptor compDescriptor = findDescriptor(name);
   if (compDescriptor != null) {
     // composite:attribute declaration...
     ValueExpression typeVE = (ValueExpression) compDescriptor.getValue("type");
     if (typeVE == null) {
       return Object.class;
     } else {
       String className =
           (String) typeVE.getValue(FacesContext.getCurrentInstance().getELContext());
       if (className != null) {
         className = prefix(className);
         try {
           return ReflectionUtil.forName(className);
         } catch (ClassNotFoundException cnfe) {
           throw new FacesException(cnfe);
         }
       } else {
         return Object.class;
       }
     }
   } else {
     // defer to the default processing which will inspect the
     // PropertyDescriptor of the UIComponent type
     return super.getPropertyType(name);
   }
 }
  public static void main(String[] args) throws Exception {

    BeanInfo i = Introspector.getBeanInfo(C.class, Object.class);
    Checker.checkEq("description", i.getBeanDescriptor().getShortDescription(), "CHILD");

    PropertyDescriptor[] pds = i.getPropertyDescriptors();
    Checker.checkEq("number of properties", pds.length, 1);
    PropertyDescriptor p = pds[0];

    Checker.checkEq("property description", p.getShortDescription(), "CHILDPROPERTY");
    Checker.checkEq("isBound", p.isBound(), childFlag);
    Checker.checkEq("isExpert", p.isExpert(), childFlag);
    Checker.checkEq("isHidden", p.isHidden(), childFlag);
    Checker.checkEq("isPreferred", p.isPreferred(), childFlag);
    Checker.checkEq("required", p.getValue("required"), childFlag);
    Checker.checkEq("visualUpdate", p.getValue("visualUpdate"), childFlag);

    Checker.checkEnumEq(
        "enumerationValues",
        p.getValue("enumerationValues"),
        new Object[] {"BOTTOM", 3, "javax.swing.SwingConstants.BOTTOM"});
  }
  /*
   * see java.beans.FeatureDescriptor#FeatureDescriptor(FeatureDescriptor)
   */
  public static void copyNonMethodProperties(PropertyDescriptor source, PropertyDescriptor target)
      throws IntrospectionException {

    target.setExpert(source.isExpert());
    target.setHidden(source.isHidden());
    target.setPreferred(source.isPreferred());
    target.setName(source.getName());
    target.setShortDescription(source.getShortDescription());
    target.setDisplayName(source.getDisplayName());

    // copy all attributes (emulating behavior of private FeatureDescriptor#addTable)
    Enumeration<String> keys = source.attributeNames();
    while (keys.hasMoreElements()) {
      String key = keys.nextElement();
      target.setValue(key, source.getValue(key));
    }

    // see java.beans.PropertyDescriptor#PropertyDescriptor(PropertyDescriptor)
    target.setPropertyEditorClass(source.getPropertyEditorClass());
    target.setBound(source.isBound());
    target.setConstrained(source.isConstrained());
  }
示例#5
0
  /**
   * @see ViewHandler#retargetMethodExpressions(javax.faces.context.FacesContext,
   *     javax.faces.component.UIComponent)
   */
  @Override
  public void retargetMethodExpressions(FacesContext context, UIComponent topLevelComponent) {
    BeanInfo componentBeanInfo =
        (BeanInfo) topLevelComponent.getAttributes().get(UIComponent.BEANINFO_KEY);
    // PENDING(edburns): log error message if componentBeanInfo is null;
    if (null == componentBeanInfo) {
      return;
    }
    PropertyDescriptor attributes[] = componentBeanInfo.getPropertyDescriptors();
    String targets = null, attrName = null, strValue = null, methodSignature = null;
    UIComponent target = null;
    ExpressionFactory expressionFactory = null;
    ValueExpression valueExpression = null;
    MethodExpression toApply = null;
    Class expectedReturnType = null;
    Class expectedParameters[] = null;

    for (PropertyDescriptor cur : attributes) {
      // If the current attribute represents a ValueExpression
      if (null != (valueExpression = (ValueExpression) cur.getValue("type"))) {
        // take no action on this attribute.
        continue;
      }
      // If the current attribute representes a MethodExpression
      if (null != (valueExpression = (ValueExpression) cur.getValue("method-signature"))) {
        methodSignature = (String) valueExpression.getValue(context.getELContext());
        if (null != methodSignature) {

          // This is the name of the attribute on the top level component,
          // and on the inner component.
          if (null != (valueExpression = (ValueExpression) cur.getValue("targets"))) {
            targets = (String) valueExpression.getValue(context.getELContext());
          }

          if (null == targets) {
            targets = cur.getName();
          }

          if (null == targets || 0 == targets.length()) {
            // PENDING error message in page?
            logger.severe("Unable to retarget MethodExpression: " + methodSignature);
            continue;
          }

          String[] targetIds = targets.split(" ");

          for (String curTarget : targetIds) {

            attrName = cur.getName();

            // Find the attribute on the top level component
            valueExpression = (ValueExpression) topLevelComponent.getAttributes().get(attrName);
            if (null == valueExpression) {
              // PENDING error message in page?
              logger.severe(
                  "Unable to find attribute with name \""
                      + attrName
                      + "\" in top level component in consuming page.  "
                      + "Page author error.");
              continue;
            }

            // lazily initialize this local variable
            if (null == expressionFactory) {
              expressionFactory = context.getApplication().getExpressionFactory();
            }

            // If the attribute is one of the pre-defined
            // MethodExpression attributes
            boolean isAction = false,
                isActionListener = false,
                isValidator = false,
                isValueChangeListener = false;
            if ((isAction = attrName.equals("action"))
                || (isActionListener = attrName.equals("actionListener"))
                || (isValidator = attrName.equals("validator"))
                || (isValueChangeListener = attrName.equals("valueChangeListener"))) {
              // This is the inner component to which the attribute should
              // be applied
              target = topLevelComponent.findComponent(curTarget);
              if (null == targets) {
                // PENDING error message in page?
                logger.severe(
                    "Unable to retarget MethodExpression.  "
                        + "Unable to find inner component with id "
                        + targets
                        + ".");
                continue;
              }

              if (isAction) {
                expectedReturnType = Object.class;
                expectedParameters = new Class[] {};
                toApply =
                    expressionFactory.createMethodExpression(
                        context.getELContext(),
                        valueExpression.getExpressionString(),
                        expectedReturnType,
                        expectedParameters);
                ((ActionSource2) target).setActionExpression(toApply);
              } else if (isActionListener) {
                expectedReturnType = Void.TYPE;
                expectedParameters = new Class[] {ActionEvent.class};
                toApply =
                    expressionFactory.createMethodExpression(
                        context.getELContext(),
                        valueExpression.getExpressionString(),
                        expectedReturnType,
                        expectedParameters);
                ((ActionSource2) target)
                    .addActionListener(new MethodExpressionActionListener(toApply));
              } else if (isValidator) {
                expectedReturnType = Void.TYPE;
                expectedParameters =
                    new Class[] {FacesContext.class, UIComponent.class, Object.class};
                toApply =
                    expressionFactory.createMethodExpression(
                        context.getELContext(),
                        valueExpression.getExpressionString(),
                        expectedReturnType,
                        expectedParameters);
                ((EditableValueHolder) target).addValidator(new MethodExpressionValidator(toApply));
              } else if (isValueChangeListener) {
                expectedReturnType = Void.TYPE;
                expectedParameters = new Class[] {ValueChangeEvent.class};
                toApply =
                    expressionFactory.createMethodExpression(
                        context.getELContext(),
                        valueExpression.getExpressionString(),
                        expectedReturnType,
                        expectedParameters);
                ((EditableValueHolder) target)
                    .addValueChangeListener(new MethodExpressionValueChangeListener(toApply));
              }
            } else {
              // There is no explicit methodExpression property on
              // an inner component to which this MethodExpression
              // should be retargeted.  In this case, replace the
              // ValueExpression with a method expresson.

              // Pull apart the methodSignature to derive the
              // expectedReturnType and expectedParameters

              // PENDING(rlubke,jimdriscoll) bulletproof this

              assert (null != methodSignature);
              methodSignature = methodSignature.trim();

              // Get expectedReturnType
              int j, i = methodSignature.indexOf(" ");
              if (-1 != i) {
                strValue = methodSignature.substring(0, i);
                try {
                  expectedReturnType = Util.getTypeFromString(strValue);
                } catch (ClassNotFoundException cnfe) {
                  logger.log(
                      Level.SEVERE,
                      "Unable to determine expected return type for " + methodSignature,
                      cnfe);
                  continue;
                }
              } else {
                logger.severe("Unable to determine expected return type for " + methodSignature);
                continue;
              }

              // derive the arguments
              i = methodSignature.indexOf("(");
              if (-1 != i) {
                j = methodSignature.indexOf(")", i + 1);
                if (-1 != j) {
                  strValue = methodSignature.substring(i + 1, j);
                  if (0 < strValue.length()) {
                    String[] params = strValue.split(",");
                    expectedParameters = new Class[params.length];
                    boolean exceptionThrown = false;
                    for (i = 0; i < params.length; i++) {
                      try {
                        expectedParameters[i] = Util.getTypeFromString(params[i]);
                      } catch (ClassNotFoundException cnfe) {
                        logger.log(
                            Level.SEVERE,
                            "Unable to determine expected return type for " + methodSignature,
                            cnfe);
                        exceptionThrown = true;
                        break;
                      }
                    }
                    if (exceptionThrown) {
                      continue;
                    }

                  } else {
                    expectedParameters = new Class[] {};
                  }
                }
              }

              assert (null != expectedReturnType);
              assert (null != expectedParameters);

              toApply =
                  expressionFactory.createMethodExpression(
                      context.getELContext(),
                      valueExpression.getExpressionString(),
                      expectedReturnType,
                      expectedParameters);
              topLevelComponent.getAttributes().put(attrName, toApply);
            }
          }
        }
      }
    }
  }