Example usage for java.awt FileDialog getDirectory

List of usage examples for java.awt FileDialog getDirectory

Introduction

In this page you can find the example usage for java.awt FileDialog getDirectory.

Prototype

public String getDirectory() 

Source Link

Document

Gets the directory of this file dialog.

Usage

From source file:org.neo4j.desktop.ui.BrowseForDatabaseActionListener.java

private File macFileSelection() {
    System.setProperty("apple.awt.fileDialogForDirectories", "true");
    FileDialog fileDialog = new FileDialog(frame);

    fileDialog.setDirectory(directoryDisplay.getText());
    fileDialog.setVisible(true);//from ww  w.  j av  a  2 s .  c  o m

    File selectedFile = new File(fileDialog.getDirectory(), fileDialog.getFile());
    System.setProperty("apple.awt.fileDialogForDirectories", "false");

    return selectedFile;
}

From source file:ustc.sse.rjava.RJavaInterface.java

public String rChooseFile(Rengine re, int newFile) {
    FileDialog fd = new FileDialog(new Frame(), (newFile == 0) ? "Select a file" : "Select a new file",
            (newFile == 0) ? FileDialog.LOAD : FileDialog.SAVE);
    fd.setVisible(true);//ww  w  . ja v a2 s  .  c  om
    String res = null;
    if (fd.getDirectory() != null)
        res = fd.getDirectory();
    if (fd.getFile() != null)
        res = (res == null) ? fd.getFile() : (res + fd.getFile());
    return res;
}

From source file:FileViewer.java

/**
 * Handle button clicks/*from   w  w w .  j  av  a2s  .  co m*/
 */
public void actionPerformed(ActionEvent e) {
    String cmd = e.getActionCommand();
    if (cmd.equals("open")) { // If user clicked "Open" button
        // Create a file dialog box to prompt for a new file to display
        FileDialog f = new FileDialog(this, "Open File", FileDialog.LOAD);
        f.setDirectory(directory); // Set the default directory

        // Display the dialog and wait for the user's response
        f.show();

        directory = f.getDirectory(); // Remember new default directory
        setFile(directory, f.getFile()); // Load and display selection
        f.dispose(); // Get rid of the dialog box
    } else if (cmd.equals("close")) // If user clicked "Close" button
        this.dispose(); // then close the window
}

From source file:uk.nhs.cfh.dsp.srth.desktop.actions.querycrud.core.actions.SaveQueryToFileTask.java

/**
 * Do in background./* w  w w .j  a v  a2s.com*/
 * 
 * @return the void
 * 
 * @throws Exception the exception
 */
@Override
protected Boolean doInBackground() throws Exception {

    boolean result = false;
    QueryStatement query = queryService.getActiveQuery();
    // open file dialog to get save location
    FileDialog fd = new FileDialog(applicationService.getFrameView().getActiveFrame(),
            "Select location to save", FileDialog.SAVE);
    fd.setVisible(true);

    String selectedFile = fd.getFile();
    String selectedDirectory = fd.getDirectory();
    fileSelected = fd.getFile();
    if (selectedDirectory != null && selectedFile != null) {
        // save to selected location
        File physicalLocation = new File(selectedDirectory, selectedFile);
        query.setPhysicalLocation(physicalLocation.toURI());
        queryExpressionFileOutputter.save(query, physicalLocation.toURI());

        // notify query has changed
        queryService.queryChanged(query, this);
        // change result to true
        result = true;
    }

    return result;
}

From source file:uk.nhs.cfh.dsp.srth.desktop.actions.querycrud.core.actions.LoadSavedQueryTask.java

/**
 * Do in background.//from w w w .jav  a  2 s  .  c o  m
 *
 * @return the query statement
 *
 * @throws Exception the exception
 */
@Override
protected Void doInBackground() throws Exception {

    // open file dialog
    FileDialog fd = new FileDialog(applicationService.getFrameView().getActiveFrame(), "Select file to load",
            FileDialog.LOAD);
    fd.setVisible(true);

    fileSelected = fd.getFile();
    String directorySelected = fd.getDirectory();
    if (fileSelected != null && directorySelected != null) {
        File physicalFile = new File(directorySelected, fileSelected);
        // use file name load file from
        XMLLoader loader = new XMLLoader();
        Document doc = loader.getXMLDoc(physicalFile);

        query = queryExpressionXMLConverter.getQueryStatementFromElement(doc.getRootElement());
        if (query != null) {
            // assign physical location to query
            query.setPhysicalLocation(physicalFile.toURI());

            // register query and set to active query
            queryService.registerQuery(query);
            queryService.queryChanged(query, this);
        }
    }

    return null;
}

