<commit_before>package com.intellij.openapi.wm.impl;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.IdeGlassPane;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class IdeGlassPaneImpl extends JPanel implements IdeGlassPane {

  private Set<EventListener> myMouseListeners = new LinkedHashSet<EventListener>();
  private JRootPane myRootPane;

  private Component myCurrentOverComponent;
  private Component myMousePressedComponent;
  private Container myMousePressedContainer;

  public IdeGlassPaneImpl(JRootPane rootPane) {
    myRootPane = rootPane;
    setOpaque(false);
    setVisible(false);
    addMouseListener(new MouseAdapter() {
    });
    addMouseMotionListener(new MouseMotionAdapter() {
    });
    addMouseWheelListener(new MouseWheelListener() {
      public void mouseWheelMoved(final MouseWheelEvent e) {
      }
    });
  }

  protected void processMouseEvent(final MouseEvent e) {
    process(e, false);
  }

  protected void processMouseMotionEvent(final MouseEvent e) {
    process(e, true);
  }

  protected void processMouseWheelEvent(final MouseWheelEvent e) {
    process(e, false);
  }

  public void removeNotify() {
    // clear references to prevent memory leaks
    myCurrentOverComponent = null;
    myMousePressedComponent = null;
    myMousePressedContainer = null;
    super.removeNotify();
  }

  private void process(final MouseEvent e, boolean motionEvent) {
    final boolean processingDragEnd = myMousePressedComponent != null && e.getID() == MouseEvent.MOUSE_RELEASED;
    if (processingDragEnd) {
      final Point releasePoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), myMousePressedComponent);
      redispatch(convertEvent(e, myMousePressedComponent, releasePoint, MouseEvent.MOUSE_RELEASED), motionEvent, myMousePressedComponent);

      myMousePressedComponent = null;
      myMousePressedContainer = null;
      return;
    }

    final boolean processingDrag = myMousePressedComponent != null && e.getID() == MouseEvent.MOUSE_DRAGGED;
    final JLayeredPane lp = myRootPane.getLayeredPane();
    final Point lpPoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), lp);
    if (processForContainer(e, motionEvent, lp, lpPoint)) return;

    final Container cp = myRootPane.getContentPane();
    final Point cpPoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), cp);
    if (!processingDrag && cpPoint.y < 0) {
      final JMenuBar mb = myRootPane.getJMenuBar();
      if (mb != null) {
        if (processForContainer(e, motionEvent, mb, SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), mb))) return;
      }
    }

    processForContainer(e, motionEvent, cp, cpPoint);
  }

  private boolean processForContainer(final MouseEvent e, final boolean motionEvent, final Container container, final Point containerPoint) {
    final boolean dragEvent = e.getID() == MouseEvent.MOUSE_DRAGGED;
    Component target = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);

    boolean processed = target != null;

    if (dragEvent && myMousePressedComponent != null) {
      target = myMousePressedComponent;
    }

    if (myCurrentOverComponent != target && !dragEvent) {
      if (myCurrentOverComponent != null) {
        redispatch(convertEventType(e, MouseEvent.MOUSE_EXITED), false, myCurrentOverComponent);
        if (target != null) {
          redispatch(convertEventType(e, MouseEvent.MOUSE_ENTERED), false, target);
        }
        processed = true;
      }
    }


    if (target != null) {
      redispatch(e, motionEvent, target);
      setCursor(target.getCursor());
      if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        myMousePressedComponent = target;
        myMousePressedContainer = container;
      }
    }
    else {
      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    if (e.getID() == MouseEvent.MOUSE_RELEASED) {
      myMousePressedComponent = null;
      myMousePressedContainer = null;
    }

    myCurrentOverComponent = target;

    return processed;
  }

  private boolean redispatch(final MouseEvent originalEvent, final boolean isMotion, final Component target) {
    Container actualContainer = getParentOf(target);
    Component actualTarget = target;

    if (isEndComponent(actualTarget)) return false;

    MouseEvent targetEvent = SwingUtilities.convertMouseEvent(originalEvent.getComponent(), originalEvent, actualTarget);
    for (EventListener eachListener : myMouseListeners) {
      if (isMotion && eachListener instanceof MouseMotionListener) {
        fireMouseMotion((MouseMotionListener)eachListener, targetEvent);
      }
      else if (!isMotion && eachListener instanceof MouseListener) {
        fireMouseEvent((MouseListener)eachListener, targetEvent);
      }
    }

    if (targetEvent.isConsumed()) {
      return true;
    }


    Set<Component> processed = new HashSet<Component>();
    while (true) {
      if (actualTarget == null || actualContainer == null) break;
      targetEvent = SwingUtilities.convertMouseEvent(originalEvent.getComponent(), originalEvent, actualTarget);

      actualTarget.dispatchEvent(targetEvent);
      processed.add(actualTarget);

      if (targetEvent.isConsumed()) {
        return true;
      }

      boolean shouldProceed = isMotion ? actualTarget.getMouseMotionListeners().length == 0  : actualTarget.getMouseListeners().length == 0;
      if (shouldProceed) {
        Component sibling = null;
        Point containerPoint = SwingUtilities.convertPoint(originalEvent.getComponent(), originalEvent.getPoint(), actualContainer);
        for (int i = 0; i < actualContainer.getComponentCount(); i++) {
          final Component eachCandidate = actualContainer.getComponent(i);
          if (processed.contains(eachCandidate)) continue;
          if (eachCandidate.getBounds().contains(containerPoint)) {
            sibling = eachCandidate;
            break;
          }
        }
        if (sibling != null) {
          actualTarget = sibling;
        } else {
          actualTarget = getParentOf(actualTarget);
          actualContainer = getParentOf(actualTarget);
        }
      } else {
        break;
      }
      if (isEndComponent(actualTarget)) return false;
    }

    return false;
  }

  private static @Nullable Container getParentOf(final Component actualTarget) {
    return actualTarget != null ? actualTarget.getParent() : null;
  }

  private boolean isEndComponent(final Component actualTarget) {
    if (actualTarget == null) return true;
    if (actualTarget == myRootPane.getContentPane()) return true;
    if (actualTarget == myRootPane.getJMenuBar()) return true;
    if (actualTarget == myRootPane.getLayeredPane()) return true;
    return false;
  }

  private static MouseEvent convertEventType(MouseEvent template, int id) {
    return new MouseEvent(template.getComponent(), id, System.currentTimeMillis(), template.getModifiersEx(), template.getX(), template.getY(),
                          template.getClickCount(), template.isPopupTrigger(), template.getButton());
  }<commit_msg>glasspane - proper mouse event conversion<commit_after>package com.intellij.openapi.wm.impl;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.IdeGlassPane;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

