1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @key headful
  27  * @bug 8029979
  28  * @summary Checks if acceptDrop() can be called several times
  29  * @library ../../regtesthelpers
  30  * @build Util
  31  * @compile AcceptDropMultipleTimes.java
  32  * @run main/othervm AcceptDropMultipleTimes
  33  * @author anthony.petrov@oracle.com
  34  */
  35 
  36 import test.java.awt.regtesthelpers.Util;
  37 
  38 import javax.swing.*;
  39 import java.awt.*;
  40 import java.awt.datatransfer.*;
  41 import java.awt.dnd.*;
  42 import java.awt.event.InputEvent;
  43 
  44 public class AcceptDropMultipleTimes {
  45 
  46     private static final int FRAME_SIZE = 100;
  47     private static final int FRAME_LOCATION = 100;
  48 
  49     private static volatile Frame f;
  50 
  51     private static void initAndShowUI() {
  52         f = new Frame("Test frame");
  53         f.setBounds(FRAME_LOCATION, FRAME_LOCATION, FRAME_SIZE, FRAME_SIZE);
  54 
  55         final DraggablePanel dragSource = new DraggablePanel();
  56         dragSource.setBackground(Color.yellow);
  57         DropTarget dt = new DropTarget(dragSource, new DropTargetAdapter() {
  58             @Override public void drop(DropTargetDropEvent dtde) {
  59                 // The first call always succeeds
  60                 dtde.acceptDrop(DnDConstants.ACTION_COPY);
  61 
  62                 // The second call should succeed if the fix works
  63                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
  64 
  65                 dtde.dropComplete(true);
  66             }
  67         });
  68         dragSource.setDropTarget(dt);
  69         f.add(dragSource);
  70 
  71         f.setVisible(true);
  72     }
  73 
  74     public static void main(String[] args) throws Throwable {
  75         try {
  76 
  77             SwingUtilities.invokeAndWait(() -> initAndShowUI());
  78 
  79             Robot r = new Robot();
  80             Util.waitForIdle(r);
  81             Util.drag(r,
  82                     new Point(FRAME_LOCATION + FRAME_SIZE / 3, FRAME_LOCATION + FRAME_SIZE / 3),
  83                     new Point(FRAME_LOCATION + FRAME_SIZE / 3 * 2, FRAME_LOCATION + FRAME_SIZE / 3 * 2),
  84                     InputEvent.BUTTON1_MASK);
  85             Util.waitForIdle(r);
  86         } finally {
  87             if (f != null) {
  88                 f.dispose();
  89             }
  90         }
  91     }
  92 
  93     private static class DraggablePanel extends Panel implements DragGestureListener {
  94 
  95         public DraggablePanel() {
  96             (new DragSource()).createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
  97         }
  98 
  99         @Override
 100         public void dragGestureRecognized(DragGestureEvent dge) {
 101             dge.startDrag(Cursor.getDefaultCursor(), new StringSelection("test"));
 102         }
 103     }
 104 }