Search in sources :

Example 61 with FileDialog

use of java.awt.FileDialog in project briefcase by opendatakit.

the class FileChooser method buildFileDialog.

static FileDialog buildFileDialog(Container parent, Optional<File> initialLocation, JFileChooser fileChooser) {
    System.setProperty("apple.awt.fileDialogForDirectories", fileChooser.getFileSelectionMode() == DIRECTORIES_ONLY ? "true" : "false");
    Window windowAncestor = SwingUtilities.getWindowAncestor(parent);
    FileDialog fileDialog = windowAncestor instanceof Frame ? new FileDialog((Frame) windowAncestor, fileChooser.getDialogTitle()) : new FileDialog((Dialog) windowAncestor, fileChooser.getDialogTitle());
    if (fileChooser.getFileSelectionMode() == DIRECTORIES_ONLY)
        fileDialog.setFilenameFilter((dir, name) -> new File(dir, name).isDirectory());
    initialLocation.ifPresent(file -> fileDialog.setFile(file.getAbsolutePath()));
    return fileDialog;
}
Also used : Window(java.awt.Window) OPEN_DIALOG(javax.swing.JFileChooser.OPEN_DIALOG) Frame(java.awt.Frame) Predicate(java.util.function.Predicate) Window(java.awt.Window) FindDirectoryStructure.isUnix(org.opendatakit.briefcase.util.FindDirectoryStructure.isUnix) FileFilter(javax.swing.filechooser.FileFilter) FILES_ONLY(javax.swing.JFileChooser.FILES_ONLY) File(java.io.File) FindDirectoryStructure.isWindows(org.opendatakit.briefcase.util.FindDirectoryStructure.isWindows) SwingUtilities(javax.swing.SwingUtilities) FileDialog(java.awt.FileDialog) Optional(java.util.Optional) Container(java.awt.Container) JFileChooser(javax.swing.JFileChooser) DIRECTORIES_ONLY(javax.swing.JFileChooser.DIRECTORIES_ONLY) Dialog(java.awt.Dialog) Frame(java.awt.Frame) FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 62 with FileDialog

use of java.awt.FileDialog in project cytoscape-impl by cytoscape.

the class FileUtilImpl method getFolder.

@Override
public File getFolder(Component parent, String title, String startDir) {
    if (parent == null)
        throw new NullPointerException("\"parent\" must not be null.");
    final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
    final String osName = System.getProperty("os.name");
    if (osName.startsWith("Mac")) {
        final String property = System.getProperty("apple.awt.fileDialogForDirectories");
        System.setProperty("apple.awt.fileDialogForDirectories", "true");
        try {
            FileDialog chooser;
            if (parent instanceof Dialog)
                chooser = new FileDialog((Dialog) parent, title, FileDialog.LOAD);
            else if (parent instanceof Frame)
                chooser = new FileDialog((Frame) parent, title, FileDialog.LOAD);
            else
                throw new IllegalArgumentException("parent must be Dialog or Frame");
            if (startDir != null)
                chooser.setDirectory(startDir);
            else
                chooser.setDirectory(applicationManager.getCurrentDirectory().getAbsolutePath());
            chooser.setModal(true);
            chooser.setLocationRelativeTo(parent);
            chooser.setVisible(true);
            String file = chooser.getFile();
            String dir = chooser.getDirectory();
            if (file == null || dir == null) {
                return null;
            }
            return new File(dir + File.separator + file);
        } finally {
            if (property != null) {
                System.setProperty("apple.awt.fileDialogForDirectories", property);
            }
        }
    } else {
        // this is not a Mac, use the Swing based file dialog
        final JFileChooser chooser;
        if (startDir != null)
            chooser = new JFileChooser(new File(startDir));
        else
            chooser = new JFileChooser(applicationManager.getCurrentDirectory());
        chooser.setDialogTitle(title);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
        File result = null;
        if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
            result = chooser.getSelectedFile();
        }
        if (result != null && chooser.getCurrentDirectory().getPath() != null)
            applicationManager.setCurrentDirectory(chooser.getCurrentDirectory());
        return result;
    }
}
Also used : CyApplicationManager(org.cytoscape.application.CyApplicationManager) Frame(java.awt.Frame) JFileChooser(javax.swing.JFileChooser) FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 63 with FileDialog

