How can I drag virtual files from java swing to windows desktop? - java

Is it possible to drag a "virtual" file from Java swing application and drop it onto windows desktop?
By "virtual" I mean "not local" file that needs to be downloaded/prepared which could potentially take a long time. I have a client-server application that shows a view of remote files. When user drops that file on desktop, I would like to receive a notification of the destination folder which I would use to start the download process with the Java application.
Please offer any help or guidance as I've been looking for a solution for a while now.

I'm working on something similar myself, and I have some unfortunate news for you: Windows cannot handle promised files, nor does it inform your java application as to where the files are dropped. Simply put, there is no way to handle remote drag-and-drop via the DnD api only.
You need to create empty files in some temp directory somewhere, and hand windows those files when the mouse leaves the swing window. You also need to set up a WatchService on the local filesystem to receive notification of file creation. After DropEnd, you check for empty files of the correct name but not in your temp directory, and effect the transfer at that point (move the remote files into the empty files that were dropped).

Related

How to call the OS functionality to copy over files to a folder?

I a Java app, I need to synchronize the contents of two user-selected directories, Source and Target(simple local machine app, no need to consider any filesystem or server stuff and whatnot). The app should copy any new and changed files from Source to the Target and delete any files that are not in the Source but are in the Target. The synchronization process needs to display a progress bar and be cancelable.
The deleting part I guess I'll have to write entirely, but the other behavior is the same as the OS functionality to copy over files from one directory to another, so I could save myself a lot of work by using the functionality that is already there. The problem is, I have no idea how to access it from Java.
So, how do I make Java do the same thing that happens on most OSs if you select some files and drag them onto a folder icon?
You can use Java's WatchService to watch a directory for changes. Since you are using two directories, you would need to watch both the directories for changes.
For progress bars and drag and drop, you would need either JavaFX or Swing.
Trying to communicate with native OS calls would be - difficult and if possible, platform dependent. Moreover, you would need to know if the OS actually exposes these sort of features to a non OS code.
Apache fileutils does have quite a few handy methods dealing with copying files. Check this thread Progress bar with Apache FileUtils.copyDirectory(...) for some inspiration
/Bjorn

JSP Limit the directories of a File Open dialog

I am currently coding a program using the language JSP. I was wondering if/how you would restrict the directories a File Open dialogue box can browse, for instance:
The default directory is in C:/userFiles/username/ I want the user to be able to browse files in that /username/ directory, or directories within that directory, but not any directories above like C:/, or C:/userFiles. I have done some research, and I could not come across an answer. Is this possible with JSP, HTML, or Java and if so, how would I do it? I would prefer JSP or HTML.
Additional Info:
The dialogue box would browse files on a remote server, not on the user's computer.
The File Open dialog is on the client machine and is part of the operating system. You cannot change its behaviour.
In order to list files on a SERVER you would not use a File Open dialog, you'd be turning the list of files into HTML and serving that to the browser. Since you're the one writing the server-side code, you are ALREADY the one in control over which files make it into that list in the first place.
Saying you'd prefer "JSP or HTML" is a pretty good indicator that you don't fully understand the client-server relationship of the web.

Problems with deployment, advice needed for a web-based java application

