jdk/test/java/awt/dnd/ImageDecoratedDnD/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
        this.bgColor = bgColor;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    43
        this.htColor = htColor;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    44
        setBackground(bgColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    45
        setDropTarget(new DropTarget(this, this));
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    46
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    47
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
    public void dragEnter(DropTargetDragEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    50
        System.out.println("[Target] dragEnter");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    51
        setBackground(htColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    52
        repaint();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    53
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    54
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    55
    public void dragOver(DropTargetDragEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    56
        System.out.println("[Target] dragOver");
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 dragExit(DropTargetEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    60
        System.out.println("[Target] dragExit");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    61
        setBackground(bgColor);
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
    public void dragScroll(DropTargetDragEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    66
        System.out.println("[Target] dragScroll");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    67
    }
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
    public void dropActionChanged(DropTargetDragEvent e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    70
        System.out.println("[Target] dropActionChanged");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    71
    }
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
    public void drop(DropTargetDropEvent dtde) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    74
        System.out.println("[Target] drop");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    75
        boolean success = false;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    76
        if ((dtde.getDropAction() & dragOperation) == 0) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    77
            dtde.rejectDrop();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    78
            Label label = new Label("[no links here :) ]");
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    79
            label.setBackground(Color.cyan);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    80
            add(label);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    81
        } else {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    82
            dtde.acceptDrop(dragOperation);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    83
            DataFlavor[] dfs = dtde.getCurrentDataFlavors();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    84
            if (dfs != null && dfs.length >= 1){
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    85
                Transferable transfer = dtde.getTransferable();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    86
                try {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    87
                    Button button = (Button)transfer.getTransferData(dfs[0]);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    88
                    if( button != null ){
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    89
                        add(button);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    90
                        success = true;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    91
                    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    92
                } catch (IOException ioe) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    93
                    System.out.println(ioe.getMessage());
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    94
                    return;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    95
                } catch (UnsupportedFlavorException ufe) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    96
                    System.out.println(ufe.getMessage());
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    97
                    return;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    98
                }  catch (Exception e) {
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
    99
                    System.out.println(e.getMessage());
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   100
                    return;
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   101
                }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   102
            }
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
        setBackground(bgColor);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   105
        dtde.dropComplete(success);
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   106
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   107
        invalidate();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   108
        validate();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   109
        repaint();
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   110
    }
85d01a4fe115 4874070: invoking DragSource's startDrag with an Image renders no image on drag
uta
parents:
diff changeset
   111
}