use of java.awt.FileDialog in project cytoscape-impl by cytoscape.

the class FileUtilImpl method getFiles.

@Override
public File[] getFiles(final Component parent, final String title, final int loadSaveCustom, String startDir, final String customApproveText, final boolean multiselect, final Collection<FileChooserFilter> filters) {
    if (parent == null)
        throw new NullPointerException("\"parent\" must not be null.");
    final String osName = System.getProperty("os.name");
    final CyApplicationManager applicationManager = serviceRegistrar.getService(CyApplicationManager.class);
    if (osName.startsWith("Mac")) {
        // This is a Macintosh, use the AWT style file dialog
        final String fileDialogForDirectories = System.getProperty("apple.awt.fileDialogForDirectories");
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
        try {
            final FileDialog chooser;
            if (parent instanceof Frame)
                chooser = new FileDialog((Frame) parent, title, loadSaveCustom);
            else if (parent instanceof Dialog)
                chooser = new FileDialog((Dialog) parent, title, loadSaveCustom);
            else if (parent instanceof JMenuItem) {
                JComponent jcomponent = (JComponent) ((JPopupMenu) parent.getParent()).getInvoker();
                chooser = new FileDialog((Frame) jcomponent.getTopLevelAncestor(), title, loadSaveCustom);
            } else {
                throw new IllegalArgumentException("Cannot (not implemented yet) create a dialog " + "own by a parent component of type: " + parent.getClass().getCanonicalName());
            }
            if (startDir != null)
                chooser.setDirectory(startDir);
            else
                chooser.setDirectory(applicationManager.getCurrentDirectory().getAbsolutePath());
            chooser.setModal(true);
            chooser.setFilenameFilter(new CombinedFilenameFilter(filters));
            chooser.setLocationRelativeTo(parent);
            chooser.setMultipleMode(multiselect);
            chooser.setVisible(true);
            if (chooser.getFile() != null) {
                final File[] results;
                if (loadSaveCustom == SAVE) {
                    String newFileName = chooser.getFile();
                    final String fileNameWithExt = addFileExt(filters, newFileName);
                    if (!fileNameWithExt.equals(newFileName)) {
                        final File file = new File(chooser.getDirectory() + File.separator + fileNameWithExt);
                        if (file.exists()) {
                            int answer = JOptionPane.showConfirmDialog(parent, "The file '" + file.getName() + "' already exists. Are you sure you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                            if (// Try again
                            answer == JOptionPane.NO_OPTION)
                                return getFiles(parent, title, loadSaveCustom, file.getParent(), customApproveText, multiselect, filters);
                        }
                        newFileName = fileNameWithExt;
                    }
                    results = new File[1];
                    results[0] = new File(chooser.getDirectory() + File.separator + newFileName);
                } else
                    results = chooser.getFiles();
                if (chooser.getDirectory() != null)
                    applicationManager.setCurrentDirectory(new File(chooser.getDirectory()));
                return results;
            }
        } finally {
            if (fileDialogForDirectories != null)
                System.setProperty("apple.awt.fileDialogForDirectories", fileDialogForDirectories);
        }
        return null;
    } else {
        // this is not a Mac, use the Swing based file dialog
        final JFileChooser chooser;
        if (startDir != null)
            chooser = new JFileChooser(new File(startDir));
        else
            chooser = new JFileChooser(applicationManager.getCurrentDirectory());
        // set multiple selection, if applicable
        chooser.setMultiSelectionEnabled(multiselect);
        // set the dialog title
        chooser.setDialogTitle(title);
        chooser.setAcceptAllFileFilterUsed(loadSaveCustom == LOAD);
        int i = 0;
        FileChooserFilter defaultFilter = null;
        for (final FileChooserFilter filter : filters) {
            // do it now!
            if (++i == filters.size() && defaultFilter == null)
                defaultFilter = filter;
            else // with "All ", make it the default.
            if (defaultFilter == null && filter.getDescription().startsWith("All "))
                defaultFilter = filter;
            chooser.addChoosableFileFilter(filter);
        }
        File[] results = null;
        File tmp = null;
        // set the dialog type
        if (loadSaveCustom == LOAD) {
            if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
                if (multiselect)
                    results = chooser.getSelectedFiles();
                else if ((tmp = chooser.getSelectedFile()) != null) {
                    results = new File[1];
                    results[0] = tmp;
                }
                if (filters != null && !filters.isEmpty()) {
                    boolean extensionFound = false;
                    for (int k = 0; k < results.length; ++k) {
                        String path = results[k].getPath();
                        for (final FileChooserFilter filter : filters) {
                            String[] filterExtensions = filter.getExtensions();
                            for (int t = 0; t < filterExtensions.length; ++t) {
                                if (filterExtensions[t].equals("") || path.endsWith("." + filterExtensions[t]))
                                    extensionFound = true;
                            }
                        }
                        if (!extensionFound) {
                            JOptionPane.showMessageDialog(chooser, "Cytoscape does not recognize files with suffix '" + path.substring(path.lastIndexOf(".")) + "' . Please choose another file.", "File extension incorrect", JOptionPane.WARNING_MESSAGE);
                            return null;
                        }
                        extensionFound = false;
                    }
                }
            }
        } else if (loadSaveCustom == SAVE) {
            if (chooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
                if (multiselect) {
                    results = chooser.getSelectedFiles();
                } else if ((tmp = chooser.getSelectedFile()) != null) {
                    results = new File[1];
                    results[0] = tmp;
                }
                // not, so we need to do so ourselves:
                for (int k = 0; k < results.length; ++k) {
                    File file = results[k];
                    final String filePath = file.getAbsolutePath();
                    final String filePathWithExt = addFileExt(filters, filePath);
                    // Add an extension to the filename, if necessary and possible
                    if (!filePathWithExt.equals(filePath)) {
                        file = new File(filePathWithExt);
                        results[k] = file;
                    }
                    if (file.exists()) {
                        int answer = JOptionPane.showConfirmDialog(chooser, "The file '" + file.getName() + "' already exists. Are you sure you want to overwrite it?", "File exists", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                        if (// Try again
                        answer == JOptionPane.NO_OPTION)
                            return getFiles(parent, title, loadSaveCustom, file.getParent(), customApproveText, multiselect, filters);
                    }
                }
            }
        } else {
            if (chooser.showDialog(parent, customApproveText) == JFileChooser.APPROVE_OPTION) {
                if (multiselect)
                    results = chooser.getSelectedFiles();
                else if ((tmp = chooser.getSelectedFile()) != null) {
                    results = new File[1];
                    results[0] = tmp;
                }
            }
        }
        if (results != null && chooser.getCurrentDirectory().getPath() != null)
            applicationManager.setCurrentDirectory(chooser.getCurrentDirectory());
        return results;
    }
}
Also used : Frame(java.awt.Frame) JComponent(javax.swing.JComponent) FileChooserFilter(org.cytoscape.util.swing.FileChooserFilter) CyApplicationManager(org.cytoscape.application.CyApplicationManager) JFileChooser(javax.swing.JFileChooser) FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) JMenuItem(javax.swing.JMenuItem) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 64 with FileDialog

