Example usage for java.awt FileDialog getFile

List of usage examples for java.awt FileDialog getFile

Introduction

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

Prototype

public String getFile() 

Source Link

Document

Gets the selected file of this file dialog.

Usage

From source file:Main.java

/**
 * Returns the full path to the file, if a file was selected.
 * Otherwise it returns null./* www  .  j  a va2  s.  com*/
 */
public static String getCanonicalFilenameFromFileDialog(FileDialog fileDialog) {
    if (fileDialog.getDirectory() == null)
        return null;
    if (fileDialog.getFile() == null)
        return null;
    // this line not needed on mac os x
    //return fileDialog.getDirectory() + System.getProperty("file.separator") + fileDialog.getFile();
    return fileDialog.getDirectory() + fileDialog.getFile();
}

From source file:org.tinymediamanager.ui.TmmUIHelper.java

private static Path openFileDialog(String title, int mode, String filename) throws Exception, Error {
    FileDialog chooser = new FileDialog(MainWindow.getFrame(), title, mode);
    if (lastDir != null) {
        chooser.setDirectory(lastDir.toFile().getAbsolutePath());
    }//ww w .  j a v  a 2  s  .co  m
    if (mode == FileDialog.SAVE) {
        chooser.setFile(filename);
    }
    chooser.setVisible(true);

    if (StringUtils.isNotEmpty(chooser.getFile())) {
        lastDir = Paths.get(chooser.getDirectory());
        return Paths.get(chooser.getDirectory(), chooser.getFile());
    } else {
        return null;
    }
}

From source file:org.tinymediamanager.ui.TmmUIHelper.java

private static Path openDirectoryDialog(String title) throws Exception, Error {
    // set system property to choose directories
    System.setProperty("apple.awt.fileDialogForDirectories", "true");

    FileDialog chooser = new FileDialog(MainWindow.getFrame(), title);
    if (lastDir != null) {
        chooser.setDirectory(lastDir.toFile().getAbsolutePath());
    }//from   w ww . j a  v a2s.  c o m
    chooser.setVisible(true);

    // reset system property
    System.setProperty("apple.awt.fileDialogForDirectories", "false");

    if (StringUtils.isNotEmpty(chooser.getFile())) {
        lastDir = Paths.get(chooser.getDirectory());
        return Paths.get(chooser.getDirectory(), chooser.getFile());
    } else {
        return null;
    }
}

From source file:com.stacksync.desktop.util.FileUtil.java

private static File showBrowseDialogMac(BrowseType type) {
    // AWT looks best on Mac:
    // http://today.java.net/pub/a/today/2004/01/29/swing.html

    String title = "";
    if (type == BrowseType.DIRECTORIES_ONLY) {
        System.setProperty("apple.awt.fileDialogForDirectories", "true");
        title = "Choose Folder";
    } else if (type == BrowseType.FILES_ONLY) {
        System.setProperty("apple.awt.fileDialogForDirectories", "false");
        title = "Choose File";
    }/*w  ww.  ja v a2 s  .  co  m*/

    FileDialog dialog = new FileDialog(new Frame(), title);
    dialog.setVisible(true);

    String path = dialog.getDirectory() + dialog.getFile();
    return new File(path);
}

From source file:edu.ku.brc.specify.tasks.services.PickListUtils.java

/**
 * @param localizableIO//from   w ww  . j  a v  a2 s .  c  om
 * @param collection
 * @return
 */
public static boolean importPickLists(final LocalizableIOIFace localizableIO, final Collection collection) {
    // Apply is Import All PickLists

    FileDialog dlg = new FileDialog(((Frame) UIRegistry.getTopWindow()),
            getResourceString(getI18n("PL_IMPORT")), FileDialog.LOAD);
    dlg.setDirectory(UIRegistry.getUserHomeDir());
    dlg.setFile(getPickListXMLName());
    UIHelper.centerAndShow(dlg);

    String dirStr = dlg.getDirectory();
    String fileName = dlg.getFile();
    if (StringUtils.isEmpty(dirStr) || StringUtils.isEmpty(fileName)) {
        return false;
    }

    final String path = dirStr + fileName;

    File file = new File(path);
    if (!file.exists()) {
        UIRegistry.showLocalizedError(getI18n("PL_FILE_NOT_EXIST"), file.getAbsoluteFile());
        return false;
    }
    List<BldrPickList> bldrPickLists = DataBuilder.getBldrPickLists(null, file);

    Integer cnt = null;
    boolean wasErr = false;

    DataProviderSessionIFace session = null;
    try {
        session = DataProviderFactory.getInstance().createSession();
        session.beginTransaction();

        HashMap<String, PickList> plHash = new HashMap<String, PickList>();
        List<PickList> items = getPickLists(localizableIO, true, false);

        for (PickList pl : items) {
            plHash.put(pl.getName(), pl);
            //System.out.println("["+pl.getName()+"]");
        }

        for (BldrPickList bpl : bldrPickLists) {
            PickList pickList = plHash.get(bpl.getName());
            //System.out.println("["+bpl.getName()+"]["+(pickList != null ? pickList.getName() : "null") + "]");
            if (pickList == null) {
                // External PickList is new
                pickList = createPickList(bpl, collection);
                session.saveOrUpdate(pickList);
                if (cnt == null)
                    cnt = 0;
                cnt++;

            } else if (!pickListsEqual(pickList, bpl)) {
                session.delete(pickList);
                collection.getPickLists().remove(pickList);
                pickList = createPickList(bpl, collection);
                session.saveOrUpdate(pickList);
                collection.getPickLists().add(pickList);
                if (cnt == null)
                    cnt = 0;
                cnt++;
            }
        }
        session.commit();

    } catch (Exception ex) {
        wasErr = true;
        if (session != null)
            session.rollback();

        ex.printStackTrace();
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(PickListEditorDlg.class, ex);

    } finally {
        if (session != null) {
            session.close();
        }
    }

    String key = wasErr ? "PL_ERR_IMP" : cnt != null ? "PL_WASIMPORT" : "PL_MATCHIMP";

    UIRegistry.displayInfoMsgDlgLocalized(getI18n(key), cnt);

    return true;
}

