There are plenty of tutorials out there on how to copy files to the clipboard using the SWT API. However, I've never seen an explanation of how to cut files to the clipboard. What I'm trying to do is write a simple file manager in Java where you can select a file and press Ctrl+X, and when you press Ctrl+V in the native file manager, the files are moved (not copied) from their original location to the destination. Does the SWT API actually support this, or is some lower-level API required to get this done?
Check out this image sample. Note that there is a FileTransfer class instead of ImageTranfer class which you can use instead.
Concerning CUT operation:
To do this, you must manually erase the data once it is copied to the clipboard. Java provides no implementation of a cut operation.. This is with AWT/Swing but I am pretty sure it's the same thing with SWT. You can remove it after successful clipboard copy or successful paste...your choice.
Related
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
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).
How can I know what is the source of data in clipboard?
For example when I copy something from Firefox to clipboard I want to get that the source is Firefox.
Is there any command to do that in Linux? Or is there a way to do that in Java?
Also, is there any way to track were the user pasted the data.
I want to detect if for example the user try to copy important data from organization's system, then s/he try to paste it some where else.
Thanks in advance for any help (:
It's not a language issue, it's an API issue. In Windows, the GetClipboardOwner() API call usually indicates the application that last updated the clipboard (via a handle). It's not 100% reliable. For example, if the application has terminated, then it will be null.
This function exists in Java but seems to be even less reliable, and may be mostly unused, according to this post:
Java clipboardOwner Purpose?
I think your best solution is to use the presence/absence of various clipboard formats to look for evidence. Simple formats like TEXT won't help, but complex formats like RTF and HTML will reveal clues. On Windows, there is a format for "html fragments" called CF_HTML, which has a header that looks different from one browser to the next. I would imagine that something similar exits with Java/Linux? Looking at the docs, there's something called a DataFlavor, that seems to have a very rich set of properties such as mimetype. It's possible that you could get a "fingerprint" of FireFox by looking at the attributes of each dataflavor present when copying.
There is no way to determine the source of the data in the clipboard in Java (for Linux I'm not sure but I doubt it).
To ensure that sensible Data does not leave your application do not allow the data to be copied or keep the data in an application internal clipboard and do not forward it to the system clipboard. In Java you have the control over your data being copied to the system clipboard or not.
If you use an application internal clipboard then be aware that a user could copy the sensible data first to a target that allows copying the content to the system clipboard and then copy it further out of your application.
I'm trying to use Java WatchEvent ENTRY_MODIFY to check if a file is being access (ie: read, copied to clipboard). However from the documentation and a small test case I've made, that event isn't being fired. It's only fired when the file is changed.
Am I doing something wrong? If so, how can I monitor a file on the filesystem?
This isn't directly built into java. Your best bet is to jump into a native OS solution. This can be tedious if you want to support multiple systems though.
If you can get away with supporting windows take a look at THIS LINK . Scroll down to the bottom and look at similar apps. You would be interested in any app that contains a command line interface. What you will need to do is install one of the software and then kick off a process using Runtime.exec. You could potentially just use a direct dll, but I'm not qualified to tell you which dll will give you that information or if it even exists. It might be something you want to look into though if you do not want a 3rd party dependency.
You will read the results of the process that hooks into the windows dll's and will tell you if the file is currently open (See this link for more details). Your application will have to pull data (consistently asking the Application if the file is open). It is not ideal, but probably it is a potential solution.
Answering from your definition of file being accessed (copied and being read), however for file alteration there are several existing API available. Here is an example given to monitor file alteration.
To check file is copied to clipboard, you can use Clipboard#hasFiles() method when content of clipboard modified. If it returns true than file is copied to clipboard.
To check file is being read currently, you can check if the file is locked or not using implementation of FileLock abstract class. It has acquiredBy() method which returns the channel currently holding the lock on file.
you can try other libraries to accomplish that task, for example http://jnotify.sourceforge.net/
or http://java.dzone.com/announcements/new-java-library-monitor-file the latter specifically stands: File Access Monitoring- You will be able to receive notifications about events when access or modification date is changed.
Hey i am hoping to write a program where the program automatically just copy pastes all my dad's documents from D:\office folder. So whenever I plug-in my pen-drive , the program silently copies all documents inside my pen-drive. Also all files should be pasted to a hidden folder in the pen-drive (so it remains private) . Synchronization capability also required ...So which language should be easy and where to get started ...any idea ??.
Seems to me that some spyin' is about to be goin' on here. :P
I'd recommend C++. Not extremely easy as .Net's tillyvally but fast, framework independent, convenient to manipulate Windows API. You wanna do advanced stealth app, you can't pick the easy way.
Why use the clipboard when you could just use shell commands???
Maybe write an autostart batch file on your pen drive that copies files to/from your flash drive as needed.
I infer you are on Windows. Window has a plethora of functions to manipulate files. A few functions are below.
CopyFile Copies an existing file to a new file.
FindFirstFile Searches a directory for a file or subdirectory name that matches a specified name.
FindFirstFileEx Searches a directory for a file or subdirectory name and attributes that match those that are specified.
FindNextFile Continues a file search.
MoveFile Moves an existing file or directory and its children.
On and on. These and many more functions are documented here.
File Management Functions
Copy or move the files to the pen drive.
HTH
I hate to be the person who suggests this (I don't like .NET that much):
Make a C# (or VB if you must) Console app, or Forms app (if you want to get fancy). The .NET framework will make this kind of program VERY easy and it might be fun. Unless you want to increase your proficiency in C/C++, i would suggest NOT doing it in those languages since there is a learning curve and it is a little complex to do some simple things.
"Just paste" or "synchronize"?
For synchronization, unison is a good bet, see http://en.wikipedia.org/wiki/Unison_(file_synchronizer). For "just paste", you could code a call to a batch calling XCOPY from with the Windows Autoplay mechanism.
Sounds like a secret covert operation you're talking about, though...
I think that you will need to do a lot of work to get a less than satisfactory result.
I would suggestion you instead have a look at DropBox, which is free up to 2Gb of storage which automatically synchronizes between all registered computers, plus has a special folder which allows for web access. Very nice.
I would suggest to use Camel framework of Java, there you can easily run service which will for example automatically copy data from your flash disk after plug in, to folder which you specify etc.
Good tutorial how to start is here:
https://www.youtube.com/watch?v=dmtXkA7FlwA