From source file:GUIMediaStatistics.java

/************************************************************************
* Respond to user button presses - either browsing for a file or
* gathering format info on the current file.
************************************************************************/
public void actionPerformed(ActionEvent e) {

    /////////////////////////////////
    // Browse - use FileDialog class.
    /////////////////////////////////
    if (e.getSource() == browseButton) {
        FileDialog choice = new FileDialog(this, "Media File Choice", FileDialog.LOAD);
        if (lastDirectory != null)
            choice.setDirectory(lastDirectory);
        choice.show();/*w w w.  j  av  a2 s .  c om*/
        String selection = choice.getFile();
        if (selection != null) {
            lastDirectory = choice.getDirectory();
            mediaField.setText("file://" + choice.getDirectory() + selection);
        }
    }
    ////////////////////////////////////////////////////////
    // Get statistics - create a MediaStatistics object and
    // monitor its progress.
    ///////////////////////////////////////////////////////
    else {
        stats = new MediaStatistics(mediaField.getText());
        monitorAndReport();
    }
}

From source file:org.mbs3.juniuploader.gui.pnlWoWDirectories.java

private void btnAddDirectoryActionPerformed(ActionEvent evt) {
    if (evt.getSource() == this.btnAddDirectory) {
        boolean failed = false;
        boolean mac = Util.isMac();
        if (mac) {
            java.awt.Frame f = jUniUploader.inst;
            FileDialog fd = new FileDialog(f, "Select a World of Warcraft Directory", FileDialog.LOAD);
            try {
                fd.show();//w w  w . jav a 2 s . c o  m

                String fileName = fd.getFile();
                String rootDir = fd.getDirectory();
                String completePath = (rootDir == null ? "" : rootDir) + (fileName == null ? "" : fileName);
                log.debug("Adding OS X style " + completePath);
                if (completePath != null) {
                    File file = new File(completePath);
                    if (file != null && file.exists() && file.isDirectory()) {
                        WDirectory wd = new WDirectory(file);
                        frmMain.wowDirectories.addElement(wd);
                    }

                }

            } catch (Exception ex) {
                failed = true;
                log.warn("Failed trying to display a FileDialog, falling back to JFileChooser", ex);
            }

        }
        if (!mac || failed) {
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fc.showOpenDialog(this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                if (file != null && file.exists() && file.isDirectory()) {
                    WDirectory wd = new WDirectory(file);
                    frmMain.wowDirectories.addElement(wd);
                }

            } else {
                log.trace("Open command cancelled by user.");
            }

        }
    }
}

From source file:uk.nhs.cfh.dsp.srth.desktop.modules.queryresultspanel.actions.ExportResultTask.java

@Override
protected Void doInBackground() throws Exception {

    // open file dialog to get save location
    FileDialog fd = new FileDialog(applicationService.getFrameView().getActiveFrame(),
            "Select location to save", FileDialog.SAVE);
    fd.setVisible(true);/*from  w ww .ja  v  a 2s.c o m*/

    String selectedFile = fd.getFile();
    String selectedDirectory = fd.getDirectory();
    if (selectedDirectory != null && selectedFile != null && resultSet != null) {
        try {
            // save to selected location
            File file = new File(selectedDirectory, selectedFile);
            // export to flat file
            FileWriter fw = new FileWriter(file);
            fw.flush();

            // add column names to data
            String cols = "";
            int colNum = resultSet.getMetaData().getColumnCount();
            for (int c = 0; c < colNum; c++) {
                cols = cols + "\t" + resultSet.getMetaData().getColumnName(c + 1);
            }

            // trim line and add new line character
            cols = cols.trim();
            cols = cols + "\n";
            // write to file
            fw.write(cols);

            float totalRows = resultSetCount;
            float percent = 0;
            int counter = 0;

            // reset resultSet to first row in case its been iterated over before
            resultSet.beforeFirst();
            while (resultSet.next()) {
                String line = "";
                for (int l = 0; l < colNum; l++) {
                    Object o = resultSet.getObject(l + 1);
                    if (o instanceof Date) {
                        o = sdf.format((Date) o);
                    } else if (o instanceof byte[]) {
                        byte[] bytes = (byte[]) o;
                        o = UUID.nameUUIDFromBytes(bytes).toString();
                    }

                    line = line + "\t" + o.toString();
                }

                // trim line
                line = line.trim();
                // append new line character to line
                line = line + "\n";
                // write to file
                fw.write(line);

                // update progress bar
                percent = (counter / totalRows) * 100;
                setProgress((int) percent);
                setMessage("Exported " + counter + " of " + totalRows + " rows");

                counter++;
            }

            // close file writer
            fw.close();

        } catch (IOException e) {
            logger.warn("Nested exception is : " + e.fillInStackTrace());
        }
    }

    return null;
}

