<commit_before>/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * @author max
 */
package com.intellij.util.ui;

import com.intellij.ui.SeparatorComponent;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class FormBuilder {
  private static final boolean ALIGN_LABELS_TO_RIGHT = UIUtil.isUnderAquaLookAndFeel();

  private int line = 0;
  private int indent;
  private final JPanel panel;
  private boolean vertical;

  /**
   * @param vertical labels will be placed on their own rows
   */
  public FormBuilder(final boolean vertical, final int indent) {
    this.vertical = vertical;
    panel = new JPanel(new GridBagLayout());
    this.indent = indent;
  }

  public FormBuilder(final boolean vertical) {
    this(vertical, 5);
  }

  public FormBuilder() {
    this(false, 5);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component, final int verticalSpace) {
    return addLabeledComponent(labelText, component, verticalSpace, false);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component, final int verticalSpace, boolean labelOnTop) {
    JLabel label = null;
    if (labelText != null) {
      label = new JLabel(UIUtil.removeMnemonic(labelText));
      final int index = UIUtil.getDisplayMnemonicIndex(labelText);
      if (index != -1) {
        label.setDisplayedMnemonic(labelText.charAt(index+1));
      }
      label.setLabelFor(component);
    }

    return addLabelAndValueComponents(label, component, verticalSpace, false, labelOnTop);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component) {
    return addLabeledComponent(labelText, component, 10);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component, boolean labelOnTop) {
    return addLabeledComponent(labelText, component, 10, labelOnTop);
  }

  public FormBuilder addSeparator(final int verticalSpace) {
    return addLabelAndValueComponents(new SeparatorComponent(3, 0), new SeparatorComponent(3, 0), verticalSpace, true, false);
  }

  public FormBuilder addSeparator() {
    return addSeparator(10);
  }

  private FormBuilder addLabelAndValueComponents(@Nullable JComponent label,
                                                JComponent value,
                                                final int verticalSpace,
                                                boolean fillLabel,
                                                boolean labelOnTop) {
    GridBagConstraints c = new GridBagConstraints();
    int verticalInset = line > 0 ? verticalSpace : 0;

    if (vertical) {
      c.gridwidth = 1;

      c.gridx = 0;
      c.gridy = line;
      c.weightx = 1.0;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.WEST;
      c.insets = new Insets(verticalInset, 0, this.indent, 0);

      if (label != null) panel.add(label, c);

      c.gridx = 0;
      c.gridy = line + 1;
      c.weightx = 1.0;
      c.fill = value instanceof JComboBox ? GridBagConstraints.NONE : GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.WEST;
      c.insets = new Insets(0, 0, 0, this.indent);

      panel.add(value, c);

      line += 2;
    }
    else {
      c.gridx = 0;
      c.gridy = line;
      c.weightx = 0;
      
      if (ALIGN_LABELS_TO_RIGHT) {
        if (labelOnTop) {
          c.anchor = GridBagConstraints.NORTHEAST;
        } else {
          c.anchor = GridBagConstraints.EAST;
        }
      } else {
        if (labelOnTop) {
          c.anchor = GridBagConstraints.NORTHWEST;
        } else {
          c.anchor = GridBagConstraints.WEST;
        }
      }
      
      c.insets = new Insets(verticalInset, 0, 0, fillLabel ? 0 : this.indent);

      if (fillLabel) c.fill = GridBagConstraints.HORIZONTAL;

      if (label != null) panel.add(label, c);

      c.gridx = 1;
      c.gridy = line;
      c.fill = value instanceof JComboBox ? GridBagConstraints.NONE : GridBagConstraints.HORIZONTAL;
      c.anchor = GridBagConstraints.WEST;
      c.weightx = 1;
      c.insets = new Insets(verticalInset, 0, 0, 0);
      panel.add(value, c);

      line++;
    }

    return this;
  }

  public JPanel getPanel() {
    return panel;
  }

  public int getLine() {
    return line;
  }
}<commit_msg>AppCode: visual artifacts on resizing Create New File dialog (OC-2091) Platform: form builder correctly lays out 'big' components (lists, tables, etc) + correctly sets left and right indents. (cherry picked from commit 28e40d4)<commit_after>/*
 * Copyright 2000-2010 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * @author max
 */
package com.intellij.util.ui;

