Plex-FAWE/src/main/java/com/sk89q/worldedit/util/FileDialogUtil.java

97 lines
2.8 KiB
Java
Raw Normal View History

2011-01-31 05:32:52 +00:00
/*
2014-04-04 22:03:18 +00:00
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
2011-01-31 05:32:52 +00:00
*
2014-04-04 22:03:18 +00:00
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
2011-01-31 05:32:52 +00:00
* (at your option) any later version.
*
2014-04-04 22:03:18 +00:00
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
2011-01-31 05:32:52 +00:00
*
2014-04-04 22:03:18 +00:00
* You should have received a copy of the GNU Lesser General Public License
2011-01-31 05:32:52 +00:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2014-04-04 22:03:18 +00:00
*/
2011-01-31 05:32:52 +00:00
package com.sk89q.worldedit.util;
import com.sk89q.util.StringUtil;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
2011-12-25 23:36:23 +00:00
import java.io.File;
2011-01-31 05:32:52 +00:00
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public final class FileDialogUtil {
private FileDialogUtil() {
}
2011-01-31 05:32:52 +00:00
public static File showSaveDialog(String[] exts) {
JFileChooser dialog = new JFileChooser();
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
if (exts != null) {
dialog.setFileFilter(new ExtensionFilter(exts));
}
int returnVal = dialog.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return dialog.getSelectedFile();
}
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
return null;
}
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
public static File showOpenDialog(String[] exts) {
JFileChooser dialog = new JFileChooser();
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
if (exts != null) {
dialog.setFileFilter(new ExtensionFilter(exts));
}
int returnVal = dialog.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return dialog.getSelectedFile();
}
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
return null;
}
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
private static class ExtensionFilter extends FileFilter {
private Set<String> exts;
private String desc;
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
public ExtensionFilter(String[] exts) {
this.exts = new HashSet<String>(Arrays.asList(exts));
2011-11-23 01:29:48 +00:00
2011-01-31 05:32:52 +00:00
desc = StringUtil.joinString(exts, ",");
}
2011-11-23 01:29:48 +00:00
@Override
2011-01-31 05:32:52 +00:00
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String path = f.getPath();
int index = path.lastIndexOf('.');
if (index == -1 || index == path.length() - 1) {
return false;
} else {
return exts.contains(path.indexOf(index + 1));
}
}
@Override
2011-01-31 05:32:52 +00:00
public String getDescription() {
return desc;
}
}
}