FileDialog mock files - java

I am implementing an Applet which needs to access to file system and I need to show to user mock/fake files under mypc or userhome.
There is a way to do it using JFileChooser with FileSystemView:
This is a short example for Windows OS:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSystemView(new FileSystemView() {
#Override
public File createNewFolder(File containingDir) throws IOException {
//do something, not important here
return null;
}
#Override
public File[] getFiles(File dir, boolean useFileHiding) {
File[] files = super.getFiles(dir, useFileHiding);
// my pc -> desktop -> null
if (dir.getParentFile() == null
|| dir.getParentFile().getParentFile() != null) {
return files;
}
List<File> newFiles = new ArrayList(Arrays.asList(files));
newFiles.add(new File("Custom_File_1"));
newFiles.add(new File("Custom_File_2"));
return newFiles.toArray(new File[newFiles.size()]);
}
});
But the issue is that JFileChooser does not work properly on Mac OS. There is no ability to navigate into any subdirectory, move up from the current directory.
Here is a similar problem:
JFileChooser for directories on the Mac: how to make it not suck? (I switched off DIRECTORIES_ONLY mode, but it did not help)
Now I am trying to use FileDialog, but there is another problem:
I can not find a way to show mock/fake files under mypc or userhome (like FileSystemView).
The question is: Is there an ability to fix JFileChooser in MAC OS? Or add mock/fake files into specific directory using FileDialog?

Related

How do you filter hidden files using DirectoryStream.Filter

I am trying to filter hidden files using the NIO classes.
When I run the attached code on Windows 10 I get the following output:
Files:
c:\Documents and Settings
c:\PerfLogs
c:\Program Files
c:\Program Files (x86)
c:\Users
c:\Windows
Paths:
c:\$Recycle.Bin
c:\Config.Msi
c:\Documents and Settings
c:\Intel
c:\IntelOptaneData
c:\OEM
c:\OneDriveTemp
c:\PerfLogs
c:\Program Files
c:\Program Files (x86)
c:\ProgramData
c:\Recovery
c:\System Volume Information
c:\Users
c:\Windows
The list displayed under Files (using the old File.listFiles(FileFilter) method) is the list I see in Windows File Explorer and is what I am expecting to see (except for the Document and Setting and I know how to fix that)
Why is the NIO approach not filtering hidden files the same way?
How do I get NIO filtering to be the same?
Here is the test code:
import java.io.*;
import java.nio.file.*;
public class ListFilesNIO
{
public static void main(String[] args) throws Exception
{
String directory = "c:\\";
// Use old File I/O
FileFilter fileFilter = new FileFilter()
{
#Override
public boolean accept(File entry)
{
if (entry.isHidden()) return false;
return true;
}
};
System.out.println("Files:");
File[] files = new File( directory ).listFiles( fileFilter );
for (File file : files)
{
System.out.println( "\t" + file );
}
// Use NIO
DirectoryStream.Filter<Path> pathFilter = new DirectoryStream.Filter<Path>()
{
#Override
public boolean accept(Path entry) throws IOException
{
if (Files.isHidden( entry )) return false;
return true;
}
};
System.out.println();
System.out.println("Paths:");
DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get( directory ), pathFilter);
for (Path path : paths)
{
System.out.println( "\t" + path );
}
}
}
Note: when I run the code without the filter, in both cases 18 files are displayed. So the first approach is filtering 12 hidden files and the second approach is only filtering 3 files.
It's not a bug but a feature(!) known since jdk7, Windows hidden directory are not detected as hidden, see this bug and this one (fix jdk13).
As a workaround, you can do this :
import java.nio.file.attribute.DosFileAttributes;
...
DirectoryStream.Filter<Path> pathFilter = new DirectoryStream.Filter<Path>()
{
#Override
public boolean accept(Path entry) throws IOException
{
DosFileAttributes attr = Files.readAttributes(entry, DosFileAttributes.class);
return !attr.isHidden();
}
};
I ended up using:
DirectoryStream.Filter<Path> pathFilter = new DirectoryStream.Filter<Path>()
{
#Override
public boolean accept(Path entry) throws IOException
{
DosFileAttributes attr = Files.readAttributes(entry, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
return !attr.isHidden();
}
};
As I mentioned in my question, I also want the Documents and Settings to be hidden.
The Documents and Settings is a link to C:\Users.
The default implementation for the Files.readAttributes(…) method is to follow links. So I guess because the c:\Users directory is not hidden, the Documents and Settings is also considered not hidden.
By using LinkOption.NOFOLLOW_LINKS it is considered hidden, which is what I want.

is it possible to read the downloaded file from chrome browser using selenium

I downloaded a text file by a click button functionality, using Selenium Java.
then the file is downloaded to a particular location in the system, for example,
C://myAppfiles.
But I can't access that downloaded folder because of some reason. But I have to read that file while downloading.
How to do it? is it possible to read that file from the browser(chrome) using selenium or any other method is available?
so I'd suggest to do the following:
wait until file download is done completely.
After that- try to list all the files in the given directory:
all files inside folder and sub-folder
public static void main(String[]args)
{
File curDir = new File(".");
getAllFiles(curDir);
}
private static void getAllFiles(File curDir) {
File[] filesList = curDir.listFiles();
for(File f : filesList){
if(f.isDirectory())
getAllFiles(f);
if(f.isFile()){
System.out.println(f.getName());
}
}
}
files/folder only
public static void main(String[]args)
{
File curDir = new File(".");
getAllFiles(curDir);
}
private static void getAllFiles(File curDir) {
File[] filesList = curDir.listFiles();
for(File f : filesList){
if(f.isDirectory())
System.out.println(f.getName());
if(f.isFile()){
System.out.println(f.getName());
}
}
}
That will help You to understand if there any files at all (in the given directory).
Dont forget to make paths platform independent (to the folder/ file), like:
//platform independent and safe to use across Unix and Windows
File fileSafe = new File("tmp"+File.separator+"myDownloadedFile.txt");
Also, You might want to check whether file actually exists via Path methods.
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) throws Exception {
Path filePath= Paths.get("C:\\myAppfiles\\downloaded.txt");
System.out.println("if exists: " + Files.exists(firstPath));
}
}
Additionally, path suggests You to check some other options on the file:
The following code snippet verifies that a particular file exists and that the program has the ability to execute the file.
Path file = ...;
boolean isRegularExecutableFile = Files.isRegularFile(file) &
Files.isReadable(file) & Files.isExecutable(file);
Once You face any exception- feel free to post it here.
Hope this helps You

