Example usage for javax.swing JFileChooser CANCEL_OPTION

List of usage examples for javax.swing JFileChooser CANCEL_OPTION

Introduction

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

Prototype

int CANCEL_OPTION

To view the source code for javax.swing JFileChooser CANCEL_OPTION.

Click Source Link

Document

Return value if cancel is chosen.

Usage

From source file:com.intuit.tank.tools.debugger.ActionProducer.java

/**
 * /*ww w  .  ja v a 2 s .  c om*/
 * @return
 */
public Action getOpenAction() {
    Action ret = actionMap.get(ACTION_OPEN);
    if (ret == null) {
        ret = new AbstractAction(ACTION_OPEN, getIcon("script_go.png", IconSize.SMALL)) {
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent event) {
                try {
                    int option = jFileChooser.showOpenDialog(debuggerFrame);
                    if (option != JFileChooser.CANCEL_OPTION) {
                        File selectedFile = jFileChooser.getSelectedFile();
                        try {
                            String scriptXml = FileUtils.readFileToString(selectedFile);
                            setFromString(scriptXml);
                            debuggerFrame.setScriptSource(
                                    new ScriptSource(selectedFile.getAbsolutePath(), SourceType.file));
                        } catch (Exception e) {
                            LOG.error("Error reading file " + selectedFile.getName() + ": " + e);
                            JOptionPane.showMessageDialog(debuggerFrame, e.getMessage(),
                                    "Error unmarshalling xml", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } catch (HeadlessException e) {
                    showError("Error opening file: " + e);
                }
            }
        };
        ret.putValue(Action.SHORT_DESCRIPTION, "Open Agent xml from filesystem.");
        ret.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke('O', menuActionMods));
        ret.putValue(Action.MNEMONIC_KEY, new Integer('O'));
        actionMap.put(ACTION_OPEN, ret);
    }
    return ret;
}

From source file:com.dieterholvoet.scoutsapp.util.CSVParser.java

public void chooseFile() {
    int returnVal = chooser.showOpenDialog(this.parent);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        selectedFile = chooser.getSelectedFile().getPath();

    } else if (returnVal != JFileChooser.CANCEL_OPTION) {
        JOptionPane.showMessageDialog(this.parent, "Invalid file!");
    }/*from   w  w w  . ja  v  a2 s.  c  o m*/
}

From source file:com.alvermont.terraj.util.ui.JNLPFileChooser.java

/**
 * Carry out the file open dialog/* ww  w  .ja v  a 2 s .  c o  m*/
 * 
 * @param parent The parent for the dialog
 * @return The result of the user selection
 */
public int showOpenDialog(Component parent) {
    //        if (chooser != null)
    //        {
    int option = chooser.showOpenDialog(parent);

    if (option == JFileChooser.APPROVE_OPTION) {
        File target = addExtension(chooser.getSelectedFile());

        fileContents = new MyFileContents(target);
    } else {
        fileContents = null;
    }
    //        }
    //        else
    //        {
    //            FileOpenService fos;
    //
    //            try
    //            {
    //                fos = (FileOpenService) ServiceManager.lookup(
    //                        "javax.jnlp.FileOpenService");
    //
    //                try
    //                {
    //                    String[] extensions = new String[defaultExtensions.size()];
    //
    //                    defaultExtensions.toArray(extensions);
    //
    //                    fileContents = fos.openFileDialog(null, extensions);
    //                }
    //                catch (IOException ex)
    //                {
    //                    log.error("IOException in open file selection", ex);
    //
    //                    return JFileChooser.ERROR_OPTION;
    //                }
    //            }
    //            catch (UnavailableServiceException e)
    //            {
    //                // we assume that JNLP is not available and some other security manager
    //                // related reason applies
    //                log.error(
    //                    "Couldn't create a file selector or lookup JNLP service", e);
    //
    //                throw new SecurityException(
    //                    "Could not create file selector or use JNLP service", e);
    //            }
    //        }

    if (fileContents != null) {
        return JFileChooser.APPROVE_OPTION;
    } else {
        return JFileChooser.CANCEL_OPTION;
    }
}

From source file:net.sf.jabref.gui.FindUnlinkedFilesDialog.java

