1/* 2 * Copyright (c) 2007, 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 24import test.java.awt.regtesthelpers.Util; 25 26import java.awt.*; 27import java.awt.datatransfer.DataFlavor; 28import java.awt.datatransfer.UnsupportedFlavorException; 29import java.awt.event.WindowAdapter; 30import java.awt.event.WindowEvent; 31import java.awt.dnd.*; 32import java.io.File; 33import java.io.IOException; 34 35class TargetFileListFrame extends Frame implements DropTargetListener { 36 37 private List list = new List(FileListBetweenJVMsTest.VISIBLE_RAWS_IN_LIST); 38 private int expectationTransferredFilesNumber; 39 40 TargetFileListFrame(Point location, int expectationTransferredFilesNumber) { 41 this.expectationTransferredFilesNumber = expectationTransferredFilesNumber; 42 initGUI(location); 43 setDropTarget(new DropTarget(list, DnDConstants.ACTION_COPY, 44 this)); 45 } 46 47 private void initGUI(Point location) { 48 this.setLocation(location); 49 this.addWindowListener(new WindowAdapter() { 50 public void windowClosing(WindowEvent e) { 51 TargetFileListFrame.this.dispose(); 52 } 53 }); 54 this.add(new Panel().add(list)); 55 this.pack(); 56 this.setVisible(true); 57 } 58 59 public void dragEnter(DropTargetDragEvent dtde) { 60 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { 61 dtde.acceptDrag(DnDConstants.ACTION_COPY); 62 } 63 } 64 65 public void dragOver(DropTargetDragEvent dtde) { 66 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { 67 dtde.acceptDrag(DnDConstants.ACTION_COPY); 68 } 69 } 70 71 public void dropActionChanged(DropTargetDragEvent dtde) { 72 if (dtde.getCurrentDataFlavorsAsList().contains(DataFlavor.javaFileListFlavor)) { 73 dtde.acceptDrag(DnDConstants.ACTION_COPY); 74 } 75 } 76 77 public void dragExit(DropTargetEvent dte) {} 78 79 public void drop(DropTargetDropEvent dtde) { 80 list.removeAll(); 81 dtde.acceptDrop(DnDConstants.ACTION_COPY); 82 java.util.List<File> fileList = extractListOfFiles(dtde); 83 for (File file:fileList) { 84 list.add(file.getName()); 85 } 86 87 if (fileList.size() != expectationTransferredFilesNumber) 88 { 89 System.err.println("ERROR: Expected file number:" 90 + expectationTransferredFilesNumber 91 + "; Received file number: " 92 + fileList.size()); 93 TargetFileListFrame.this.dispose(); 94 System.exit(InterprocessMessages.WRONG_FILES_NUMBER_ON_TARGET); 95 } 96 97 TargetFileListFrame.this.dispose(); 98 99 } 100 101 private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) { 102 java.util.List<File> fileList = null; 103 try { 104 fileList = (java.util.List<File>)dtde.getTransferable(). 105 getTransferData(DataFlavor.javaFileListFlavor); 106 } catch (UnsupportedFlavorException e) { 107 e.printStackTrace(); 108 } catch (IOException e) { 109 e.printStackTrace(); 110 } 111 return fileList; 112 } 113 114 Point getDropTargetPoint() { 115 return new Point((int)list.getLocationOnScreen().getX()+(list.getWidth()/2), 116 (int)list.getLocationOnScreen().getY()+(list.getHeight()/2)); 117 } 118} 119