Search in sources :

Example 41 with FileDialog

use of java.awt.FileDialog in project processing by processing.

the class Sketch method handleAddFile.

/**
   * Prompt the user for a new file to the sketch, then call the
   * other addFile() function to actually add it.
   */
public void handleAddFile() {
    // make sure the user didn't hide the sketch folder
    ensureExistence();
    // if read-only, give an error
    if (isReadOnly()) {
        // if the files are read-only, need to first do a "save as".
        Messages.showMessage(Language.text("add_file.messages.is_read_only"), Language.text("add_file.messages.is_read_only.description"));
        return;
    }
    // get a dialog, select a file to add to the sketch
    String prompt = Language.text("file");
    //FileDialog fd = new FileDialog(new Frame(), prompt, FileDialog.LOAD);
    FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD);
    fd.setVisible(true);
    String directory = fd.getDirectory();
    String filename = fd.getFile();
    if (filename == null)
        return;
    // copy the file into the folder. if people would rather
    // it move instead of copy, they can do it by hand
    File sourceFile = new File(directory, filename);
    // now do the work of adding the file
    boolean result = addFile(sourceFile);
    if (result) {
    //      editor.statusNotice("One file added to the sketch.");
    //Done from within TaskAddFile inner class when copying is completed
    }
}
Also used : FileDialog(java.awt.FileDialog)

Example 42 with FileDialog

use of java.awt.FileDialog in project processing by processing.

the class Chooser method selectFolder.

/**
   * See selectInput() for details.
   *
   * @webref input:files
   * @param prompt message to the user
   * @param callback name of the method to be called when the selection is made
   */
//  public void selectFolder(String prompt, String callback) {
//    selectFolder(prompt, callback, null);
//  }
//
//
//  public void selectFolder(String prompt, String callback, File file) {
//    selectFolder(prompt, callback, file, this);
//  }
//
//
//  public void selectFolder(String prompt, String callback,
//                           File file, Object callbackObject) {
//    selectFolder(prompt, callback, file, callbackObject, selectFrame());
//  }
public static void selectFolder(final Frame parentFrame, final String prompt, final File defaultSelection, final Callback callback) {
    //    EventQueue.invokeLater(new Runnable() {
    //      public void run() {
    File selectedFile = null;
    if (System.getProperty("os.name").contains("Mac") && useNativeSelect) {
        FileDialog fileDialog = new FileDialog(parentFrame, prompt, FileDialog.LOAD);
        System.setProperty("apple.awt.fileDialogForDirectories", "true");
        fileDialog.setVisible(true);
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
        String filename = fileDialog.getFile();
        if (filename != null) {
            selectedFile = new File(fileDialog.getDirectory(), fileDialog.getFile());
        }
    } else {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle(prompt);
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (defaultSelection != null) {
            fileChooser.setSelectedFile(defaultSelection);
        }
        int result = fileChooser.showOpenDialog(parentFrame);
        if (result == JFileChooser.APPROVE_OPTION) {
            selectedFile = fileChooser.getSelectedFile();
        }
    }
    //selectCallback(selectedFile, callbackMethod, callbackObject);
    callback.handle(selectedFile);
//      }
//    });
}
Also used : JFileChooser(javax.swing.JFileChooser) File(java.io.File) FileDialog(java.awt.FileDialog)

Example 43 with FileDialog

use of java.awt.FileDialog in project processing by processing.

the class Chooser method selectImpl.

