Esempio n. 1
0
  /**
   * Creates an <code>ComponentUI</code> implementation for the specified component. In other words
   * create the look and feel specific delegate object for <code>target</code>. This is done in two
   * steps:
   *
   * <ul>
   *   <li>Look up the name of the <code>ComponentUI</code> implementation class under the value
   *       returned by <code>target.getUIClassID()</code>.
   *   <li>Use the implementation classes static <code>createUI()</code> method to construct a look
   *       and feel delegate.
   * </ul>
   *
   * @param target the <code>JComponent</code> which needs a UI
   * @return the <code>ComponentUI</code> object
   */
  public ComponentUI getUI(JComponent target) {

    Object cl = get("ClassLoader");
    ClassLoader uiClassLoader =
        (cl != null) ? (ClassLoader) cl : target.getClass().getClassLoader();
    Class<? extends ComponentUI> uiClass = getUIClass(target.getUIClassID(), uiClassLoader);
    Object uiObject = null;

    if (uiClass == null) {
      getUIError("no ComponentUI class for: " + target);
    } else {
      try {
        Method m = (Method) get(uiClass);
        if (m == null) {
          m = uiClass.getMethod("createUI", new Class[] {JComponent.class});
          put(uiClass, m);
        }
        uiObject = MethodUtil.invoke(m, null, new Object[] {target});
      } catch (NoSuchMethodException e) {
        getUIError("static createUI() method not found in " + uiClass);
      } catch (Exception e) {
        getUIError("createUI() failed for " + target + " " + e);
      }
    }

    return (ComponentUI) uiObject;
  }
  private static <T> T convertFromString(String s, OpenType<T> openType) {
    Class<T> c;
    try {
      ReflectUtil.checkPackageAccess(openType.safeGetClassName());
      c = cast(Class.forName(openType.safeGetClassName()));
    } catch (ClassNotFoundException e) {
      throw new NoClassDefFoundError(e.toString()); // can't happen
    }

    // Look for: public static T valueOf(String)
    Method valueOf;
    try {
      // It is safe to call this plain Class.getMethod because the class "c"
      // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
      valueOf = c.getMethod("valueOf", String.class);
      if (!Modifier.isStatic(valueOf.getModifiers()) || valueOf.getReturnType() != c)
        valueOf = null;
    } catch (NoSuchMethodException e) {
      valueOf = null;
    }
    if (valueOf != null) {
      try {
        return c.cast(MethodUtil.invoke(valueOf, null, new Object[] {s}));
      } catch (Exception e) {
        final String msg = "Could not convert \"" + s + "\" using method: " + valueOf;
        throw new IllegalArgumentException(msg, e);
      }
    }

    // Look for: public T(String)
    Constructor<T> con;
    try {
      // It is safe to call this plain Class.getConstructor because the class "c"
      // was checked before by ReflectUtil.checkPackageAccess(openType.safeGetClassName());
      con = c.getConstructor(String.class);
    } catch (NoSuchMethodException e) {
      con = null;
    }
    if (con != null) {
      try {
        return con.newInstance(s);
      } catch (Exception e) {
        final String msg = "Could not convert \"" + s + "\" using constructor: " + con;
        throw new IllegalArgumentException(msg, e);
      }
    }

    throw new IllegalArgumentException(
        "Don't know how to convert " + "string to " + openType.getTypeName());
  }