1 /*
   2  * Copyright (c) 2009, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27 * Panel is a DropTarget
  28 *
  29 */
  30 
  31 import java.awt.*;
  32 import java.awt.datatransfer.*;
  33 import java.awt.dnd.*;
  34 import java.io.*;
  35 
  36 
  37 class DnDTarget extends Panel implements DropTargetListener {
  38     private int dragOperation = DnDConstants.ACTION_COPY | DnDConstants.ACTION_MOVE;
  39     Color bgColor;
  40     Color htColor;
  41 
  42     DnDTarget(Color bgColor, Color htColor) {
  43         super();
  44         this.bgColor = bgColor;
  45         this.htColor = htColor;
  46         setBackground(bgColor);
  47         setDropTarget(new DropTarget(this, this));
  48     }
  49 
  50 
  51     public void dragEnter(DropTargetDragEvent e) {
  52         System.out.println("[Target] dragEnter");
  53         setBackground(htColor);
  54         repaint();
  55     }
  56 
  57     public void dragOver(DropTargetDragEvent e) {
  58         System.out.println("[Target] dragOver");
  59     }
  60 
  61     public void dragExit(DropTargetEvent e) {
  62         System.out.println("[Target] dragExit");
  63         setBackground(bgColor);
  64         repaint();
  65     }
  66 
  67     public void dragScroll(DropTargetDragEvent e) {
  68         System.out.println("[Target] dragScroll");
  69     }
  70 
  71     public void dropActionChanged(DropTargetDragEvent e) {
  72         System.out.println("[Target] dropActionChanged");
  73     }
  74 
  75     public void drop(DropTargetDropEvent dtde) {
  76         System.out.println("[Target] drop");
  77         boolean success = false;
  78         if ((dtde.getDropAction() & dragOperation) == 0) {
  79             dtde.rejectDrop();
  80             Label label = new Label("[no links here :) ]");
  81             label.setBackground(Color.cyan);
  82             add(label);
  83         } else {
  84             dtde.acceptDrop(dragOperation);
  85             DataFlavor[] dfs = dtde.getCurrentDataFlavors();
  86             if (dfs != null && dfs.length >= 1){
  87                 Transferable transfer = dtde.getTransferable();
  88                 try {
  89                     Button button = (Button)transfer.getTransferData(dfs[0]);
  90                     if( button != null ){
  91                         add(button);
  92                         success = true;
  93                     }
  94                 } catch (IOException ioe) {
  95                     System.out.println(ioe.getMessage());
  96                     return;
  97                 } catch (UnsupportedFlavorException ufe) {
  98                     System.out.println(ufe.getMessage());
  99                     return;
 100                 }  catch (Exception e) {
 101                     System.out.println(e.getMessage());
 102                     return;
 103                 }
 104             }
 105         }
 106         setBackground(bgColor);
 107         dtde.dropComplete(success);
 108 
 109         invalidate();
 110         validate();
 111         repaint();
 112     }
 113 }