Java Code Examples for javax.swing.JFileChooser#getChoosableFileFilters()

The following examples show how to use javax.swing.JFileChooser#getChoosableFileFilters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MercurialOptionsPanelController.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void onExportFilenameBrowseClick() {
    File oldFile = getExecutableFile();
    JFileChooser fileChooser = new AccessibleJFileChooser(NbBundle.getMessage(MercurialOptionsPanelController.class, "ACSD_ExportBrowseFolder"), oldFile);   // NOI18N
    fileChooser.setDialogTitle(NbBundle.getMessage(MercurialOptionsPanelController.class, "ExportBrowse_title"));                                            // NOI18N
    fileChooser.setMultiSelectionEnabled(false);
    FileFilter[] old = fileChooser.getChoosableFileFilters();
    for (int i = 0; i < old.length; i++) {
        FileFilter fileFilter = old[i];
        fileChooser.removeChoosableFileFilter(fileFilter);
    }
    fileChooser.showDialog(panel, NbBundle.getMessage(MercurialOptionsPanelController.class, "OK_Button"));                                            // NOI18N
    File f = fileChooser.getSelectedFile();
    if (f != null) {
        panel.exportFilenameTextField.setText(f.getAbsolutePath());
    }
}
 
Example 2
Source File: OpenFileAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and initializes a file chooser.
 *
 * @return  the initialized file chooser
 */
protected JFileChooser prepareFileChooser() {
    FileChooserBuilder fcb = new FileChooserBuilder(OpenFileAction.class);
    fcb.setSelectionApprover(new OpenFileSelectionApprover());
    fcb.setFilesOnly(true);
    fcb.addDefaultFileFilters();
    for (OpenFileDialogFilter filter :
            Lookup.getDefault().lookupAll(OpenFileDialogFilter.class)) {
        fcb.addFileFilter(filter);
    }
    JFileChooser chooser = fcb.createFileChooser();
    chooser.setMultiSelectionEnabled(true);
    chooser.getCurrentDirectory().listFiles(); //preload
    chooser.setCurrentDirectory(getCurrentDirectory());
    if (currentFileFilter != null) {
        for (FileFilter ff : chooser.getChoosableFileFilters()) {
            if (currentFileFilter.equals(ff.getDescription())) {
                chooser.setFileFilter(ff);
                break;
            }
        }
    }
    HelpCtx.setHelpIDString(chooser, getHelpCtx().getHelpID());
    return chooser;
}
 
Example 3
Source File: SvnProperties.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void loadFromFile() {
    final JFileChooser chooser = new AccessibleJFileChooser(NbBundle.getMessage(SvnProperties.class, "ACSD_Properties"));
    chooser.setDialogTitle(NbBundle.getMessage(SvnProperties.class, "CTL_Load_Value_Title"));
    chooser.setMultiSelectionEnabled(false);
    javax.swing.filechooser.FileFilter[] fileFilters = chooser.getChoosableFileFilters();
    for (int i = 0; i < fileFilters.length; i++) {
        javax.swing.filechooser.FileFilter fileFilter = fileFilters[i];
        chooser.removeChoosableFileFilter(fileFilter);
    }

    chooser.setCurrentDirectory(roots[0].getParentFile()); // NOI18N
    chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.exists();
        }
        @Override
        public String getDescription() {
            return "";
        }
    });

    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setApproveButtonMnemonic(NbBundle.getMessage(SvnProperties.class, "MNE_LoadValue").charAt(0));
    chooser.setApproveButtonText(NbBundle.getMessage(SvnProperties.class, "CTL_LoadValue"));
    DialogDescriptor dd = new DialogDescriptor(chooser, NbBundle.getMessage(SvnProperties.class, "CTL_Load_Value_Title"));
    dd.setOptions(new Object[0]);
    final Dialog dialog = DialogDisplayer.getDefault().createDialog(dd);

    chooser.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            String state = e.getActionCommand();
            if (state.equals(JFileChooser.APPROVE_SELECTION)) {
                File source = chooser.getSelectedFile();

                if (Utils.isFileContentText(source)) {
                    if (source.canRead()) {
                        StringWriter sw = new StringWriter();
                        try {
                            Utils.copyStreamsCloseAll(sw, new FileReader(source));
                            panel.txtAreaValue.setText(sw.toString());
                        } catch (IOException ex) {
                            Subversion.LOG.log(Level.SEVERE, null, ex);
                        }
                    }
                } else {
                    handleBinaryFile(source);
                }
            }
            dialog.dispose();
        }
    });
    dialog.setVisible(true);

}
 
Example 4
Source File: ExportDiffSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private JFileChooser createFileChooser(File curentDir) {
    final JFileChooser chooser = new AccessibleJFileChooser(NbBundle.getMessage(ExportDiffSupport.class, "ACSD_Export"));
    chooser.setDialogTitle(NbBundle.getMessage(ExportDiffSupport.class, "CTL_Export_Title"));
    chooser.setMultiSelectionEnabled(false);
    javax.swing.filechooser.FileFilter[] old = chooser.getChoosableFileFilters();
    for (int i = 0; i < old.length; i++) {
        javax.swing.filechooser.FileFilter fileFilter = old[i];
        chooser.removeChoosableFileFilter(fileFilter);

    }
    chooser.setCurrentDirectory(curentDir); // NOI18N
    chooser.addChoosableFileFilter(getFileFilter());

    chooser.setDialogType(JFileChooser.SAVE_DIALOG);
    chooser.setApproveButtonMnemonic(NbBundle.getMessage(ExportDiffSupport.class, "MNE_Export_ExportAction").charAt(0));
    chooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String state = e.getActionCommand();
            if (state.equals(JFileChooser.APPROVE_SELECTION)) {
                File destination = chooser.getSelectedFile();
                destination = getTargetFile(destination);
                if (destination.exists()) {
                    NotifyDescriptor nd = new NotifyDescriptor.Confirmation(NbBundle.getMessage(ExportDiffSupport.class, "BK3005", destination.getAbsolutePath()));
                    nd.setOptionType(NotifyDescriptor.YES_NO_OPTION);
                    DialogDisplayer.getDefault().notify(nd);
                    if (nd.getValue().equals(NotifyDescriptor.OK_OPTION) == false) {
                        return;
                    }
                }
                preferences.put("ExportDiff.saveFolder", destination.getParent()); // NOI18N
                panel.setOutputFileText(destination.getAbsolutePath());
            } else {
                dd.setValue(null);
            }
            if(dialog != null) {
                dialog.dispose();
            }
        }
    });
    return chooser;
}