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:com.lfv.lanzius.server.LanziusServer.java

public void menuChoiceLoadExercise() {
    if (isSwapping)
        return;//from w w  w . java 2 s.c  o  m
    log.info("Menu: Load exercise");
    JFileChooser fc = new JFileChooser("data/exercises");
    fc.setDialogTitle("Load exercise...");
    fc.setMultiSelectionEnabled(false);
    fc.setFileFilter(new FileFilter() {
        public boolean accept(File f) {
            return !f.isHidden() && (f.isDirectory() || f.getName().endsWith(".xml"));
        }

        public String getDescription() {
            return "Exercise (*.xml)";
        }
    });

    int returnVal = JFileChooser.APPROVE_OPTION;
    if (Config.SERVER_AUTOLOAD_EXERCISE == null)
        returnVal = fc.showOpenDialog(frame);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        //File file;
        if (Config.SERVER_AUTOLOAD_EXERCISE == null)
            exerciseFile = fc.getSelectedFile();
        else
            exerciseFile = new File(Config.SERVER_AUTOLOAD_EXERCISE);
        log.info("Loading exercise " + exerciseFile);
        if (exerciseFile.exists()) {
            loadExercise(exerciseFile);
        } else
            JOptionPane.showMessageDialog(frame, "Unable to load exercise! File not found!", "Error!",
                    JOptionPane.ERROR_MESSAGE);
    }
}

From source file:com.nikonhacker.gui.EmulatorUI.java

private void openLoadImageDialog(final int chip) {
    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(new FileFilter() {
        @Override//from w  w  w.j  a v a  2  s  . co  m
        public boolean accept(File f) {
            if (f != null) {
                //noinspection SimplifiableIfStatement
                if (f.isDirectory()) {
                    return true;
                }
                return f.getName().toLowerCase().startsWith((chip == Constants.CHIP_FR) ? "b" : "a")
                        && f.getName().toLowerCase().endsWith(".bin");
            }
            return false;
        }

        @Override
        public String getDescription() {
            return Constants.CHIP_LABEL[chip] + " firmware file (" + ((chip == Constants.CHIP_FR) ? "b" : "a")
                    + "*.bin)";
        }
    });
    fc.setCurrentDirectory(new File("."));

    int returnVal = fc.showOpenDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        final File firmwareFile = fc.getSelectedFile();
        if (firmwareFile.exists()) {
            imageFile[chip] = firmwareFile;
            reset(chip);
        } else {
            JOptionPane
                    .showMessageDialog(this,
                            "Given " + Constants.CHIP_LABEL[chip] + " firmware file does not exist:\n"
                                    + firmwareFile.getAbsolutePath(),
                            "File not found", JOptionPane.WARNING_MESSAGE);
        }
    }
}

From source file:gda.plots.SimplePlot.java

/**
 * Function that is called when the new save as option on the right click menu is clicked.
 *///from   www  . j  a v  a  2  s. co  m
private void saveAs() {
    try {
        if (fc == null)
            fc = new JFileChooser();
        fc.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if (f.isDirectory()) {
                    return true;
                }
                String path = f.getPath();
                if (path.endsWith(".png")) {
                    return true;
                }
                return false;
            }

            @Override
            public String getDescription() {
                return "PNG files";
            }

        });
        int returnValue = fc.showSaveDialog(this);
        logger.debug("check the save dialogue");
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            BufferedImage im = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
            super.paintComponent(im.createGraphics());
            ImageIO.write(im, "png", fc.getSelectedFile());
        }
    } catch (Exception e) {
        logger.error("Error in Save As", e);
    }
}

From source file:canreg.client.gui.dataentry.PDSEditorInternalFrame.java

