jdk/test/java/awt/dnd/DnDFileGroupDescriptor/DnDTarget.java
author serb
Tue, 12 Nov 2013 20:24:25 +0400
changeset 21596 0e3a39f29dbc
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8027696: Incorrect copyright header in the tests Reviewed-by: alanb, malenkov, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4831
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
4831
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     4
 *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 5506
diff changeset
     7
 * published by the Free Software Foundation.
4831
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     8
 *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    13
 * accompanied this code).
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    14
 *
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4831
diff changeset
    21
 * questions.
4831
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    22
 */
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    23
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    24
/*
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    25
* Panel is a DropTarget
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    26
*
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    27
*/
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    28
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    29
import java.awt.*;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    30
import java.awt.datatransfer.*;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    31
import java.awt.dnd.*;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    32
import java.io.*;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    33
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    34
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    35
class DnDTarget extends Panel implements DropTargetListener {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    36
    //private int dragOperation = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    37
    Color bgColor;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    38
    Color htColor;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    39
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    40
    DnDTarget(Color bgColor, Color htColor) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    41
        super();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    42
        setLayout(new FlowLayout());
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    43
        this.bgColor = bgColor;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    44
        this.htColor = htColor;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    45
        setBackground(bgColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    46
        setDropTarget(new DropTarget(this, this));
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    47
        add(new Label("drop here"));
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    48
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    49
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    50
    boolean check(DropTargetDragEvent dtde)
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    51
    {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    52
        if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    53
            dtde.acceptDrag(DnDConstants.ACTION_COPY);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    54
            return true;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    55
        }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    56
        return false;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    57
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    58
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    59
    public void dragEnter(DropTargetDragEvent dtde) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    60
        if(check(dtde)){
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    61
            setBackground(htColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    62
            repaint();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    63
        }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    64
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    65
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    66
    public void dragOver(DropTargetDragEvent dtde) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    67
        check(dtde);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    68
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    69
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    70
    public void dropActionChanged(DropTargetDragEvent dtde) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    71
        check(dtde);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    72
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    73
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    74
    public void dragExit(DropTargetEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    75
        setBackground(bgColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    76
        repaint();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    77
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    78
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    79
    public void dragScroll(DropTargetDragEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    80
        System.out.println("[Target] dragScroll");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    81
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    82
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    83
    public void drop(DropTargetDropEvent dtde) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    84
        System.out.println("[Target] drop");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    85
        boolean success = false;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    86
        dtde.acceptDrop(DnDConstants.ACTION_COPY);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    87
        if( dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor) ){
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    88
            System.out.println("[Target] DROP OK!");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    89
            try {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    90
                Transferable transfer = dtde.getTransferable();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    91
                java.util.List<File> fl = (java.util.List<File>)transfer.getTransferData(DataFlavor.javaFileListFlavor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    92
                for(File f : fl){
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    93
                    add(new Button(f.getCanonicalPath()));
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    94
                    System.out.println("[Target] drop file:" + f.getCanonicalPath());
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    95
                }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    96
                validate();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    97
            } catch(Exception ex) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    98
                ex.printStackTrace();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    99
            }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   100
            setBackground(bgColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   101
            repaint();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   102
            success = true;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   103
        }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   104
        dtde.dropComplete(success);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   105
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   106
}