Search in sources :

Example 31 with FileDialog

use of java.awt.FileDialog in project fql by CategoricalData.

the class FileChooser method chooseFileAWT.

/*
	 * AWT FileDialog implementation of the file chooser. This is because
	 * JFileChooser doesn't work very well on non-Windows, but FileDialog
	 * doesn't work on Windows (it doesn't support file filters on Windows).
	 */
/**
 * @param title
 * @param mode
 * @param filter
 *
 * @return
 */
private static File chooseFileAWT(String title, Mode mode, FileFilter filter) {
    Easik e = Easik.getInstance();
    EasikSettings s = e.getSettings();
    FileDialog dialog = new FileDialog(e.getFrame(), title, (mode == Mode.SAVE) ? FileDialog.SAVE : FileDialog.LOAD);
    dialog.setDirectory(s.getDefaultFolder());
    if ((mode == Mode.DIRECTORY) && EasikConstants.RUNNING_ON_MAC) {
        System.setProperty("apple.awt.fileDialogForDirectories", "true");
    } else if (filter != null) {
        dialog.setFilenameFilter(filter);
    }
    // Show the dialog (this blocks until the user is done)
    dialog.setVisible(true);
    if ((mode == Mode.DIRECTORY) && EasikConstants.RUNNING_ON_MAC) {
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
    }
    String filename = dialog.getFile();
    if (filename == null) {
        return null;
    }
    File selected = new File(dialog.getDirectory(), filename);
    if (mode != Mode.DIRECTORY) {
        s.setProperty("folder_last", selected.getParentFile().getAbsolutePath());
    }
    return selected;
}
Also used : Easik(easik.Easik) FileDialog(java.awt.FileDialog) File(java.io.File) EasikSettings(easik.EasikSettings)

Example 32 with FileDialog

use of java.awt.FileDialog in project CodenameOne by codenameone.

the class ResourceEditorView method showFileChooser.