I have developed a command-line (read: no GUI) Java application which crunches through numbers based on a given dataset and a series of parameters; and spits out a series of HTML files as resultant reports. These reports hold a large amount of data in tables, so in order to give the users a easy and quick overview of the results, I utilized the JUNG2 library and created a nice graph.
Here's where it gets interesting; since I would like the graph to be interactive it should be deployed after the application has run and files are generated, whenever the user wants to view the reports. I decided to go with an applet based deployment, however I am not too happy with the current setup due to the following reasons:
I want to make the software as simple to use as possible (my users won't be tech-savvy, and even tech-intimidated in most cases). I would really like to distribute one JAR only, which forced me to put the applet with everything else it needs in a package in the same JAR as the main application.
The applet and the main application need to communicate the results, so I create a xML-based report which is used to hold information. As long as the files are on a local machine and are not moved around it all works fine. Unfortunately I also need the files to be moved around. A user should be able to take the "results" folder to a USB stick, go anywhere plug the stick to another computer and be able to use the report as he/she likes.
For the time being the applets are implemented with the following html code:
<applet code="package.myapp.visualization.GraphApplet.class"
codebase="file:/home/user/myApp"
archive="myApp-0.2.6-r28.jar"
width="750" height="750">
<param name=input value="results/test_name/results.fxml">
</applet>
As you can see this applet will not work if the parent folder is moved to another location.
As far as I know I have a couple of alternatives:
a) Change codebase to point to an URL on our webserver where I could put the jar file. This however creates the problem with permissions, as the applet will not be able to read the results file. Alternative is to upload the results file to the server when the user wants to visualize the graph, although I am not sure if that's a good option due to server security and also if it could be made so that upload happens automatically without bothering the user.
b) I can use a relative path on the codebase attribute, but then the whole folder hierarchy needs to be intact upon copy. This could be a last resort, if I cant come up with a better way to do it.
c) change the deployment method (would like to avoid this alternative to not spend more time on the development phase)
Any ideas? Am I missing something? How could I tackle this problem?
Thanks,
I'm not sure I entirely understand your use-case, but from what I do understand, I would suggest this:
Dump the applet for an application launched using Java Web Start. Have the JNLP file declare a file association for the fxml file type. When the user double clicks an fxml file, it will be passed as an argument to the main(String[]) of the JWS application.
A sand-boxed JWS application can gain access to resources on the local file system using the JNLP API. Here is my demo. of the JNLP API file services.

How can I determine the way to access certain third-party programs and resources from my Java app?

I'm trying to open a PDF file after I generate a report. I mean, the user logs in (it's a Swing-based app) and clicks to generate a report. Then, a PDF file is generated. I would like to launch the PDF reader at that moment. I could do something like exec("evince "+path_to_pdf_file). It's just for Ubuntu, Windows would be more difficult. I'm thinking I need to explore the registry.
How can I achieve this?
What you need is the method java.awt.Desktop#open
Launches the associated application to open the file.
If the specified file is a directory, the file manager of the current platform is launched to open it.

Java Applet to manage file uploads to server

Looking for a descent looking Java File Upload Applet (or even Flash) that fits the following requirements:
Free/cheap (yes looking to use in a commercial web application)
Preferably allow multiple file selection
Ability to create/edit folders on destination server? (within a secure root folder)
Ability to overwrite files in the destination directory?
IE + Firefox support required... Safari, Chrome, etc. a bonus
Ability to "hook" into the flow to apply additional logic
Notes:
In particular I'm looking for the ability to upload to a directory on another server (e.g. not where my web server or app server is running)
The destination server has a shared drive where all users of the app have permission to store files.
By "decent looking" I'd really, really like to avoid a UI that is "skinned" like this:
(source: swtguibuilder.com)
Before I'm shut down for "not programming related" I would need to be able to hook into such an applet to apply some additional permission logic etc. I don't want to re-invent the wheel if someone else has made a component that already does what I need.
This is relatively easy with Java. Look into Apache Commons Fileupload for the server-side code. For the front-end, there are many examples of flash-based file uploads.
The simple answer to your question used to be Flash. Since the release of Flash Player 10 it isn't anymore. Adobe has added a restriction on File upload which only allows a file to be uploaded in the scope of UIA - User Initiated Action. This means that a file upload must be started with a mouse click.
There are some of free file uploaders built in flash available. The only problem is that the queuing is broken by FP10. Adding the files to the pending uploads list is no problem. Starting the upload is no problem either. But when one file upload is done and the next should begin, Flash Player will not allow it if you don't click again which is pretty much useless.
There at least two requirements:
Applet file uploader These are available, e.g.: http://sourceforge.net/projects/jupload/
Widget to configure destination This is unusual in my experience, but I wrote one once for a Swing app which would be similar for an applet. It was necessary to provide the user a view of the remote file system, enable manipulation of the view, and then upload the changes. Note that there are transactions issues here, so it's not trivial.

Categories

Resources