Source code: jpl2/link/gui/JavaDragAndDrop.java
1
2 /***********************************************************************
3 * JavaPsionLink 1.0, a java implementation of the psion link protocol
4 * Copyright (C) 2001-2003 John S Montgomery (john.montgomery@lineone.net)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ************************************************************************/
20 package jpl2.link.gui;
21
22 import jpl2.PsionLink;
23 import jpl2.common.*;
24
25 import java.awt.dnd.*;
26 import java.awt.datatransfer.*;
27 import java.io.File;
28 import java.util.List;
29 import java.lang.reflect.Field;
30
31 /** For people running 1.2+
32 **/
33
34 public class JavaDragAndDrop implements DropTargetListener, DragAndDrop {
35 private PsionLink link = null;
36 private PsionBrowserPane browserPane = null;
37 private DropTarget dropTarget = null;
38
39 public JavaDragAndDrop() throws Exception {
40 // see if we have got the javaFileListFlavor flavor available
41 // so we can accept incoming file drops
42 Class clazz = Class.forName( "java.awt.datatransfer.DataFlavor" );
43 Field field = clazz.getDeclaredField( "javaFileListFlavor" );
44 }
45
46 public void install( PsionLink link, PsionBrowserPane browserPane ) {
47 this.link = link;
48 this.browserPane = browserPane;
49 dropTarget = new DropTarget( browserPane, this );
50 }
51
52 public void dragEnter( DropTargetDragEvent dtde ) {
53 System.out.println( dtde );
54 if ( canAccept( dtde ) ) {
55 browserPane.setDragSelected( true );
56 //dtde.acceptDrop( dtde.getDropAction() );
57 }
58 }
59
60 public void dragExit( DropTargetEvent dte ) {
61 browserPane.setDragSelected( false );
62 }
63
64 public void dragOver( DropTargetDragEvent dtde ) {
65 if ( canAccept( dtde ) ) {
66 browserPane.setDragSelected( true );
67 //dtde.acceptDrop( dtde.getDropAction() );
68 }
69 }
70
71 public void dropActionChanged( DropTargetDragEvent dtde ) {
72 if ( canAccept( dtde ) ) {
73 browserPane.setDragSelected( true );
74 //dtde.acceptDrop( dtde.getDropAction() );
75 }
76 }
77
78 private void dumpTransferable( Transferable trans ) {
79 DataFlavor[] flavors = trans.getTransferDataFlavors();
80 System.out.println( flavors.length + " flavors" );
81 for ( int i = 0; i < flavors.length; i++ ) {
82 System.out.println( flavors[ i ] );
83 System.out.println( flavors[ i ].getMimeType() );
84 System.out.println( flavors[ i ].getHumanPresentableName() );
85 }
86 }
87
88 private File[] files = null;
89
90 public void drop( DropTargetDropEvent dtde ) {
91 //System.out.println( "Drag dropped" );
92 browserPane.setDragSelected( false );
93 dumpTransferable( dtde.getTransferable() );
94
95 if ( !canAccept( dtde ) ) {
96 dtde.rejectDrop();
97 return;
98 }
99 else
100 dtde.acceptDrop( dtde.getDropAction() );
101
102 //System.out.println( "drop accepted" );
103
104 files = getFiles( dtde );
105 if ( files != null ) {
106 WorkerThread worker = link.getWorkerThread();
107
108 worker.schedule( new Runnable() {
109 public void run() {
110 try {
111 link.upload( files );
112 }
113 catch( Exception e ) {
114 PsionLink.handleException( e, true );
115 }
116 }
117 });
118 }
119
120 dtde.dropComplete( true );
121 //System.out.println( "drop complete" );
122 }
123
124 private boolean canAccept( DropTargetDropEvent dtde ) {
125 return link.isConnected() && dtde.isDataFlavorSupported( DataFlavor.javaFileListFlavor );
126 }
127
128 private boolean canAccept( DropTargetDragEvent dtde ) {
129 return link.isConnected() && dtde.isDataFlavorSupported( DataFlavor.javaFileListFlavor );
130 }
131
132 private File[] getFiles( DropTargetDropEvent dtde ) {
133 if ( !link.isConnected() )
134 return null;
135
136 try {
137 Transferable trans = dtde.getTransferable();
138 System.out.println( trans );
139 List fileList = (List)trans.getTransferData( DataFlavor.javaFileListFlavor );
140 File[] fileArray = new File[ fileList.size() ];
141 fileArray = (File[])fileList.toArray( fileArray );
142 //System.out.println( fileArray.length );
143 return fileArray;
144 }
145 catch( Exception e ) {
146 }
147 return null;
148 }
149
150 }