Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drag'n'drop of files that don't yet exist  (Read 2759 times)
0 Members and 1 Guest are viewing this topic.
Offline lilspikey

Senior Newbie




Computers Stole My Social Skills


« Posted 2003-04-10 17:20:02 »

Ok,
   so I have this program (http://sourceforge.net/projects/javapsionlink/) that for the purposes of this query can be considered equivalent to an FTP program.
  So far I have managed to get it so you can drag files into the window and get them uploaded.  Easy as the files already exist (in java.io.File terms).  
  My question is how to go about the drag from the window?  In this case the files (in the java.io.File sense) don't exist.  
  Has anyone worked out how to do this kind of thing?  The best lead I found was about "deferred" drag'n'drop, but that reckoned it wasn't possible in java.

  cheers,
      John



Offline leknor

Junior Duke




ROCK!!!


« Reply #1 - Posted 2003-04-10 21:12:55 »

I'm pulling this outta my butt and I may be way off base but it's an idea.

What if you used a URL or URI to represent files, I'm not gonna claim to understand the difference. They should be abstract enough to handle both locations and I think you can add new protocol handlers at runtime somehow.
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #2 - Posted 2003-04-11 13:31:10 »

If I understand correctly, a URL is global (find it anywhere, anytime) while a URI is contextual (i.e. /usr/local/myfile).

Java Game Console Project
Last Journal Entry: 12/17/04
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lilspikey

Senior Newbie




Computers Stole My Social Skills


« Reply #3 - Posted 2003-04-11 16:07:31 »

In principal I see what you mean.  However how would the OS know how to get at the files?  The url/uri would hve to be something like:

 psion://c/documents/myfile.txt

But seeing as the only program running that undersood that would be mine, how can it then get called back?

I know on the mac you have to have a "promised hfs" ostype, which basically says I promise to put a file where you ask me to, but there doesn't seem to be anything like that in teh dnd api.

Is this another missing feature, such as irregular windows, or has anyone got this to work?

Offline javatypo

Junior Duke





« Reply #4 - Posted 2003-04-11 19:14:43 »

interesting question.

but i dont think that many applications do that.

i know u can drag and drop files into photoshop.. but i dont think it goes the other way.

of course your using a list right? do u know if its possible in c++? or c?
Offline leknor

Junior Duke




ROCK!!!


« Reply #5 - Posted 2003-04-11 19:26:54 »

Quote
However how would the OS know how to get at the files?
I missunderstood what you were trying to do. I thought all drag-n-dropping was happening within the application.
Offline javatypo

Junior Duke





« Reply #6 - Posted 2003-04-12 14:13:09 »

if u can find out if its doable in C++, then it is probably dobale in java.. just not in the current api
Offline lilspikey

Senior Newbie




Computers Stole My Social Skills


« Reply #7 - Posted 2003-04-14 15:47:08 »

I know it is possible in C++, as I have dragged files out of ftp programs before.  I also know that you can use the file list flavor when dragging out, as long as the files already exist.  With that in mind it _might_ be possible to fake it:  somehow work out when the user has dragged and dropped the files, and download them to a temporary directory, _before_ the OS tries to move them.  
I guess some experimentation may be needed.  But I was just wondering if anyone had had any success before I attempted to re-invent he wheel.

 cheers,
          John

Offline lilspikey

Senior Newbie




Computers Stole My Social Skills


« Reply #8 - Posted 2003-05-23 18:07:49 »

Ooh ooh.  Whilst googling for the elusive drag'n'drop of files that don't yet exist answer, I have thought up a possible (slightly hacked) solution.

As said earlier we effectively need to provide a URL to the file to download and let the OS decide how/when to grab the file pointed to by the URL.  So if the app runs a webserver and the urls are specified for that webserver (e.g. http://127.0.0.1/c/documents/myfile.txt ) then it might actually work.  

I think I shall have to see if that can/will work and if there is another way that does not involve networking, as it's really just a hack to make a call-back from the OS.  I guess under linux/unix there would be named pipes and/or domain sockets.

Anyway, just wanted to get this written down, it might be another clue for someone else to figure out the right way (tm) of doing it.

Offline lilspikey

Senior Newbie




Computers Stole My Social Skills


« Reply #9 - Posted 2003-05-25 13:23:31 »

Ok, the URL thing looks like it might have been a red herring.  It seems to only allow you to place a shortcut/link/alias to that URL.

Instead I went down the platform specific route.  With the help of Appl technote TN1085 (Using the Drag Manager to Interact With and Manipulate File System Entities):

http://developer.apple.com/technotes/tn/tn1085.html

With that and that fact the java<->native code interaction on the mac is dead easy (no C glue code is needed) I was eventually able to figure out how to the get directory that a drop occurs in, therefore allowing me to copy the dragged files to the correct location.  I had to use a couple of un-documented apple classes tho.  Mainly to allow me to turn an FSSpec to a java.io.File.

This code is for MacOS classic, but it may be possible to convert it to MacOS X easily - I can't say for definate as I don't have MacOS X yet.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
import com.apple.mrj.datatransfer.*;
import com.apple.mrj.dnd.*;
import com.apple.mrj.*;
import com.apple.mrj.jdirect.*;
import com.apple.mrj.macos.toolbox.AEDesc;
import com.apple.mrj.macos.toolbox.FSSpec;
import com.apple.mrj.macos.libraries.*;
import java.awt.*;
import java.net.*;
import java.io.*;


public class DragTest extends Frame implements DragLib, InterfaceLib {
      private final OSTypeFlavor kDragPromisedFlavor   = new OSTypeFlavor( new MRJOSType( "fssP" ) );
      private final OSTypeFlavor flavorTypePromisedHFS = new OSTypeFlavor( new MRJOSType( "phfs" ) );
      private final MRJOSType typeAlias = new MRJOSType( "alis" );
      private final MRJOSType typeFSS   = new MRJOSType( "fss " );
     
      public DragTest() {
            addMouseListener( new DragInit() );
      }
     
      public native static short GetDropLocation( int theDrag, byte[] dropLocation );
     
      public native static short AECoerceDesc( byte[] theAEDesc, int toType, byte[] result );
     
      class PromiseHFSFlavor extends ByteArrayStruct {
           
            public PromiseHFSFlavor() {
                  // 4+4+2+4
                  super( 14 );
            }
           
            public void setFileType( MRJOSType type ) {
                  setIntAt( 0, type.toInt() );
            }
           
            public void setFileCreator( MRJOSType type ) {
                  setIntAt( 4, type.toInt() );
            }
           
            public void setFlags( int flags ) {
                  setShortAt( 8, (short)flags );
            }
           
            public void setPromisedFlavor( OSTypeFlavor flavor ) {
                  setIntAt( 0, flavor.getOSType().toInt() );
            }
      }
     
     
      class FSSpecHandle extends HandleStruct {
           
            public FSSpecHandle( int handle ) {
                  super( handle );
            }
           
            public short getVolumeRef() {
                  return getShortAt( 0 );
            }
           
            public int getParentID() {
                  return getIntAt( 2 );
            }
           
            public String getName() {
                  byte[] bytes = getBytesAt( 6, 64 );
                  int len = 0xFF & bytes[ 0 ];
                  if ( len >= 64 )
                        throw new RuntimeException( "error in FSSpec name length" );
                       
                  return new String( bytes, 1, len );
            }
     
            public int getSize() {
                  return 70;
            }
           
      }
     
     
     
      class DragInit extends DragInitiatorAdapter {
           
            public void dragGesture( DragInitiatorEvent e ) {
                  System.out.println( e );
                  OutgoingDrag drag = e.getDrag();
                  try {
                        /*URL url = new URL( "http://10.0.0.6/manual/index.html" );
                        String urld = url.toString() + "\nNew Features in Version 2.0";
                        Transfer trans = new Transfer();
                       
                        OSTypeFlavor flav = new OSTypeFlavor( new MRJOSType( "DNLD" ) );
                        trans.addFlavor( flav, new ByteArrayInputStream( urld.getBytes() ) );
                        drag.addItem( trans );*/

                       
                        Transfer trans = new Transfer();
                       
                        PromiseHFSFlavor phfs = new PromiseHFSFlavor();
                        // doesn't seem to matter what filetype/creator we
                        // specify
                        //phfs.setFileType(       new MRJOSType( "TEXT" ) );
                        //phfs.setFileCreator(    new MRJOSType( "JPL " ) );
                        phfs.setFlags(          0 );
                        phfs.setPromisedFlavor( kDragPromisedFlavor );
                       
                       
                        trans.addFlavor( flavorTypePromisedHFS, phfs.getBytes() );
                        trans.addFlavor( kDragPromisedFlavor, new byte[ 0 ] );
                        drag.addItem( trans );
                  }
                  catch( Exception ex ) {
                        ex.printStackTrace();
                  }
            }

            public void dragCompleted( DragInitiatorEvent e ) {
                  OutgoingDrag drag = e.getDrag();
                  if ( !drag.isDropAccepted() )
                        return;
                       
                  int dragRef = drag.getRef();
                  //System.out.println( "dragRef=" + dragRef );
                  //System.out.println( e );
                  AEDesc dropLocAlias = new AEDesc();
                  if ( GetDropLocation( dragRef, dropLocAlias.getByteArray() ) != 0 ) {
                        //System.err.println( "error getting drop location" );
                        return; // an error occurred
                  }
                  //System.out.println( "got drop location" );
                 
                  if ( dropLocAlias.getDescriptorType() != typeAlias.toInt() ) {
                        System.err.println( "error descriptor not alis: " + new MRJOSType( dropLocAlias.getDescriptorType() ) );
                        return; // an error occurred
                  }
                 
                  //System.out.println( dropLocAlias.getDescriptorType() );
                 
                  AEDesc dropLocFSS = new AEDesc();
                 
                  if ( AECoerceDesc( dropLocAlias.getByteArray(), typeFSS.toInt(), dropLocFSS.getByteArray() ) != 0 ) {
                        System.err.println( "error coercing to typeFSS" );
                        return; // an error occurred
                  }
                       
                 
                 
                  //System.out.println( dropLocFSS.getDescriptorType() );
                 
                  //AEDisposeDesc( dropLocAlias );
                 
                  int dataHandle = dropLocFSS.getDataHandle();
                 
                  FSSpecHandle fss = new FSSpecHandle( dataHandle );
                 
                  //String name = fss.getName();
                  //System.out.println( name );
                 
                  //int parentID = fss.getParentID();
                  //System.out.println( parentID );
                 
                  FSSpec fsspec = new FSSpec( fss );
                 
                  File file = fsspec.toFile();
                 
                  System.out.println( file );
                 
                  dropLocAlias.dispose();
                  dropLocFSS.dispose();
                 
                  if ( file.isDirectory() ) {
                        File index = new File( file, "index.html" );
                        if ( !index.exists() ) {
                              try {
                                   
                                    Writer writer = new OutputStreamWriter( new BufferedOutputStream( new FileOutputStream( index ) ) );
                                    writer.write( "<html>\n<head><title>Test Drag</title></head>\n<body>Testing MRJ drag</body>\n</html>" );
                                    writer.flush();
                                    writer.close();
                              }
                              catch( Exception ex ) {
                                    ex.printStackTrace();
                              }
                             
                        }
                  }
            }

            public void dragFailed( DragInitiatorEvent e ) {
                  System.out.println( e );            
            }
      }
           
      public Dimension getPreferredSize() {
            return new Dimension( 320, 240 );
      }

      public static void main( String[] args ) {
            DragTest dt = new DragTest();
            dt.pack();
            dt.show();
      }
     
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #10 - Posted 2003-05-25 15:46:19 »

Are you aware that JDirect does not work with Apple's 1.4.1 JRE?  You will have to go with JNI like everyone else, or you could grab JNIDirect which some dude wrote to do some JDirect-like things via JNI.

Offline lilspikey

Senior Newbie




Computers Stole My Social Skills


« Reply #11 - Posted 2003-05-25 16:07:36 »

No didn't know that.  I guess in MacOS X I'd have to do another version using the cocoa classes.  The app I am doing this for tries to dynamically load stuff, so if it doesn't work under MacOS X, you just won't be able to drag files.

I though Apple had continued on with JDirect?  I know that JDirect 3 was out, and that's based on JNI anyway.  I guess you don't pay close enough attention untill you actually use an OS, and I can't afford to upgrade at the moment.

Anyway, I wish that sun would add this kind of functionality to "normal" java.  It can't be that hard really, or are Windows, Linux, Unix etc a bit more ackward for that kind of thing?

Offline CornedBee

Junior Newbie




Eat me!


« Reply #12 - Posted 2003-06-02 10:41:10 »

I know of an FTP app where you can drag remote files to explorer. I guess it works using OLE DnD. This mechanism says that when initiating the drop the source app only says what type of thing gets dragged, so that another app can decide whether it wants the object. Only when the object is really dropped the source application must provide data, which gives it a chance to download the file and save it to a temporary location, where the target app then reads it from.

All the buzzt
CornedBee :rolleyes:
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (38 views)
2014-10-17 03:59:02

Norakomi (28 views)
2014-10-16 15:22:06

Norakomi (24 views)
2014-10-16 15:20:20

lcass (28 views)
2014-10-15 16:18:58

TehJavaDev (53 views)
2014-10-14 00:39:48

TehJavaDev (54 views)
2014-10-14 00:35:47

TehJavaDev (43 views)
2014-10-14 00:32:37

BurntPizza (64 views)
2014-10-11 23:24:42

BurntPizza (36 views)
2014-10-11 23:10:45

BurntPizza (78 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!