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:org.yccheok.jstock.gui.Utils.java

public static File promptOpenCSVAndExcelJFileChooser() {
    final FileNameExtensionFilter csvFilter = new FileNameExtensionFilter("CSV Documents (*.csv)", "csv");
    final FileNameExtensionFilter xlsFilter = new FileNameExtensionFilter("Microsoft Excel (*.xls)", "xls");
    return promptOpenJFileChooser(csvFilter, xlsFilter);
}

From source file:org.yccheok.jstock.gui.Utils.java

public static File promptOpenZippedJFileChooser() {
    final FileNameExtensionFilter zippedFilter = new FileNameExtensionFilter("Zipped Files (*.zip)", "zip");
    return promptOpenJFileChooser(zippedFilter);
}

From source file:org.yccheok.jstock.gui.Utils.java

public static File promptSaveZippedJFileChooser(String suggestedFileName) {
    final FileNameExtensionFilter zippedFilter = new FileNameExtensionFilter("Zipped Files (*.zip)", "zip");
    return promptSaveJFileChooser(suggestedFileName, zippedFilter);
}

From source file:org.yccheok.jstock.gui.Utils.java

public static File promptSaveCSVAndExcelJFileChooser(String suggestedFileName) {
    final FileNameExtensionFilter csvFilter = new FileNameExtensionFilter("CSV Documents (*.csv)", "csv");
    final FileNameExtensionFilter xlsFilter = new FileNameExtensionFilter("Microsoft Excel (*.xls)", "xls");
    return promptSaveJFileChooser(suggestedFileName, csvFilter, xlsFilter);
}

From source file:parser.esperanto.gui.FileInputPanel.java

private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenActionPerformed
    JFileChooser fileChooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Szvegfjl (*.txt)", "txt");
    fileChooser.addChoosableFileFilter(filter);
    fileChooser.setCurrentDirectory(new File("./src/main/resources/text/"));

    int result = fileChooser.showOpenDialog(this);
    if (result == JFileChooser.APPROVE_OPTION) {
        String path = fileChooser.getSelectedFile().getAbsolutePath();
        try {//from ww  w . j a  v  a2s  . c om
            readFile(path);
            setOutput();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "A fjl nem tallhat");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, "Beolvassi hiba");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }
}

From source file:pcgen.gui2.dialog.ExportDialog.java

