Java JOptionPane Prompt promptForDatsDir(Component parentComponent)

Here you can find the source of promptForDatsDir(Component parentComponent)

Description

Modally prompts the user for the FTL resources dir.

License

Open Source License

Parameter

Parameter Description
parentComponent a parent for Swing dialogs, or null

Declaration

public static File promptForDatsDir(Component parentComponent) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

public class Main {
    /**/*from w  ww  .j  a va  2s  .c om*/
     * Modally prompts the user for the FTL resources dir.
     *
     * Reminder: GUI dialogs need to be in the event dispatch thread.
     *
     * @param parentComponent
     *            a parent for Swing dialogs, or null
     */
    public static File promptForDatsDir(Component parentComponent) {
        File result = null;

        String message = "";
        message += "You will now be prompted to locate FTL manually.\n";
        message += "Select '(FTL dir)/resources/data.dat'.\n";
        message += "Or 'FTL.app', if you're on OSX.";
        JOptionPane.showMessageDialog(parentComponent, message, "Find FTL",
                JOptionPane.INFORMATION_MESSAGE);

        final JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Find data.dat or FTL.app");
        fc.setFileHidingEnabled(false);
        fc.addChoosableFileFilter(new FileFilter() {
            @Override
            public String getDescription() {
                return "FTL Data File - (FTL dir)/resources/data.dat";
            }

            @Override
            public boolean accept(File f) {
                return f.isDirectory() || f.getName().equals("data.dat")
                        || f.getName().equals("FTL.app");
            }
        });
        fc.setMultiSelectionEnabled(false);

        if (fc.showOpenDialog(parentComponent) == JFileChooser.APPROVE_OPTION) {
            File f = fc.getSelectedFile();
            if (f.getName().equals("data.dat")) {
                result = f.getParentFile();
            } else if (f.getName().endsWith(".app") && f.isDirectory()) {
                File contentsPath = new File(f, "Contents");
                if (contentsPath.exists() && contentsPath.isDirectory()
                        && new File(contentsPath, "Resources").exists())
                    result = new File(contentsPath, "Resources");
            }
        }

        if (result != null && isDatsDirValid(result)) {
            return result;
        }

        return null;
    }

    /**
     * Confirms the FTL resources dir exists and contains the dat files.
     */
    public static boolean isDatsDirValid(File d) {
        if (!d.exists() || !d.isDirectory())
            return false;
        if (!new File(d, "data.dat").exists())
            return false;
        if (!new File(d, "resource.dat").exists())
            return false;
        return true;
    }
}

Related

  1. askYesNo(String title, String prompt, Object... args)
  2. promptForDecryptionPassword()
  3. promptForDelete(Component c, String title, String prompt)
  4. promptForDelete(Component c, String title, String prompt)
  5. showDbxAccessDeniedPrompt()