@Action
public void savePNGAction() {
    if (chart != null) {
        JFileChooser chooser = new JFileChooser();
        FileFilter filter = new FileFilter() {

            @Override/*from   ww w  . j  a  va 2s .  c om*/
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith("png"); //NOI18N
            }

            @Override
            public String getDescription() {
                return java.util.ResourceBundle
                        .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                        .getString("PNG GRAPHICS FILES");
            }
        };
        chooser.setFileFilter(filter);
        int result = chooser.showDialog(this,
                java.util.ResourceBundle
                        .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                        .getString("CHOOSE FILENAME"));
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                File file = chooser.getSelectedFile();
                if (!file.getName().toLowerCase().endsWith("png")) { //NOI18N
                    file = new File(file.getAbsolutePath() + ".png"); //NOI18N
                }
                ChartUtilities.saveChartAsPNG(file, chart, pyramidPanelHolder.getWidth(),
                        pyramidPanelHolder.getHeight());
            } catch (IOException ex) {
                Logger.getLogger(PDSEditorInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:canreg.client.gui.dataentry.PDSEditorInternalFrame.java

@Action
public void saveSVGAction() {
    if (chart != null) {
        JFileChooser svgFileChooser = new JFileChooser();
        FileFilter filter = new FileFilter() {

            @Override//w w  w  .  ja  v a2  s .  c om
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith("svg"); //NOI18N
            }

            @Override
            public String getDescription() {
                return java.util.ResourceBundle
                        .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                        .getString("SVG GRAPHICS FILES");
            }
        };
        svgFileChooser.setFileFilter(filter);
        int result = svgFileChooser.showDialog(this,
                java.util.ResourceBundle
                        .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                        .getString("CHOOSE FILENAME"));
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                File file = svgFileChooser.getSelectedFile();
                if (!file.getName().toLowerCase().endsWith("svg")) { //NOI18N
                    file = new File(file.getAbsolutePath() + ".svg"); //NOI18N
                }
                canreg.client.analysis.Tools.exportChartAsSVG(chart,
                        new Rectangle(pyramidPanelHolder.getWidth(), pyramidPanelHolder.getHeight()), file);
            } catch (IOException ex) {
                Logger.getLogger(PDSEditorInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

From source file:canreg.client.gui.dataentry.PDSEditorInternalFrame.java

@Action
public void writeToFile() {

    if (chooser == null) {
        chooser = new JFileChooser();
        FileFilter filter = new FileFilter() {

            @Override//  ww  w.  j  av a  2 s . c om
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().toLowerCase().endsWith("json"); //NOI18N
            }

            @Override
            public String getDescription() {
                return java.util.ResourceBundle
                        .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                        .getString("JSON FILES");
            }
        };
        chooser.setFileFilter(filter);
    }

    chooser.setSelectedFile(new File("CR5POP_"
            + pds.getPopulationDatasetName().replace(",", "_").replace(" ", "_").replace("__", "_") + ".json"));
    int returnVal = chooser.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        try {
            buildPDSfromTable();
            String fileName = chooser.getSelectedFile().getCanonicalPath();
            if (!fileName.toLowerCase().endsWith(".json")) {
                fileName += ".json";
            }
            canreg.common.database.Tools.writePopulationDatasetToJSON(pds, fileName);
            JOptionPane.showInternalMessageDialog(
                    CanRegClientApp.getApplication().getMainFrame().getContentPane(),
                    java.util.ResourceBundle
                            .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                            .getString("SUCCESSFULLY_SAVED_PDS_TO_FILE:_") + fileName + ".",
                    java.util.ResourceBundle
                            .getBundle("canreg/client/gui/dataentry/resources/PDSEditorInternalFrame")
                            .getString("PDS_SAVED."),
                    JOptionPane.INFORMATION_MESSAGE);
        } catch (IOException ex) {
            Logger.getLogger(PDSEditorInternalFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:v800_trainer.JCicloTronic.java

private void jMenuOpenallActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuOpenallActionPerformed
    // Add your handling code here:

    int i;/* w  ww.  ja  v  a2 s  .  c o  m*/
    StringBuffer Buffer = new StringBuffer();
    String[] liste = new String[1];
    byte Data[] = new byte[81930];
    File path = new File(Properties.getProperty("data.dir"));
    File Datei;

    FileFilter directoryFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
        }

        public String getDescription() {
            return "";
        };
    };

    chooser.setCurrentDirectory(
            new java.io.File(Properties.getProperty("import.dir", Properties.getProperty("data.dir"))));
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    ExampleFileFilter filtera = new ExampleFileFilter();
    ExampleFileFilter filterb = new ExampleFileFilter();
    ExampleFileFilter filterc = new ExampleFileFilter();
    ExampleFileFilter filterd = new ExampleFileFilter();
    ExampleFileFilter filtere = new ExampleFileFilter();

    filtera.addExtension("dat");
    filtera.setDescription("HAC Rohdaten");
    filterb.addExtension("tur");
    filterb.setDescription("Hactronic Dateien");
    filterc.addExtension("hrm");
    filterc.setDescription("Polar Daten");
    filterd.addExtension("");
    filterd.setDescription("Polar V800 Verzeichnis");
    filtere.addExtension("csv");
    filtere.setDescription("Polar V800 CSV Flow export");

    chooser.resetChoosableFileFilters();
    chooser.addChoosableFileFilter(filtera);
    chooser.addChoosableFileFilter(filterb);
    chooser.addChoosableFileFilter(filterc);
    chooser.addChoosableFileFilter(filterd);
    chooser.addChoosableFileFilter(filtere);
    chooser.setFileFilter(filtere);

    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    int returnVal = chooser.showDialog(this, null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {

        if (chooser.getSelectedFile().getName().endsWith(".dat")) {
            path = new File(chooser.getCurrentDirectory().getPath());
        } else {
            path = new File(chooser.getSelectedFile().getPath());
        }

        if (chooser.getFileFilter().equals(filtera)) {
            liste = path.list(new DirFilter(".dat"));
        }
        if (chooser.getFileFilter().equals(filterb)) {
            liste = path.list(new DirFilter(".tur"));
        }
        if (chooser.getFileFilter().equals(filterc)) {
            liste = path.list(new DirFilter(".hrm"));
        }
        if (chooser.getFileFilter().equals(filtere)) {
            liste = path.list(new DirFilter(".csv"));
        }

        if (chooser.getFileFilter().equals(filterd)) {

            File[] files = path.listFiles();
            ArrayList<String> pathliste = new ArrayList();
            for (File file : files) {
                try {
                    if (file.isDirectory()) {
                        pathliste.add(file.getCanonicalPath());
                    }
                } catch (Exception e) {
                }
            }

            Thread thread = new Thread(new Runnable() {

                public void run() {
                    setCursor(new Cursor(Cursor.WAIT_CURSOR));
                    pm = new ProgressMonitor(JCicloTronic.this, "Importiere...", "", 0, 100);
                    pm.setMillisToPopup(1);

                    v800export V800_export = new v800export();
                    V800_export.export_sessions(JCicloTronic.this, pathliste);
                    pm.close();
                    ChangeModel();
                    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

                    try {

                        Thread.sleep(100);
                    } catch (Exception e) {
                        if (Thread.interrupted()) {
                            return;
                        }
                    }
                }

            });

            thread.start();

            return;

        }

        //alle auer V800 Dateien importieren
        if (liste == null) {
            JOptionPane.showMessageDialog(null, "Keine Rohdaten-Files gefunden!", "Achtung!",
                    JOptionPane.ERROR_MESSAGE);
            return;
        }
        final String[] liste_final = liste.clone();
        final File path_final = path;

        Thread thread = new Thread(new Runnable() {

            public void run() {
                File Datei;
                byte Data[] = new byte[81930];

                setCursor(new Cursor(Cursor.WAIT_CURSOR));
                pm = new ProgressMonitor(JCicloTronic.this, "Importiere...", "", 0, 100);
                pm.setMillisToPopup(1);

                for (int i = 0; i < liste_final.length; i++) {
                    pm.setProgress((int) 100.0 * i / liste_final.length);
                    pm.setNote(liste_final[i]);

                    try {
                        Eingabedatei = new java.io.FileInputStream(path_final.getPath()
                                + SystemProperties.getProperty("file.separator") + liste_final[i]);

                        try {
                            Datei = new File(path_final.getPath()
                                    + SystemProperties.getProperty("file.separator") + liste_final[i]);
                            Data = new byte[(int) Datei.length()];
                            //                        Eingabedatei.read()

                            Eingabedatei.read(Data);
                            Eingabedatei.close();
                            if (chooser.getFileFilter().equals(filtera)) {
                                ExtractTour(Data);
                            }
                            if (chooser.getFileFilter().equals(filterb)) {
                                ExtractHactronicFile(Data);
                            }
                            if (chooser.getFileFilter().equals(filterc)) {
                                ExtractPolarFile(Data);
                            }
                            if (chooser.getFileFilter().equals(filtere)) {
                                ExtractCSV(Data);
                            }

                        } catch (IOException e) {
                            JOptionPane.showMessageDialog(null, "IO-Fehler bei Datenlesen", "Achtung!",
                                    JOptionPane.ERROR_MESSAGE);
                        }

                    } catch (FileNotFoundException e) {
                        JOptionPane.showMessageDialog(null,
                                "IO-Fehler bei " + path_final.getPath()
                                        + SystemProperties.getProperty("file.separator") + liste_final[i],
                                "Achtung!", JOptionPane.ERROR_MESSAGE);

                    }

                }
                pm.close();
                JOptionPane.showMessageDialog(null, "Daten  Ende", "Achtung!", JOptionPane.ERROR_MESSAGE);

                Properties.setProperty("import.dir", chooser.getCurrentDirectory().getPath());

                ChangeModel();
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));

                try {

                    Thread.sleep(100);
                } catch (Exception e) {
                    if (Thread.interrupted()) {
                        return;
                    }
                }
            }

        });

        thread.start();

    }
}

From source file:ru.apertum.qsystem.client.forms.FAdmin.java

private void buttonExportToCSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExportToCSVActionPerformed
        final JFileChooser fc = new JFileChooser();
        fc.setDialogTitle(getLocaleMessage("save.statictic"));
        fc.setFileFilter(new FileFilter() {

            @Override//  w  ww  .  ja  v  a  2 s. com
            public boolean accept(File f) {
                return !f.isFile() || f.getAbsolutePath().toLowerCase().endsWith(".csv");
            }

            @Override
            public String getDescription() {
                return getLocaleMessage("files.type.csv");
            }
        });
        //fc.setCurrentDirectory(new File("config"));
        //fc.setSelectedFile(new File(configuration.getSystemName()));
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            final File file;
            //This is where a real application would open the file.
            if (!fc.getSelectedFile().getAbsolutePath().toLowerCase().endsWith(".csv")) {
                file = new File(fc.getSelectedFile().getAbsoluteFile() + ".csv");
            } else {
                file = fc.getSelectedFile();
            }

            Spring.getInstance().getHt().getSessionFactory().openSession().doWork((Connection connection) -> {
                final GregorianCalendar gc = new GregorianCalendar();
                gc.setTime(dateChooserStartCsv.getDate());
                gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
                gc.set(GregorianCalendar.MINUTE, 0);
                gc.set(GregorianCalendar.SECOND, 0);
                gc.set(GregorianCalendar.MILLISECOND, 0);
                final String std = Uses.format_for_rep.format(gc.getTime());
                gc.setTime(dateChooserFinishCsv.getDate());
                gc.set(GregorianCalendar.HOUR_OF_DAY, 0);
                gc.set(GregorianCalendar.MINUTE, 0);
                gc.set(GregorianCalendar.SECOND, 0);
                gc.set(GregorianCalendar.MILLISECOND, 0);
                gc.add(GregorianCalendar.HOUR, 24);
                final String find = Uses.format_for_rep.format(gc.getTime());
                final String sql = " SELECT " + "    s.client_id as id, "
                        + "    concat(c.service_prefix , c.number) as num, " + "    c.input_data as inp,  "
                        + "    DATE_FORMAT(s.client_stand_time, '%d.%m.%y %H:%i') as stnd, "
                        + "    sv.name as srv, " + "    DATE_FORMAT(s.user_start_time, '%d.%m.%y %H:%i') as strt, "
                        + "    DATE_FORMAT(s.user_finish_time, '%d.%m.%y %H:%i') as fin, " + "    u.name as usr, "
                        + "    s.client_wait_period as wt, " + "    s.user_work_period as wrk, "
                        + "    IFNULL(r.name, '') as res "
                        + " FROM statistic s left join results r on s.results_id=r.id, clients c, users u, services sv "
                        + " WHERE s.client_id=c.id and s.user_id=u.id and s.service_id=sv.id "
                        + "    and s.client_stand_time>='" + std + "' and s.client_stand_time<='" + find + "'";
                try (ResultSet set = connection.createStatement().executeQuery(sql)) {
                    final Writer writer;
                    try {
                        writer = new OutputStreamWriter(new FileOutputStream(file), "cp1251").append("");
                        writer.append("");
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.number"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.data"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.stand_time"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.service_name"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.start_time"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.finish_time"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.user"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.wait"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.work"));
                        writer.append(cbSeparateCSV.getSelectedItem().toString());
                        writer.append(getLocaleMessage("csv.result"));
                        writer.append('\n');

                        while (set.next()) {
                            writer.append(set.getString("id"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("num"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("inp"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("stnd"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(
                                    set.getString("srv").replace(cbSeparateCSV.getSelectedItem().toString(), " "));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("strt"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("fin"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("usr"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("wt"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("wrk"));
                            writer.append(cbSeparateCSV.getSelectedItem().toString());
                            writer.append(set.getString("res"));
                            writer.append('\n');
                        }
                        //generate whatever data you want

                        writer.flush();
                        writer.close();
                    } catch (IOException ex) {
                        throw new ClientException(ex);
                    }
                }
                JOptionPane.showMessageDialog(fc, getLocaleMessage("stat.saved"), getLocaleMessage("stat.saving"),
                        JOptionPane.INFORMATION_MESSAGE);
            });

        }
    }

From source file:nitf.imagej.NITF_Reader.java

public NITF_Reader() {
    super("NITF Reader");
    Java2.setSystemLookAndFeel();/*from   w  w w .  j a v  a2  s  .  com*/
    fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Open NITF Image(s)...");
    String dir;
    if ((dir = Prefs.get(NITF_READER_DIR, null)) != null)
        fileChooser.setCurrentDirectory(new File(dir));
    fileChooser.setMultiSelectionEnabled(true);
    fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    fileChooser.setAcceptAllFileFilterUsed(true);
    fileFilter = new FileFilter() {
        public boolean accept(File f) {
            String name = f.getName().toLowerCase().trim();
            return f.isDirectory()
                    || ((name.endsWith("ntf") || name.endsWith("nitf") || name.endsWith("nsf")) && f.canRead());
        }

        public String getDescription() {
            return "NITF Images (*.nitf,*.ntf, *.nsif)";
        }
    };
    fileChooser.setFileFilter(fileFilter);
}

From source file:nz.ac.massey.cs.gql4jung.browser.ResultBrowser.java

private void actExport2CSV() {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(this);
    FileFilter filter = new FileFilter() {
        @Override//from   ww w  .  java 2s  . c om
        public boolean accept(File f) {
            return f.getAbsolutePath().endsWith(".csv");
        }

        @Override
        public String getDescription() {
            return "csv files";
        }
    };
    fc.setFileFilter(filter);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        QueryResultsExporter2CSV exporter = new QueryResultsExporter2CSV();
        try {
            exporter.export(this.results, file);
            log("results exported to " + file.getAbsolutePath());
            JOptionPane.showMessageDialog(this, "Results have been exported to\n" + file.getAbsolutePath());
        } catch (IOException x) {
            this.handleException("Error exporting file", x);
        }
    }
}