Example usage for javax.swing.filechooser FileFilter FileFilter

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

Introduction

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

Prototype

FileFilter

Source Link

Usage

From source file:jeplus.TRNSYSConfig.java

/**
 * Get a/*  w w w .j  a  v  a  2  s  .  c  o  m*/
 * <code>javax.swing.filechooser.FileFilter</code>
 *
 * @param type Predefined JEPlus file types
 * @return Swing FileFilter of the specific type
 */
public static FileFilter getFileFilter(final int type) {
    FileFilter ff = new FileFilter() {
        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
            String filename = f.getName();
            String name = filename.split("\\s*[.]\\s*")[0] + ".";
            String extension = getFileExtension(f);
            if (extension != null) {
                extension = extension.toLowerCase();
                switch (type) {
                case TRNINPUT:
                    return (extension.equals(getTRNSYSDCKExt()) || extension.equals(getTRNSYSTRDExt())
                            || extension.equals(".lst")) ? true : false;
                case DCK:
                    return (extension.equals(".dck")) ? true : false;
                case TRD:
                    return (extension.equals(".trd")) ? true : false;
                case TREXE:
                    return (filename.equals("TRNexe.exe"));
                case TRDLL:
                    return (extension.equals(".dll"));
                case TRID:
                    return (extension.equals(".id"));
                case TRTXT:
                    return (extension.equals(".txt"));
                case TRDAT:
                    return (extension.equals(".dat"));
                case TRLOG:
                    return (extension.equals(".log"));
                case TRNSYSOUTPUT: // TRNSYS output files. Used for deleting
                    return (filename.equals(getTRNSYSDefLOG()) || name.equals(getTRNSYSFort()));
                case TRLST:
                    return (extension.equals(".lst"));
                default:
                    return EPlusConfig.getFileFilter(type).accept(f);
                }
            }
            return false;
        }

        /**
         * Get description of a specific type
         */
        @Override
        public String getDescription() {
            switch (type) {
            case TRNINPUT:
                return "TRNSYS input file (.DCK) or TRNEDIT input file (.TRD)";
            case DCK:
                return "TRNSYS input file (.DCK)";
            case TRD:
                return "TRNEDIT input file (.TRD)";
            case TREXE:
                return "TRNSYS main executable";
            case TRDLL:
                return "Library file (.DLL)";
            case TRID:
                return "Registration Data file (.ID)";
            case TRTXT:
                return "Text file (.TXT)";
            case TRDAT:
                return "Data file (.DAT)";
            case TRLOG:
                return "TRNSYS output file to check problems in the running (.LOG)";
            case TRNSYSOUTPUT:
                return "TRNSYS output files";
            case TRLST:
                return "Extended TRNSYS output file to check problems in the running (.LST)";
            default:
                return EPlusConfig.getFileFilter(type).getDescription();
            }

        }
    };
    return ff;
}

From source file:biz.wolschon.finance.jgnucash.panels.TaxReportPanel.java

/**
 * Show a dialog to export//from  w w  w .  j a  va 2  s  . c o  m
 * a CSV-file that contains the
 * shown {@link TransactionSum}s
 * for each month, year or day.
 */
protected void showExportCSVDialog() {
    JFileChooser fc = new JFileChooser();
    fc.setAcceptAllFileFilterUsed(true);
    fc.addChoosableFileFilter(new FileFilter() {

        @Override
        public boolean accept(final File aF) {
            return aF.isDirectory() || aF.getName().endsWith(".csv");
        }

        @Override
        public String getDescription() {
            return "CSV-file";
        }
    });
    int dialogResult = fc.showSaveDialog(this);
    if (dialogResult != JFileChooser.APPROVE_OPTION) {
        return;
    }
    File file = fc.getSelectedFile();
    if (file.exists()) {
        int confirmation = JOptionPane.showConfirmDialog(this, "File exists. Replace file?");
        if (confirmation != JOptionPane.YES_OPTION) {
            showExportCSVDialog();
            return;
        }
    }
    ExportGranularities gran = (ExportGranularities) myExportGranularityCombobox.getSelectedItem();
    try {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        exportCSV(file, gran);
    } finally {
        setCursor(Cursor.getDefaultCursor());
    }
}

From source file:org.streamspinner.harmonica.application.CQGraphTerminal.java

