Search in sources :

Example 1 with FileNameExtensionFilter

use of com.revolsys.io.file.FileNameExtensionFilter in project com.revolsys.open by revolsys.

the class IoFactory method newFileFilters.

public static List<FileNameExtensionFilter> newFileFilters(final Set<String> allExtensions, final Class<? extends IoFactory> factoryClass) {
    final List<FileNameExtensionFilter> filters = new ArrayList<>();
    final List<? extends IoFactory> factories = IoFactory.factories(factoryClass);
    for (final IoFactory factory : factories) {
        final FileNameExtensionFilter filter = factory.newFileFilterAllExtensions();
        filters.add(filter);
        if (allExtensions != null) {
            for (final String fileNameExtension : filter.getExtensions()) {
                allExtensions.add(fileNameExtension);
            }
        }
    }
    sortFilters(filters);
    return filters;
}
Also used : ArrayList(java.util.ArrayList) FileNameExtensionFilter(com.revolsys.io.file.FileNameExtensionFilter)

Example 2 with FileNameExtensionFilter

use of com.revolsys.io.file.FileNameExtensionFilter in project com.revolsys.open by revolsys.

the class IoFactory method addFileFilters.

default void addFileFilters(final List<FileNameExtensionFilter> filters) {
    for (final String fileExtension : getFileExtensions()) {
        final String description = getName() + " (" + fileExtension + ")";
        final FileNameExtensionFilter filter = new FileNameExtensionFilter(description, fileExtension);
        filters.add(filter);
    }
}
Also used : FileNameExtensionFilter(com.revolsys.io.file.FileNameExtensionFilter)

Example 3 with FileNameExtensionFilter

use of com.revolsys.io.file.FileNameExtensionFilter in project com.revolsys.open by revolsys.

the class SwingIo method exportToFile.

public static <F extends IoFactory> void exportToFile(final String title, final String preferencesGroup, final Class<F> factoryClass, final Predicate<F> factoryFilter, final String initialFileExtension, final boolean primaryExtension, final String baseName, final Consumer<File> exportAction) {
    Invoke.later(() -> {
        final JFileChooser fileChooser = SwingUtil.newFileChooser("Export " + title, preferencesGroup, "directory");
        fileChooser.setAcceptAllFileFilterUsed(false);
        final String defaultFileExtension = PreferencesUtil.getUserString(preferencesGroup, "fileExtension", initialFileExtension);
        final List<FileNameExtensionFilter> fileFilters = new ArrayList<>();
        for (final F factory : IoFactory.factories(factoryClass)) {
            if (factoryFilter.test(factory)) {
                final FileNameExtensionFilter fileFilter;
                if (primaryExtension) {
                    fileFilter = factory.newFileFilter();
                } else {
                    fileFilter = factory.newFileFilterAllExtensions();
                }
                fileFilters.add(fileFilter);
                final List<String> extensions = fileFilter.getExtensions();
                if (extensions.contains(defaultFileExtension)) {
                    fileChooser.setFileFilter(fileFilter);
                }
            }
        }
        IoFactory.sortFilters(fileFilters);
        for (final FileNameExtensionFilter fileFilter : fileFilters) {
            fileChooser.addChoosableFileFilter(fileFilter);
        }
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setSelectedFile(new File(fileChooser.getCurrentDirectory(), baseName));
        final int returnVal = fileChooser.showSaveDialog(SwingUtil.getActiveWindow());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            final FileNameExtensionFilter fileFilter = (FileNameExtensionFilter) fileChooser.getFileFilter();
            File file = fileChooser.getSelectedFile();
            if (file != null) {
                final String fileExtension = FileUtil.getFileNameExtension(file);
                final String expectedExtension = fileFilter.getExtensions().get(0);
                if (!fileExtension.equals(expectedExtension)) {
                    file = FileUtil.getFileWithExtension(file, expectedExtension);
                }
                final File targetFile = file;
                PreferencesUtil.setUserString(preferencesGroup, "fileExtension", expectedExtension);
                PreferencesUtil.setUserString(preferencesGroup, "directory", file.getParent());
                final String description = "Export " + baseName + " to " + targetFile.getAbsolutePath();
                Invoke.background(description, () -> exportAction.accept(targetFile));
            }
        }
    });
}
Also used : JFileChooser(javax.swing.JFileChooser) ArrayList(java.util.ArrayList) FileNameExtensionFilter(com.revolsys.io.file.FileNameExtensionFilter) File(java.io.File)

