Example usage for javax.swing JFileChooser getAcceptAllFileFilter

List of usage examples for javax.swing JFileChooser getAcceptAllFileFilter

Introduction

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

Prototype

@BeanProperty(bound = false)
public FileFilter getAcceptAllFileFilter() 

Source Link

Document

Returns the AcceptAll file filter.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    JFileChooser chooser = new JFileChooser();
    File f = new File(new File(".").getCanonicalPath());
    chooser.setCurrentDirectory(f);// w w  w  . ja va  2  s . com
    chooser.setCurrentDirectory(null);
    chooser.showOpenDialog(null);
    FileFilter obj = chooser.getAcceptAllFileFilter();
}

From source file:net.sf.jabref.importer.ImportFormats.java

private static JFileChooser createImportFileChooser(String currentDir) {

    SortedSet<ImportFormat> importers = Globals.IMPORT_FORMAT_READER.getImportFormats();

    String lastUsedFormat = Globals.prefs.get(JabRefPreferences.LAST_USED_IMPORT);
    FileFilter defaultFilter = null;
    JFileChooser fc = new JFileChooser(currentDir);
    Set<ImportFileFilter> filters = new TreeSet<>();
    for (ImportFormat format : importers) {
        ImportFileFilter filter = new ImportFileFilter(format);
        filters.add(filter);/*w w  w  .  j a  v  a2s .  c  om*/
        if (format.getFormatName().equals(lastUsedFormat)) {
            defaultFilter = filter;
        }
    }
    for (ImportFileFilter filter : filters) {
        fc.addChoosableFileFilter(filter);
    }

    if (defaultFilter == null) {
        fc.setFileFilter(fc.getAcceptAllFileFilter());
    } else {
        fc.setFileFilter(defaultFilter);
    }
    return fc;
}

From source file:org.pgptool.gui.ui.tools.browsefs.SaveFileChooserDialog.java

private String enforceExtension(String filePathName, JFileChooser ofd) {
    FileFilter fileExtFilter = ofd.getFileFilter();
    if (fileExtFilter == ofd.getAcceptAllFileFilter()) {
        return filePathName;
    }/*from   w w  w . j ava  2 s  . c  om*/
    FileNameExtensionFilter fileNameExtensionFilter = (FileNameExtensionFilter) fileExtFilter;
    String ext = fileNameExtensionFilter.getExtensions()[0];
    if (!ext.equalsIgnoreCase(FilenameUtils.getExtension(filePathName))) {
        filePathName += "." + ext;
    }
    return filePathName;
}

From source file:org.pgptool.gui.ui.importkey.KeyImporterPm.java

public MultipleFilesChooserDialog getSourceFileChooser() {
    if (sourceFileChooser == null) {
        sourceFileChooser = new MultipleFilesChooserDialog(findRegisteredWindowIfAny(), appProps,
                BROWSE_FOLDER) {/*from  w ww.  j  a va 2  s .co m*/
            @Override
            protected void doFileChooserPostConstruct(JFileChooser ofd) {
                super.doFileChooserPostConstruct(ofd);
                ofd.setDialogTitle(Messages.get("action.importKey"));

                ofd.setAcceptAllFileFilterUsed(false);
                ofd.addChoosableFileFilter(keyFilesFilter);
                ofd.addChoosableFileFilter(ofd.getAcceptAllFileFilter());
                ofd.setFileFilter(ofd.getChoosableFileFilters()[0]);
            }

            private FileFilter keyFilesFilter = new FileFilter() {
                @Override
                public boolean accept(File f) {
                    if (f.isDirectory() || !f.isFile()) {
                        return true;
                    }
                    if (!isExtension(f.getName(), EXTENSIONS)) {
                        return false;
                    }

                    // NOTE: Although it gives best results -- I have a
                    // slight concern that this might be too heavy operation
                    // to perform thorough -- check for each file
                    // contents. My hope is that since we're checking only
                    // key files it shouldn't be a problem. Non-key files
                    // with same xtensions will not take a long time to fail
                    try {
                        Key readKey = keyFilesOperations.readKeyFromFile(f.getAbsolutePath());
                        Preconditions.checkState(readKey != null, "Key wasn't parsed");

                        Key existingKey = keyRingService.findKeyById(readKey.getKeyInfo().getKeyId());
                        if (existingKey == null) {
                            return true;
                        }
                        if (!existingKey.getKeyData().isCanBeUsedForDecryption()
                                && readKey.getKeyData().isCanBeUsedForDecryption()) {
                            return true;
                        }
                        return false;
                    } catch (Throwable t) {
                        // in this case it's not an issue. So it's debug
                        // level
                        log.debug(
                                "File is not accepte for file chooser becasue was not able to read it as a key",
                                t);
                    }

                    return false;
                }

                private boolean isExtension(String fileName, String[] extensions) {
                    String extension = FilenameUtils.getExtension(fileName);
                    if (!StringUtils.hasText(extension)) {
                        return false;
                    }

                    for (String ext : extensions) {
                        if (ext.equalsIgnoreCase(extension)) {
                            return true;
                        }
                    }
                    return false;
                }

                @Override
                public String getDescription() {
                    return "Key files (.asc, .bpg)";
                }
            };
        };
    }
    return sourceFileChooser;
}