/**
 * Opens a {@link JFileChooser} and receives the user input as a
 * {@link File} object, which this method returns. <br>
 * <br>//from  www. jav  a  2 s . c  o m
 * The "Open file" dialog will start at the path that is set in the
 * "directory" textfield, or at the last stored path for this dialog, if the
 * textfield is empty. <br>
 * <br>
 * If the user cancels the "Open file" dialog, this method returns null. <br>
 * <br>
 * If the user has selected a valid directory in the "Open file" dialog,
 * this path will be stored persistently for this dialog, so that it can be
 * preset at the next time this dialog is opened.
 *
 * @return The selected directory from the user, or <code>null</code>, if
 *         the user has aborted the selection.
 */
private Path chooseDirectory() {

    if (fileChooser == null) {
        fileChooser = new JFileChooser();
        fileChooser.setAutoscrolls(true);
        fileChooser.setDialogTitle(Localization.lang("Select directory"));
        fileChooser.setApproveButtonText(Localization.lang("Choose directory"));
        fileChooser.setApproveButtonToolTipText(
                Localization.lang("Use the selected directory to start with the search."));
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    }

    String path = textfieldDirectoryPath.getText();
    if (path.isEmpty()) {
        fileChooser.setCurrentDirectory(lastSelectedDirectory.toFile());
    } else {
        fileChooser.setCurrentDirectory(Paths.get(path).toFile());
    }

    int result = fileChooser.showOpenDialog(frame);
    if (result == JFileChooser.CANCEL_OPTION) {
        return null;
    }
    Path selectedDirectory = fileChooser.getSelectedFile().toPath();
    String filepath = "";
    if (selectedDirectory != null) {
        filepath = selectedDirectory.toAbsolutePath().toString();
    }
    textfieldDirectoryPath.setText(filepath);

    return selectedDirectory;
}

From source file:com.alvermont.terraj.util.ui.JNLPFileChooser.java

/**
 * Carry out the file save dialog// w w w  .j  a v a2  s  .  com
 * 
 * @param parent The parent for the dialog
 * @return The result of the user selection
 */
public int showSaveDialog(Component parent) {
    //        if (chooser != null)
    //        {
    int option = chooser.showSaveDialog(parent);

    if (option == JFileChooser.APPROVE_OPTION) {
        File target = addExtension(chooser.getSelectedFile());

        fileContents = new MyFileContents(target);
    } else {
        fileContents = null;
    }
    //        }
    //        else
    //        {
    //            // NOTE: we use the FileOpenService here instead of FileSaveService
    //            // because I couldn't get the latter to work. I just got
    //            // StreamClosed exceptions thrown all the time.
    //            FileOpenService fos;
    //
    //            try
    //            {
    //                fos = (FileOpenService) ServiceManager.lookup(
    //                        "javax.jnlp.FileOpenService");
    //
    //                try
    //                {
    //                    String[] extensions = new String[defaultExtensions.size()];
    //
    //                    defaultExtensions.toArray(extensions);
    //
    //                    fileContents = fos.openFileDialog(null, extensions);
    //                }
    //                catch (IOException ex)
    //                {
    //                    log.error("IOException in open file selection", ex);
    //
    //                    return JFileChooser.ERROR_OPTION;
    //                }
    //            }
    //            catch (UnavailableServiceException e)
    //            {
    //                // we assume that JNLP is not available and some other security manager
    //                // related reason applies
    //                log.error(
    //                    "Couldn't create a file selector or lookup JNLP service", e);
    //
    //                throw new SecurityException(
    //                    "Could not create file selector or use JNLP service", e);
    //            }
    //        }

    if (fileContents != null) {
        return JFileChooser.APPROVE_OPTION;
    } else {
        return JFileChooser.CANCEL_OPTION;
    }
}

From source file:com.evanbelcher.DrillBook.display.DBMenuBar.java

/**
 * Gets the show to open and opens the respective json file.
 *///from   ww  w .  j a va  2  s  . co  m
