Example usage for javax.swing.filechooser FileNameExtensionFilter FileNameExtensionFilter

List of usage examples for javax.swing.filechooser FileNameExtensionFilter FileNameExtensionFilter

Introduction

In this page you can find the example usage for javax.swing.filechooser FileNameExtensionFilter FileNameExtensionFilter.

Prototype

public FileNameExtensionFilter(String description, String... extensions) 

Source Link

Document

Creates a FileNameExtensionFilter with the specified description and file name extensions.

Usage

From source file:uk.ac.liverpool.narrative.SolutionGraphics.java

@Override
public void run() {
    // set up action cost filter
    if (actionCostsFilePath != null && (new File(actionCostsFilePath)).exists()) {
        CharacterActionSolutionFilter tc = new CharacterActionSolutionFilter();
        if (actionCostsFilePath != null && (new File(actionCostsFilePath)).exists()) {
            tc.setActionCosts(BranchingStoryGenerator.readCosts(actionCostsFilePath,
                    BranchingStoryGenerator.actionCostType));
        }/*from  w ww  .j a va2s  . com*/
        if (characterCostsFilePath != null && (new File(characterCostsFilePath)).exists()) {
            tc.setCharacterCosts(BranchingStoryGenerator.readCosts(characterCostsFilePath,
                    BranchingStoryGenerator.characterCostType));
        }
        if (authorDirection != null && (new File(authorDirection)).exists()) {
            tc.setAuthorDirection(
                    BranchingStoryGenerator.readCosts(authorDirection, BranchingStoryGenerator.authorCostType)
                            .get(BranchingStoryGenerator.authorCostType));
        }
        sg.filters.add(tc);
        tc.setThreshold(threshold);
    }
    if (character != null) {
        charFilter = new CharacterFilter(character);
    }

    if (fc) {
        JFileChooser fc = new JFileChooser(BranchingStoryGenerator.DestinationFolder);
        fc.setAccessory(new SolutionPreview(fc));
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Solution files", "sol");
        fc.setFileFilter(filter);
        fc.setMultiSelectionEnabled(true);
        fc.showOpenDialog(vvv);
        File[] files = fc.getSelectedFiles();
        for (File f : files) {
            try {
                sg.readSolutionFile(f);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    } else {
        for (File f : BranchingStoryGenerator.DestinationFolder.listFiles()) {
            if (!f.isFile() || !f.getName().endsWith(".sol")) {
                continue;
            }
            try {
                sg.readSolutionFile(f);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    System.out.println("done");

}

From source file:uk.chromis.pos.imports.JPanelCSVImport.java

private void jbtnDbDriverLibActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnDbDriverLibActionPerformed
    resetFields();/*from ww  w  .j  a v  a2 s .  co  m*/
    setWorker();
    // If CSV.last_file is null then use c:\ otherwise use saved dir
    JFileChooser chooser = new JFileChooser(last_folder == null ? "C:\\" : last_folder);
    FileNameExtensionFilter filter = new FileNameExtensionFilter("csv files", "csv");
    chooser.setFileFilter(filter);
    chooser.showOpenDialog(null);
    File csvFile = chooser.getSelectedFile();
    // check if a file was selected        
    if (csvFile == null) {
        return;
    }

    File current_folder = chooser.getCurrentDirectory();
    // If we have a file lets save the directory for later use if it's different from the old
    if (last_folder == null || !last_folder.equals(current_folder.getAbsolutePath())) {
        AppConfig.getInstance().setProperty("CSV.last_folder", current_folder.getAbsolutePath());
        last_folder = current_folder.getAbsolutePath();
        try {
            AppConfig.getInstance().save();
        } catch (IOException ex) {
            Logger.getLogger(JPanelCSVImport.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    String csv = csvFile.getName();
    if (!(csv.trim().equals(""))) {
        csvFileName = csvFile.getAbsolutePath();
        jFileName.setText(csvFileName);
    }
}

From source file:updater.UpdaterGUI.java

@SuppressWarnings("resource")
public UpdaterGUI() {
    try {//  w w w  .j a  v  a  2 s. com
        URL url1 = new URL(
                "https://raw.githubusercontent.com/kvsjxd/Droid-PC-Suite/master/.release-version.txt");
        ReadableByteChannel obj1 = Channels.newChannel(url1.openStream());
        FileOutputStream outputstream1 = new FileOutputStream(".release-version.txt");
        outputstream1.getChannel().transferFrom(obj1, 0, Long.MAX_VALUE);
        URL url2 = new URL(
                "https://raw.githubusercontent.com/kvsjxd/Droid-PC-Suite/master/.release-changelog.txt");
        ReadableByteChannel obj2 = Channels.newChannel(url2.openStream());
        FileOutputStream outputstream2 = new FileOutputStream(".release-changelog.txt");
        outputstream2.getChannel().transferFrom(obj2, 0, Long.MAX_VALUE);
        FileReader file = new FileReader(".release-version.txt");
        BufferedReader reader = new BufferedReader(file);
        String DownloadedString = reader.readLine();
        File file2 = new File(".release-version.txt");
        if (file2.exists() && !file2.isDirectory()) {
            file2.delete();
        }
        AvailableUpdate = Double.parseDouble(DownloadedString);
        InputStreamReader reader2 = new InputStreamReader(
                getClass().getResourceAsStream("/others/app-version.txt"));
        String tmp = IOUtils.toString(reader2);
        ApplicationVersion = Double.parseDouble(tmp);
    } catch (Exception e) {
        e.printStackTrace();
    }
    setIconImage(Toolkit.getDefaultToolkit().getImage(UpdaterGUI.class.getResource("/graphics/Icon.png")));
    setResizable(false);
    setType(Type.UTILITY);
    setTitle("Updater");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 430, 415);
    contentPane = new JPanel();
    contentPane.setBackground(Color.WHITE);
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblApplicationVersion = new JLabel("App Version: v" + ApplicationVersion);
    lblApplicationVersion.setBounds(12, 12, 222, 15);
    contentPane.add(lblApplicationVersion);

    JLabel lblUpdateVersion = new JLabel("Update Version: v" + AvailableUpdate);
    lblUpdateVersion.setBounds(12, 30, 222, 15);
    contentPane.add(lblUpdateVersion);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(0, 51, 422, 281);
    contentPane.add(scrollPane);

    JTextArea UpdateChangelogViewer = new JTextArea();
    scrollPane.setViewportView(UpdateChangelogViewer);

    JButton btnDownload = new JButton("Download");
    btnDownload.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JFrame parentFrame = new JFrame();
            JFileChooser fileChooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Zip Files", "zip");
            fileChooser.setFileFilter(filter);
            fileChooser.setDialogTitle("Save as");
            int userSelection = fileChooser.showSaveDialog(parentFrame);
            if (userSelection == JFileChooser.APPROVE_OPTION) {
                File fileToSave = fileChooser.getSelectedFile();
                try {
                    URL url = new URL("https://github.com/kvsjxd/Droid-PC-Suite/releases/download/"
                            + AvailableUpdate + "/DPCS.v" + AvailableUpdate + ".Stable.zip");
                    ReadableByteChannel obj = Channels.newChannel(url.openStream());
                    FileOutputStream outputstream = new FileOutputStream(fileToSave.getAbsolutePath() + ".zip");
                    outputstream.getChannel().transferFrom(obj, 0, Long.MAX_VALUE);
                    JOptionPane.showMessageDialog(null,
                            "Download complete!\nPlease delete this version and extract the downloaded zip\nwhich is saved at "
                                    + fileToSave.getAbsolutePath() + ".zip");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
    btnDownload.setBounds(140, 344, 117, 25);
    contentPane.add(btnDownload);
    try {
        FileReader reader3 = new FileReader(new File(".release-changelog.txt"));
        UpdateChangelogViewer.read(reader3, "");
        File file3 = new File(".release-changelog.txt");
        if (file3.exists() && !file3.isDirectory()) {
            file3.delete();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:view.CertificatePropertiesDialog.java

private void export(X509Certificate x509c) {
    try {//from  w  w w  .ja  va2 s.c  om
        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.GraphicalView.GameConfiguration.ChooseCircuitfileChooser.java

public ChooseCircuitfileChooser(Game game) {
    FileFilter filter = new FileNameExtensionFilter("tar.gz", ".tar.gz");
    this.setFileFilter(filter);
    this.game = game;
    this.addActionListener(this);
}

From source file:view.MainWindow.java

private void menuItemOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemOpenActionPerformed
    JFileChooser jfc = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            Bundle.getBundle().getString("filter.pdfDocuments") + " (*.pdf)", "pdf");
    File path;/* ww w.  ja  v  a2  s. c om*/
    if (lastOpenedFilePath == null) {
        path = new File(System.getProperty("user.home"));
    } else {
        path = lastOpenedFilePath;
    }
    jfc.setCurrentDirectory(path);
    jfc.setFileFilter(filter);
    int ret = jfc.showOpenDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = jfc.getSelectedFile();
        try {
            if (loadPdf(file, true)) {
                lastOpenedFilePath = file;
            }
        } catch (Exception e) {
            controller.Logger.getLogger().addEntry(e);
        }
    }
}

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  w  w.j  av 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 {/*from ww  w. j a v a  2  s .  c om*/
        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);

}

From source file:vista.promocion.DiagPromocion.java

private void importar() {
    JFileChooser jChooser = new JFileChooser(System.getProperty("user.dir"));
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos Excel", "xls");
    jChooser.setFileFilter(filter);//from w  w w  .j  av a  2 s . c om
    jChooser.showOpenDialog(this);
    File file = jChooser.getSelectedFile();
    if (file == null || !file.getName().endsWith("xls")) {
        JOptionPane.showMessageDialog(null, "Please select only Excel file.", "Error",
                JOptionPane.ERROR_MESSAGE);
    } else {
        leerDatosExcel(file);
        List<Integer> columnasNoEditables = new ArrayList<>();
        columnasNoEditables.add(0);
        columnasNoEditables.add(1);
        columnasNoEditables.add(2);
        ModeloTablaNoEditable model = new ModeloTablaNoEditable(data, headers, columnasNoEditables);
        tbArticulos.setModel(model);
    }
}