DroppableFrame.java 4.09 KB
Newer Older
1
/**
2
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
3
 *
4 5 6 7 8 9 10 11 12 13 14
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
15 16
 */

17
package org.jivesoftware.openfire.launcher;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

import javax.swing.JFrame;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.*;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * A droppable frame allows for DnD of file objects from the OS onto the actual
 * frame via <code>File</code>.
 */
public class DroppableFrame extends JFrame implements DropTargetListener, DragSourceListener,
        DragGestureListener
 {

    private DragSource dragSource = DragSource.getDefaultDragSource();

    /**
     * Creates a droppable rame.
     */
    public DroppableFrame() {
        new DropTarget(this, this);
        dragSource.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, this);
    }

48
    @Override
49 50 51
    public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
    }

52
    @Override
53 54 55
    public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
    }

56
    @Override
57 58 59
    public void dragExit(DragSourceEvent DragSourceEvent) {
    }

60
    @Override
61 62 63
    public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
    }

64
    @Override
65 66 67
    public void dropActionChanged(DragSourceDragEvent DragSourceDragEvent) {
    }

68
    @Override
69 70 71 72
    public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
        dropTargetDragEvent.acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
    }

73
    @Override
74 75 76
    public void dragExit(DropTargetEvent dropTargetEvent) {
    }

77
    @Override
78 79 80
    public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
    }

81
    @Override
82 83 84
    public void dropActionChanged(DropTargetDragEvent dropTargetDragEvent) {
    }

85
    @Override
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    public void drop(DropTargetDropEvent dropTargetDropEvent) {
        try {
            Transferable transferable = dropTargetDropEvent.getTransferable();
            if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
                dropTargetDropEvent.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
                List fileList = (List) transferable.getTransferData(DataFlavor.javaFileListFlavor);
                Iterator iterator = fileList.iterator();
                while (iterator.hasNext()) {
                    File file = (File) iterator.next();
                    if (file.isFile()) {
                        fileDropped(file);
                    }

                    if (file.isDirectory()) {
                        directoryDropped(file);
                    }
                }
                dropTargetDropEvent.getDropTargetContext().dropComplete(true);
            }
            else {
                dropTargetDropEvent.rejectDrop();
            }
        }
109
        catch (IOException | UnsupportedFlavorException io) {
110 111 112 113 114
            io.printStackTrace();
            dropTargetDropEvent.rejectDrop();
        }
    }

115
    @Override
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {

    }

    /**
     * Notified when a file has been dropped onto the frame.
     *
     * @param file the file that has been dropped.
     */
    public void fileDropped(File file){

    }

    /**
     * Notified when a directory has been dropped onto the frame.
     *
     * @param file the directory that has been dropped.
     */
    public void directoryDropped(File file){

    }
}