Example usage for javax.swing JFileChooser setSelectedFile

List of usage examples for javax.swing JFileChooser setSelectedFile

Introduction

In this page you can find the example usage for javax.swing JFileChooser setSelectedFile.

Prototype

@BeanProperty(preferred = true)
public void setSelectedFile(File file) 

Source Link

Document

Sets the selected file.

Usage

From source file:view.CertificatePropertiesDialog.java

private void export(X509Certificate x509c) {
    try {// w w  w  .  j ava2s .  com
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle(Bundle.getBundle().getString("title.saveAs"));
        FileNameExtensionFilter cerFilter = new FileNameExtensionFilter(
                Bundle.getBundle().getString("filter.certificateFiles") + " (*.cer)", "cer");
        fileChooser.setFileFilter(cerFilter);
        File preferedFile = new File(getCertificateCN(x509c) + ".cer");
        fileChooser.setSelectedFile(preferedFile);

        int userSelection = fileChooser.showSaveDialog(this);
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            String dest = fileChooser.getSelectedFile().getAbsolutePath();
            File file = new File(dest);
            byte[] buf = x509c.getEncoded();

            FileOutputStream os = new FileOutputStream(file);
            os.write(buf);
            os.close();

            Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8"));
            wr.write(new sun.misc.BASE64Encoder().encode(buf));
            JOptionPane.showMessageDialog(this, Bundle.getBundle().getString("certSuccessfullyExported"), "",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    } catch (CertificateEncodingException ex) {
        JOptionPane.showMessageDialog(this, Bundle.getBundle().getString("certExportFailed") + "\n"
                + Bundle.getBundle().getString("certInvalidEncoding"), "", JOptionPane.ERROR_MESSAGE);
        //Logger.getLogger(CertificatePropertiesDialog.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, Bundle.getBundle().getString("certExportFailed") + "\n"
                + Bundle.getBundle().getString("noWritePermissions"), "", JOptionPane.ERROR_MESSAGE);
        //Logger.getLogger(CertificatePropertiesDialog.class.getName()).log(Level.SEVERE, null, ex);
        export(x509c);
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(this,
                Bundle.getBundle().getString("certExportFailed") + "\n"
                        + Bundle.getBundle().getString("errorCreatingOutputFile"),
                "", JOptionPane.ERROR_MESSAGE);
        //Logger.getLogger(CertificatePropertiesDialog.class.getName()).log(Level.SEVERE, null, ex);
        export(x509c);
    }
}

From source file:view.MultipleValidationDialog.java

private void writeToFile(String str) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle(Bundle.getBundle().getString("title.saveAs"));
    boolean validPath = false;
    FileNameExtensionFilter pdfFilter = new FileNameExtensionFilter(
            Bundle.getBundle().getString("filter.textFiles") + " (*.txt)", "txt");
    fileChooser.setFileFilter(pdfFilter);
    File preferedFile = new File(Bundle.getBundle().getString("validationReport") + ".txt");
    fileChooser.setSelectedFile(preferedFile);

    while (!validPath) {
        int userSelection = fileChooser.showSaveDialog(this);
        if (userSelection == JFileChooser.CANCEL_OPTION) {
            return;
        }/*from  w ww .j  a  v  a 2  s  . c  om*/
        if (userSelection == JFileChooser.APPROVE_OPTION) {
            String dest = fileChooser.getSelectedFile().getAbsolutePath();
            if (new File(dest).exists()) {
                String msg = Bundle.getBundle().getString("msg.reportFileNameAlreadyExists");
                Object[] options = { Bundle.getBundle().getString("btn.overwrite"),
                        Bundle.getBundle().getString("btn.chooseNewPath"),
                        Bundle.getBundle().getString("btn.cancel") };
                int opt = JOptionPane.showOptionDialog(null, msg, "", JOptionPane.DEFAULT_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                if (opt == JOptionPane.YES_OPTION) {
                    validPath = true;
                } else if (opt == JOptionPane.CANCEL_OPTION) {
                    return;
                }
            } else {
                validPath = true;
            }

            if (validPath) {
                try (PrintStream out = new PrintStream(new FileOutputStream(dest))) {
                    out.print(str);
                    JOptionPane.showMessageDialog(null,
                            Bundle.getBundle().getString("msg.reportSavedSuccessfully"), "",
                            JOptionPane.INFORMATION_MESSAGE);
                } catch (FileNotFoundException ex) {
                    controller.Logger.getLogger().addEntry(ex);
                    JOptionPane.showMessageDialog(null, Bundle.getBundle().getString("msg.reportSaveFailed"),
                            "", JOptionPane.ERROR_MESSAGE);
                }
                break;
            }
        }
    }
}

From source file:view.WorkspacePanel.java

