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. 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.BufferedReader; 33import java.io.File; 34import java.io.IOException; 35import java.io.Reader; 36import java.net.URI; 37import java.net.URISyntaxException; 38import java.util.ArrayList; 39 40class TargetFileListFrame extends Frame implements DropTargetListener { 41 42 private List list = new List(URIListBetweenJVMsTest.VISIBLE_RAWS_IN_LIST); 43 private int expectationTransferredFilesNumber; 44 private DataFlavor dropFlavor; 45 46 TargetFileListFrame(Point location, int expectationTransferredFilesNumber) { 47 try { 48 dropFlavor = new DataFlavor("text/uri-list;class=java.io.Reader"); 49 } catch (Exception ex) { 50 } 51 this.expectationTransferredFilesNumber = expectationTransferredFilesNumber; 52 initGUI(location); 53 setDropTarget(new DropTarget(list, DnDConstants.ACTION_COPY, 54 this)); 55 } 56 57 private void initGUI(Point location) { 58 this.setLocation(location); 59 this.addWindowListener(new WindowAdapter() { 60 public void windowClosing(WindowEvent e) { 61 TargetFileListFrame.this.dispose(); 62 } 63 }); 64 this.add(new Panel().add(list)); 65 this.pack(); 66 this.setVisible(true); 67 } 68 69 public void dragEnter(DropTargetDragEvent dtde) { 70 if (dtde.getCurrentDataFlavorsAsList().contains(dropFlavor)) { 71 dtde.acceptDrag(DnDConstants.ACTION_COPY); 72 } 73 } 74 75 public void dragOver(DropTargetDragEvent dtde) { 76 if (dtde.getCurrentDataFlavorsAsList().contains(dropFlavor)) { 77 dtde.acceptDrag(DnDConstants.ACTION_COPY); 78 } 79 } 80 81 public void dropActionChanged(DropTargetDragEvent dtde) { 82 if (dtde.getCurrentDataFlavorsAsList().contains(dropFlavor)) { 83 dtde.acceptDrag(DnDConstants.ACTION_COPY); 84 } 85 } 86 87 public void dragExit(DropTargetEvent dte) {} 88 89 public void drop(DropTargetDropEvent dtde) { 90 list.removeAll(); 91 dtde.acceptDrop(DnDConstants.ACTION_COPY); 92 java.util.List<File> fileList = extractListOfFiles(dtde); 93 for (File file:fileList) { 94 list.add(file.getName()); 95 } 96 97 if (fileList.size() != expectationTransferredFilesNumber) 98 { 99 System.err.println("ERROR: Expected file number:" 100 + expectationTransferredFilesNumber 101 + "; Received file number: " 102 + fileList.size()); 103 TargetFileListFrame.this.dispose(); 104 System.exit(InterprocessMessages.WRONG_FILES_NUMBER_ON_TARGET); 105 } 106 107 TargetFileListFrame.this.dispose(); 108 109 } 110 111 private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) { 112 BufferedReader reader = null; 113 ArrayList<File> files = new ArrayList<File>(); 114 try { 115 reader = new BufferedReader((Reader)dtde.getTransferable(). 116 getTransferData(dropFlavor)); 117 String line; 118 while ((line = reader.readLine()) != null) { 119 files.add(new File(new URI(line))); 120 } 121 } catch (UnsupportedFlavorException e) { 122 e.printStackTrace(); 123 } catch (IOException e) { 124 e.printStackTrace(); 125 } catch (IllegalArgumentException e) { 126 e.printStackTrace(); 127 } catch (URISyntaxException e) { 128 e.printStackTrace(); 129 } finally { 130 if (reader != null) { 131 try { 132 reader.close(); 133 } catch (IOException ignored) { 134 } 135 } 136 } 137 return files; 138 } 139 140 Point getDropTargetPoint() { 141 return new Point((int)list.getLocationOnScreen().getX()+(list.getWidth()/2), 142 (int)list.getLocationOnScreen().getY()+(list.getHeight()/2)); 143 } 144} 145