use of javax.swing.JFileChooser in project ORFpred by JonathanFeenstra.
the class FileHandler method selectFile.
/**
* Opent een JFileChooser om een bestand te selecteren.
*
* @param parent de parent voor de JFileChooser opendialog
* @return het geselecteerde bestand (of null)
* @throws FileNotFoundException als het bestand niet gevonden is
*/
public static File selectFile(Component parent) throws FileNotFoundException {
JFileChooser chooser = new JFileChooser();
for (FileType ft : FileType.values()) {
chooser.addChoosableFileFilter(ft.getFileFilter());
}
chooser.setFileFilter(chooser.getChoosableFileFilters()[1]);
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
if (chooser.getSelectedFile().exists()) {
return chooser.getSelectedFile();
}
throw new FileNotFoundException();
}
return null;
}
use of javax.swing.JFileChooser in project jgnash by ccavanaugh.
the class ExportTransactionsAction method exportTransactions.
public static void exportTransactions(final Account account, final LocalDate startDate, final LocalDate endDate) {
final ResourceBundle rb = ResourceUtils.getBundle();
final Preferences pref = Preferences.userNodeForPackage(ExportTransactionsAction.class);
JFileChooser chooser = new JFileChooser(pref.get(CURRENT_DIR, null));
FileNameExtensionFilter csvFilter = new FileNameExtensionFilter(rb.getString("Label.CsvFiles") + " (*.csv)", "csv");
FileNameExtensionFilter ofxFilter = new FileNameExtensionFilter(rb.getString("Label.OfxFiles") + " (*.ofx)", OFX);
FileNameExtensionFilter ssFilter = new FileNameExtensionFilter(rb.getString("Label.SpreadsheetFiles") + " (*.xls, *.xlsx)", "xls", "xlsx");
chooser.addChoosableFileFilter(csvFilter);
chooser.addChoosableFileFilter(ofxFilter);
chooser.addChoosableFileFilter(ssFilter);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setMultiSelectionEnabled(false);
chooser.setFileFilter(csvFilter);
if (chooser.showSaveDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
pref.put(CURRENT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
final File file = chooser.getSelectedFile();
final class Export extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
if (OFX.equals(FileUtils.getFileExtension(file.getName()))) {
OfxExport export = new OfxExport(account, startDate, endDate, file);
export.exportAccount();
} else if (FileUtils.getFileExtension(file.getName()).contains(XLS)) {
AccountExport.exportAccount(account, RegisterFactory.getColumnNames(account), startDate, endDate, file);
} else {
CsvExport.exportAccount(account, startDate, endDate, file);
}
return null;
}
@Override
protected void done() {
UIApplication.getFrame().stopWaitMessage();
}
}
new Export().execute();
}
}
use of javax.swing.JFileChooser in project jgnash by ccavanaugh.
the class DynamicJasperReportPanel method saveAction.
private void saveAction() {
Preferences p = Preferences.userNodeForPackage(DynamicJasperReportPanel.class);
JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
saveContributors.forEach(fileChooser::addChoosableFileFilter);
// restore the last save format
if (p.get(LAST_CONTRIBUTOR, null) != null) {
String last = p.get(LAST_CONTRIBUTOR, null);
for (JRSaveContributor saveContributor : saveContributors) {
if (saveContributor.getDescription().equals(last)) {
fileChooser.setFileFilter(saveContributor);
break;
}
}
} else if (!saveContributors.isEmpty()) {
fileChooser.setFileFilter(saveContributors.get(0));
}
if (p.get(LAST_DIRECTORY, null) != null) {
fileChooser.setCurrentDirectory(new File(p.get(LAST_DIRECTORY, null)));
}
int retValue = fileChooser.showSaveDialog(this);
if (retValue == JFileChooser.APPROVE_OPTION) {
FileFilter fileFilter = fileChooser.getFileFilter();
File file = fileChooser.getSelectedFile();
p.put(LAST_DIRECTORY, file.getParent());
JRSaveContributor contributor = null;
if (fileFilter instanceof JRSaveContributor) {
// save format chosen from the list
contributor = (JRSaveContributor) fileFilter;
} else {
for (JRSaveContributor saveContributor : saveContributors) {
// need to determine the best match
if (saveContributor.accept(file)) {
contributor = saveContributor;
break;
}
}
if (contributor == null) {
JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
}
}
if (contributor != null) {
p.put(LAST_CONTRIBUTOR, contributor.getDescription());
try {
if (contributor instanceof JRSingleSheetXlsSaveContributor) {
LOG.info("Formatting for xls file");
JasperPrint print = report.createJasperPrint(true);
contributor.save(print, file);
} else if (contributor instanceof JRCsvSaveContributor) {
LOG.info("Formatting for csv file");
JasperPrint print = report.createJasperPrint(true);
contributor.save(print, file);
} else {
contributor.save(jasperPrint, file);
}
} catch (final JRException ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
}
}
}
}
use of javax.swing.JFileChooser in project jgnash by ccavanaugh.
the class BudgetPanel method exportBudgetAction.
private void exportBudgetAction() {
final Preferences pref = Preferences.userNodeForPackage(BudgetPanel.class);
final ResourceBundle rb = ResourceUtils.getBundle();
JFileChooser chooser = new JFileChooser(pref.get(CURRENT_DIR, null));
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
chooser.addChoosableFileFilter(new FileNameExtensionFilter(rb.getString("Label.SpreadsheetFiles") + " (*.xls, *.xlsx)", "xls", "xlsx"));
if (chooser.showSaveDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
pref.put(CURRENT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
final File file = new File(chooser.getSelectedFile().getAbsolutePath());
final class Export extends SwingWorker<String, Void> {
@Override
protected String doInBackground() throws Exception {
UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
return BudgetResultsExport.exportBudgetResultsModel(file.toPath(), resultsModel);
}
@Override
protected void done() {
UIApplication.getFrame().stopWaitMessage();
try {
String message = get();
// display any errors that may have occurred
if (message != null) {
StaticUIMethods.displayError(message);
}
} catch (InterruptedException | ExecutionException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
}
new Export().execute();
}
}
use of javax.swing.JFileChooser in project jgnash by ccavanaugh.
the class SaveFileAsAction method saveFileAs.
/**
* Opens a Save as Dialog. If the extension of the destination file is different than the file currently open, then
* an attempt is made to identify the new file format and save accordingly. Otherwise, a copy of the file is made.
*/
private static void saveFileAs() {
final ResourceBundle rb = ResourceUtils.getBundle();
final Preferences pref = Preferences.userNodeForPackage(SaveFileAsAction.class);
JFileChooser chooser = new JFileChooser(pref.get(CURRENT_DIR, null));
chooser.setMultiSelectionEnabled(false);
chooser.setDialogTitle(rb.getString("Title.SaveAs"));
final DataStoreType[] types = DataStoreType.values();
final String[] ext = new String[types.length];
for (int i = 0; i < types.length; i++) {
ext[i] = types[i].getDataStore().getFileExt();
}
StringBuilder description = new StringBuilder(rb.getString("Label.jGnashFiles") + " (");
for (int i = 0; i < types.length; i++) {
description.append("*");
description.append(types[i].getDataStore().getFileExt());
if (i < types.length - 1) {
description.append(", ");
}
}
description.append(')');
chooser.addChoosableFileFilter(new DataStoreFilter(description.toString(), ext));
if (chooser.showSaveDialog(UIApplication.getFrame()) == JFileChooser.APPROVE_OPTION) {
pref.put(CURRENT_DIR, chooser.getCurrentDirectory().getAbsolutePath());
final class SaveAs extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
UIApplication.getFrame().displayWaitMessage(rb.getString("Message.PleaseWait"));
EngineFactory.saveAs(chooser.getSelectedFile().getAbsolutePath());
return null;
}
@Override
protected void done() {
UIApplication.getFrame().stopWaitMessage();
}
}
new SaveAs().execute();
}
}
Aggregations