Search in sources :

Example 1 with DragGestureRecognizer

use of java.awt.dnd.DragGestureRecognizer in project intellij-community by JetBrains.

the class SwingCleanuper method cleanup.

public static void cleanup() {
    // KeyboardFocusManager.newFocusOwner
    ReflectionUtil.resetStaticField(KeyboardFocusManager.class, "newFocusOwner");
    // Clear "realOppositeComponent", "realOppositeWindow"
    final KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    resetField(focusManager, Component.class, "realOppositeComponent");
    resetField(focusManager, Window.class, "realOppositeWindow");
    try {
        Object helperObject = ReflectionUtil.getStaticFieldValue(BasicPopupMenuUI.class, Object.class, "menuKeyboardHelper");
        if (helperObject != null) {
            resetField(helperObject, Component.class, "lastFocused");
        }
    } catch (Exception e) {
    // Ignore
    }
    try {
        DragGestureRecognizer recognizer = ReflectionUtil.getStaticFieldValue(TransferHandler.class, DragGestureRecognizer.class, "recognizer");
        if (recognizer != null) {
            // that is memory leak
            recognizer.setComponent(null);
        }
    } catch (Exception e) {
    // Ignore
    }
    try {
        fixJTextComponentMemoryLeak();
    } catch (Exception e) {
    // Ignore
    }
    //Remove focus leaks
    focusManager.setGlobalCurrentFocusCycleRoot(null);
    try {
        final Method m = ReflectionUtil.getDeclaredMethod(KeyboardFocusManager.class, "setGlobalFocusOwner", Component.class);
        m.invoke(focusManager, new Object[] { null });
    } catch (Exception e) {
    // Ignore
    }
    ReflectionUtil.resetStaticField(KeyboardFocusManager.class, "newFocusOwner");
    ReflectionUtil.resetStaticField(KeyboardFocusManager.class, "permanentFocusOwner");
    ReflectionUtil.resetStaticField(KeyboardFocusManager.class, "currentFocusCycleRoot");
    // not anymore in JDK 9
    assert Patches.USE_REFLECTION_TO_ACCESS_JDK8;
    LinkedList heavyweightRequests = ReflectionUtil.getField(KeyboardFocusManager.class, null, LinkedList.class, "heavyweightRequests");
    if (heavyweightRequests != null) {
        synchronized (heavyweightRequests) {
            heavyweightRequests.clear();
        }
    }
}
Also used : Method(java.lang.reflect.Method) DragGestureRecognizer(java.awt.dnd.DragGestureRecognizer) LinkedList(java.util.LinkedList)

Example 2 with DragGestureRecognizer

use of java.awt.dnd.DragGestureRecognizer in project jdk8u_jdk by JetBrains.

the class TestDragSourceAdapter method main.

public static void main(String[] args) throws Exception {
    DragSource ds = new DragSource();
    TestDragSourceAdapter dsa1 = new TestDragSourceAdapter(1);
    TestDragSourceAdapter dsa2 = new TestDragSourceAdapter(2);
    Component c = new Button();
    DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(c, DnDConstants.ACTION_COPY, e -> e.startDrag(null, null));
    MouseEvent me = new MouseEvent(c, MouseEvent.MOUSE_PRESSED, 0, InputEvent.CTRL_MASK, 100, 100, 0, false);
    DragGestureEvent dge = new DragGestureEvent(dgr, DnDConstants.ACTION_COPY, new Point(100, 100), Arrays.asList(me));
    DragSourceContext dsc = new DragSourceContext(Toolkit.getDefaultToolkit().createDragSourceContextPeer(dge), dge, new Cursor(Cursor.HAND_CURSOR), null, null, new StringSelection("TEXT"), null);
    ds.addDragSourceListener(dsa1);
    ds.addDragSourceListener(dsa2);
    ds.addDragSourceListener(dsa2);
    ds.addDragSourceMotionListener(dsa1);
    ds.addDragSourceMotionListener(dsa1);
    ds.addDragSourceMotionListener(dsa2);
    dsc.addDragSourceListener(dsa2);
    byte[] serialized;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        oos.writeObject(dsc);
        serialized = bos.toByteArray();
    }
    DragSourceContext dsc_copy;
    try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
        dsc_copy = (DragSourceContext) ois.readObject();
    }
    try {
        dsc_copy.addDragSourceListener(dsa1);
        throw new RuntimeException("Test failed. Listener addition succeeded");
    } catch (TooManyListenersException ignored) {
    }
    try {
        dsc_copy.addDragSourceListener(dsa2);
        throw new RuntimeException("Test failed. Listener addition succeeded");
    } catch (TooManyListenersException ignored) {
    }
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos)) {
        oos.writeObject(ds);
        serialized = bos.toByteArray();
    }
    DragSource ds_copy;
    try (ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
        ObjectInputStream ois = new ObjectInputStream(bis)) {
        ds_copy = (DragSource) ois.readObject();
    }
    DragSourceListener[] dsls = ds_copy.getDragSourceListeners();
    assertEquals(3, dsls.length, "DragSourceListeners number");
    assertEquals(1, Stream.of(dsls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
    assertEquals(2, Stream.of(dsls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
    DragSourceMotionListener[] dsmls = ds_copy.getDragSourceMotionListeners();
    assertEquals(3, dsmls.length, "DragSourceMotionListeners number");
    assertEquals(2, Stream.of(dsmls).filter(dsa1::equals).collect(Collectors.counting()).intValue());
    assertEquals(1, Stream.of(dsmls).filter(dsa2::equals).collect(Collectors.counting()).intValue());
}
Also used : MouseEvent(java.awt.event.MouseEvent) DragGestureEvent(java.awt.dnd.DragGestureEvent) DragSource(java.awt.dnd.DragSource) Point(java.awt.Point) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DragGestureRecognizer(java.awt.dnd.DragGestureRecognizer) DragSourceContext(java.awt.dnd.DragSourceContext) Cursor(java.awt.Cursor) ObjectOutputStream(java.io.ObjectOutputStream) DragSourceListener(java.awt.dnd.DragSourceListener) StringSelection(java.awt.datatransfer.StringSelection) TooManyListenersException(java.util.TooManyListenersException) Button(java.awt.Button) ByteArrayInputStream(java.io.ByteArrayInputStream) DragSourceMotionListener(java.awt.dnd.DragSourceMotionListener) Component(java.awt.Component) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

DragGestureRecognizer (java.awt.dnd.DragGestureRecognizer)2 Button (java.awt.Button)1 Component (java.awt.Component)1 Cursor (java.awt.Cursor)1 Point (java.awt.Point)1 StringSelection (java.awt.datatransfer.StringSelection)1 DragGestureEvent (java.awt.dnd.DragGestureEvent)1 DragSource (java.awt.dnd.DragSource)1 DragSourceContext (java.awt.dnd.DragSourceContext)1 DragSourceListener (java.awt.dnd.DragSourceListener)1 DragSourceMotionListener (java.awt.dnd.DragSourceMotionListener)1 MouseEvent (java.awt.event.MouseEvent)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Method (java.lang.reflect.Method)1 LinkedList (java.util.LinkedList)1 TooManyListenersException (java.util.TooManyListenersException)1