1 /*
   2  * Copyright (c) 2014, 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  * @bug 8029979
  27  * @summary Checks if acceptDrop() can be called several times
  28  * @library ../../regtesthelpers
  29  * @build Util
  30  * @compile AcceptDropMultipleTimes.java
  31  * @run main/othervm AcceptDropMultipleTimes
  32  * @author anthony.petrov@oracle.com
  33  */
  34 
  35 import test.java.awt.regtesthelpers.Util;
  36 
  37 import javax.swing.*;
  38 import java.awt.*;
  39 import java.awt.datatransfer.*;
  40 import java.awt.dnd.*;
  41 import java.awt.event.InputEvent;
  42 
  43 public class AcceptDropMultipleTimes {
  44 
  45     private static final int FRAME_SIZE = 100;
  46     private static final int FRAME_LOCATION = 100;
  47 
  48     private static volatile Frame f;
  49 
  50     private static void initAndShowUI() {
  51         f = new Frame("Test frame");
  52         f.setBounds(FRAME_LOCATION, FRAME_LOCATION, FRAME_SIZE, FRAME_SIZE);
  53 
  54         final DraggablePanel dragSource = new DraggablePanel();
  55         dragSource.setBackground(Color.yellow);
  56         DropTarget dt = new DropTarget(dragSource, new DropTargetAdapter() {
  57             @Override public void drop(DropTargetDropEvent dtde) {
  58                 // The first call always succeeds
  59                 dtde.acceptDrop(DnDConstants.ACTION_COPY);
  60 
  61                 // The second call should succeed if the fix works
  62                 dtde.acceptDrop(DnDConstants.ACTION_MOVE);
  63 
  64                 dtde.dropComplete(true);
  65             }
  66         });
  67         dragSource.setDropTarget(dt);
  68         f.add(dragSource);
  69 
  70         f.setVisible(true);
  71     }
  72 
  73     public static void main(String[] args) throws Throwable {
  74         try {
  75 
  76             SwingUtilities.invokeAndWait(() -> initAndShowUI());
  77 
  78             Robot r = new Robot();
  79             Util.waitForIdle(r);
  80             Util.drag(r,
  81                     new Point(FRAME_LOCATION + FRAME_SIZE / 3, FRAME_LOCATION + FRAME_SIZE / 3),
  82                     new Point(FRAME_LOCATION + FRAME_SIZE / 3 * 2, FRAME_LOCATION + FRAME_SIZE / 3 * 2),
  83                     InputEvent.BUTTON1_MASK);
  84             Util.waitForIdle(r);
  85         } finally {
  86             if (f != null) {
  87                 f.dispose();
  88             }
  89         }
  90     }
  91 
  92     private static class DraggablePanel extends Panel implements DragGestureListener {
  93 
  94         public DraggablePanel() {
  95             (new DragSource()).createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY, this);
  96         }
  97 
  98         @Override
  99         public void dragGestureRecognized(DragGestureEvent dge) {
 100             dge.startDrag(Cursor.getDefaultCursor(), new StringSelection("test"));
 101         }
 102     }
 103 }