public class IdeGlassPaneImpl extends JPanel implements IdeGlassPane {

  private Set<EventListener> myMouseListeners = new LinkedHashSet<EventListener>();
  private JRootPane myRootPane;

  private Component myCurrentOverComponent;
  private Component myMousePressedComponent;
  private Container myMousePressedContainer;

  public IdeGlassPaneImpl(JRootPane rootPane) {
    myRootPane = rootPane;
    setOpaque(false);
    setVisible(false);
    addMouseListener(new MouseAdapter() {
    });
    addMouseMotionListener(new MouseMotionAdapter() {
    });
    addMouseWheelListener(new MouseWheelListener() {
      public void mouseWheelMoved(final MouseWheelEvent e) {
      }
    });
  }

  protected void processMouseEvent(final MouseEvent e) {
    process(e, false);
  }

  protected void processMouseMotionEvent(final MouseEvent e) {
    process(e, true);
  }

  protected void processMouseWheelEvent(final MouseWheelEvent e) {
    process(e, false);
  }

  public void removeNotify() {
    // clear references to prevent memory leaks
    myCurrentOverComponent = null;
    myMousePressedComponent = null;
    myMousePressedContainer = null;
    super.removeNotify();
  }

  private void process(final MouseEvent e, boolean motionEvent) {
    final boolean processingDragEnd = myMousePressedComponent != null && e.getID() == MouseEvent.MOUSE_RELEASED;
    if (processingDragEnd) {
      final Point releasePoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), myMousePressedComponent);
      redispatch(convertEvent(e, myMousePressedComponent, releasePoint, MouseEvent.MOUSE_RELEASED), motionEvent, myMousePressedComponent);

