01: /*
02: * Beryl - A web platform based on XML, XSLT and Java
03: * This file is part of the Beryl XML GUI
04: *
05: * Copyright (C) 2004 Wenzel Jakob <wazlaf@tigris.org>
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11:
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-3107 USA
20: */
21:
22: package org.beryl.gui;
23:
24: import java.awt.datatransfer.DataFlavor;
25: import java.awt.datatransfer.Transferable;
26: import java.awt.dnd.DropTargetDragEvent;
27: import java.awt.dnd.DropTargetDropEvent;
28: import java.awt.dnd.DropTargetEvent;
29: import java.awt.dnd.DropTargetListener;
30:
31: /**
32: * Internally used class to facilitate the use of DnD
33: */
34: public class DropHandler implements DropTargetListener {
35: private Widget widget;
36: private String name;
37: private DataFlavor[] flavors = null;
38: private GUIEventListener listener = null;
39:
40: public DropHandler(Widget widget, GUIEventListener listener,
41: String name, DataFlavor[] flavors) {
42: this .widget = widget;
43: this .listener = listener;
44: this .flavors = flavors;
45: this .name = name;
46: }
47:
48: public void dragEnter(DropTargetDragEvent e) {
49: }
50:
51: public void dragExit(DropTargetEvent e) {
52: }
53:
54: public void dragOver(DropTargetDragEvent e) {
55: }
56:
57: public void drop(DropTargetDropEvent e) {
58: Transferable tr = e.getTransferable();
59: DataFlavor[] trFlavors = tr.getTransferDataFlavors();
60:
61: try {
62: for (int i = 0; i < trFlavors.length; i++) {
63: for (int o = 0; o < flavors.length; o++) {
64: if (trFlavors[i].match(flavors[o])) {
65: e.acceptDrop(e.getDropAction());
66: Object data = tr.getTransferData(trFlavors[i]);
67: GUIEvent event = new GUIEvent(widget, name, e,
68: data);
69: listener.eventOccured(event);
70: e.dropComplete(true);
71: return;
72: }
73: }
74: }
75: } catch (Exception ex) {
76: e.dropComplete(false);
77: throw new RuntimeException(ex);
78: }
79: e.rejectDrop();
80: }
81:
82: public void dropActionChanged(DropTargetDragEvent e) {
83: }
84: };
|