public static File[] showFileChooser(boolean multi, boolean open, boolean dir, String dialogTitle, final String label, final String... type) {
    if (!dir && multi && ResourceEditorApp.IS_MAC) {
        FileDialog fd = new FileDialog(java.awt.Frame.getFrames()[0]);
        // check if we can hack Java 7
        try {
            Method m = fd.getClass().getMethod("setMultipleMode", Boolean.TYPE);
            m.invoke(fd, true);
            fd.setFilenameFilter(new FilenameFilter() {

                public boolean accept(File dir, String name) {
                    name = name.toLowerCase();
                    for (String t : type) {
                        if (name.endsWith(t)) {
                            return true;
                        }
                    }
                    return false;
                }
            });
            if (open) {
                fd.setMode(FileDialog.LOAD);
            } else {
                fd.setMode(FileDialog.SAVE);
            }
            if (dialogTitle != null) {
                fd.setTitle(dialogTitle);
            }
            String defaultDir = Preferences.userNodeForPackage(ResourceEditorView.class).get("lastDir", System.getProperty("user.home"));
            fd.setDirectory(defaultDir);
            fd.pack();
            fd.setLocationByPlatform(true);
            fd.setVisible(true);
            m = fd.getClass().getMethod("getFiles");
            File[] files = (File[]) m.invoke(fd);
            if (files != null && files.length > 0) {
                Preferences.userNodeForPackage(ResourceEditorView.class).put("lastDir", files[0].getAbsolutePath());
                return files;
            }
            return null;
        } catch (Throwable t) {
            // failed...
            t.printStackTrace();
        }
    }
    if (ResourceEditorApp.IS_MAC && !dir && !multi) {
        // on Mac we prefer the native AWT file chooser which is far superior
        FileDialog fd = new FileDialog(java.awt.Frame.getFrames()[0]);
        fd.setFilenameFilter(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                name = name.toLowerCase();
                for (String t : type) {
                    if (name.endsWith(t)) {
                        return true;
                    }
                }
                return false;
            }
        });
        if (open) {
            fd.setMode(FileDialog.LOAD);
        } else {
            fd.setMode(FileDialog.SAVE);
        }
        if (dialogTitle != null) {
            fd.setTitle(dialogTitle);
        }
        String defaultDir = Preferences.userNodeForPackage(ResourceEditorView.class).get("lastDir", System.getProperty("user.home"));
        fd.setDirectory(defaultDir);
        fd.pack();
        fd.setLocationByPlatform(true);
        fd.setVisible(true);
        if (fd.getFile() != null) {
            File selection = new File(fd.getDirectory(), fd.getFile());
            Preferences.userNodeForPackage(ResourceEditorView.class).put("lastDir", selection.getAbsolutePath());
            return new File[] { selection };
        }
        return null;
    }
    JFileChooser c = createFileChooser(label, type);
    c.setMultiSelectionEnabled(multi);
    if (dialogTitle != null) {
        c.setDialogTitle(dialogTitle);
    }
    for (String t : type) {
        if (t.endsWith("jpg") || t.endsWith("gif") || t.endsWith("png") || t.endsWith("svg")) {
            new PreviewPane(c);
            break;
        }
    }
    if (dir) {
        c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    } else {
        c.setFileSelectionMode(JFileChooser.FILES_ONLY);
    }
    if (open) {
        if (c.showOpenDialog(JFrame.getFrames()[0]) != JFileChooser.APPROVE_OPTION) {
            return null;
        }
    } else {
        if (c.showSaveDialog(JFrame.getFrames()[0]) != JFileChooser.APPROVE_OPTION) {
            return null;
        }
    }
    Preferences.userNodeForPackage(ResourceEditorView.class).put("lastDir", c.getSelectedFile().getParentFile().getAbsolutePath());
    if (multi) {
        return c.getSelectedFiles();
    } else {
        return new File[] { c.getSelectedFile() };
    }
}
Also used : FilenameFilter(java.io.FilenameFilter) JFileChooser(javax.swing.JFileChooser) Method(java.lang.reflect.Method) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 33 with FileDialog

use of java.awt.FileDialog in project CodenameOne by codenameone.

the class JavaSEPort method pickFile.

private File pickFile(final String[] types, String name) {
    FileDialog fd = new FileDialog(java.awt.Frame.getFrames()[0]);
    fd.setFilenameFilter(new FilenameFilter() {

        public boolean accept(File dir, String name) {
            name = name.toLowerCase();
            for (String t : types) {
                if (name.endsWith(t) || "*".equals(t)) {
                    return true;
                }
            }
            return false;
        }
    });
    fd.setFile(name);
    fd.pack();
    fd.setLocationByPlatform(true);
    fd.setVisible(true);
    if (fd.getFile() != null) {
        name = fd.getFile().toLowerCase();
        for (String t : types) {
            if (name.endsWith(t)) {
                File f = new File(fd.getDirectory(), fd.getFile());
                if (!f.exists()) {
                    return new File(fd.getDirectory());
                } else {
                    return f;
                }
            }
        }
    }
    return null;
}
Also used : FilenameFilter(java.io.FilenameFilter) FileDialog(java.awt.FileDialog)

Example 34 with FileDialog

use of java.awt.FileDialog in project hid-serial by rayshobby.

the class G4P method selectImpl.

/**
 * The implementation of the select input and output methods.
 * @param prompt
 * @param mode
 * @param types
 * @param typeDesc
 * @return the absolute path name for the selected folder, or null if action
 * cancelled.
 */