protected static void selectImpl(final Frame parentFrame, final String prompt, final File defaultSelection, final Callback callback, final int mode) {
    //    EventQueue.invokeLater(new Runnable() {
    //      public void run() {
    File selectedFile = null;
    if (useNativeSelect) {
        FileDialog dialog = new FileDialog(parentFrame, prompt, mode);
        if (defaultSelection != null) {
            dialog.setDirectory(defaultSelection.getParent());
            dialog.setFile(defaultSelection.getName());
        }
        dialog.setVisible(true);
        String directory = dialog.getDirectory();
        String filename = dialog.getFile();
        if (filename != null) {
            selectedFile = new File(directory, filename);
        }
    } else {
        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(prompt);
        if (defaultSelection != null) {
            chooser.setSelectedFile(defaultSelection);
        }
        int result = -1;
        if (mode == FileDialog.SAVE) {
            result = chooser.showSaveDialog(parentFrame);
        } else if (mode == FileDialog.LOAD) {
            result = chooser.showOpenDialog(parentFrame);
        }
        if (result == JFileChooser.APPROVE_OPTION) {
            selectedFile = chooser.getSelectedFile();
        }
    }
    //selectCallback(selectedFile, callbackMethod, callbackObject);
    callback.handle(selectedFile);
//      }
//    });
}
Also used : JFileChooser(javax.swing.JFileChooser) File(java.io.File) FileDialog(java.awt.FileDialog)

Example 44 with FileDialog

use of java.awt.FileDialog in project EnrichmentMapApp by BaderLab.

the class FileBrowser method browseForRootFolderMac.

private Optional<File> browseForRootFolderMac(Component parent) {
    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, "Choose Root Folder", FileDialog.LOAD);
        else if (parent instanceof Frame)
            chooser = new FileDialog((Frame) parent, "Choose Root Folder", FileDialog.LOAD);
        else
            throw new IllegalArgumentException("parent must be Dialog or Frame");
        chooser.setDirectory(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 Optional.empty();
        }
        setCurrentDirectory(new File(dir));
        return Optional.of(new File(dir + File.separator + file));
    } finally {
        if (property != null) {
            System.setProperty("apple.awt.fileDialogForDirectories", property);
        }
    }
}
Also used : Frame(java.awt.Frame) FileDialog(java.awt.FileDialog) Dialog(java.awt.Dialog) FileDialog(java.awt.FileDialog) File(java.io.File)

Example 45 with FileDialog

use of java.awt.FileDialog in project processing by processing.

the class Base method handleOpenPrompt.

/**
   * Prompt for a sketch to open, and open it in a new window.
   */
public void handleOpenPrompt() {
    final StringList extensions = new StringList();
    for (Mode mode : getModeList()) {
        extensions.append(mode.getDefaultExtension());
    }
    final String prompt = Language.text("open");
    // don't use native dialogs on Linux (or anyone else w/ override)
    if (Preferences.getBoolean("chooser.files.native")) {
        //$NON-NLS-1$
        // use the front-most window frame for placing file dialog
        FileDialog openDialog = new FileDialog(activeEditor, prompt, FileDialog.LOAD);
        // Only show .pde files as eligible bachelors
        openDialog.setFilenameFilter(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                // confirmed to be working properly [fry 110128]
                for (String ext : extensions) {
                    if (name.toLowerCase().endsWith("." + ext)) {
                        //$NON-NLS-1$
                        return true;
                    }
                }
                return false;
            }
        });
        openDialog.setVisible(true);
        String directory = openDialog.getDirectory();
        String filename = openDialog.getFile();
        if (filename != null) {
            File inputFile = new File(directory, filename);
            handleOpen(inputFile.getAbsolutePath());
        }
    } else {
        if (openChooser == null) {
            openChooser = new JFileChooser();
        }
        openChooser.setDialogTitle(prompt);
        openChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {

            public boolean accept(File file) {
                // http://code.google.com/p/processing/issues/detail?id=1151
                if (file.isDirectory()) {
                    return true;
                }
                for (String ext : extensions) {
                    if (file.getName().toLowerCase().endsWith("." + ext)) {
                        //$NON-NLS-1$
                        return true;
                    }
                }
                return false;
            }

            public String getDescription() {
                return "Processing Sketch";
            }
        });
        if (openChooser.showOpenDialog(activeEditor) == JFileChooser.APPROVE_OPTION) {
            handleOpen(openChooser.getSelectedFile().getAbsolutePath());
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) StringList(processing.data.StringList) 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