<commit_before>/*
 * Copyright 2000-2012 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.
 */
package com.intellij.application.options.codeStyle.arrangement;

import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.psi.codeStyle.arrangement.model.ArrangementAtomMatchCondition;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Consumer;
import com.intellij.util.ui.GridBag;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * {@link ArrangementNodeComponent} for {@link ArrangementAtomMatchCondition} representation.
 * <p/>
 * Not thread-safe.
 * 
 * @author <NAME>
 * @since 8/8/12 10:06 AM
 */
public class ArrangementAtomNodeComponent implements ArrangementNodeComponent {

  public static final int VERTICAL_PADDING = 2;
  public static final int HORIZONTAL_PADDING = 8;

  @NotNull private final JPanel myRenderer = new JPanel(new GridBagLayout()) {
    @Override
    public void paint(Graphics g) {
      Point point = ArrangementConfigUtil.getLocationOnScreen(this);
      if (point != null) {
        Rectangle bounds = myRenderer.getBounds();
        myScreenBounds = new Rectangle(point.x, point.y, bounds.width, bounds.height);
      }
      if (!myEnabled && g instanceof Graphics2D) {
        ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
      }
      super.paint(g);
    }
  };

  @NotNull private final JLabel myLabel = new JLabel() {
    @Override
    public Dimension getMinimumSize() {
      return getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
      return getPreferredSize();
    }

    @Override
    public Dimension getPreferredSize() {
      return myLabelSize == null ? super.getPreferredSize() : myLabelSize;
    }
  };

  @NotNull private final  ArrangementAtomMatchCondition           myCondition;
  @Nullable private final ActionButton                            myCloseButton;
  @Nullable private final Consumer<ArrangementAtomMatchCondition> myCloseCallback;

  @Nullable private Dimension myLabelSize;
  @Nullable private Rectangle myScreenBounds;

  private boolean myEnabled = true;
  private boolean mySelected;
  private boolean myInverted;
  private boolean myCloseButtonHovered;

  public ArrangementAtomNodeComponent(@NotNull ArrangementNodeDisplayManager manager,
                                      @NotNull ArrangementAtomMatchCondition condition,
                                      @Nullable Consumer<ArrangementAtomMatchCondition> closeCallback)
  {
    myCondition = condition;
    myCloseCallback = closeCallback;
    myLabel.setHorizontalAlignment(SwingConstants.CENTER);
    myLabel.setText(manager.getDisplayValue(condition));
    myLabelSize = new Dimension(manager.getMaxWidth(condition.getType()), myLabel.getPreferredSize().height);
    
    final ArrangementRemoveConditionAction action;
    if (closeCallback == null) {
      myCloseButton = null;
      action = null;
    }
    else {
      action = new ArrangementRemoveConditionAction();

      Icon buttonIcon = action.getTemplatePresentation().getIcon();
      Dimension buttonSize = new Dimension(buttonIcon.getIconWidth(), buttonIcon.getIconHeight());
      myCloseButton = new ActionButton(action, action.getTemplatePresentation().clone(), ArrangementConstants.RULE_TREE_PLACE, buttonSize) {
        @Override
        protected Icon getIcon() {
          return myCloseButtonHovered ? action.getTemplatePresentation().getHoveredIcon() : action.getTemplatePresentation().getIcon();
        }
      };
    }
    
    GridBagConstraints constraints = new GridBag().anchor(GridBagConstraints.CENTER).insets(0, 0, 0, 0);

    JPanel labelPanel = new JPanel(new GridBagLayout());
    labelPanel.add(myLabel, constraints);
    labelPanel.setBorder(IdeBorderFactory.createEmptyBorder(VERTICAL_PADDING, HORIZONTAL_PADDING, VERTICAL_PADDING, HORIZONTAL_PADDING));
    labelPanel.setOpaque(false);

    final int arcSize = myLabel.getFont().getSize();
    JPanel roundBorderPanel = new JPanel(new GridBagLayout()) {
      @Override
      public void paint(Graphics g) {
        Rectangle buttonBounds = getCloseButtonScreenLocation();
        if (buttonBounds != null && action != null) {
          Point mouseScreenLocation = MouseInfo.getPointerInfo().getLocation();
          myCloseButtonHovered = buttonBounds.contains(mouseScreenLocation);
        }
        Color color = mySelected ? UIUtil.getTreeSelectionBackground() : UIUtil.getTabbedPaneBackground();
        Rectangle bounds = getBounds();
        g.setColor(color);
        g.fillRoundRect(0, 0, bounds.width, bounds.height, arcSize, arcSize);
        super.paint(g);
      }
    };
    roundBorderPanel.add(labelPanel, new GridBag().fillCellHorizontally());
    if (myCloseButton != null) {
      roundBorderPanel.add(myCloseButton, new GridBag().anchor(GridBagConstraints.CENTER).insets(VERTICAL_PADDING, 0, 0, 0));
    }
    roundBorderPanel.setBorder(IdeBorderFactory.createRoundedBorder(arcSize));
    roundBorderPanel.setOpaque(false);
    
    myRenderer.setBorder(IdeBorderFactory.createEmptyBorder(VERTICAL_PADDING));
    myRenderer.add(roundBorderPanel, constraints);
    myRenderer.setOpaque(false);
  }