private void export(boolean pdf) {
    UIPropertyContext context = UIPropertyContext.createContext("ExportDialog");
    final JFileChooser fcExport = new JFileChooser();
    fcExport.setFileSelectionMode(JFileChooser.FILES_ONLY);
    File baseDir = null;/*from   w w  w .j  a va 2s  . c  o  m*/
    {
        String path;
        if (pdf) {
            path = context.getProperty(PDF_EXPORT_DIR_PROP);
        } else {
            path = context.getProperty(HTML_EXPORT_DIR_PROP);
        }
        if (path != null) {
            baseDir = new File(path);
        }
    }
    if (baseDir == null || !baseDir.isDirectory()) {
        baseDir = SystemUtils.getUserHome();
    }
    fcExport.setCurrentDirectory(baseDir);

    URI uri = fileList.getSelectedValue();
    String extension = ExportUtilities.getOutputExtension(uri.toString(), pdf);
    if (pdf) {
        FileFilter fileFilter = new FileNameExtensionFilter("PDF Documents (*.pdf)", "pdf");
        fcExport.addChoosableFileFilter(fileFilter);
        fcExport.setFileFilter(fileFilter);
    } else if ("htm".equalsIgnoreCase(extension) || "html".equalsIgnoreCase(extension)) {
        FileFilter fileFilter = new FileNameExtensionFilter("HTML Documents (*.htm, *.html)", "htm", "html");
        fcExport.addChoosableFileFilter(fileFilter);
        fcExport.setFileFilter(fileFilter);
    } else if ("xml".equalsIgnoreCase(extension)) {
        FileFilter fileFilter = new FileNameExtensionFilter("XML Documents (*.xml)", "xml");
        fcExport.addChoosableFileFilter(fileFilter);
        fcExport.setFileFilter(fileFilter);
    } else {
        String desc = extension + " Files (*." + extension + ")";
        fcExport.addChoosableFileFilter(new FileNameExtensionFilter(desc, extension));
    }
    String name;
    File path;
    if (!partyBox.isSelected()) {
        CharacterFacade character = (CharacterFacade) characterBox.getSelectedItem();
        path = character.getFileRef().get();
        if (path != null) {
            path = path.getParentFile();
        } else {
            path = new File(PCGenSettings.getPcgDir());
        }
        name = character.getTabNameRef().get();
        if (StringUtils.isEmpty(name)) {
            name = character.getNameRef().get();
        }
    } else {
        path = new File(PCGenSettings.getPcgDir());
        name = "Entire Party";
    }
    if (pdf) {
        fcExport.setSelectedFile(new File(path, name + ".pdf"));
    } else {
        fcExport.setSelectedFile(new File(path, name + "." + extension));
    }
    fcExport.setDialogTitle("Export " + name);
    if (fcExport.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) {
        return;
    }

    final File outFile = fcExport.getSelectedFile();
    if (pdf) {
        context.setProperty(PDF_EXPORT_DIR_PROP, outFile.getParent());
    } else {
        context.setProperty(HTML_EXPORT_DIR_PROP, outFile.getParent());
    }

    if (StringUtils.isEmpty(outFile.getName())) {
        pcgenFrame.showErrorMessage("PCGen", "You must set a filename.");
        return;
    }

    if (outFile.isDirectory()) {
        pcgenFrame.showErrorMessage("PCGen", "You cannot overwrite a directory with a file.");
        return;
    }

    if (outFile.exists() && !SettingsHandler.getAlwaysOverwrite()) {
        int reallyClose = JOptionPane.showConfirmDialog(this,
                "The file " + outFile.getName() + " already exists, are you sure you want to overwrite it?",
                "Confirm overwriting " + outFile.getName(), JOptionPane.YES_NO_OPTION);

        if (reallyClose != JOptionPane.YES_OPTION) {
            return;
        }
    }
    if (pdf) {
        new PDFExporter(outFile, extension, name).execute();
    } else {
        if (!printToFile(outFile)) {
            String message = "The character export failed. Please see the log for details.";
            pcgenFrame.showErrorMessage(Constants.APPLICATION_NAME, message);
            return;
        }
        maybeOpenFile(outFile);
        Globals.executePostExportCommandStandard(outFile.getAbsolutePath());
    }
}

From source file:pcgen.gui2.PCGenFrame.java

boolean showSavePartyChooser() {
    PartyFacade party = CharacterManager.getCharacters();
    PCGenSettings context = PCGenSettings.getInstance();
    String parentPath = context.getProperty(PCGenSettings.PCP_SAVE_PATH);
    chooser.setCurrentDirectory(new File(parentPath));
    File file = party.getFileRef().get();

    chooser.setSelectedFile(file);//w w  w  .  j  av  a2 s .c om
    chooser.resetChoosableFileFilters();
    FileFilter filter = new FileNameExtensionFilter("Pcp files only", "pcp");
    chooser.addChoosableFileFilter(filter);
    chooser.setFileFilter(filter);
    int ret = chooser.showSaveDialog(this);
    if (ret != JFileChooser.APPROVE_OPTION) {
        return false;
    }
    file = chooser.getSelectedFile();
    if (!file.getName().endsWith(Constants.EXTENSION_PARTY_FILE)) {
        file = new File(file.getParent(), file.getName() + Constants.EXTENSION_PARTY_FILE);
    }
    if (file.isDirectory()) {
        showErrorMessage(Constants.APPLICATION_NAME, LanguageBundle.getString("in_savePcDirOverwrite")); //$NON-NLS-1$
        return showSavePartyChooser();
    }
    if (file.exists()) {
        boolean overwrite = showWarningConfirm(
                LanguageBundle.getFormattedString("in_savePcConfirmOverTitle", file.getName()), //$NON-NLS-1$
                LanguageBundle.getFormattedString("in_savePcConfirmOverMsg", file.getName())); //$NON-NLS-1$
        if (!overwrite) {
            return showSavePartyChooser();
        }
    }
    party.setFile(file);
    context.setProperty(PCGenSettings.PCP_SAVE_PATH, file.getParent());
    if (!saveAllCharacters()) {
        showErrorMessage(LanguageBundle.getString("in_savePartyFailTitle"), //$NON-NLS-1$
                LanguageBundle.getString("in_savePartyFailMsg")); //$NON-NLS-1$
        return false;
    }
    if (!CharacterManager.saveCurrentParty()) {
        return showSavePartyChooser();
    }
    return true;
}

