Example usage for javax.swing JFileChooser addChoosableFileFilter

List of usage examples for javax.swing JFileChooser addChoosableFileFilter

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "Adds a filter to the list of user choosable file filters.")
public void addChoosableFileFilter(FileFilter filter) 

Source Link

Document

Adds a filter to the list of user choosable file filters.

Usage

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Allow the user to select a file, which is expected to be an ontology, and
 * then load the file./*ww w .j a va  2 s  .  c om*/
 */
private void openSparqlQueryFile() {
    JFileChooser fileChooser;
    FileFilter defaultFileFilter = null;
    FileFilter preferredFileFilter = null;
    File chosenFile;

    if (lastDirectoryUsed == null) {
        lastDirectoryUsed = new File(".");
    }

    fileChooser = new JFileChooser(lastDirectoryUsed);

    for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) {
        if (filterDefinition.name().startsWith("SPARQL")) {
            final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(),
                    filterDefinition.acceptedSuffixes());
            if (filterDefinition.isPreferredOption()) {
                preferredFileFilter = fileFilter;
            }
            fileChooser.addChoosableFileFilter(fileFilter);
            if (filterDefinition.description().equals(latestChosenSparqlFileFilterDescription)) {
                defaultFileFilter = fileFilter;
            }
        }
    }

    if (defaultFileFilter != null) {
        fileChooser.setFileFilter(defaultFileFilter);
    } else if (latestChosenSparqlFileFilterDescription != null
            && latestChosenSparqlFileFilterDescription.startsWith("All")) {
        fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    } else if (preferredFileFilter != null) {
        fileChooser.setFileFilter(preferredFileFilter);
    }

    fileChooser.setDialogTitle("Open SPARQL Query File");
    fileChooser.showOpenDialog(this);

    try {
        latestChosenSparqlFileFilterDescription = fileChooser.getFileFilter().getDescription();
    } catch (Throwable throwable) {
        LOGGER.warn("Unable to determine which SPARQL file filter was chosen", throwable);
    }

    chosenFile = fileChooser.getSelectedFile();

    if (chosenFile != null) {
        loadSparqlQueryFile(chosenFile);
    }
}

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Allow the user to select a file, which is expected to be an ontology, and
 * then load the file./*  ww w . j  a  va 2  s .c o m*/
 */
private void openOntologyFile() {
    JFileChooser fileChooser;
    FileFilter defaultFileFilter = null;
    FileFilter preferredFileFilter = null;
    File chosenFile;

    if (lastDirectoryUsed == null) {
        lastDirectoryUsed = new File(".");
    }

    fileChooser = new JFileChooser(lastDirectoryUsed);

    for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) {
        if (filterDefinition.name().startsWith("ONTOLOGY")) {
            final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(),
                    filterDefinition.acceptedSuffixes());
            if (filterDefinition.isPreferredOption()) {
                preferredFileFilter = fileFilter;
            }
            fileChooser.addChoosableFileFilter(fileFilter);
            if (filterDefinition.description().equals(latestChosenRdfFileFilterDescription)) {
                defaultFileFilter = fileFilter;
            }
        }
    }

    if (defaultFileFilter != null) {
        fileChooser.setFileFilter(defaultFileFilter);
    } else if (latestChosenRdfFileFilterDescription != null
            && latestChosenRdfFileFilterDescription.startsWith("All")) {
        fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    } else if (preferredFileFilter != null) {
        fileChooser.setFileFilter(preferredFileFilter);
    }

    fileChooser.setDialogTitle("Open Assertions File");
    fileChooser.showOpenDialog(this);

    try {
        latestChosenRdfFileFilterDescription = fileChooser.getFileFilter().getDescription();
    } catch (Throwable throwable) {
        LOGGER.warn("Unable to determine which ontology file filter was chosen", throwable);
    }

    chosenFile = fileChooser.getSelectedFile();

    if (chosenFile != null) {
        setupToLoadOntologyFile(new FileSource(chosenFile));
    }
}

From source file:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Save the text from the SPARQL query text area to a file.
 * /*from www  .  j av  a2  s  .c  om*/
 */
