Exemplo n.º 1
0
  /**
   * Creates a parameter editor instance for the given parameter.
   *
   * @param parameter the parameter
   * @return the parameter editor, never null
   */
  public static ParamEditor createParamEditor(Parameter parameter) {
    Guardian.assertNotNull("parameter", parameter);
    ParamProperties paramProps = parameter.getProperties();
    Debug.assertNotNull(paramProps);

    ParamEditor editor = null;

    // Step 1: Try to create an editor from the 'editorClass' property
    //
    Class editorClass = paramProps.getEditorClass();
    if (editorClass != null) {
      Constructor editorConstructor = null;
      try {
        editorConstructor = editorClass.getConstructor(Parameter.class);
      } catch (NoSuchMethodException e) {
        Debug.trace(e);
      } catch (SecurityException e) {
        Debug.trace(e);
      }
      if (editorConstructor != null) {
        try {
          editor = (ParamEditor) editorConstructor.newInstance(parameter);
        } catch (InstantiationException e) {
          Debug.trace(e);
        } catch (IllegalAccessException e) {
          Debug.trace(e);
        } catch (IllegalArgumentException e) {
          Debug.trace(e);
        } catch (InvocationTargetException e) {
          Debug.trace(e);
        }
      }
    }
    if (editor != null) {
      return editor;
    }

    // Step 2: Create a default editor based on the parameter's type info
    //
    if (parameter.isTypeOf(Boolean.class)) {
      editor = new BooleanEditor(parameter);
    } else if (parameter.isTypeOf(Color.class)) {
      editor = new ColorEditor(parameter);
    } else if (parameter.isTypeOf(File.class)) {
      editor = new FileEditor(parameter);
    } else if (paramProps.getValueSet() != null && paramProps.getValueSet().length > 0) {
      if (parameter.isTypeOf(String[].class)) {
        editor = new ListEditor(parameter);
      } else {
        editor = new ComboBoxEditor(parameter);
      }
    }

    // The last choice works always: a text field!
    if (editor == null) {
      editor = new TextFieldEditor(parameter);
    }

    return editor;
  }