<commit_before>/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package com.intellij.util.xml.ui;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiClass;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomUtil;
import com.intellij.util.xml.GenericValue;
import com.intellij.util.xml.impl.ui.*;
import com.intellij.util.xml.reflect.DomCollectionChildDescription;

import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * @author peter
 */
public class DomUIFactory {
  private static final Logger LOG;
  private static Method GET_VALUE_METHOD = null;
  private static Method SET_VALUE_METHOD = null;
  private static Method GET_STRING_METHOD = null;
  private static Method SET_STRING_METHOD = null;

  static {
    LOG = Logger.getInstance("#com.intellij.util.xml.ui.DomUIFactory");
    try {
      GET_VALUE_METHOD = GenericValue.class.getMethod("getValue");
      GET_STRING_METHOD = GenericValue.class.getMethod("getStringValue");
      SET_VALUE_METHOD = findMethod(GenericValue.class, "setValue");
      SET_STRING_METHOD = findMethod(GenericValue.class, "setStringValue");
    }
    catch (NoSuchMethodException e) {
      LOG.error(e);
    }
  }


  public static DomUIControl createControl(GenericValue element) {
    return createGenericValueControl(DomUtil.extractParameterClassFromGenericType(element.getDomElementType()), element);
  }

  private static BaseControl createGenericValueControl(final Type type, final GenericValue element) {
    if (type.equals(boolean.class) || type.equals(Boolean.class)) {
      return new BooleanControl(element, GET_VALUE_METHOD, SET_VALUE_METHOD);
    }
    else if (type.equals(String.class)) {
      return new StringControl(element, GET_VALUE_METHOD, SET_VALUE_METHOD);
    }
    else if (type.equals(PsiClass.class)) {
      return new PsiClassControl(element, GET_STRING_METHOD, SET_STRING_METHOD);
    }
    else if (type instanceof Class && Enum.class.isAssignableFrom((Class)type)) {
      return new EnumControl(element, (Class)type, GET_STRING_METHOD, SET_STRING_METHOD);
    }
    throw new IllegalArgumentException("Not supported: " + type);
  }

  private static Method findMethod(Class clazz, String methodName) {
    final Method[] methods = clazz.getMethods();
    for (Method method : methods) {
      if (methodName.equals(method.getName())) {
        return method;
      }
    }
    return null;
  }

  public static DomUIControl createCollectionControl(DomElement element, DomCollectionChildDescription description) {
    final Class aClass = DomUtil.extractParameterClassFromGenericType(description.getType());
    if (aClass != null) {
      final String presentableName = description.getCommonPresentableName(element);

      if (String.class.equals(aClass)) {
        return new DomCollectionControl<GenericValue<String>>(element, description, new StringColumnInfo(presentableName));
      }

      if (PsiClass.class.equals(aClass)) {
        return new DomCollectionControl<GenericValue<PsiClass>>(element, description, new PsiClassColumnInfo(presentableName, element));
      }

      assert false : "Type not supported: " + aClass;
    }

    return new CollectionControl(element, description);
  }

}<commit_msg>refactoring && boolean & enum controls<commit_after>/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package com.intellij.util.xml.ui;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.PsiClass;
import com.intellij.util.xml.DomElement;
import com.intellij.util.xml.DomUtil;
import com.intellij.util.xml.GenericValue;
import com.intellij.util.xml.impl.ui.*;
import com.intellij.util.xml.reflect.DomCollectionChildDescription;
import com.intellij.util.ui.ColumnInfo;

import javax.swing.table.TableCellEditor;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * @author peter
 */
public class DomUIFactory {
  private static final Logger LOG;
  private static Method GET_VALUE_METHOD = null;
  private static Method SET_VALUE_METHOD = null;
  private static Method GET_STRING_METHOD = null;
  private static Method SET_STRING_METHOD = null;

  static {
    LOG = Logger.getInstance("#com.intellij.util.xml.ui.DomUIFactory");
    try {
      GET_VALUE_METHOD = GenericValue.class.getMethod("getValue");
      GET_STRING_METHOD = GenericValue.class.getMethod("getStringValue");
      SET_VALUE_METHOD = findMethod(GenericValue.class, "setValue");
      SET_STRING_METHOD = findMethod(GenericValue.class, "setStringValue");
    }
    catch (NoSuchMethodException e) {
      LOG.error(e);
    }
  }


  public static DomUIControl createControl(GenericValue element) {
    return createGenericValueControl(DomUtil.extractParameterClassFromGenericType(element.getDomElementType()), element);
  }

  private static BaseControl createGenericValueControl(final Type type, final GenericValue element) {
    if (type.equals(boolean.class) || type.equals(Boolean.class)) {
      return new BooleanControl(element, GET_VALUE_METHOD, SET_VALUE_METHOD);
    }
    else if (type.equals(String.class)) {
      return new StringControl(element, GET_VALUE_METHOD, SET_VALUE_METHOD);
    }
    else if (type.equals(PsiClass.class)) {
      return new PsiClassControl(element, GET_STRING_METHOD, SET_STRING_METHOD);
    }
    else if (type instanceof Class && Enum.class.isAssignableFrom((Class)type)) {
      return new EnumControl(element, (Class)type, GET_STRING_METHOD, SET_STRING_METHOD);
    }
    throw new IllegalArgumentException("Not supported: " + type);
  }

  private static Method findMethod(Class clazz, String methodName) {
    final Method[] methods = clazz.getMethods();
    for (Method method : methods) {
      if (methodName.equals(method.getName())) {
        return method;
      }
    }
    return null;
  }

  private static TableCellEditor createCellEditor(DomElement element, Class type) {
    if (String.class.equals(type)) {
      return new DefaultCellEditor(removeBorder(new JTextField()));
    }

    if (PsiClass.class.equals(type)) {
      return new PsiClassTableCellEditor(element);
    }

    if (Enum.class.equals(type)) {
      return new DefaultCellEditor(removeBorder(EnumControl.createEnumComboBox(type)));
    }

    assert false : "Type not supported: " + type;
    return null;
  }

  private static <T extends JComponent> T removeBorder(final T component) {
    component.setBorder(new EmptyBorder(0, 0, 0, 0));
    return component;
  }

  public static DomUIControl createCollectionControl(DomElement element, DomCollectionChildDescription description) {
    final Class aClass = DomUtil.extractParameterClassFromGenericType(description.getType());
    if (aClass != null) {
      return new DomCollectionControl<GenericValue<?>>(element, description, createColumnInfo(description, element, aClass));
    }

    return new CollectionControl(element, description);
  }

  private static ColumnInfo createColumnInfo(final DomCollectionChildDescription description,
                                                         final DomElement element,
                                                         final Class aClass) {
    final String presentableName = description.getCommonPresentableName(element);
    if (Boolean.class.equals(aClass) || boolean.class.equals(aClass)) {
      return new BooleanColumnInfo(presentableName);
    }

    return new GenericValueColumnInfo(presentableName, aClass, createCellEditor(element, aClass));
  }

}
{'fraction_non_alphanumeric': 0.09941829719725014, 'fraction_numerical': 0.0015864621893178213, 'mean_word_length': 4.118403247631935, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1209781', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2095, 'n_words': 468}