From source file:processing.app.tools.Archiver.java

public void run() {
    SketchController sketch = editor.getSketchController();

    // first save the sketch so that things don't archive strangely
    boolean success = false;
    try {//w w w . jav  a  2 s.c  o m
        success = sketch.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (!success) {
        Base.showWarning(tr("Couldn't archive sketch"),
                tr("Archiving the sketch has been canceled because\nthe sketch couldn't save properly."), null);
        return;
    }

    File location = sketch.getSketch().getFolder();
    String name = location.getName();
    File parent = new File(location.getParent());

    //System.out.println("loc " + location);
    //System.out.println("par " + parent);

    File newbie = null;
    String namely = null;
    int index = 0;
    do {
        // only use the date if the sketch name isn't the default name
        useDate = !name.startsWith("sketch_");

        if (useDate) {
            String purty = dateFormat.format(new Date());
            String stamp = purty + ((char) ('a' + index));
            namely = name + "-" + stamp;
            newbie = new File(parent, namely + ".zip");

        } else {
            String diggie = numberFormat.format(index + 1);
            namely = name + "-" + diggie;
            newbie = new File(parent, namely + ".zip");
        }
        index++;
    } while (newbie.exists());

    // open up a prompt for where to save this fella
    FileDialog fd = new FileDialog(editor, tr("Archive sketch as:"), FileDialog.SAVE);
    fd.setDirectory(parent.getAbsolutePath());
    fd.setFile(newbie.getName());
    fd.setVisible(true);

    String directory = fd.getDirectory();
    String filename = fd.getFile();

    // only write the file if not canceled
    if (filename != null) {
        newbie = new File(directory, filename);

        ZipOutputStream zos = null;
        try {
            //System.out.println(newbie);
            zos = new ZipOutputStream(new FileOutputStream(newbie));

            // recursively fill the zip file
            buildZip(location, name, zos);

            // close up the jar file
            zos.flush();
            editor.statusNotice("Created archive " + newbie.getName() + ".");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(zos);
        }
    } else {
        editor.statusNotice(tr("Archive sketch canceled."));
    }
}

From source file:org.cds06.speleograph.actions.OpenAction.java

/**
 * Invoked when an action occurs.//from   w  w  w.  j av  a 2  s  .  co  m
 */
@Override
public void actionPerformed(ActionEvent e) {
    chooser.setCurrentDirectory(SpeleoGraphApp.getWorkingDirectory());
    File file = null;
    if (SpeleoGraphApp.isMac()) {
        do {
            if (file != null && !fileFilter.accept(file)) {
                JOptionPane.showMessageDialog(SpeleoGraphApp.getInstance(),
                        I18nSupport.translate("actions.open.formaterror"), I18nSupport.translate("error"),
                        JOptionPane.ERROR_MESSAGE);
            }
            FileDialog chooserMac = new FileDialog(SpeleoGraphApp.getInstance());
            chooserMac.setDirectory(SpeleoGraphApp.getWorkingDirectory().getAbsolutePath());
            chooserMac.setVisible(true);
            if (chooserMac.getFile() == null)
                return;
            file = FileUtils.getFile(chooserMac.getDirectory(), chooserMac.getFile());
            if (file.isDirectory())
                return;
        } while (!fileFilter.accept(file));
    } else {
        int result = chooser.showOpenDialog(parent);
        switch (result) {
        case JFileChooser.APPROVE_OPTION:
            file = chooser.getSelectedFile();
            if (file.isDirectory())
                return;
            break;
        case JFileChooser.CANCEL_OPTION:
        default:
            SpeleoGraphApp.setWorkingDirectory(chooser.getCurrentDirectory());
            return;
        }
    }
    try {
        SpeleoGraphApp.setWorkingDirectory(file.getParentFile());
        reader.readFile(file);
    } catch (FileReadingError e1) {
        log.error("Error when try to read a SpeleoGraph File", e1);
    }
}