<commit_before>/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.ui;

import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.popup.AbstractPopup;

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

/**
 * User: anna
 * Date: 13-Mar-2006
 */
public class ResizeComponentListener extends MouseAdapter implements MouseMotionListener {
  private static final int SENSITIVITY = 4;
  private final AbstractPopup myPopup;
  private final AbstractPopup.MyContentPanel myComponent;
  private Point myStartPoint = null;
  private int myDirection = -1;

  public ResizeComponentListener(final AbstractPopup popup) {
    myPopup = popup;
    myComponent = (AbstractPopup.MyContentPanel)popup.getContent();
  }

  public void mousePressed(MouseEvent e) {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow != null) {
      myStartPoint = new RelativePoint(e).getScreenPoint();
      myDirection = getDirection(myStartPoint, popupWindow.getBounds());
      if (myDirection == Cursor.DEFAULT_CURSOR){
        myStartPoint = null;
      } else {
        if (isToShowBorder()) {
          myComponent.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black.brighter()));
        }
      }
    }
  }

  public void mouseClicked(MouseEvent e) {
    endOperation();
  }

  public void mouseReleased(MouseEvent e) {
    endOperation();
  }


  public void mouseExited(MouseEvent e) {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow == null) return;
    clearBorder(popupWindow);
  }

  private void endOperation() {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow != null) {
      if (isToShowBorder()) {
        myComponent.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
      }

      Dimension size = popupWindow.getSize();
      Dimension minSize = popupWindow.getMinimumSize();
      if (size.width < minSize.width) {
        size.width = minSize.width;
      }
      if (size.height < minSize.height) {
        size.height = minSize.height;
      }

      popupWindow.setSize(size);

      popupWindow.validate();
      popupWindow.repaint();
      setWindowCursor(popupWindow, Cursor.DEFAULT_CURSOR);
      myPopup.storeDimensionSize(popupWindow.getSize());
    }
    myStartPoint = null;
    myDirection = -1;
  }

  private boolean isToShowBorder() {
    return false;
  }

  private void doResize(final Point point) {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    final Rectangle bounds = popupWindow.getBounds();
    final Point location = popupWindow.getLocation();
    switch (myDirection){
      case Cursor.NW_RESIZE_CURSOR :
        popupWindow.setBounds(location.x + point.x - myStartPoint.x,
                              location.y + point.y - myStartPoint.y,
                              bounds.width + myStartPoint.x - point.x,
                              bounds.height + myStartPoint.y - point.y );
        break;
      case Cursor.N_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y + point.y - myStartPoint.y,
                              bounds.width,
                              bounds.height + myStartPoint.y - point.y);
        break;
      case Cursor.NE_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y + point.y - myStartPoint.y,
                              bounds.width + point.x - myStartPoint.x,
                              bounds.height + myStartPoint.y - point.y);
        break;
      case Cursor.E_RESIZE_CURSOR :
        popupWindow.setBounds(location.x ,
                              location.y,
                              bounds.width + point.x - myStartPoint.x,
                              bounds.height);
        break;
      case Cursor.SE_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y,
                              bounds.width + point.x - myStartPoint.x,
                              bounds.height + point.y - myStartPoint.y);
        break;
      case Cursor.S_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y,
                              bounds.width ,
                              bounds.height + point.y - myStartPoint.y);
        break;
      case Cursor.SW_RESIZE_CURSOR :
        popupWindow.setBounds(location.x + point.x - myStartPoint.x,
                              location.y,
                              bounds.width + myStartPoint.x - point.x,
                              bounds.height + point.y - myStartPoint.y);
        break;
      case Cursor.W_RESIZE_CURSOR :
        popupWindow.setBounds(location.x + point.x - myStartPoint.x,
                              location.y,
                              bounds.width + myStartPoint.x - point.x,
                              bounds.height);
        break;
    }

    popupWindow.validate();
  }

  public void mouseMoved(MouseEvent e) {
    Point point = new RelativePoint(e).getScreenPoint();
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow == null) return;
    final int cursor = getDirection(point, popupWindow.getBounds());
    if (cursor != Cursor.DEFAULT_CURSOR){
      if (isToShowBorder()) {
        if (myStartPoint == null) {
          myComponent.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black.brighter()));
        }
      }
      setWindowCursor(popupWindow, cursor);
      e.consume();
    } else {
      clearBorder(popupWindow);
    }
  }

  private void setWindowCursor(final Window popupWindow, final int cursor) {
    popupWindow.setCursor(myPopup.isToDrawMacCorner()? Cursor.getDefaultCursor() : Cursor.getPredefinedCursor(cursor));
  }

  private void clearBorder(final Window popupWindow) {
    if (isToShowBorder()){
      myComponent.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
      setWindowCursor(popupWindow, Cursor.DEFAULT_CURSOR);
    }
  }

  public void mouseDragged(MouseEvent e) {
    if (e.isConsumed()) return;
    final Point point = new RelativePoint(e).getScreenPoint();
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow == null) return;
    if (myStartPoint != null) {
      if (isToShowBorder()) {
        setWindowCursor(popupWindow, myDirection);
      }
      doResize(point);
      myStartPoint = point;
      e.consume();
    } else {
      if (isToShowBorder()) {
        final int cursor = getDirection(point, popupWindow.getBounds());
        setWindowCursor(popupWindow, cursor);
      }
    }
  }

  private int getDirection(Point startPoint, Rectangle bounds){
    if (myPopup.isToDrawMacCorner()){
      if (bounds.x + bounds.width - startPoint.x < 16 && //inside icon<commit_msg>Fixed the bug with handling cursor chnage for resizable popups.<commit_after>/*
 * Copyright (c) 2000-2006 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.ui;

import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.popup.AbstractPopup;

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

/**
 * User: anna
 * Date: 13-Mar-2006
 */
public class ResizeComponentListener extends MouseAdapter implements MouseMotionListener {
  private static final int SENSITIVITY = 4;
  private final AbstractPopup myPopup;
  private final AbstractPopup.MyContentPanel myComponent;
  private Point myStartPoint = null;
  private int myDirection = -1;

  public ResizeComponentListener(final AbstractPopup popup) {
    myPopup = popup;
    myComponent = (AbstractPopup.MyContentPanel)popup.getContent();
  }

  public void mousePressed(MouseEvent e) {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow != null) {
      myStartPoint = new RelativePoint(e).getScreenPoint();
      myDirection = getDirection(myStartPoint, popupWindow.getBounds());
      if (myDirection == Cursor.DEFAULT_CURSOR){
        myStartPoint = null;
      } else {
        if (isToShowBorder()) {
          myComponent.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black.brighter()));
        }
      }
    }
  }

  public void mouseClicked(MouseEvent e) {
    endOperation();
  }

  public void mouseReleased(MouseEvent e) {
    endOperation();
  }


  public void mouseExited(MouseEvent e) {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow == null) return;
    clearBorder(popupWindow);
  }

  private void endOperation() {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow != null) {
      if (isToShowBorder()) {
        myComponent.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
      }

      Dimension size = popupWindow.getSize();
      Dimension minSize = popupWindow.getMinimumSize();
      if (size.width < minSize.width) {
        size.width = minSize.width;
      }
      if (size.height < minSize.height) {
        size.height = minSize.height;
      }

      popupWindow.setSize(size);

      popupWindow.validate();
      popupWindow.repaint();
      setWindowCursor(popupWindow, Cursor.DEFAULT_CURSOR);
      myPopup.storeDimensionSize(popupWindow.getSize());
    }
    myStartPoint = null;
    myDirection = -1;
  }

  private boolean isToShowBorder() {
    return false;
  }

  private void doResize(final Point point) {
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    final Rectangle bounds = popupWindow.getBounds();
    final Point location = popupWindow.getLocation();
    switch (myDirection){
      case Cursor.NW_RESIZE_CURSOR :
        popupWindow.setBounds(location.x + point.x - myStartPoint.x,
                              location.y + point.y - myStartPoint.y,
                              bounds.width + myStartPoint.x - point.x,
                              bounds.height + myStartPoint.y - point.y );
        break;
      case Cursor.N_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y + point.y - myStartPoint.y,
                              bounds.width,
                              bounds.height + myStartPoint.y - point.y);
        break;
      case Cursor.NE_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y + point.y - myStartPoint.y,
                              bounds.width + point.x - myStartPoint.x,
                              bounds.height + myStartPoint.y - point.y);
        break;
      case Cursor.E_RESIZE_CURSOR :
        popupWindow.setBounds(location.x ,
                              location.y,
                              bounds.width + point.x - myStartPoint.x,
                              bounds.height);
        break;
      case Cursor.SE_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y,
                              bounds.width + point.x - myStartPoint.x,
                              bounds.height + point.y - myStartPoint.y);
        break;
      case Cursor.S_RESIZE_CURSOR :
        popupWindow.setBounds(location.x,
                              location.y,
                              bounds.width ,
                              bounds.height + point.y - myStartPoint.y);
        break;
      case Cursor.SW_RESIZE_CURSOR :
        popupWindow.setBounds(location.x + point.x - myStartPoint.x,
                              location.y,
                              bounds.width + myStartPoint.x - point.x,
                              bounds.height + point.y - myStartPoint.y);
        break;
      case Cursor.W_RESIZE_CURSOR :
        popupWindow.setBounds(location.x + point.x - myStartPoint.x,
                              location.y,
                              bounds.width + myStartPoint.x - point.x,
                              bounds.height);
        break;
    }

    popupWindow.validate();
  }

  public void mouseMoved(MouseEvent e) {
    Point point = new RelativePoint(e).getScreenPoint();
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow == null) return;
    final int cursor = getDirection(point, popupWindow.getBounds());
    if (cursor != Cursor.DEFAULT_CURSOR){
      if (isToShowBorder()) {
        if (myStartPoint == null) {
          myComponent.setBorder(BorderFactory.createMatteBorder(2, 2, 2, 2, Color.black.brighter()));
        }
      }
      setWindowCursor(popupWindow, cursor);
      e.consume();
    } else {
      clearBorder(popupWindow);
    }
  }

  private void setWindowCursor(final Window popupWindow, final int cursor) {
    popupWindow.setCursor(myPopup.isToDrawMacCorner()? Cursor.getDefaultCursor() : Cursor.getPredefinedCursor(cursor));
  }

  private void clearBorder(final Window popupWindow) {
    if (isToShowBorder()){
      myComponent.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    }
    setWindowCursor(popupWindow, Cursor.DEFAULT_CURSOR);
  }

  public void mouseDragged(MouseEvent e) {
    if (e.isConsumed()) return;
    final Point point = new RelativePoint(e).getScreenPoint();
    final Window popupWindow = SwingUtilities.windowForComponent(myComponent);
    if (popupWindow == null) return;
    if (myStartPoint != null) {
      if (isToShowBorder()) {
        setWindowCursor(popupWindow, myDirection);
      }
      doResize(point);
      myStartPoint = point;
      e.consume();
    } else {
      if (isToShowBorder()) {
        final int cursor = getDirection(point, popupWindow.getBounds());
        setWindowCursor(popupWindow, cursor);
      }
    }
  }

  private int getDirection(Point startPoint, Rectangle bounds){
    if (myPopup.isToDrawMacCorner()){
      if (bounds.x + bounds.width - startPoint.x < 16 && //inside icon
{'fraction_non_alphanumeric': 0.09895317583816664, 'fraction_numerical': 0.004951195360022634, 'mean_word_length': 2.212678936605317, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6078287', 'n_tokens_mistral': 4119, 'n_tokens_neox': 3999, 'n_words': 796}