SWT Drag and Drop not working on Windows 10 in exported Eclipse RCP exe

I've been using Drag and Drop in my Eclipse RCP GUI plugin project for several years and have upgraded to Windows 10 Home recently. I have a widget that just sets itself up as a file drop listener using this code:
Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() };
int ops = DND.DROP_COPY | DND.DROP_MOVE;
DropTarget target = new DropTarget(this, ops);
target.setTransfer(transfers);
target.addDropListener(new DropTargetAdapter() {
#Override
public void drop(DropTargetEvent event) {
if (event.data instanceof String[]) {
List<File> files = new ArrayList<>();
String[] paths = (String[]) event.data;
for (String path : paths) {
File file = new File(path);
if (file.isDirectory()) {
Iterator<File> recursive = FileUtils.iterateFiles(file, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
while (recursive.hasNext()) {
files.add(recursive.next());
}
} else {
files.add(file);
}
}
notifyListeners(files);
}
}
#Override
public void dragEnter(final DropTargetEvent event)
{
log.error(event.detail+"");
event.detail = DND.DROP_COPY;
}
});
Now in my new Win 10 environment when i run the application from Eclipse (my dev environment) the DnD works and the log get's my test error message. When I use the Export Product Wizard and run the resulting exe the DnD doesn't work, what I get is the cursor showing that I cannot drop my files on to my widget and there's no errors being written to the log from the dragEnter method.
The only thing I have to go on is that the export works if I run it on a Windows 7 PC but no on the Win 10 if that could possibly be the cause?

Need to acces a folder in a server with desktop java application

I have developed a face recognition application using javaCV and like to put my MySQL DB and Training image set in a to server accomplish a centralized online data set that i can access anywhere , Can anyone suggest me a method to get training images folder for processing in desktop java application
below is the method i pass images for recognition
public int FRecognizer(String path) {
String trainingDir = "C:\\Users\\Kavinda\\Documents\\NetBeansProjects\\Eagle_EYE\\trainingImages";
Mat testImage = imread(path, CV_LOAD_IMAGE_GRAYSCALE);
File root = new File(trainingDir);
FilenameFilter imgFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
name = name.toLowerCase();
return name.endsWith(".jpg") || name.endsWith(".pgm") || name.endsWith(".png");
}
};

java: Open folder on button click

In java, how can we open a separate folder (e.g. c:) for user on click of a button, e.g like the way " locate this file on disk" or "open containing folder" does when we download a file and we want to know where it was saved. The goal is to save user's time to open a browser and locate the file on disk.
Thanks ( image below is an example from what firefox does)
I got the answer:
Here is what worked for me in Windows 7:
File foler = new File("C:\\"); // path to the directory to be opened
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}
try {
desktop.open(foler);
} catch (IOException e) {
}
Thanks to #AlexS
I assume you have a file. With java.awt.Desktop you can use something like this:
public static void openContaiingFolder(File file) {
String absoluteFilePath = file.getAbsolutePath();
File folder = new File(absoluteFilePath.substring(0, absoluteFilePath.lastIndexOf(File.separator)));
openFolder(folder);
}
public static void openFolder(File folder) {
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(folder);
}
}
Be awrae that if you call this with a File that is no directory at least Windows will try to open the file with the default program for the filetype.
But I don't know on which platforms this is supported.

Categories

Resources