AMAPstudio

### Sidebar

This is an old revision of the document!

# File preview and checking in file chooser

fc-5.12.2017

## FileAccessory

It is possible to add a preview in a file chooser to throw an eye at the files during the selection.

chooser = new JFileChooser(Settings.getProperty("heterofor.species.path", PathManager.getInstallDir()));
new FileAccessory(chooser);

## CheckingFileChooser

It is also possible to use the CheckingFileChooser instead of the JFileChooser to check during selection which files will be loaded without error. The process relies on a method to be redefined called check (File f). When the file given to this method is correct, the method must return true.

The result is a green check mark associated to each correct file, and a red mark for those which will not load correctly.

The Preview is always shown in the CheckingFileChooser, it will show the mark for the selected file.

Warning: some Java look and feels do not show the icon for each file during selection (e.g. GTK+ under Linux). In this case, the mark can still be seen in the FileAccessory preview.

// fc-4.12.2017 A file chooser showing which files can be loaded
JFileChooser chooser = new CheckingFileChooser(Settings.getProperty("capsis.inventory.path",
PathManager.getDir("data"))) {

public boolean check(File f) {
String fileName = f.getAbsolutePath();
try {
// Use fake objects to avoid side effects
Samsa2Model modelFake = new Samsa2Model();
Samsa2InitialParameters ipFake = modelFake.getSettings();

// Check if the file can be loaded
ipFake.vParamMS.virtualStand = false;
ipFake.fileName = fileName;
ipFake.buildInitScene(modelFake);

// Seems ok
return true;

} catch (Throwable e) {
// Seems problematic
return false;

}
}
};

## Fast checking

This is a proposal of a simple check () implementation for the CheckingFileChooser when the file is to be loaded by a RecordSet / FileLoader.

It will check the file format interpretation by the loader but will not go further: an error might still happen later during effective file loading, when interpreting the content of the fields and lines.

Note: jeeb.lib.util.RecordSet is a tool to build text file loaders quickly, by specifying the expected line formats in objects extending the Record class. Giving a fileName to the tool, it will use introspection to check if the lines match the given Records and turn the file into a List of Records, easying their interpretation in a load () method called after.

String path = inventoryFileName.getText();

JFileChooser chooser = new CheckingFileChooser(path) {
public boolean check(File file) {
try {
// Just checks if the loader loads, do not check until Plot creation
new FileAccessory(chooser);