      myMousePressedComponent = null;
      myMousePressedContainer = null;
      return;
    }

    final boolean processingDrag = myMousePressedComponent != null && e.getID() == MouseEvent.MOUSE_DRAGGED;
    final JLayeredPane lp = myRootPane.getLayeredPane();
    final Point lpPoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), lp);
    if (processForContainer(e, motionEvent, lp, lpPoint)) return;

    final Container cp = myRootPane.getContentPane();
    final Point cpPoint = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), cp);
    if (!processingDrag && cpPoint.y < 0) {
      final JMenuBar mb = myRootPane.getJMenuBar();
      if (mb != null) {
        if (processForContainer(e, motionEvent, mb, SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), mb))) return;
      }
    }

    processForContainer(e, motionEvent, cp, cpPoint);
  }

  private boolean processForContainer(final MouseEvent e, final boolean motionEvent, final Container container, final Point containerPoint) {
    final boolean dragEvent = e.getID() == MouseEvent.MOUSE_DRAGGED;
    Component target = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);

    boolean processed = target != null;

    if (dragEvent && myMousePressedComponent != null) {
      target = myMousePressedComponent;
    }

    if (myCurrentOverComponent != target && !dragEvent) {
      if (myCurrentOverComponent != null) {
        redispatch(convertEventType(e, MouseEvent.MOUSE_EXITED), false, myCurrentOverComponent);
        if (target != null) {
          redispatch(convertEventType(e, MouseEvent.MOUSE_ENTERED), false, target);
        }
        processed = true;
      }
    }


    if (target != null) {
      redispatch(e, motionEvent, target);
      setCursor(target.getCursor());
      if (e.getID() == MouseEvent.MOUSE_PRESSED) {
        myMousePressedComponent = target;
        myMousePressedContainer = container;
      }
    }
    else {
      setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }

    if (e.getID() == MouseEvent.MOUSE_RELEASED) {
      myMousePressedComponent = null;
      myMousePressedContainer = null;
    }

    myCurrentOverComponent = target;

    return processed;
  }

  private boolean redispatch(final MouseEvent originalEvent, final boolean isMotion, final Component target) {
    Container actualContainer = getParentOf(target);
    Component actualTarget = target;

    if (isEndComponent(actualTarget)) return false;

    MouseEvent targetEvent = convertEvent(originalEvent, actualTarget);
    for (EventListener eachListener : myMouseListeners) {
      if (isMotion && eachListener instanceof MouseMotionListener) {
        fireMouseMotion((MouseMotionListener)eachListener, targetEvent);
      }
      else if (!isMotion && eachListener instanceof MouseListener) {
        fireMouseEvent((MouseListener)eachListener, targetEvent);
      }
    }

    if (targetEvent.isConsumed()) {
      return true;
    }


    Set<Component> processed = new HashSet<Component>();
    while (true) {
      if (actualTarget == null || actualContainer == null) break;
      targetEvent = convertEvent(originalEvent, actualTarget);

      actualTarget.dispatchEvent(targetEvent);
      processed.add(actualTarget);

      if (targetEvent.isConsumed()) {
        return true;
      }

      boolean shouldProceed = isMotion ? actualTarget.getMouseMotionListeners().length == 0  : actualTarget.getMouseListeners().length == 0;
      if (shouldProceed) {
        Component sibling = null;
        Point containerPoint = SwingUtilities.convertPoint(originalEvent.getComponent(), originalEvent.getPoint(), actualContainer);
        for (int i = 0; i < actualContainer.getComponentCount(); i++) {
          final Component eachCandidate = actualContainer.getComponent(i);
          if (processed.contains(eachCandidate)) continue;
          if (eachCandidate.getBounds().contains(containerPoint)) {
            sibling = eachCandidate;
            break;
          }
        }
        if (sibling != null) {
          actualTarget = sibling;
        } else {
          actualTarget = getParentOf(actualTarget);
          actualContainer = getParentOf(actualTarget);
        }
      } else {
        break;
      }
      if (isEndComponent(actualTarget)) return false;
    }

    return false;
  }

  private MouseEvent convertEvent(final MouseEvent e, final Component target) {
    final Point point = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), target);
    return new MouseEvent(target, e.getID(), e.getWhen(), e.getModifiersEx(), point.x, point.y, e.getClickCount(), e.isPopupTrigger(), e.getButton());
  }

  private static @Nullable Container getParentOf(final Component actualTarget) {
    return actualTarget != null ? actualTarget.getParent() : null;
  }

  private boolean isEndComponent(final Component actualTarget) {
    if (actualTarget == null) return true;
    if (actualTarget == myRootPane.getContentPane()) return true;
    if (actualTarget == myRootPane.getJMenuBar()) return true;
    if (actualTarget == myRootPane.getLayeredPane()) return true;
    return false;
  }

  private static MouseEvent convertEventType(MouseEvent template, int id) {
    return new MouseEvent(template.getComponent(), id, System.currentTimeMillis(), template.getModifiersEx(), template.getX(), template.getY(),
                          template.getClickCount(), template.isPopupTrigger(), template.getButton());
  }
{'fraction_non_alphanumeric': 0.09995310511154284, 'fraction_numerical': 0.0005359415823675219, 'mean_word_length': 3.7435653002859866, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '254604', 'n_tokens_mistral': 4168, 'n_tokens_neox': 4115, 'n_words': 937}