import com.intellij.ui.SeparatorComponent;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class FormBuilder {
  private static final boolean ALIGN_LABELS_TO_RIGHT = UIUtil.isUnderAquaLookAndFeel();

  private int line = 0;
  private int indent;
  private final JPanel panel;
  private boolean vertical;

  /**
   * @param vertical labels will be placed on their own rows
   */
  public FormBuilder(final boolean vertical, final int indent) {
    this.vertical = vertical;
    panel = new JPanel(new GridBagLayout());
    this.indent = indent;
  }

  public FormBuilder(final boolean vertical) {
    this(vertical, 5);
  }

  public FormBuilder() {
    this(false, 5);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component, final int verticalSpace) {
    return addLabeledComponent(labelText, component, verticalSpace, false);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component, final int verticalSpace, boolean labelOnTop) {
    JLabel label = null;
    if (labelText != null) {
      label = new JLabel(UIUtil.removeMnemonic(labelText));
      final int index = UIUtil.getDisplayMnemonicIndex(labelText);
      if (index != -1) {
        label.setDisplayedMnemonic(labelText.charAt(index+1));
      }
      label.setLabelFor(component);
    }

    return addLabelAndValueComponents(label, component, verticalSpace, false, labelOnTop);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component) {
    return addLabeledComponent(labelText, component, 10);
  }

  public FormBuilder addLabeledComponent(String labelText, JComponent component, boolean labelOnTop) {
    return addLabeledComponent(labelText, component, 10, labelOnTop);
  }

  public FormBuilder addSeparator(final int verticalSpace) {
    return addLabelAndValueComponents(new SeparatorComponent(3, 0), new SeparatorComponent(3, 0), verticalSpace, true, false);
  }

  public FormBuilder addSeparator() {
    return addSeparator(10);
  }

  private FormBuilder addLabelAndValueComponents(@Nullable JComponent label,
                                                JComponent value,
                                                final int verticalSpace,
                                                boolean fillLabel,
                                                boolean labelOnTop) {
    GridBagConstraints c = new GridBagConstraints();
    int verticalInset = line > 0 ? verticalSpace : 0;

    if (vertical) {
      c.gridwidth = 1;

      c.gridx = 0;
      c.gridy = line;
      c.weightx = 1.0;
      c.fill = GridBagConstraints.NONE;
      c.anchor = GridBagConstraints.WEST;
      c.insets = new Insets(verticalInset, this.indent, this.indent, 0);

      if (label != null) panel.add(label, c);

      c.gridx = 0;
      c.gridy = line + 1;
      c.weightx = 1.0;

      c.fill = getFill(value);
      c.weighty = getWeightY(value);

      c.anchor = GridBagConstraints.WEST;
      c.insets = new Insets(0, this.indent, 0, this.indent);

      panel.add(value, c);

      line += 2;
    }
    else {
      c.gridx = 0;
      c.gridy = line;
      c.weightx = 0;
      
      if (ALIGN_LABELS_TO_RIGHT) {
        if (labelOnTop) {
          c.anchor = GridBagConstraints.NORTHEAST;
        } else {
          c.anchor = GridBagConstraints.EAST;
        }
      } else {
        if (labelOnTop) {
          c.anchor = GridBagConstraints.NORTHWEST;
        } else {
          c.anchor = GridBagConstraints.WEST;
        }
      }
      
      c.insets = new Insets(verticalInset, this.indent, 0, fillLabel ? 0 : this.indent);

      if (fillLabel) c.fill = GridBagConstraints.HORIZONTAL;

      if (label != null) panel.add(label, c);

      c.gridx = 1;
      c.gridy = line;
      c.fill = getFill(value);
      c.weighty = getWeightY(value);
      c.anchor = GridBagConstraints.WEST;
      c.weightx = 1;
      c.insets = new Insets(verticalInset, this.indent, 0, this.indent);
      panel.add(value, c);

      line++;
    }

    return this;
  }

  private static int getFill(JComponent value) {
    if (value instanceof JComboBox) return GridBagConstraints.NONE;
    else if (value instanceof JScrollPane) return GridBagConstraints.BOTH;
    return GridBagConstraints.HORIZONTAL;
  }

  private static int getWeightY(JComponent value) {
    if (value instanceof JScrollPane) return 1;
    return 0;
  }

  public JPanel getPanel() {
    return panel;
  }

  public int getLine() {
    return line;
  }
}
{'fraction_non_alphanumeric': 0.09319066147859922, 'fraction_numerical': 0.010894941634241245, 'mean_word_length': 2.677038626609442, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5996873', 'n_tokens_mistral': 3094, 'n_tokens_neox': 2956, 'n_words': 850}