All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.invation.code.toval.graphic.dialog.ConditionalFileDialog Maven / Gradle / Ivy

package de.invation.code.toval.graphic.dialog;

import java.awt.Window;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * This class can be used to request a file/directory with the help of a JFileChooserDialog.
* It allows to customize the accepted files/directories by overriding the method {@link #isValid(File)}.
* The user will be shown a JFileChooserDialog until he hits cancel or selects a valid file. * * @author Thomas Stocker */ public abstract class ConditionalFileDialog { private JFileChooser fileChooser = null; private Window parent = null; public ConditionalFileDialog(Window parent, FileChooserType type, String title) { this.parent = parent; fileChooser = new JFileChooser(); fileChooser.setDialogTitle(title); switch(type){ case DIRECTORY: fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); break; case FILE: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); break; } } public String chooseFile(){ while(fileChooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION && !isValid(fileChooser.getSelectedFile())){ JOptionPane.showMessageDialog(parent, getErrorMessage(), "Invalid or corrupted file/directory", JOptionPane.ERROR_MESSAGE); } if(fileChooser.getSelectedFile() == null) return null; return fileChooser.getSelectedFile().getAbsolutePath(); } protected abstract String getErrorMessage(); protected abstract boolean isValid(File file); public enum FileChooserType { DIRECTORY, FILE; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy