use of javax.swing.filechooser.FileNameExtensionFilter in project ChatGameFontificator by GlitchCog.
the class ControlWindow method getTargetSaveFile.
/**
* Gets a file to save to from the given file chooser, including options to overwrite
*
* @param chooser
* the chooser to use to get the file
* @param extension
* the extension of the type of file being saved, or null if there is no default extension
* @return file or null if selection is canceled
*/
public static File getTargetSaveFile(JFileChooser chooser, String extension) {
int overwrite = JOptionPane.YES_OPTION;
// overwrite an existing file, but if they select cancel it just breaks out of the loop
do {
int result = chooser.showSaveDialog(me);
// Default to yes, so it writes even if there's no existing file
overwrite = JOptionPane.YES_OPTION;
if (result == JFileChooser.APPROVE_OPTION) {
File saveFile = chooser.getSelectedFile();
if (chooser.getFileFilter() instanceof FileNameExtensionFilter) {
String[] exts = ((FileNameExtensionFilter) (chooser.getFileFilter())).getExtensions();
boolean endsInExt = false;
for (String ext : exts) {
if (saveFile.getName().toLowerCase().endsWith(ext.toLowerCase())) {
endsInExt = true;
break;
}
}
if (extension != null && !endsInExt) {
saveFile = new File(saveFile.getPath() + "." + extension);
}
}
if (saveFile.exists()) {
overwrite = JOptionPane.showConfirmDialog(me, "File " + saveFile.getName() + " already exists. Overwrite?", "Overwrite?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
}
if (overwrite == JOptionPane.YES_OPTION) {
return saveFile;
}
}
} while (overwrite == JOptionPane.NO_OPTION);
return null;
}
use of javax.swing.filechooser.FileNameExtensionFilter in project JMRI by JMRI.
the class OpSessionLog method makeLogFile.
public static synchronized boolean makeLogFile(java.awt.Component parent) {
JFileChooser fileChooser = new JFileChooser(FileUtil.getUserFilesPath());
fileChooser.setDialogTitle(Bundle.getMessage("logSession"));
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt", "TXT"));
int retVal = fileChooser.showDialog(parent, Bundle.getMessage("logFile"));
if (retVal != JFileChooser.APPROVE_OPTION) {
return false;
}
File file = fileChooser.getSelectedFile();
String fileName = file.getAbsolutePath();
String fileNameLC = fileName.toLowerCase();
if (!fileNameLC.endsWith(".txt")) {
fileName = fileName + ".txt";
file = new File(fileName);
}
// check for possible overwrite
if (file.exists()) {
if (JOptionPane.showConfirmDialog(parent, Bundle.getMessage("overWritefile", fileName), Bundle.getMessage("QuestionTitle"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION) {
return false;
}
}
try {
_outBuff = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
writeHeader(fileName);
} catch (FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(parent, fnfe.getMessage(), Bundle.getMessage("WarningTitle"), JOptionPane.WARNING_MESSAGE);
return false;
}
return true;
}
use of javax.swing.filechooser.FileNameExtensionFilter in project JMRI by JMRI.
the class FullBackupImportAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent e) {
// ensure preferences will be found for read
FileUtil.createDirectory(LocoFile.getFileLocation());
// make sure instance loaded
Roster.getDefault();
// set up to read import file
ZipInputStream zipper = null;
FileInputStream inputfile = null;
try {
JFileChooser chooser = new JFileChooser();
String roster_filename_extension = "roster";
FileNameExtensionFilter filter = new FileNameExtensionFilter("JMRI full roster files", roster_filename_extension);
chooser.addChoosableFileFilter(filter);
int returnVal = chooser.showOpenDialog(mParent);
if (returnVal != JFileChooser.APPROVE_OPTION) {
return;
}
String filename = chooser.getSelectedFile().getAbsolutePath();
inputfile = new FileInputStream(filename);
zipper = new ZipInputStream(inputfile) {
@Override
public void close() {
}
};
// now iterate through each item in the stream. The get next
// entry call will return a ZipEntry for each file in the
// stream
ZipEntry entry;
while ((entry = zipper.getNextEntry()) != null) {
log.debug(String.format("Entry: %s len %d added %TD", entry.getName(), entry.getSize(), new Date(entry.getTime())));
// reading until read returns 0 or less.
try {
// need a dummy object to do this operation in next line
LocoFile xfile = new LocoFile();
Element lroot = xfile.rootFromInputStream(zipper).clone();
if (lroot.getChild("locomotive") == null) {
// that's the roster file
continue;
}
mToID = lroot.getChild("locomotive").getAttributeValue("id");
// see if user wants to do it
int retval = JOptionPane.showOptionDialog(mParent, Bundle.getMessage("ConfirmImportID", mToID), Bundle.getMessage("ConfirmImport"), 0, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { Bundle.getMessage("CancelImports"), Bundle.getMessage("Skip"), Bundle.getMessage("ButtonOK") }, null);
if (retval == 0) {
break;
}
if (retval == 1) {
continue;
}
// see if duplicate
RosterEntry currentEntry = Roster.getDefault().getEntryForId(mToID);
if (currentEntry != null) {
retval = JOptionPane.showOptionDialog(mParent, Bundle.getMessage("ConfirmImportDup", mToID), Bundle.getMessage("ConfirmImport"), 0, JOptionPane.INFORMATION_MESSAGE, null, new Object[] { Bundle.getMessage("CancelImports"), Bundle.getMessage("Skip"), Bundle.getMessage("ButtonOK") }, null);
if (retval == 0) {
break;
}
if (retval == 1) {
continue;
}
// turn file into backup
// need a dummy object to do this operation in next line
LocoFile df = new LocoFile();
df.makeBackupFile(LocoFile.getFileLocation() + currentEntry.getFileName());
// delete entry
Roster.getDefault().removeEntry(currentEntry);
}
loadEntryFromElement(lroot);
addToEntryToRoster();
// use the new roster
Roster.getDefault().reloadRosterFile();
} catch (org.jdom2.JDOMException ex) {
ex.printStackTrace();
}
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (inputfile != null) {
try {
// zipper.close() is meaningless, see above, but this will do
inputfile.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
use of javax.swing.filechooser.FileNameExtensionFilter in project JMRI by JMRI.
the class ProfilePreferencesPanel method btnExportProfileActionPerformed.
//GEN-LAST:event_btnRemoveSearchPathActionPerformed
private void btnExportProfileActionPerformed(ActionEvent evt) {
//GEN-FIRST:event_btnExportProfileActionPerformed
Profile p = ProfileManager.getDefault().getProfiles(profilesTbl.getSelectedRow());
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("ZIP Archives", "zip"));
chooser.setFileView(new ProfileFileView());
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setSelectedFile(new File(p.getName() + ".zip"));
if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
try {
if (chooser.getSelectedFile().exists()) {
int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.overwriteMessage", chooser.getSelectedFile().getName(), chooser.getSelectedFile().getParentFile().getName()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.overwriteTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
if (!chooser.getSelectedFile().delete()) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.failureToDeleteMessage", chooser.getSelectedFile().getName(), chooser.getSelectedFile().getParentFile().getName()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.failureToDeleteTitle"), JOptionPane.ERROR_MESSAGE);
}
} else {
this.btnExportProfileActionPerformed(evt);
return;
}
}
boolean exportExternalUserFiles = false;
boolean exportExternalRoster = false;
if (!(new File(FileUtil.getUserFilesPath())).getCanonicalPath().startsWith(p.getPath().getCanonicalPath())) {
int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalUserFilesMessage"), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalUserFilesTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
exportExternalUserFiles = true;
}
}
if (!(new File(Roster.getDefault().getRosterLocation())).getCanonicalPath().startsWith(p.getPath().getCanonicalPath()) && !Roster.getDefault().getRosterLocation().startsWith(FileUtil.getUserFilesPath())) {
int result = JOptionPane.showConfirmDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalRosterMessage"), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.externalRosterTitle"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
exportExternalRoster = true;
}
}
//if (ProfileManager.getDefault().getActiveProfile() == p) {
// // TODO: save roster, panels, operations if needed and safe to do so
//}
ProfileManager.getDefault().export(p, chooser.getSelectedFile(), exportExternalUserFiles, exportExternalRoster);
log.info("Profile \"{}\" exported to \"{}\"", p.getName(), chooser.getSelectedFile().getName());
JOptionPane.showMessageDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.successMessage", p.getName(), chooser.getSelectedFile().getName()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.successTitle"), JOptionPane.INFORMATION_MESSAGE);
} catch (IOException | JDOMException ex) {
log.warn("Unable to export profile \"{}\" to {}", p.getName(), chooser.getSelectedFile().getPath(), ex);
JOptionPane.showMessageDialog(this, Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.errorMessage", p.getName(), chooser.getSelectedFile().getPath(), ex.getLocalizedMessage()), Bundle.getMessage("ProfilePreferencesPanel.btnExportProfile.errorTitle"), JOptionPane.ERROR_MESSAGE);
}
}
}
use of javax.swing.filechooser.FileNameExtensionFilter in project pcgen by PCGen.
the class DiceBagPluginController method chooseSaveFile.
/**
* <p>
* This static method opens a file save chooser and returns the chosen file.
* </p>
*
* @param bag
* Bag to save
* @return Returns the file to save to
*/
static File chooseSaveFile(DiceBagModel bag) {
File returnValue = null;
JFileChooser save = new JFileChooser();
String fileExt = "dbg";
FileFilter ff = new FileNameExtensionFilter("GMGen Dice Bag", fileExt);
save.addChoosableFileFilter(ff);
save.setFileFilter(ff);
if (bag.getFilePath() != null) {
save.setSelectedFile(new File(bag.getFilePath()));
} else {
String sFile = SettingsHandler.getGMGenOption(DiceBagPlugin.LOG_NAME + ".LastFile", System.getProperty("user.dir"));
save.setCurrentDirectory(new File(sFile));
}
if (save.showSaveDialog(GMGenSystem.inst) == JFileChooser.APPROVE_OPTION) {
SettingsHandler.setGMGenOption(DiceBagPlugin.LOG_NAME + ".LastFile", save.getSelectedFile().getParent());
String fileName = save.getSelectedFile().getName();
String dirName = save.getSelectedFile().getParent();
String ext = "";
if (!fileName.contains(".dbg")) {
ext = ".dbg";
}
returnValue = new File(dirName + File.separator + fileName + ext);
}
return returnValue;
}
Aggregations