private void saveSparqlQueryToFile() {
    FileWriter out;
    JFileChooser fileChooser;
    FileFilter defaultFileFilter = null;
    FileFilter preferredFileFilter = null;
    File destinationFile;
    int choice;

    out = null;

    if (lastDirectoryUsed == null) {
        lastDirectoryUsed = new File(".");
    }

    fileChooser = new JFileChooser();

    for (FileFilterDefinition filterDefinition : FileFilterDefinition.values()) {
        if (filterDefinition.name().startsWith("SPARQL")) {
            final FileFilter fileFilter = new SuffixFileFilter(filterDefinition.description(),
                    filterDefinition.acceptedSuffixes());
            if (filterDefinition.isPreferredOption()) {
                preferredFileFilter = fileFilter;
            }
            fileChooser.addChoosableFileFilter(fileFilter);
            if (filterDefinition.description().equals(latestChosenSparqlFileFilterDescription)) {
                defaultFileFilter = fileFilter;
            }
        }
    }

    if (defaultFileFilter != null) {
        fileChooser.setFileFilter(defaultFileFilter);
    } else if (latestChosenSparqlFileFilterDescription != null
            && latestChosenSparqlFileFilterDescription.startsWith("All")) {
        fileChooser.setFileFilter(fileChooser.getAcceptAllFileFilter());
    } else if (preferredFileFilter != null) {
        fileChooser.setFileFilter(preferredFileFilter);
    }

    if (sparqlQueryFile != null) {
        fileChooser.setSelectedFile(sparqlQueryFile);
    } else {
        fileChooser.setSelectedFile(lastDirectoryUsed);
    }

    fileChooser.setDialogTitle("Save SPARQL Query to File");
    choice = fileChooser.showSaveDialog(this);

    try {
        latestChosenSparqlFileFilterDescription = fileChooser.getFileFilter().getDescription();
    } catch (Throwable throwable) {
        LOGGER.warn("Unable to determine which SPARQL file filter was chosen", throwable);
    }

    destinationFile = fileChooser.getSelectedFile();

    // Did not click save, did not select a file or chose a directory
    // So do not write anything
    if (choice != JFileChooser.APPROVE_OPTION || destinationFile == null
            || (destinationFile.exists() && !destinationFile.isFile())) {
        return; // EARLY EXIT!
    }

    // Adjust file suffix if necessary
    final FileFilter fileFilter = fileChooser.getFileFilter();
    if (fileFilter != null && fileFilter instanceof SuffixFileFilter
            && !fileChooser.getFileFilter().accept(destinationFile)) {
        destinationFile = ((SuffixFileFilter) fileFilter).makeWithPrimarySuffix(destinationFile);
    }

    if (okToOverwriteFile(destinationFile)) {

        LOGGER.info("Write SPARQL query to file, " + destinationFile);

        try {
            out = new FileWriter(destinationFile, false);

            if (sparqlServiceUrl.getSelectedIndex() != 0) {

                out.write(
                        "# " + SPARQL_QUERY_SAVE_SERVICE_URL_PARAM + sparqlServiceUrl.getSelectedItem() + "\n");
            } else {
                out.write("# " + SPARQL_QUERY_SAVE_SERVICE_URL_PARAM
                        + SPARQL_QUERY_SAVE_SERVICE_URL_VALUE_FOR_NO_SERVICE_URL + "\n");
            }

            if (defaultGraphUri.getText().trim().length() > 0) {
                out.write("# " + SPARQL_QUERY_SAVE_SERVICE_DEFAULT_GRAPH_PARAM
                        + defaultGraphUri.getText().trim() + "\n");
            }
            out.write(sparqlInput.getText());
            setSparqlQueryFile(destinationFile);
        } catch (IOException ioExc) {
            final String errorMessage = "Unable to write to file: " + destinationFile;
            LOGGER.error(errorMessage, ioExc);
            throw new RuntimeException(errorMessage, ioExc);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Throwable throwable) {
                    final String errorMessage = "Failed to close output file: " + destinationFile;
                    LOGGER.error(errorMessage, throwable);
                    throw new RuntimeException(errorMessage, throwable);
                }
            }
            setTitle();
        }
    }
}

From source file:app.RunApp.java

/**
 * Save heatmap graph as image/*  w ww.ja  v a 2  s .c  om*/
 * 
 * @throws AWTException
 * @throws IOException 
 */