private FileFilter createFilter() {
    FileFilter filter = new FileFilter() {
        public boolean accept(File f) {
            if (f.isDirectory())
                return true;
            if (!f.isFile())
                return false;
            if (f.getName().matches("([^.]+\\.hamql|[^.]+\\.cq)$"))
                return true;

            return false;
        }//from w w  w. j  a  v  a 2s.c  o m

        public String getDescription() {
            return "HamQL?(*.hamql)?CSpinQL?(*.cq)";
        }
    };

    return filter;
}

From source file:com.intuit.tank.tools.script.ScriptFilterRunner.java

/**
 * @param loadChooser/*w  w  w.j a v  a 2  s . c om*/
 * @return
 */
public Component createBottomPanel() {
    loadChooser = new JFileChooser();
    loadChooser.setFileFilter(new FileFilter() {

        @Override
        public String getDescription() {
            return "Script Files";
        }

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || ConfiguredLanguage.getConfiguredExtensions()
                    .contains(FilenameUtils.getExtension(f.getName()));
        }
    });
    runBT = new JButton("Run Script");
    runBT.setEnabled(false);
    runBT.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            runScript();
        }
    });
    JButton chooseBT = new JButton("Open Script...");
    chooseBT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            loadScript();
        }
    });

    JButton saveScriptBT = new JButton("Save Script...");
    saveScriptBT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            saveScript(false);
        }
    });

    JButton saveAsScriptBT = new JButton("Save Script As...");
    saveAsScriptBT.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            saveScript(true);
        }
    });

    JButton localBT = new JButton("Toggle Storage Mode");
    localBT.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            toggleStorageMode();
        }

    });
    localLB = new JLabel();
    setLocalLable();

    JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
    bottomPanel.add(runBT);
    bottomPanel.add(chooseBT);
    bottomPanel.add(saveScriptBT);
    bottomPanel.add(saveAsScriptBT);
    bottomPanel.add(localLB);
    bottomPanel.add(localBT);
    return bottomPanel;
}

From source file:dylemator.UserList.java

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
    if (this.filenameCombo.getSelectedIndex() == 0)
        return;/* w  w  w. java 2  s  . c  o m*/
    String sheetName = (String) this.filenameCombo.getSelectedItem();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet(sheetName);
    Row headerRow = sheet.createRow(0);
    String[] headers = exportData.get(0);
    int numOfColumns = headers.length;
    for (int i = 0; i < numOfColumns; i++) {
        Cell cell = headerRow.createCell(i);
        cell.setCellValue(headers[i]);
    }

    int rowCount = exportData.size();
    for (int rownum = 1; rownum < rowCount; rownum++) {
        Row row = sheet.createRow(rownum);
        String[] values = exportData.get(rownum);
        for (int i = 0; i < numOfColumns; i++) {
            Cell cell = row.createCell(i);
            cell.setCellValue(values[i]);
        }
    }

    String defaultFilename = "Export.xlsx";
    JFileChooser f = new JFileChooser(System.getProperty("user.dir"));
    f.setSelectedFile(new File(defaultFilename));
    f.setDialogTitle("Wybierz nazw dla pliku eksportu");
    f.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileFilter ff = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.getName().endsWith(".xlsx"))
                return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "";
        }
    };
    f.setFileFilter(ff);

    File file = null;
    int save = f.showSaveDialog(this);
    if (save == JFileChooser.APPROVE_OPTION)
        file = f.getSelectedFile();
    else
        return;

    FileOutputStream out;
    try {
        out = new FileOutputStream(file);
        wb.write(out);
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(UserList.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:ch.admin.hermes.etl.load.HermesETLApplication.java

/**
 * FileOpen Dialog//from  www.  ja v  a  2s . c  o m
 * @param title
 * @param name
 * @param ext
 * @return
 */
private static String getFile(final String title, final String[] name, final String[] ext) {
    // User Chooses a Path and Name for the html Output File
    JFileChooser fc = new JFileChooser(".");
    fc.setDialogType(JFileChooser.OPEN_DIALOG);
    fc.setDialogTitle(title);
    fc.setFileFilter(new FileFilter() {
        public String getDescription() {
            StringBuffer str = new StringBuffer();
            for (String n : name)
                str.append(n + "");
            return (str.toString());
        }

        public boolean accept(File f) {
            for (String e : ext)
                if (f.isDirectory() || f.getName().toLowerCase().endsWith(e))
                    return (true);
            return false;
        }
    });

    int state = fc.showOpenDialog(null);

    if (state == JFileChooser.APPROVE_OPTION)
        return (fc.getSelectedFile().getPath());

    return (null);
}

From source file:is.iclt.jcorpald.CorpaldView.java

public void configureFileFilters() {
    queryChooser.setFileFilter(new FileFilter() {

        @Override/*w w w  .  jav  a2  s  .  c o  m*/
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }

            return file.getAbsolutePath().endsWith(".q");
        }

        @Override
        public String getDescription() {
            return "CorpusSearch query files (*.q)";
        }

    });

    defChooser.setFileFilter(new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }

            return file.getAbsolutePath().endsWith(".def");
        }

        @Override
        public String getDescription() {
            return "CorpusSearch definitions files (*.def)";
        }

    });
}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.dialogs.hexeditors.DialogEditSVGImageValue.java