From source file:com.sf.energy.transfer.form.DataConfiguration.java

/**
 * ??//from  w ww .j a va2  s . c  o  m
 * @param evt ?
 */
private void scanActionPerformed(java.awt.event.ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    File directory = new File(dir);
    // ??
    fc.setCurrentDirectory(directory);
    fc.setDialogTitle("");
    // 
    FileNameExtensionFilter filter1 = new FileNameExtensionFilter(".xls", "xls");
    fc.addChoosableFileFilter(filter1);
    FileNameExtensionFilter filter2 = new FileNameExtensionFilter(".dmp", "dmp");
    fc.addChoosableFileFilter(filter1);
    // 
    fc.setFileFilter(fc.getAcceptAllFileFilter());
    int intRetVal = fc.showOpenDialog(new JFrame());
    // ?
    if (intRetVal == JFileChooser.APPROVE_OPTION) {
        // ?
        dir = fc.getSelectedFile().getPath();
        // 
        filePath.setText(dir);
    }

}

From source file:com.att.aro.ui.model.diagnostic.GraphPanelHelper.java

public void SaveImageAs(JViewport pane, String graphPanelSaveDirectory) {

    JFileChooser fc = new JFileChooser(graphPanelSaveDirectory);

    // Set up file types
    String[] fileTypesJPG = new String[2];
    String fileDisplayTypeJPG = ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.jpeg");
    fileTypesJPG[0] = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpeg");
    fileTypesJPG[1] = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpg");
    FileFilter filterJPG = new ExtensionFileFilter(fileDisplayTypeJPG, fileTypesJPG);

    fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
    String[] fileTypesPng = new String[1];
    String fileDisplayTypePng = ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.png");
    fileTypesPng[0] = ResourceBundleHelper.getMessageString("fileChooser.contentType.png");
    FileFilter filterPng = new ExtensionFileFilter(fileDisplayTypePng, fileTypesPng);
    fc.addChoosableFileFilter(filterPng);
    fc.setFileFilter(filterJPG);// w w w.  j  a  v  a2  s  .com
    File plotImageFile = null;

    boolean bSavedOrCancelled = false;
    while (!bSavedOrCancelled) {
        if (fc.showSaveDialog(pane) == JFileChooser.APPROVE_OPTION) {
            String strFile = fc.getSelectedFile().toString();
            String strFileLowerCase = strFile.toLowerCase();
            String fileDesc = fc.getFileFilter().getDescription();
            String fileType = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpg");
            if ((fileDesc.equalsIgnoreCase(
                    ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.png"))
                    || strFileLowerCase.endsWith(ResourceBundleHelper.getMessageString("fileType.filters.dot")
                            + fileTypesPng[0].toLowerCase()))) {
                fileType = fileTypesPng[0];
            }
            if (strFile.length() > 0) {
                // Save current directory
                graphPanelSaveDirectory = fc.getCurrentDirectory().getPath();

                if ((fileType != null) && (fileType.length() > 0)) {
                    String fileTypeLowerCaseWithDot = ResourceBundleHelper
                            .getMessageString("fileType.filters.dot") + fileType.toLowerCase();
                    if (!strFileLowerCase.endsWith(fileTypeLowerCaseWithDot)) {
                        strFile += ResourceBundleHelper.getMessageString("fileType.filters.dot") + fileType;
                    }
                }
                plotImageFile = new File(strFile);
                boolean bAttemptToWriteToFile = true;
                if (plotImageFile.exists()) {
                    if (MessageDialogFactory.showConfirmDialog(pane,
                            MessageFormat.format(
                                    ResourceBundleHelper.getMessageString("fileChooser.fileExists"),
                                    plotImageFile.getAbsolutePath()),
                            ResourceBundleHelper.getMessageString("fileChooser.confirm"),
                            JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                        bAttemptToWriteToFile = false;
                    }
                }
                if (bAttemptToWriteToFile) {
                    try {
                        if (fileType != null && fileType.equalsIgnoreCase(fileTypesPng[0])) {
                            BufferedImage bufImage = ImageHelper.createImage(pane.getBounds().width,
                                    pane.getBounds().height);
                            Graphics2D g = bufImage.createGraphics();
                            pane.paint(g);
                            ImageIO.write(bufImage, "png", plotImageFile);
                        } else {
                            BufferedImage bufImage = ImageHelper.createImage(pane.getBounds().width,
                                    pane.getBounds().height);
                            Graphics2D g = bufImage.createGraphics();
                            pane.paint(g);
                            ImageIO.write(bufImage, "jpg", plotImageFile);
                        }
                        bSavedOrCancelled = true;
                    } catch (IOException e) {
                        MessageDialogFactory.showMessageDialog(pane, ResourceBundleHelper
                                .getMessageString("fileChooser.errorWritingToFile" + plotImageFile.toString()));
                    }
                }
            }
        } else {
            bSavedOrCancelled = true;
        }
    }
}

From source file:org.pgptool.gui.ui.encryptone.EncryptOnePm.java

public ExistingFileChooserDialog getSourceFileChooser() {
    if (sourceFileChooser == null) {
        sourceFileChooser = new ExistingFileChooserDialog(findRegisteredWindowIfAny(), appProps,
                SOURCE_FOLDER) {/*from ww w . j  a v  a 2 s.c  om*/
            @Override
            protected void doFileChooserPostConstruct(JFileChooser ofd) {
                super.doFileChooserPostConstruct(ofd);
                ofd.setDialogTitle(Messages.get("phrase.selectFileToEncrypt"));

                ofd.setAcceptAllFileFilterUsed(false);
                ofd.addChoosableFileFilter(notEncryptedFiles);
                ofd.addChoosableFileFilter(ofd.getAcceptAllFileFilter());
                ofd.setFileFilter(ofd.getChoosableFileFilters()[0]);
            }

            private FileFilter notEncryptedFiles = new FileFilter() {
                @Override
                public boolean accept(File f) {
                    return !DecryptOnePm.isItLooksLikeYourSourceFile(f.getAbsolutePath());
                }

                @Override
                public String getDescription() {
                    return text("phrase.allExceptEncrypted");
                }
            };
        };
    }
    return sourceFileChooser;
}

From source file:org.pgptool.gui.ui.decryptone.DecryptOnePm.java

public ExistingFileChooserDialog getSourceFileChooser() {
    if (sourceFileChooser == null) {
        sourceFileChooser = new ExistingFileChooserDialog(findRegisteredWindowIfAny(), appProps,
                SOURCE_FOLDER) {// w ww  . j a  v a2  s.  co m
            @Override
            protected void doFileChooserPostConstruct(JFileChooser ofd) {
                super.doFileChooserPostConstruct(ofd);

                ofd.setAcceptAllFileFilterUsed(false);
                ofd.addChoosableFileFilter(
                        new FileNameExtensionFilter("Encrypted files (.gpg, .pgp, .asc)", EXTENSIONS));
                ofd.addChoosableFileFilter(ofd.getAcceptAllFileFilter());
                ofd.setFileFilter(ofd.getChoosableFileFilters()[0]);

                ofd.setDialogTitle(text("phrase.selectFileToDecrypt"));
            }

            @Override
            protected String handleFileWasChosen(String filePathName) {
                if (filePathName == null) {
                    return null;
                }
                sourceFile.setValueByOwner(filePathName);
                return filePathName;
            }
        };
    }
    return sourceFileChooser;
}

From source file:org.pgptool.gui.ui.encryptone.EncryptOnePm.java

public SaveFileChooserDialog getTargetFileChooser() {
    if (targetFileChooser == null) {
        targetFileChooser = new SaveFileChooserDialog(findRegisteredWindowIfAny(), "action.chooseTargetFile",
                "action.choose", appProps, "EncryptionTargetChooser") {
            @Override//from w w w .j a v  a  2  s.c o m
            protected String onDialogClosed(String filePathName, JFileChooser ofd) {
                String ret = super.onDialogClosed(filePathName, ofd);
                if (ret != null) {
                    targetFile.setValueByOwner(ret);
                }
                return ret;
            }

            @Override
            protected void onFileChooserPostConstrct(JFileChooser ofd) {
                ofd.setAcceptAllFileFilterUsed(false);
                ofd.addChoosableFileFilter(new FileNameExtensionFilter("GPG Files (.pgp)", "pgp"));
                // NOTE: Should we support other extensions?....
                ofd.addChoosableFileFilter(ofd.getAcceptAllFileFilter());
                ofd.setFileFilter(ofd.getChoosableFileFilters()[0]);
            }

            @Override
            protected void suggestTarget(JFileChooser ofd) {
                String sourceFileStr = sourceFile.getValue();
                if (StringUtils.hasText(targetFile.getValue())) {
                    use(ofd, targetFile.getValue());
                } else if (encryptionDialogParameters != null
                        && encryptionDialogParameters.getTargetFile() != null) {
                    if (encryptionDialogParameters.getSourceFile().equals(sourceFile.getValue())) {
                        use(ofd, encryptionDialogParameters.getTargetFile());
                    } else {
                        use(ofd, madeUpTargetFileName(sourceFile.getValue(), FilenameUtils
                                .getFullPathNoEndSeparator(encryptionDialogParameters.getTargetFile())));
                    }
                } else if (StringUtils.hasText(sourceFileStr) && new File(sourceFileStr).exists()) {
                    String basePath = FilenameUtils.getFullPathNoEndSeparator(sourceFileStr);
                    ofd.setCurrentDirectory(new File(basePath));
                    ofd.setSelectedFile(new File(madeUpTargetFileName(sourceFileStr, basePath)));
                }
            }

            private void use(JFileChooser ofd, String filePathName) {
                ofd.setCurrentDirectory(new File(FilenameUtils.getFullPathNoEndSeparator(filePathName)));
                ofd.setSelectedFile(new File(filePathName));
            }
        };
    }
    return targetFileChooser;
}

From source file:com.att.aro.diagnostics.GraphPanel.java

/**
 * Implements the saving of the graph snapshot.
 *//*from  ww  w  . ja v a 2 s  .c  o  m*/
private void saveAs() throws IOException {

    // Determine save directory
    File saveDir = traceData.getTraceDir();
    if (graphPanelSaveDirectory != null) {
        saveDir = new File(graphPanelSaveDirectory);
    }
    JFileChooser fc = new JFileChooser(saveDir);

    // Set up file types
    String[] fileTypesJPG = new String[2];
    String fileDisplayTypeJPG = rb.getString("fileChooser.contentDisplayType.jpeg");
    fileTypesJPG[0] = rb.getString("fileChooser.contentType.jpeg");
    fileTypesJPG[1] = rb.getString("fileChooser.contentType.jpg");
    FileFilter filterJPG = new ExtensionFileFilter(fileDisplayTypeJPG, fileTypesJPG);

    fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
    String[] fileTypesPng = new String[1];
    String fileDisplayTypePng = rb.getString("fileChooser.contentDisplayType.png");
    fileTypesPng[0] = rb.getString("fileChooser.contentType.png");
    FileFilter filterPng = new ExtensionFileFilter(fileDisplayTypePng, fileTypesPng);
    fc.addChoosableFileFilter(filterPng);
    fc.setFileFilter(filterJPG);
    File plotImageFile = null;

    boolean bSavedOrCancelled = false;
    while (!bSavedOrCancelled) {
        if (fc.showSaveDialog(this.getTopLevelAncestor()) == JFileChooser.APPROVE_OPTION) {
            String strFile = fc.getSelectedFile().toString();
            String strFileLowerCase = strFile.toLowerCase();
            String fileDesc = fc.getFileFilter().getDescription();
            String fileType = rb.getString("fileChooser.contentType.jpg");
            if ((fileDesc.equalsIgnoreCase(rb.getString("fileChooser.contentDisplayType.png"))
                    || strFileLowerCase
                            .endsWith(rb.getString("fileType.filters.dot") + fileTypesPng[0].toLowerCase()))) {
                fileType = fileTypesPng[0];
            }
            if (strFile.length() > 0) {
                // Save current directory
                graphPanelSaveDirectory = fc.getCurrentDirectory().getPath();

                if ((fileType != null) && (fileType.length() > 0)) {
                    String fileTypeLowerCaseWithDot = rb.getString("fileType.filters.dot")
                            + fileType.toLowerCase();
                    if (!strFileLowerCase.endsWith(fileTypeLowerCaseWithDot)) {
                        strFile += rb.getString("fileType.filters.dot") + fileType;
                    }
                }
                plotImageFile = new File(strFile);
                boolean bAttemptToWriteToFile = true;
                if (plotImageFile.exists()) {
                    if (MessageDialogFactory.showConfirmDialog(this,
                            MessageFormat.format(rb.getString("fileChooser.fileExists"),
                                    plotImageFile.getAbsolutePath()),
                            rb.getString("fileChooser.confirm"),
                            JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                        bAttemptToWriteToFile = false;
                    }
                }
                if (bAttemptToWriteToFile) {
                    try {
                        if (fileType.equalsIgnoreCase(fileTypesPng[0])) {
                            BufferedImage bufImage = createImage(pane);
                            ImageIO.write(bufImage, "png", plotImageFile);
                        } else {
                            BufferedImage bufImage = createImage(pane);
                            ImageIO.write(bufImage, "jpg", plotImageFile);
                        }
                        bSavedOrCancelled = true;
                    } catch (IOException e) {
                        MessageDialogFactory.showMessageDialog(this,
                                rb.getString("fileChooser.errorWritingToFile" + plotImageFile.toString()));
                    }
                }
            }
        } else {
            bSavedOrCancelled = true;
        }
    }
}