Example 4 with FileNameExtensionFilter

use of com.revolsys.io.file.FileNameExtensionFilter in project com.revolsys.open by revolsys.

the class LayerGroup method actionImportProject.

public void actionImportProject(final String title, final boolean resetProject) {
    final Project project = getProject();
    if (resetProject) {
        if (!project.saveWithPrompt()) {
            return;
        }
    }
    final JFileChooser fileChooser = SwingUtil.newFileChooser(title, "com.revolsys.swing.map.project", "templateDirectory");
    final FileNameExtensionFilter filter = new FileNameExtensionFilter("Project (*.rgmap)", "rgmap");
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setFileFilter(filter);
    if (OS.isMac()) {
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    } else {
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }
    final Window window = SwingUtil.getActiveWindow();
    final int returnVal = fileChooser.showOpenDialog(window);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        final File projectDirectory = fileChooser.getSelectedFile();
        if (projectDirectory != null) {
            PreferencesUtil.setUserString("com.revolsys.swing.map.project", "templateDirectory", projectDirectory.getParent());
            final PathResource resource = new PathResource(projectDirectory);
            importProject(project, resource, resetProject);
        }
    }
}
Also used : Window(java.awt.Window) JFileChooser(javax.swing.JFileChooser) PathResource(com.revolsys.spring.resource.PathResource) FileNameExtensionFilter(com.revolsys.io.file.FileNameExtensionFilter) File(java.io.File)

Example 5 with FileNameExtensionFilter

use of com.revolsys.io.file.FileNameExtensionFilter in project com.revolsys.open by revolsys.

the class Project method getSaveAsDirectory.

public Path getSaveAsDirectory() {
    File directory = null;
    final JFileChooser fileChooser = SwingUtil.newFileChooser("Save Project", "com.revolsys.swing.map.project", "directory");
    fileChooser.setFileFilter(new FileNameExtensionFilter("Project", "rgmap"));
    fileChooser.setMultiSelectionEnabled(false);
    final int returnVal = fileChooser.showSaveDialog(SwingUtil.getActiveWindow());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        directory = fileChooser.getSelectedFile();
        final String fileExtension = FileUtil.getFileNameExtension(directory);
        if (!"rgmap".equals(fileExtension)) {
            directory = new File(directory.getParentFile(), directory.getName() + ".rgmap");
        }
        PreferencesUtil.setUserString("com.revolsys.swing.map.project", "directory", directory.getParent());
        if (directory.exists()) {
            FileUtil.deleteDirectory(directory);
        }
        directory.mkdirs();
        this.resource = new PathResource(directory);
        return directory.toPath();
    } else {
        return null;
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) PathResource(com.revolsys.spring.resource.PathResource) FileNameExtensionFilter(com.revolsys.io.file.FileNameExtensionFilter) File(java.io.File)

Aggregations

FileNameExtensionFilter (com.revolsys.io.file.FileNameExtensionFilter)10 File (java.io.File)8 JFileChooser (javax.swing.JFileChooser)8 Window (java.awt.Window)4 PathResource (com.revolsys.spring.resource.PathResource)2 ArrayList (java.util.ArrayList)2 LayerGroupTreeNode (com.revolsys.swing.tree.node.layer.LayerGroupTreeNode)1 Path (java.nio.file.Path)1 TreeSet (java.util.TreeSet)1 FileFilter (javax.swing.filechooser.FileFilter)1