private void buttonSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveAsActionPerformed
    final JFileChooser dlg = new JFileChooser();
    dlg.addChoosableFileFilter(new FileFilter() {

        @Override// w  w w.  j  ava  2s. c  om
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase(Locale.ENGLISH).endsWith(".svg");
        }

        @Override
        public String getDescription() {
            return "SVG files (*.svg)";
        }
    });

    if (dlg.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
        File file = dlg.getSelectedFile();

        if (FilenameUtils.getExtension(file.getName()).isEmpty()) {
            file = new File(file.getParentFile(), file.getName() + ".svg");
        }

        if (file.exists() && JOptionPane.showConfirmDialog(this.parent,
                "Overwrite file '" + file.getAbsolutePath() + "\'?", "Overwriting",
                JOptionPane.OK_CANCEL_OPTION) == JOptionPane.CANCEL_OPTION) {
            return;
        }
        try {
            FileUtils.writeByteArrayToFile(file, this.value.getImage().getImageData());
        } catch (IOException ex) {
            Log.error("Can't write image [" + file + ']', ex);
            JOptionPane.showMessageDialog(this, "Can't save the file for error!", "IO Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    }

}

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

public ExistingFileChooserDialog getSourceFileChooser() {
    if (sourceFileChooser == null) {
        sourceFileChooser = new ExistingFileChooserDialog(findRegisteredWindowIfAny(), appProps,
                SOURCE_FOLDER) {/* ww w .  j  a va2s.c  o m*/
            @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:dylemator.UserResultList.java

private void exportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportButtonActionPerformed
    if (this.filenameCombo.getSelectedIndex() == 0)
        return;//from w  w  w.j  a  v  a2  s. co m
    String sheetName = (String) this.filenameCombo.getSelectedItem();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet(sheetName);
    Row headerRow = sheet.createRow(0);
    String[] headers = exportData.get(0);
    int numOfColumns = headers.length;
    for (int i = 0, j = 0; i < numOfColumns; i++) {
        if (i == 1 || i == 2 || i == 3) // opuszcz. date, imie, nazwisko
            continue;
        Cell cell = headerRow.createCell(j++);
        cell.setCellValue(headers[i]);
    }

    int rowCount = exportData.size();
    for (int rownum = 1; rownum < rowCount; rownum++) {
        Row row = sheet.createRow(rownum);
        String[] values = exportData.get(rownum);
        for (int i = 0, j = 0; i < numOfColumns; i++) {
            if (i == 1 || i == 2 || i == 3) // opuszcz. date, imie, nazwisko
                continue;
            Cell cell = row.createCell(j++);
            cell.setCellValue(values[i]);
        }
    }

    String defaultFilename = "Export.xlsx";
    JFileChooser f = new JFileChooser(System.getProperty("user.dir"));
    f.setSelectedFile(new File(defaultFilename));
    f.setDialogTitle("Wybierz nazw dla pliku eksportu");
    f.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileFilter ff = new FileFilter() {
        @Override
        public boolean accept(File file) {
            if (file.getName().endsWith(".xlsx"))
                return true;
            return false;
        }

        @Override
        public String getDescription() {
            return "";
        }
    };
    f.setFileFilter(ff);

    File file = null;
    int save = f.showSaveDialog(this);
    if (save == JFileChooser.APPROVE_OPTION)
        file = f.getSelectedFile();
    else
        return;

    FileOutputStream out;
    try {
        out = new FileOutputStream(file);
        wb.write(out);
        out.close();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(UserResultList.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(UserResultList.class.getName()).log(Level.SEVERE, null, ex);
    }
}