From source file:pcgen.gui2.PCGenFrame.java

/**
 * This brings up a file chooser allows the user to select
 * the location that a character should be saved to.
 * @param character the character to be saved
 *//*from   w w w .  j a  v a2 s . c  o m*/
boolean showSaveCharacterChooser(CharacterFacade character) {
    PCGenSettings context = PCGenSettings.getInstance();
    String parentPath = lastCharacterPath;
    if (parentPath == null) {
        parentPath = context.getProperty(PCGenSettings.PCG_SAVE_PATH);
    }
    chooser.setCurrentDirectory(new File(parentPath));
    File file = character.getFileRef().get();
    File prevFile = file;
    if (file == null || StringUtils.isEmpty(file.getName())) {
        file = new File(parentPath, character.getNameRef().get() + Constants.EXTENSION_CHARACTER_FILE);
    }
    chooser.setSelectedFile(file);

    chooser.resetChoosableFileFilters();
    FileFilter filter = new FileNameExtensionFilter("Pcg files only", "pcg");
    chooser.addChoosableFileFilter(filter);
    chooser.setFileFilter(filter);
    int ret = chooser.showSaveDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        if (!file.getName().endsWith(Constants.EXTENSION_CHARACTER_FILE)) {
            file = new File(file.getParent(), file.getName() + Constants.EXTENSION_CHARACTER_FILE);
        }
        UIDelegate delegate = character.getUIDelegate();
        if (file.isDirectory()) {
            delegate.showErrorMessage(Constants.APPLICATION_NAME,
                    LanguageBundle.getString("in_savePcDirOverwrite")); //$NON-NLS-1$
            return showSaveCharacterChooser(character);
        }

        if (file.exists() && (prevFile == null || !file.getName().equals(prevFile.getName()))) {
            boolean overwrite = delegate.showWarningConfirm(
                    LanguageBundle.getFormattedString("in_savePcConfirmOverTitle", //$NON-NLS-1$
                            file.getName()),
                    LanguageBundle.getFormattedString("in_savePcConfirmOverMsg", //$NON-NLS-1$
                            file.getName()));

            if (!overwrite) {
                return showSaveCharacterChooser(character);
            }
        }

        try {
            character.setFile(file);
            prepareForSave(character, false);
            if (!reallySaveCharacter(character)) {
                return showSaveCharacterChooser(character);
            }

            lastCharacterPath = chooser.getCurrentDirectory().toString();
            return true;
        } catch (Exception e) {
            Logging.errorPrint("Error saving character to new file " + file, e); //$NON-NLS-1$
            delegate.showErrorMessage(Constants.APPLICATION_NAME,
                    LanguageBundle.getFormattedString("in_saveFailMsg", file.getName())); //$NON-NLS-1$
        }
    }
    return false;
}

From source file:pcgen.gui2.PCGenFrame.java

void showOpenCharacterChooser() {
    PCGenSettings context = PCGenSettings.getInstance();
    String path = lastCharacterPath;
    if (path == null) {
        path = context.getProperty(PCGenSettings.PCG_SAVE_PATH);
    }//from   www.jav  a 2  s  .  co m
    chooser.setCurrentDirectory(new File(path));
    chooser.setSelectedFile(new File("")); //$NON-NLS-1$

    chooser.resetChoosableFileFilters();
    FileFilter filter = new FileNameExtensionFilter("Pcg files only", "pcg");
    chooser.addChoosableFileFilter(filter);
    chooser.setFileFilter(filter);

    int ret = chooser.showOpenDialog(this);
    if (ret == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        loadCharacterFromFile(file);
        lastCharacterPath = chooser.getCurrentDirectory().toString();
    }
}

From source file:pcgen.gui2.PCGenFrame.java

void showOpenPartyChooser() {
    PCGenSettings context = PCGenSettings.getInstance();
    chooser.setCurrentDirectory(new File(context.getProperty(PCGenSettings.PCP_SAVE_PATH)));
    chooser.resetChoosableFileFilters();
    chooser.setFileFilter(new FileNameExtensionFilter("Pcp files only", "pcp"));

    int returnVal = chooser.showOpenDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();
        loadPartyFromFile(file);//from  w w  w  . jav  a  2  s .c  o m
    }
}