Example #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;
  }
    /**
     * Create a new ProjectionClass from the class
     *
     * @param pc projection class
     * @throws ClassNotFoundException couldn't find the class
     * @throws IntrospectionException problem with introspection
     */
    ProjectionClass(Class pc) throws ClassNotFoundException, IntrospectionException {
      projClass = pc;

      // eliminate common properties with "stop class" for getBeanInfo()
      Class stopClass;
      try {
        stopClass = Misc.findClass("ucar.unidata.geoloc.ProjectionImpl");
      } catch (Exception ee) {
        System.err.println("constructParamInput failed ");
        stopClass = null;
      }

      // analyze current projection class as a bean; may throw IntrospectionException
      BeanInfo info = java.beans.Introspector.getBeanInfo(projClass, stopClass);

      // find read/write methods
      PropertyDescriptor[] props = info.getPropertyDescriptors();
      if (debugBeans) {
        System.out.print("Bean Properties for class " + projClass);
        if ((props == null) || (props.length == 0)) {
          System.out.println("none");
          return;
        }
        System.out.println("");
      }
      for (int i = 0; i < props.length; i++) {
        PropertyDescriptor pd = props[i];
        Method reader = pd.getReadMethod();
        Method writer = pd.getWriteMethod();
        // only interesetd in read/write properties
        if ((reader == null) || (writer == null)) {
          continue;
        }
        // A hack to exclude some attributes
        if (pd.getName().equals("name") || pd.getName().equals("defaultMapArea")) {
          continue;
        }
        ProjectionParam p = new ProjectionParam(pd.getName(), reader, writer, pd.getPropertyType());
        paramList.add(p);

        if (debugBeans) {
          System.out.println("  -->" + p);
        }
      }

      // get an instance of this class so we can call toClassName()
      Projection project;
      if (null == (project = makeDefaultProjection())) {
        name = "none";
        return;
      }

      // invoke the toClassName method
      try {
        Method m = projClass.getMethod("getProjectionTypeLabel", VOIDCLASSARG);
        name = (String) m.invoke(project, VOIDOBJECTARG);
      } catch (NoSuchMethodException ee) {
        System.err.println(
            "ProjectionManager: class "
                + projClass
                + " does not have method getProjectionTypeLabel()");
        throw new ClassNotFoundException();
      } catch (SecurityException ee) {
        System.err.println(
            "ProjectionManager: class "
                + projClass
                + " got SecurityException on getProjectionTypeLabel()"
                + ee);
        throw new ClassNotFoundException();
      } catch (Exception ee) {
        System.err.println(
            "ProjectionManager: class "
                + projClass
                + " Exception when invoking getProjectionTypeLabel()"
                + ee);
        throw new ClassNotFoundException();
      }
    }