  @NotNull<commit_msg>IDEA-19061 Integrate the Rearranger-plugin into core-IDEA<commit_after>/*
 * Copyright 2000-2012 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.
 */
package com.intellij.application.options.codeStyle.arrangement;

import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.psi.codeStyle.arrangement.model.ArrangementAtomMatchCondition;
import com.intellij.ui.IdeBorderFactory;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.util.Consumer;
import com.intellij.util.ui.GridBag;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * {@link ArrangementNodeComponent} for {@link ArrangementAtomMatchCondition} representation.
 * <p/>
 * Not thread-safe.
 * 
 * @author <NAME>
 * @since 8/8/12 10:06 AM
 */
public class ArrangementAtomNodeComponent implements ArrangementNodeComponent {

  public static final int VERTICAL_PADDING = 2;
  public static final int HORIZONTAL_PADDING = 8;

  @NotNull private final JPanel myRenderer = new JPanel(new GridBagLayout()) {
    @Override
    public void paint(Graphics g) {
      Point point = ArrangementConfigUtil.getLocationOnScreen(this);
      if (point != null) {
        Rectangle bounds = myRenderer.getBounds();
        myScreenBounds = new Rectangle(point.x, point.y, bounds.width, bounds.height);
      }
      if (!myEnabled && g instanceof Graphics2D) {
        ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
      }
      super.paint(g);
    }
  };

  @NotNull private final JLabel myLabel = new JLabel() {
    @Override
    public Dimension getMinimumSize() {
      return getPreferredSize();
    }

    @Override
    public Dimension getMaximumSize() {
      return getPreferredSize();
    }

    @Override
    public Dimension getPreferredSize() {
      return myLabelSize == null ? super.getPreferredSize() : myLabelSize;
    }
  };

  @NotNull private final  ArrangementAtomMatchCondition           myCondition;
  @Nullable private final ActionButton                            myCloseButton;
  @Nullable private final Consumer<ArrangementAtomMatchCondition> myCloseCallback;

  @Nullable private Dimension myLabelSize;
  @Nullable private Rectangle myScreenBounds;

  private boolean myEnabled = true;
  private boolean mySelected;
  private boolean myInverted;
  private boolean myCloseButtonHovered;

  public ArrangementAtomNodeComponent(@NotNull ArrangementNodeDisplayManager manager,
                                      @NotNull ArrangementAtomMatchCondition condition,
                                      @Nullable Consumer<ArrangementAtomMatchCondition> closeCallback)
  {
    myCondition = condition;
    myCloseCallback = closeCallback;
    myLabel.setHorizontalAlignment(SwingConstants.CENTER);
    myLabel.setText(manager.getDisplayValue(condition));
    myLabelSize = new Dimension(manager.getMaxWidth(condition.getType()), myLabel.getPreferredSize().height);
    
    final ArrangementRemoveConditionAction action;
    if (closeCallback == null) {
      myCloseButton = null;
      action = null;
    }
    else {
      action = new ArrangementRemoveConditionAction();

      Icon buttonIcon = action.getTemplatePresentation().getIcon();
      Dimension buttonSize = new Dimension(buttonIcon.getIconWidth(), buttonIcon.getIconHeight());
      myCloseButton = new ActionButton(action, action.getTemplatePresentation().clone(), ArrangementConstants.RULE_TREE_PLACE, buttonSize) {
        @Override
        protected Icon getIcon() {
          return myCloseButtonHovered ? action.getTemplatePresentation().getHoveredIcon() : action.getTemplatePresentation().getIcon();
        }
      };
    }
    
    GridBagConstraints constraints = new GridBag().anchor(GridBagConstraints.CENTER).insets(0, 0, 0, 0);

    JPanel labelPanel = new JPanel(new GridBagLayout());
    labelPanel.add(myLabel, constraints);
    labelPanel.setBorder(IdeBorderFactory.createEmptyBorder(VERTICAL_PADDING, HORIZONTAL_PADDING, VERTICAL_PADDING, HORIZONTAL_PADDING));
    labelPanel.setOpaque(false);

    final int arcSize = myLabel.getFont().getSize();
    JPanel roundBorderPanel = new JPanel(new GridBagLayout()) {
      @Override
      public void paint(Graphics g) {
        Rectangle buttonBounds = getCloseButtonScreenLocation();
        if (buttonBounds != null && action != null) {
          Point mouseScreenLocation = MouseInfo.getPointerInfo().getLocation();
          myCloseButtonHovered = buttonBounds.contains(mouseScreenLocation);
        }
        Color color = mySelected ? UIUtil.getTreeSelectionBackground() : UIUtil.getPanelBackground();
        Rectangle bounds = getBounds();
        g.setColor(color);
        g.fillRoundRect(0, 0, bounds.width, bounds.height, arcSize, arcSize);
        super.paint(g);
      }
    };
    roundBorderPanel.add(labelPanel, new GridBag().fillCellHorizontally());
    if (myCloseButton != null) {
      roundBorderPanel.add(myCloseButton, new GridBag().anchor(GridBagConstraints.CENTER).insets(VERTICAL_PADDING, 0, 0, 0));
    }
    roundBorderPanel.setBorder(IdeBorderFactory.createRoundedBorder(arcSize));
    roundBorderPanel.setOpaque(false);
    
    myRenderer.setBorder(IdeBorderFactory.createEmptyBorder(VERTICAL_PADDING));
    myRenderer.add(roundBorderPanel, constraints);
    myRenderer.setOpaque(false);
  }

  @NotNull
{'fraction_non_alphanumeric': 0.09395691895063281, 'fraction_numerical': 0.006286145335680161, 'mean_word_length': 3.83664369679773, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '5018081', 'n_tokens_mistral': 3416, 'n_tokens_neox': 3306, 'n_words': 760}