private void saveHeatmapGraph() throws AWTException, IOException {
    BufferedImage image = new Robot().createScreenCapture(
            new Rectangle(panelHeatmap.getLocationOnScreen().x + 31, panelHeatmap.getLocationOnScreen().y + 31,
                    panelHeatmap.getWidth() - 61, panelHeatmap.getHeight() - 61));
    JFileChooser fc = new JFileChooser();

    FileNameExtensionFilter fname1 = new FileNameExtensionFilter(".png", "png");

    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.setFileFilter(fname1);
    fc.addChoosableFileFilter(fname1);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = new File(fc.getSelectedFile().getAbsolutePath() + ".png");
        ImageIO.write(image, "png", file);

        JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:app.RunApp.java

/**
 * Save co-ocurrence graph as image/* w w  w.j a va 2  s .  com*/
 * 
 * @throws AWTException
 * @throws IOException 
 */
private void saveCoocurrenceGraph() throws AWTException, IOException {
    BufferedImage image = new Robot().createScreenCapture(new Rectangle(
            panelCoOcurrenceRight.getLocationOnScreen().x, panelCoOcurrenceRight.getLocationOnScreen().y,
            panelCoOcurrenceRight.getWidth(), panelCoOcurrenceRight.getHeight()));

    JFileChooser fc = new JFileChooser();

    FileNameExtensionFilter fname1 = new FileNameExtensionFilter(".png", "png");

    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.setFileFilter(fname1);
    fc.addChoosableFileFilter(fname1);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = new File(fc.getSelectedFile().getAbsolutePath() + ".png");

        ImageIO.write(image, "png", file);

        JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:app.RunApp.java

/**
 * Action for Save button from multiple datasets tab
 * //from w w w. j av a2  s  .co m
 * @param evt Event
 * @param jtable Table
 * @throws IOException 
 */
private void buttonSaveActionPerformedMulti(java.awt.event.ActionEvent evt, JTable jtable) throws IOException {
    ArrayList<String> metricsList = getSelectedMetricsMulti(jtable);

    if (listDatasets == null || listDatasets.isEmpty() || datasetNames.isEmpty()) {
        JOptionPane.showMessageDialog(null, "You must load a dataset.", "Warning", JOptionPane.ERROR_MESSAGE);
        return;
    }

    //JFileChooser save
    JFileChooser fc = new JFileChooser();

    FileNameExtensionFilter fname = new FileNameExtensionFilter(".txt", "txt");
    FileNameExtensionFilter fname2 = new FileNameExtensionFilter(".csv", "csv");
    FileNameExtensionFilter fname3 = new FileNameExtensionFilter(".arff", ".arff");
    FileNameExtensionFilter fname4 = new FileNameExtensionFilter(".tex", ".tex");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.addChoosableFileFilter(fname);
    fc.addChoosableFileFilter(fname2);
    fc.addChoosableFileFilter(fname3);
    fc.addChoosableFileFilter(fname4);

    fc.setFileFilter(fname);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        String path;
        BufferedWriter bw;
        PrintWriter wr;

        switch (f1.getDescription()) {
        case ".txt":
            path = file.getAbsolutePath() + ".txt";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsTxt(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".tex":
            path = file.getAbsolutePath() + ".tex";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsTex(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".csv":
            path = file.getAbsolutePath() + ".csv";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsCsv(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".arff":
            path = file.getAbsolutePath() + ".arff";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMultiMetricsArff(wr, metricsList, datasetNames, tableMetricsMulti);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        default:
            break;
        }

        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:app.RunApp.java

/**
 * Action for Save button from principal tab
 * //from   w w w.  java2  s . c om
 * @param evt Event
 * @param jtable Table
 * @throws IOException 
 */
private void buttonSaveActionPerformedPrincipal(java.awt.event.ActionEvent evt, JTable jtable)
        throws IOException {
    ArrayList<String> metricsList = getMetricsSelectedPrincipal(jtable);

    if (dataset == null) {
        JOptionPane.showMessageDialog(null, "You must load a dataset.", "Warning", JOptionPane.ERROR_MESSAGE);
        return;
    }

    JFileChooser fc = new JFileChooser();

    // extension txt
    FileNameExtensionFilter fname = new FileNameExtensionFilter(".txt", "txt");
    FileNameExtensionFilter fname2 = new FileNameExtensionFilter(".csv", "csv");
    FileNameExtensionFilter fname3 = new FileNameExtensionFilter(".arff", ".arff");
    FileNameExtensionFilter fname4 = new FileNameExtensionFilter(".tex", ".tex");

    //Remove default
    fc.removeChoosableFileFilter(fc.getChoosableFileFilters()[0]);

    fc.addChoosableFileFilter(fname);
    fc.addChoosableFileFilter(fname2);
    fc.addChoosableFileFilter(fname3);
    fc.addChoosableFileFilter(fname4);

    fc.setFileFilter(fname);

    int returnVal = fc.showSaveDialog(this);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        FileFilter f1 = fc.getFileFilter();

        String path;
        BufferedWriter bw;
        PrintWriter wr;

        switch (f1.getDescription()) {
        case ".txt":
            path = file.getAbsolutePath() + ".txt";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsTxt(wr, metricsList, dataset, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".tex":
            path = file.getAbsolutePath() + ".tex";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsTex(wr, metricsList, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".csv":
            path = file.getAbsolutePath() + ".csv";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsCsv(wr, metricsList, dataset, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        case ".arff":
            path = file.getAbsolutePath() + ".arff";
            bw = new BufferedWriter(new FileWriter(path));
            wr = new PrintWriter(bw);
            ResultsIOUtils.saveMetricsArff(wr, metricsList, dataset, tableMetrics);
            wr.close();
            bw.close();
            JOptionPane.showMessageDialog(null, "File saved.", "Successful", JOptionPane.INFORMATION_MESSAGE);
            break;
        default:
            break;
        }

        Toolkit.getDefaultToolkit().beep();
    }
}

From source file:net.thangbui.downloader.utils.FileUtils.java

public static File showSaveFileDialog(FileFilter FF, int FileSelectionMode) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setAcceptAllFileFilterUsed(false);
    fileChooser.setFileSelectionMode(FileSelectionMode);
    fileChooser.addChoosableFileFilter(FF);
    int returnValue = fileChooser.showSaveDialog(fileChooser);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        return fileChooser.getSelectedFile();
    } else {/*  w w w.j  a  v  a2s .c om*/
        return null;
    }
}

From source file:org.apache.cayenne.modeler.action.ImportDataMapAction.java

protected File selectDataMap(Frame f) {

    // find start directory in preferences
    FSPath lastDir = getApplication().getFrameController().getLastDirectory();

    // configure dialog
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    lastDir.updateChooser(chooser);/*from   ww w  .j av a 2s  . c om*/

    chooser.addChoosableFileFilter(FileFilters.getDataMapFilter());

    int status = chooser.showDialog(f, "Select DataMap");
    if (status == JFileChooser.APPROVE_OPTION) {
        File file = chooser.getSelectedFile();

        // save to preferences...
        lastDir.updateFromChooser(chooser);

        return file;
    }

    return null;
}

From source file:org.apache.cayenne.modeler.dialog.pref.ClasspathPreferences.java

protected void chooseClassEntry(FileFilter filter, String title, int selectionMode) {
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(selectionMode);
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setAcceptAllFileFilterUsed(true);

    getLastDirectory().updateChooser(chooser);

    if (filter != null) {
        chooser.addChoosableFileFilter(filter);
    }/*from  w  w w .  j ava2  s . co  m*/

    chooser.setDialogTitle(title);

    File selected = null;
    int result = chooser.showOpenDialog(view);
    if (result == JFileChooser.APPROVE_OPTION) {
        selected = chooser.getSelectedFile();
    }

    if (selected != null) {
        if (!classPathEntries.contains(selected.getAbsolutePath())) {
            // store last dir in preferences
            getLastDirectory().updateFromChooser(chooser);

            int len = classPathEntries.size();
            int key = ++counter;

            String value = selected.getAbsolutePath();
            addChangedPreferences(Integer.toString(key), value);
            classPathEntries.add(value);
            classPathKeys.add(Integer.toString(key));

            tableModel.fireTableRowsInserted(len, len);
        }
    }
}