User Tools

Checking the files in file choosers / File previews


Checking file format on the fly in a CheckingFileChooser: 2 methods

It is 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 compatible files will be associated to a green mark, the others to a red mark.

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.

The green and red marks in Windows' Look & FeelUnder GTK+ L&F, the mark can be seen in the file preview

1. Simple checking, format only, with a standard RecordSet / FileLoader

If the file is to be read with a file loader (i.e. jeeb.lib.util RecordSet or FileLoader), it is possible to pass the file loader class to a CheckingFileChooser. All the listed files will be evaluated with the file loader to evaluate their compatibility.

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.

See also jeeb.lib.util.fileloader.FileLoader for a variant.

Note: this process 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.

// fc-8.11.2017 CheckingFileChooser tells user which files are supposed
// to be correct
JFileChooser chooser = CheckingFileChooser.getInstance(
		Settings.getProperty("heterofor.samsaraLightPath", PathManager.getDir("data") + File.separator
				+ "heterofor"), SLSettingsLoader.class);
int returnVal = chooser.showDialog(this, null); // null: approveButton text was already set
if (returnVal == JFileChooser.APPROVE_OPTION && chooser.getSelectedFile().exists())

2. Advanced checking, with a more complex loading method

When the loading method is complex, it is possible to override the check () method of CheckingFileChooser to propose a complete code to check the file.

// fc-4.12.2017 A file chooser showing which files can be loaded
JFileChooser chooser = new CheckingFileChooser(Settings.getProperty("samsara2.inventory.path",
		PathManager.getDir("data"))) {
	public void check(File f) throws Exception {
		String fileName = f.getAbsolutePath();
		// Throws an exception if file can not be loaded
		// 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;

The green and red marks in Windows' Look & FeelA green mark shows which file will be loaded correctly

The files with a green mark are supposed to be loaded correctly if selected

FileAccessory without file checking

It is also possible to add a preview to a standard Java JFileChooser to throw an eye at the files during the selection.

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

The preview (here on the right side) tries to shows the file content

file_preview_and_checking_in_file_chooser.txt ยท Last modified: 2018/03/12 15:42 by coligny