private void openShow() {
    if (askToSave()) {

        File file;
        final JFileChooser fc = new JFileChooser(Main.getFilePath());
        fc.setFileFilter(new DrillFileFilter());
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        int returnVal;
        do {
            returnVal = fc.showOpenDialog(this);
            file = fc.getSelectedFile();
        } while (returnVal != JFileChooser.CANCEL_OPTION && returnVal != JFileChooser.ERROR_OPTION
                && !file.exists());
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                String name, path;
                path = file.getCanonicalPath();
                path = path.substring(0, Math.max(path.lastIndexOf('\\'), path.lastIndexOf('/')) + 1);
                name = file.getName().toLowerCase().endsWith(".drill") ? file.getName()
                        : file.getName() + ".drill";
                Main.setFilePath(path);
                Main.setPagesFileName(name);
                Main.load(false);
                desktop.createNewInternalFrames();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:jboost.visualization.HistogramFrame.java

private File selectPDFFile() {

    File fFile = new File("default.pdf");
    JFileChooser fc = new JFileChooser();

    // Start in current directory
    fc.setCurrentDirectory(new File("."));

    // Set filter for Java source files.
    fc.setFileFilter(new FileFilter() {

        public boolean accept(File f) {
            String path = f.getAbsolutePath();
            if (f.isDirectory() || path.endsWith(".pdf"))
                return true;
            else//from  ww  w.  j a v a 2s.  co m
                return false;
        }

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

    // Set to a default name for save.
    fc.setSelectedFile(fFile);

    // Open chooser dialog
    int result = fc.showSaveDialog(this);

    if (result == JFileChooser.CANCEL_OPTION) {
        return null;
    } else if (result == JFileChooser.APPROVE_OPTION) {
        fFile = fc.getSelectedFile();
        if (fFile.exists()) {
            int response = JOptionPane.showConfirmDialog(null, "Overwrite existing file?", "Confirm Overwrite",
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.CANCEL_OPTION)
                return null;
        }
        return fFile;
    } else {
        return null;
    }
}

From source file:jboost.visualization.HistogramFrame.java

private File selectDumpFile() {

    File fFile = new File("ExamplesDumpFile.txt");
    JFileChooser fc = new JFileChooser();

    // Start in current directory
    fc.setCurrentDirectory(new File("."));

    // Set filter for Java source files.
    fc.setFileFilter(new FileFilter() {

        public boolean accept(File f) {
            String path = f.getAbsolutePath();
            if (f.isDirectory() || path.endsWith(".txt"))
                return true;
            else/*from w w  w .j  av  a 2 s. c  o m*/
                return false;
        }

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

    // Set to a default name for save.
    fc.setSelectedFile(fFile);

    // Open chooser dialog
    int result = fc.showSaveDialog(this);

    if (result == JFileChooser.CANCEL_OPTION) {
        return null;
    } else if (result == JFileChooser.APPROVE_OPTION) {
        fFile = fc.getSelectedFile();
        if (fFile.exists()) {
            int response = JOptionPane.showConfirmDialog(null, "Overwrite existing file?", "Confirm Overwrite",
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.CANCEL_OPTION)
                return null;
        }
        return fFile;
    } else {
        return null;
    }
}

From source file:cc.creativecomputing.io.CCIOUtil.java

static public String selectInput(String prompt, String theFolder) {
    fileChooser.setDialogTitle(prompt);//from www . j  a v a2 s  . c  om
    if (theFolder != null) {
        fileChooser.setCurrentDirectory(new File(theFolder));
    }

    int returned = fileChooser.showOpenDialog(null);
    if (returned == JFileChooser.CANCEL_OPTION) {
        selectedFile = null;
    } else {
        selectedFile = fileChooser.getSelectedFile();
    }
    return (selectedFile == null) ? null : selectedFile.getAbsolutePath();
}

From source file:cc.creativecomputing.io.CCIOUtil.java

/**
 * Opens a platform-specific file chooser dialog to select a file for input. This function returns the full path to
 * the selected file as a <b>String</b>, or <b>null</b> if no selection. Files are filtered according to the given
 * file extensions./*from   w w  w .ja  v a2  s .c o m*/
 * 
 * @webref input:files
 * @param theExtensions file extensions for filtering
 * @return full path to the selected file, or null if canceled.
 * 
 * @see #selectOutput(String)
 * @see #selectFolder(String)
 */
static public String selectFilteredInput(String... theExtensions) {
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setFileFilter(new CCFileExtensionFilter(theExtensions));
    fileChooser.setDialogTitle("Select a file...");

    int returned = fileChooser.showOpenDialog(null);
    if (returned == JFileChooser.CANCEL_OPTION) {
        selectedFile = null;
    } else {
        selectedFile = fileChooser.getSelectedFile();
    }
    return (selectedFile == null) ? null : selectedFile.getAbsolutePath();
}