001: /**
002: * $RCSfile$
003: * $Revision: 1298 $
004: * $Date: 2005-04-24 14:22:45 -0300 (Sun, 24 Apr 2005) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.launcher;
011:
012: import javax.swing.JFrame;
013:
014: import java.awt.datatransfer.DataFlavor;
015: import java.awt.datatransfer.Transferable;
016: import java.awt.datatransfer.UnsupportedFlavorException;
017: import java.awt.dnd.*;
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: /**
024: * A droppable frame allows for DnD of file objects from the OS onto the actual
025: * frame via <code>File</code>.
026: */
027: public class DroppableFrame extends JFrame implements
028: DropTargetListener, DragSourceListener, DragGestureListener {
029:
030: private DragSource dragSource = DragSource.getDefaultDragSource();
031:
032: /**
033: * Creates a droppable rame.
034: */
035: public DroppableFrame() {
036: new DropTarget(this , this );
037: dragSource.createDefaultDragGestureRecognizer(this ,
038: DnDConstants.ACTION_COPY_OR_MOVE, this );
039: }
040:
041: public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
042: }
043:
044: public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
045: }
046:
047: public void dragExit(DragSourceEvent DragSourceEvent) {
048: }
049:
050: public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
051: }
052:
053: public void dropActionChanged(
054: DragSourceDragEvent DragSourceDragEvent) {
055: }
056:
057: public void dragEnter(DropTargetDragEvent dropTargetDragEvent) {
058: dropTargetDragEvent
059: .acceptDrag(DnDConstants.ACTION_COPY_OR_MOVE);
060: }
061:
062: public void dragExit(DropTargetEvent dropTargetEvent) {
063: }
064:
065: public void dragOver(DropTargetDragEvent dropTargetDragEvent) {
066: }
067:
068: public void dropActionChanged(
069: DropTargetDragEvent dropTargetDragEvent) {
070: }
071:
072: public void drop(DropTargetDropEvent dropTargetDropEvent) {
073: try {
074: Transferable transferable = dropTargetDropEvent
075: .getTransferable();
076: if (transferable
077: .isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
078: dropTargetDropEvent
079: .acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
080: List fileList = (List) transferable
081: .getTransferData(DataFlavor.javaFileListFlavor);
082: Iterator iterator = fileList.iterator();
083: while (iterator.hasNext()) {
084: File file = (File) iterator.next();
085: if (file.isFile()) {
086: fileDropped(file);
087: }
088:
089: if (file.isDirectory()) {
090: directoryDropped(file);
091: }
092: }
093: dropTargetDropEvent.getDropTargetContext()
094: .dropComplete(true);
095: } else {
096: dropTargetDropEvent.rejectDrop();
097: }
098: } catch (IOException io) {
099: io.printStackTrace();
100: dropTargetDropEvent.rejectDrop();
101: } catch (UnsupportedFlavorException ufe) {
102: ufe.printStackTrace();
103: dropTargetDropEvent.rejectDrop();
104: }
105: }
106:
107: public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
108:
109: }
110:
111: /**
112: * Notified when a file has been dropped onto the frame.
113: *
114: * @param file the file that has been dropped.
115: */
116: public void fileDropped(File file) {
117:
118: }
119:
120: /**
121: * Notified when a directory has been dropped onto the frame.
122: *
123: * @param file the directory that has been dropped.
124: */
125: public void directoryDropped(File file) {
126:
127: }
128: }
|