private void signDocument(Document document, boolean ocsp, boolean timestamp) {
    try {// w w w .ja  v a  2  s .com
        if (tempCCAlias.getMainCertificate().getPublicKey().equals(
                CCInstance.getInstance().loadKeyStoreAndAliases().get(0).getMainCertificate().getPublicKey())) {
            try {
                String path1 = document.getDocumentLocation();
                String path2 = null;

                if (path1.endsWith(".pdf")) {
                    path2 = path1.substring(0, path1.length() - 4).concat("(aCCinado).pdf");
                }

                JFileChooser fileChooser = new JFileChooser();
                fileChooser.setDialogTitle(Bundle.getBundle().getString("btn.saveAs"));
                if (null != path2) {
                    boolean validPath = false;
                    FileNameExtensionFilter pdfFilter = new FileNameExtensionFilter(
                            Bundle.getBundle().getString("filter.pdfDocuments") + " (*.pdf)", "pdf");
                    fileChooser.setFileFilter(pdfFilter);
                    File preferedFile = new File(path2);
                    fileChooser.setCurrentDirectory(preferedFile);
                    fileChooser.setSelectedFile(preferedFile);

                    while (!validPath) {
                        int userSelection = fileChooser.showSaveDialog(this);
                        if (userSelection == JFileChooser.CANCEL_OPTION) {
                            return;
                        }
                        if (userSelection == JFileChooser.APPROVE_OPTION) {
                            String dest = fileChooser.getSelectedFile().getAbsolutePath();
                            if (new File(dest).exists()) {
                                String msg = Bundle.getBundle().getString("msg.fileExists");
                                Object[] options = { Bundle.getBundle().getString("opt.replace"),
                                        Bundle.getBundle().getString("opt.chooseNewPath"),
                                        Bundle.getBundle().getString("btn.cancel") };
                                int opt = JOptionPane.showOptionDialog(null, msg, "",
                                        JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,
                                        options[0]);
                                if (opt == JOptionPane.YES_OPTION) {
                                    validPath = true;
                                } else if (opt == JOptionPane.CANCEL_OPTION) {
                                    return;
                                }
                            } else {
                                validPath = true;
                            }

                            signatureSettings.setOcspClient(ocsp);
                            signatureSettings.setTimestamp(timestamp);

                            if (validPath) {
                                if (!CCInstance.getInstance().signPdf(document.getDocumentLocation(), dest,
                                        signatureSettings, null)) {
                                    JOptionPane.showMessageDialog(mainWindow,
                                            Bundle.getBundle().getString("unknownErrorLog"),
                                            Bundle.getBundle().getString("label.signatureFailed"),
                                            JOptionPane.ERROR_MESSAGE);
                                    return;
                                }
                                status = Status.READY;
                                ArrayList<File> list = new ArrayList<>();
                                list.add(new File(document.getDocumentLocation()));
                                int tempPage = imagePanel.getPageNumber();
                                mainWindow.closeDocuments(list, false);
                                mainWindow.loadPdf(new File(dest), false);
                                hideRightPanel();
                                imagePanel.setPageNumber(tempPage);
                                JOptionPane.showMessageDialog(mainWindow,
                                        Bundle.getBundle().getString("label.signatureOk"), "",
                                        JOptionPane.INFORMATION_MESSAGE);
                                break;
                            }
                        }
                    }
                }
                return;
            } catch (IOException ex) {
                if (ex instanceof FileNotFoundException) {
                    JOptionPane.showMessageDialog(mainWindow,
                            Bundle.getBundle().getString("msg.keystoreFileNotFound"),
                            Bundle.getBundle().getString("label.signatureFailed"), JOptionPane.ERROR_MESSAGE);
                    controller.Logger.getLogger().addEntry(ex);
                } else if (ex.getLocalizedMessage().equals(Bundle.getBundle().getString("outputFileError"))) {
                    JOptionPane.showMessageDialog(mainWindow,
                            Bundle.getBundle().getString("msg.failedCreateOutputFile"),
                            Bundle.getBundle().getString("label.signatureFailed"), JOptionPane.ERROR_MESSAGE);
                    controller.Logger.getLogger().addEntry(ex);
                    signDocument(document, ocsp, timestamp);
                } else {
                    JOptionPane.showMessageDialog(mainWindow, Bundle.getBundle().getString("unknownErrorLog"),
                            Bundle.getBundle().getString("label.signatureFailed"), JOptionPane.ERROR_MESSAGE);
                    controller.Logger.getLogger().addEntry(ex);
                }
            } catch (DocumentException | NoSuchAlgorithmException | InvalidAlgorithmParameterException
                    | HeadlessException | CertificateException | KeyStoreException ex) {
                controller.Logger.getLogger().addEntry(ex);
            } catch (SignatureFailedException ex) {
                if (ex.getLocalizedMessage().equals(Bundle.getBundle().getString("timestampFailed"))) {
                    String msg = Bundle.getBundle().getString("msg.timestampFailedNoInternet");
                    Object[] options = { Bundle.getBundle().getString("yes"),
                            Bundle.getBundle().getString("no") };
                    int opt = JOptionPane.showOptionDialog(null, msg, "", JOptionPane.DEFAULT_OPTION,
                            JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
                    if (opt == JOptionPane.YES_OPTION) {
                        signDocument(document, false, false);
                    }
                } else {
                    controller.Logger.getLogger().addEntry(ex);
                }
            }
            return;
        } else {
            JOptionPane.showMessageDialog(mainWindow,
                    Bundle.getBundle().getString("msg.smartcardRemovedOrChanged"),
                    WordUtils.capitalize(Bundle.getBundle().getString("error")), JOptionPane.ERROR_MESSAGE);
        }
    } catch (LibraryNotLoadedException | KeyStoreNotLoadedException | CertificateException | KeyStoreException
            | LibraryNotFoundException | AliasException ex) {
        controller.Logger.getLogger().addEntry(ex);
    }
    JOptionPane.showMessageDialog(mainWindow, Bundle.getBundle().getString("msg.smartcardRemovedOrChanged"),
            WordUtils.capitalize(Bundle.getBundle().getString("error")), JOptionPane.ERROR_MESSAGE);

}