Search in sources :

Example 61 with JFileChooser

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;
}
Also used : JFileChooser(javax.swing.JFileChooser)

Example 62 with JFileChooser

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();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) SwingWorker(javax.swing.SwingWorker) CsvExport(jgnash.convert.exports.csv.CsvExport) AccountExport(jgnash.convert.exports.ssf.AccountExport) OfxExport(jgnash.convert.exports.ofx.OfxExport) ResourceBundle(java.util.ResourceBundle) OfxExport(jgnash.convert.exports.ofx.OfxExport) Preferences(java.util.prefs.Preferences) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) File(java.io.File)

Example 63 with JFileChooser

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"));
            }
        }
    }
}
Also used : JRSingleSheetXlsSaveContributor(net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor) JFileChooser(javax.swing.JFileChooser) JRException(net.sf.jasperreports.engine.JRException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSaveContributor(net.sf.jasperreports.view.JRSaveContributor) Preferences(java.util.prefs.Preferences) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRCsvSaveContributor(net.sf.jasperreports.view.save.JRCsvSaveContributor)

Example 64 with JFileChooser

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();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) SwingWorker(javax.swing.SwingWorker) BudgetResultsExport(jgnash.engine.budget.BudgetResultsExport) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 65 with JFileChooser

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();
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) SwingWorker(javax.swing.SwingWorker) ResourceBundle(java.util.ResourceBundle) Preferences(java.util.prefs.Preferences) DataStoreType(jgnash.engine.DataStoreType)

Aggregations

JFileChooser (javax.swing.JFileChooser)797 File (java.io.File)571 IOException (java.io.IOException)185 FileFilter (javax.swing.filechooser.FileFilter)109 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)96 ActionEvent (java.awt.event.ActionEvent)59 ActionListener (java.awt.event.ActionListener)48 JButton (javax.swing.JButton)44 JPanel (javax.swing.JPanel)40 Component (java.awt.Component)39 FileOutputStream (java.io.FileOutputStream)39 FileNotFoundException (java.io.FileNotFoundException)30 JMenuItem (javax.swing.JMenuItem)29 Point (java.awt.Point)28 ArrayList (java.util.ArrayList)28 FileWriter (java.io.FileWriter)25 Dimension (java.awt.Dimension)24 PrintWriter (java.io.PrintWriter)24 JLabel (javax.swing.JLabel)22 JFrame (javax.swing.JFrame)21