private static String selectImpl(String prompt, int mode, String types, String typeDesc) {
    // If no initial selection made then use last selection
    // Assume that a file will not be selected
    String selectedFile = null;
    // Get the owner
    Frame owner = (sketchApplet == null) ? null : sketchApplet.frame;
    // Create a file filter
    if (PApplet.useNativeSelect) {
        FileDialog dialog = new FileDialog(owner, prompt, mode);
        FilenameFilter filter = null;
        if (types != null && types.length() > 0) {
            filter = new FilenameChooserFilter(types);
            dialog.setFilenameFilter(filter);
        }
        dialog.setVisible(true);
        String directory = dialog.getDirectory();
        if (directory != null) {
            selectedFile = dialog.getFile();
            if (selectedFile != null) {
                try {
                    selectedFile = (new File(directory, selectedFile)).getCanonicalPath();
                } catch (IOException e) {
                    selectedFile = null;
                }
            }
        }
    } else {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(prompt);
        FileFilter filter = null;
        if (types != null && types.length() > 0) {
            filter = new FileChooserFilter(types, typeDesc);
            chooser.setFileFilter(filter);
        }
        int result = JFileChooser.ERROR_OPTION;
        if (mode == FileDialog.SAVE) {
            result = chooser.showSaveDialog(owner);
        } else if (mode == FileDialog.LOAD) {
            result = chooser.showOpenDialog(owner);
        }
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                selectedFile = chooser.getSelectedFile().getCanonicalPath();
            } catch (IOException e) {
                selectedFile = null;
            }
        }
    }
    return selectedFile;
}
Also used : FilenameFilter(java.io.FilenameFilter) Frame(java.awt.Frame) JFileChooser(javax.swing.JFileChooser) IOException(java.io.IOException) FileFilter(javax.swing.filechooser.FileFilter) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 35 with FileDialog

use of java.awt.FileDialog in project SIMVA-SoS by SESoS.

the class Main method openWindow.

// open file
// type 1: open CSV file - goal model
// type 2: open txt file - slicing criterion
public static Set<Goal> openWindow(int type, JFrame frame, JTextArea console, JTextField textField) throws IOException {
    FileDialog fileDialog = null;
    if (type == 1) {
        fileDialog = new FileDialog(frame, "Open Goal Model", FileDialog.LOAD);
        fileDialog.setFile("*.csv");
        fileDialog.setVisible(true);
        File[] goalModel = fileDialog.getFiles();
        if (goalModel.length != 0) {
            GoalModelParser gmp = new GoalModelParser();
            gmp.parseGoalModel(goalModel[0]);
            consolePrint(console, gmp.getGoalLog());
            textField.setText(goalModel[0].getAbsolutePath());
            return gmp.getGoals();
        }
    } else if (type == 2) {
        fileDialog = new FileDialog(frame, "Open Slicing Criterion", FileDialog.LOAD);
        fileDialog.setFile("*.txt");
        fileDialog.setVisible(true);
        File[] criterion = fileDialog.getFiles();
        if (criterion.length != 0) {
            CriterionParser cp = new CriterionParser();
            textField.setText(criterion[0].getAbsolutePath());
            return cp.parseCriterion(criterion[0]);
        }
    }
    return null;
}
Also used : FileDialog(java.awt.FileDialog) File(java.io.File)

Aggregations

FileDialog (java.awt.FileDialog)78 File (java.io.File)43 JFileChooser (javax.swing.JFileChooser)18 Frame (java.awt.Frame)16 IOException (java.io.IOException)16 FilenameFilter (java.io.FilenameFilter)10 Dialog (java.awt.Dialog)8 FileFilter (javax.swing.filechooser.FileFilter)7 FileOutputStream (java.io.FileOutputStream)5 PrintStream (java.io.PrintStream)4 Button (java.awt.Button)3 Dimension (java.awt.Dimension)3 MenuItem (java.awt.MenuItem)3 ActionEvent (java.awt.event.ActionEvent)3 ActionListener (java.awt.event.ActionListener)3 MalformedURLException (java.net.MalformedURLException)3 SQLException (java.sql.SQLException)3 JLabel (javax.swing.JLabel)3 Component (java.awt.Component)2 GridLayout (java.awt.GridLayout)2