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