Example usage for javax.swing JFileChooser showSaveDialog

List of usage examples for javax.swing JFileChooser showSaveDialog

Introduction

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

Prototype

public int showSaveDialog(Component parent) throws HeadlessException 

Source Link

Document

Pops up a "Save File" file chooser dialog.

Usage

From source file:app.RunApp.java

/**
 * Action for Save button from principal tab
 * /*from w  w  w.j a va2 s  .  co m*/
 * @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:com.monead.semantic.workbench.SemanticWorkbench.java

/**
 * Save the text from the SPARQL query text area to a file.
 * //www  . j  ava2s  .  c o m
 */
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:com.peterbochs.PeterBochsDebugger.java

private void jButton7ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        PeterBochsCommonLib.exportTableModelToExcel(file, this.jGDTTable.getModel(), "GDT");
    }//from  ww w  .  ja v  a2 s. c  o  m
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton8ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        PeterBochsCommonLib.exportTableModelToExcel(file, this.jIDTTable.getModel(), "IDT");
    }//from   w  w w.  ja  va 2s. c  om
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton12ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        PeterBochsCommonLib.exportTableModelToExcel(file, PeterBochsDebugger.instructionTable.getModel(),
                "instruction 0x" + this.jInstructionComboBox.getSelectedItem().toString());
    }//w w  w . java  2s  .  c  om
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton21ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        PeterBochsCommonLib.exportTableModelToExcel(file, this.jPageDirectoryTable.getModel(),
                this.jPageTableTable.getModel(), jMemoryAddressComboBox.getSelectedItem().toString());
    }//from  w  w w  .j a v a  2s.c o m
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton18ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        PeterBochsCommonLib.exportTableModelToExcel(file, this.jAddressTranslateTable2.getModel(),
                jMemoryAddressComboBox.getSelectedItem().toString());
    }/*  ww  w.  j a v  a2s  .c  o m*/
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton3ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        if (!PeterBochsCommonLib.saveImage(instructionTable, file)) {
            JOptionPane.showMessageDialog(this, "Cannot save image.", "Error", JOptionPane.ERROR_MESSAGE);
        }/*www  .jav  a  2  s  .  co  m*/
    }
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton6ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        if (!PeterBochsCommonLib.saveImage(this.jGDTTable, file)) {
            JOptionPane.showMessageDialog(this, "Cannot save image.", "Error", JOptionPane.ERROR_MESSAGE);
        }/* w w  w .  j  a v  a2  s  .c o  m*/
    }
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jButton9ActionPerformed(ActionEvent evt) {
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(this);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        if (!PeterBochsCommonLib.saveImage(this.jIDTTable, file)) {
            JOptionPane.showMessageDialog(this, "Cannot save image.", "Error", JOptionPane.ERROR_MESSAGE);
        }/*from  w w  w  .jav  a  2 s  . c  o m*/
    }
}