use of javax.swing.filechooser.FileFilter in project bytecode-viewer by Konloch.
the class FileChooser method run.
public void run() {
File currentFile = new File(target.get() == null || target.get().isEmpty() ? System.getProperty("user.home") : target.get());
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return true;
}
@Override
public String getDescription() {
return message;
}
});
if (currentFile.isDirectory()) {
fc.setCurrentDirectory(currentFile);
} else {
fc.setSelectedFile(currentFile);
}
fc.setFileHidingEnabled(false);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
target.set(fc.getSelectedFile().getAbsolutePath());
} catch (Exception e1) {
new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e1);
}
}
}
use of javax.swing.filechooser.FileFilter in project antlrworks by antlr.
the class XJFileChooser method displayOpenDialog.
public boolean displayOpenDialog(Component parent, List extensions, List<String> descriptions, boolean multiple) {
if (parent != null)
parent = XJDialog.resolveOwner(parent.getParent());
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(multiple);
if (extensions == null || extensions.size() == 0)
chooser.setAcceptAllFileFilterUsed(true);
else {
for (int i = 0; i < extensions.size(); i++) {
XJFileFilter ff = XJFileFilter.createFileFilter(extensions.get(i), descriptions.get(i));
chooser.addChoosableFileFilter(ff);
if (extensions.size() == 1 && i == 0)
chooser.setFileFilter(ff);
}
if (extensions.size() > 1)
chooser.setFileFilter(chooser.getAcceptAllFileFilter());
}
loadDefaultDirectory(chooser);
if (chooser.showOpenDialog(parent == null ? null : parent) == JFileChooser.APPROVE_OPTION) {
selectedFilePath = chooser.getSelectedFile().getAbsolutePath();
selectedFilePaths = filesToList(chooser.getSelectedFiles());
if (extensions != null && extensions.size() >= 0) {
FileFilter ff = chooser.getFileFilter();
if (ff instanceof XJFileFilter) {
XJFileFilter filter = (XJFileFilter) ff;
if (selectedFilePath.indexOf(".") == -1)
selectedFilePath += "." + filter.getDefaultExtension();
}
}
saveCurrentDirectory(chooser);
return XJApplication.YES;
} else
return XJApplication.NO;
}
use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class PCGenFrame method showSaveCharacterChooser.
/**
* This brings up a file chooser allows the user to select
* the location that a character should be saved to.
* @param character the character to be saved
*/
boolean showSaveCharacterChooser(CharacterFacade character) {
PCGenSettings context = PCGenSettings.getInstance();
String parentPath = lastCharacterPath;
if (parentPath == null) {
parentPath = context.getProperty(PCGenSettings.PCG_SAVE_PATH);
}
chooser.setCurrentDirectory(new File(parentPath));
File file = character.getFileRef().get();
File prevFile = file;
if (file == null || StringUtils.isEmpty(file.getName())) {
file = new File(parentPath, character.getNameRef().get() + Constants.EXTENSION_CHARACTER_FILE);
}
chooser.setSelectedFile(file);
chooser.resetChoosableFileFilters();
FileFilter filter = new FileNameExtensionFilter("Pcg files only", "pcg");
chooser.addChoosableFileFilter(filter);
chooser.setFileFilter(filter);
int ret = chooser.showSaveDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
if (!file.getName().endsWith(Constants.EXTENSION_CHARACTER_FILE)) {
file = new File(file.getParent(), file.getName() + Constants.EXTENSION_CHARACTER_FILE);
}
UIDelegate delegate = character.getUIDelegate();
if (file.isDirectory()) {
delegate.showErrorMessage(Constants.APPLICATION_NAME, //$NON-NLS-1$
LanguageBundle.getString("in_savePcDirOverwrite"));
return showSaveCharacterChooser(character);
}
if (file.exists() && (prevFile == null || !file.getName().equals(prevFile.getName()))) {
boolean overwrite = delegate.showWarningConfirm(LanguageBundle.getFormattedString(//$NON-NLS-1$
"in_savePcConfirmOverTitle", file.getName()), LanguageBundle.getFormattedString(//$NON-NLS-1$
"in_savePcConfirmOverMsg", file.getName()));
if (!overwrite) {
return showSaveCharacterChooser(character);
}
}
try {
character.setFile(file);
prepareForSave(character, false);
if (!reallySaveCharacter(character)) {
return showSaveCharacterChooser(character);
}
lastCharacterPath = chooser.getCurrentDirectory().toString();
return true;
} catch (Exception e) {
//$NON-NLS-1$
Logging.errorPrint("Error saving character to new file " + file, e);
delegate.showErrorMessage(Constants.APPLICATION_NAME, LanguageBundle.getFormattedString("in_saveFailMsg", //$NON-NLS-1$
file.getName()));
}
}
return false;
}
use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class PCGenFrame method showOpenCharacterChooser.
void showOpenCharacterChooser() {
PCGenSettings context = PCGenSettings.getInstance();
String path = lastCharacterPath;
if (path == null) {
path = context.getProperty(PCGenSettings.PCG_SAVE_PATH);
}
chooser.setCurrentDirectory(new File(path));
//$NON-NLS-1$
chooser.setSelectedFile(new File(""));
chooser.resetChoosableFileFilters();
FileFilter filter = new FileNameExtensionFilter("Pcg files only", "pcg");
chooser.addChoosableFileFilter(filter);
chooser.setFileFilter(filter);
int ret = chooser.showOpenDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
loadCharacterFromFile(file);
lastCharacterPath = chooser.getCurrentDirectory().toString();
}
}
use of javax.swing.filechooser.FileFilter in project pcgen by PCGen.
the class InitiativePlugin method fileOpen.
/**
* Handles the clicking of the <b>Add </b> button on the GUI.
*/
public void fileOpen() {
JFileChooser chooser = ImagePreview.decorateWithImagePreview(new JFileChooser());
File defaultFile = new File(PCGenSettings.getPcgDir());
if (defaultFile.exists()) {
chooser.setCurrentDirectory(defaultFile);
}
// TODO should probably handle zip pcg
String[] pcgs = { "pcg", "pcp" };
String[] init = { "gmi", "init" };
FileFilter ff = new FileNameExtensionFilter("Initiative Export", init);
chooser.addChoosableFileFilter(ff);
chooser.addChoosableFileFilter(new FileNameExtensionFilter("PCGen File", pcgs));
chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
chooser.setFileFilter(ff);
chooser.setMultiSelectionEnabled(true);
Cursor originalCursor = theView.getCursor();
theView.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
int option = chooser.showOpenDialog(theView);
if (option == JFileChooser.APPROVE_OPTION) {
File[] pcFiles = chooser.getSelectedFiles();
for (File pcFile : pcFiles) {
if (PCGFile.isPCGenCharacterOrPartyFile(pcFile)) {
messageHandler.handleMessage(new RequestOpenPlayerCharacterMessage(this, pcFile, false));
//loadPCG(pcFiles[i]);
} else if (pcFile.toString().endsWith(".init") || pcFile.toString().endsWith(".gmi")) {
loadINIT(pcFile);
}
}
/* loop through selected files */
theView.refreshTable();
} else {
/* this means the file is invalid */
}
theView.setCursor(originalCursor);
}
Aggregations