From source file:edu.ku.brc.specify.tasks.services.PickListUtils.java

/**
 * @param localizableIO//from w w w .j a  v a  2 s. com
 * @param hash HashSet of names of picklists to be pre-selected (can be null)
 */
public static void exportPickList(final LocalizableIOIFace localizableIO, final HashSet<String> hash) {
    // Cancel is Export All PickLists

    List<PickList> items = getPickLists(localizableIO, true, false);
    List<PickList> selectedItems = new ArrayList<PickList>();
    if (hash != null) {
        for (PickList pl : items) {
            if (hash.contains(pl.getName())) {
                selectedItems.add(pl);
            }
        }
    }

    Window window = UIRegistry.getTopWindow();
    boolean isDialog = window instanceof Dialog;

    ToggleButtonChooserDlg<PickList> pickDlg;
    if (isDialog) {
        pickDlg = new ToggleButtonChooserDlg<PickList>((Dialog) window, getI18n("PL_EXPORT"), items);
    } else {
        pickDlg = new ToggleButtonChooserDlg<PickList>((JFrame) window, getI18n("PL_EXPORT"), items);
    }

    pickDlg.setUseScrollPane(true);
    pickDlg.setAddSelectAll(true);
    pickDlg.createUI();
    pickDlg.setSelectedObjects(selectedItems);
    UIHelper.centerAndShow(pickDlg);

    Integer cnt = null;
    if (!pickDlg.isCancelled()) {
        items = pickDlg.getSelectedObjects();

        String dlgTitle = getResourceString(getI18n("RIE_ExportResource"));

        FileDialog dlg;
        if (isDialog) {
            dlg = new FileDialog((Dialog) window, dlgTitle, FileDialog.SAVE);
        } else {
            dlg = new FileDialog((Frame) window, dlgTitle, FileDialog.SAVE);
        }
        dlg.setDirectory(UIRegistry.getUserHomeDir());
        dlg.setFile(getPickListXMLName());

        UIHelper.centerAndShow(dlg);

        String dirStr = dlg.getDirectory();
        String fileName = dlg.getFile();

        if (StringUtils.isNotEmpty(dirStr) && StringUtils.isNotEmpty(fileName)) {
            String ext = FilenameUtils.getExtension(fileName);
            if (StringUtils.isEmpty(ext) || !ext.equalsIgnoreCase("xml")) {
                fileName += ".xml";
            }

            try {
                File xmlFile = new File(dirStr + File.separator + fileName);

                ArrayList<BldrPickList> bldrPickLists = new ArrayList<BldrPickList>();
                for (PickList pl : items) {
                    bldrPickLists.add(new BldrPickList(pl));
                }
                DataBuilder.writePickListsAsXML(xmlFile, bldrPickLists);
                cnt = bldrPickLists.size();

            } catch (Exception ex) {
                ex.printStackTrace();
            }
            UIRegistry.displayInfoMsgDlgLocalized(getI18n(cnt != null ? "PL_WASEXPORT" : "PL_ERR_IMP"), cnt);
        }
    }
}

From source file:phex.gui.common.FileDialogHandler.java

private static File openMacDirectoryChooser(String title, String notifyPopupTitle,
        String notifyPopupShortMessage) {
    // create folder dialog through other class this prevents 
    // NoClassDefFoundError on Windows systems since the import of the
    // required OS X classes is elsewhere.
    FileDialog dia = MacOsxGUIUtils.createFolderDialog();
    dia.setTitle(title);// w w  w .  j  a v a 2s  . c o m

    // unfortunatly its not possible to display notification popup
    // besides heavy weight dialog.
    //if ( notifyPopupTitle != null || notifyPopupShortMessage != null )
    //{
    //displayMacNotificationPopup( dia, notifyPopupTitle, 
    //    notifyPopupShortMessage );
    //}

    DirectoryOnlyFileFilter filter = new DirectoryOnlyFileFilter();
    dia.setFilenameFilter(new FileFilterWrapper(filter));
    dia.setVisible(true);
    String dirStr = dia.getDirectory();
    String fileStr = dia.getFile();

    if (dirStr == null || fileStr == null) {
        return null;
    }
    File file = new File(dirStr, fileStr);
    // validate filter
    if (!filter.accept(file)) {
        return null;
    }
    return file;
}

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  w w w.j av  a 2  s  .co m

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

    return selectedFile;
}

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();/*from ww  w.j av a 2 s.  c  o  m*/
        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:uk.nhs.cfh.dsp.srth.desktop.actions.querycrud.core.actions.SaveQueryToFileTask.java

/**
 * Do in background./*from  w ww  .  j  av a2s  .c  o  m*/
 * 
 * @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;
}