use of java.awt.FileDialog in project AlgorithmsSolutions by Allenskoo856.

the class Draw method actionPerformed.

/**
 * This method cannot be called directly.
 */
@Override
public void actionPerformed(ActionEvent e) {
    FileDialog chooser = new FileDialog(frame, "Use a .png or .jpg extension", FileDialog.SAVE);
    chooser.setVisible(true);
    String filename = chooser.getFile();
    if (filename != null) {
        save(chooser.getDirectory() + File.separator + chooser.getFile());
    }
}
Also used : FileDialog(java.awt.FileDialog)

Example 65 with FileDialog

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

the class GUI method getOpenDialog.

private static FileDialog getOpenDialog() {
    if (openDialog != null) {
        openDialog.setFile(AllNameFilter.getAllString());
        return openDialog;
    }
    openDialog = new FileDialog((Dialog) null, "Open", FileDialog.LOAD);
    openDialog.setFile(AllNameFilter.getAllString());
    openDialog.setFilenameFilter(new AllNameFilter());
    // if (!GlobalOptions.debug.general.file_path.isEmpty()) {
    openDialog.setDirectory(IdeOptions.theCurrentOptions.getFile(IdeOption.FILE_PATH).getAbsolutePath());
    // }
    openDialog.setMultipleMode(true);
    return openDialog;
}
Also used : FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) FileDialog(java.awt.FileDialog)

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