1968N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1968N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1968N/A *
1968N/A * This code is free software; you can redistribute it and/or modify it
1968N/A * under the terms of the GNU General Public License version 2 only, as
1968N/A * published by the Free Software Foundation.
1968N/A *
1968N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1968N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1968N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1968N/A * version 2 for more details (a copy is included in the LICENSE file that
1968N/A * accompanied this code).
1968N/A *
1968N/A * You should have received a copy of the GNU General Public License version
1968N/A * 2 along with this work; if not, write to the Free Software Foundation,
1968N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1968N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1968N/A */
1968N/A
1968N/Aimport test.java.awt.regtesthelpers.Util;
1968N/A
1968N/Aimport java.awt.*;
1968N/Aimport java.awt.datatransfer.DataFlavor;
1968N/Aimport java.awt.datatransfer.UnsupportedFlavorException;
1968N/Aimport java.awt.event.WindowAdapter;
1968N/Aimport java.awt.event.WindowEvent;
1968N/Aimport java.awt.dnd.*;
1968N/Aimport java.io.BufferedReader;
1968N/Aimport java.io.File;
1968N/Aimport java.io.IOException;
1968N/Aimport java.io.Reader;
1968N/Aimport java.net.URI;
1968N/Aimport java.net.URISyntaxException;
1968N/Aimport java.util.ArrayList;
1968N/A
1968N/Aclass TargetFileListFrame extends Frame implements DropTargetListener {
1968N/A
1968N/A private List list = new List(URIListBetweenJVMsTest.VISIBLE_RAWS_IN_LIST);
1968N/A private int expectationTransferredFilesNumber;
1968N/A private DataFlavor dropFlavor;
1968N/A
1968N/A TargetFileListFrame(Point location, int expectationTransferredFilesNumber) {
1968N/A try {
1968N/A dropFlavor = new DataFlavor("text/uri-list;class=java.io.Reader");
1968N/A } catch (Exception ex) {
1968N/A }
1968N/A this.expectationTransferredFilesNumber = expectationTransferredFilesNumber;
1968N/A initGUI(location);
1968N/A setDropTarget(new DropTarget(list, DnDConstants.ACTION_COPY,
1968N/A this));
1968N/A }
1968N/A
1968N/A private void initGUI(Point location) {
1968N/A this.setLocation(location);
1968N/A this.addWindowListener(new WindowAdapter() {
1968N/A public void windowClosing(WindowEvent e) {
1968N/A TargetFileListFrame.this.dispose();
1968N/A }
1968N/A });
1968N/A this.add(new Panel().add(list));
1968N/A this.pack();
1968N/A this.setVisible(true);
1968N/A }
1968N/A
1968N/A public void dragEnter(DropTargetDragEvent dtde) {
1968N/A if (dtde.getCurrentDataFlavorsAsList().contains(dropFlavor)) {
1968N/A dtde.acceptDrag(DnDConstants.ACTION_COPY);
1968N/A }
1968N/A }
1968N/A
1968N/A public void dragOver(DropTargetDragEvent dtde) {
1968N/A if (dtde.getCurrentDataFlavorsAsList().contains(dropFlavor)) {
1968N/A dtde.acceptDrag(DnDConstants.ACTION_COPY);
1968N/A }
1968N/A }
1968N/A
1968N/A public void dropActionChanged(DropTargetDragEvent dtde) {
1968N/A if (dtde.getCurrentDataFlavorsAsList().contains(dropFlavor)) {
1968N/A dtde.acceptDrag(DnDConstants.ACTION_COPY);
1968N/A }
1968N/A }
1968N/A
1968N/A public void dragExit(DropTargetEvent dte) {}
1968N/A
1968N/A public void drop(DropTargetDropEvent dtde) {
1968N/A list.removeAll();
1968N/A dtde.acceptDrop(DnDConstants.ACTION_COPY);
1968N/A java.util.List<File> fileList = extractListOfFiles(dtde);
1968N/A for (File file:fileList) {
1968N/A list.add(file.getName());
1968N/A }
1968N/A
1968N/A if (fileList.size() != expectationTransferredFilesNumber)
1968N/A {
1968N/A System.err.println("ERROR: Expected file number:"
1968N/A + expectationTransferredFilesNumber
1968N/A + "; Received file number: "
1968N/A + fileList.size());
1968N/A TargetFileListFrame.this.dispose();
1968N/A System.exit(InterprocessMessages.WRONG_FILES_NUMBER_ON_TARGET);
1968N/A }
1968N/A
1968N/A TargetFileListFrame.this.dispose();
1968N/A
1968N/A }
1968N/A
1968N/A private java.util.List<File> extractListOfFiles(DropTargetDropEvent dtde) {
1968N/A BufferedReader reader = null;
1968N/A ArrayList<File> files = new ArrayList<File>();
1968N/A try {
1968N/A reader = new BufferedReader((Reader)dtde.getTransferable().
1968N/A getTransferData(dropFlavor));
1968N/A String line;
1968N/A while ((line = reader.readLine()) != null) {
1968N/A files.add(new File(new URI(line)));
1968N/A }
1968N/A } catch (UnsupportedFlavorException e) {
1968N/A e.printStackTrace();
1968N/A } catch (IOException e) {
1968N/A e.printStackTrace();
1968N/A } catch (IllegalArgumentException e) {
1968N/A e.printStackTrace();
1968N/A } catch (URISyntaxException e) {
1968N/A e.printStackTrace();
1968N/A } finally {
1968N/A if (reader != null) {
1968N/A try {
1968N/A reader.close();
1968N/A } catch (IOException ignored) {
1968N/A }
1968N/A }
1968N/A }
1968N/A return files;
1968N/A }
1968N/A
1968N/A Point getDropTargetPoint() {
1968N/A return new Point((int)list.getLocationOnScreen().getX()+(list.getWidth()/2),
1968N/A (int)list.getLocationOnScreen().getY()+(list.getHeight()/2));
1968N/A }
1968N/A}