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:Main.java

public static String getDirectoryFromFileDialog(FileDialog fileDialog) {
    return fileDialog.getDirectory();
}

From source file:Main.java

/**
 * Returns the full path to the file, if a file was selected.
 * Otherwise it returns null./* ww  w .j  a  va  2s. c o m*/
 */
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:Main.java

/**
 * Select the file path via {@linkplain FileDialog}.
 * //from ww w  .j  av  a  2  s  . c  o m
 * @param parent
 *            the parent of {@linkplain FileDialog}
 * @param title
 *            the title of {@linkplain FileDialog}
 * @return the selected path
 */
public static String selectPath(Frame parent, String title) {
    FileDialog dialog = new FileDialog(parent, title, FileDialog.LOAD);
    dialog.setVisible(true);
    String dir = dialog.getDirectory();
    dialog.dispose();
    return dir;
}

From source file:net.brtly.monkeyboard.gui.MasterControlPanel.java

private static String createAndShowSdkChooser() {
    String sdk;//from   ww  w  .ja  v a 2  s  .  c  o  m
    System.setProperty("apple.awt.fileDialogForDirectories", "true");
    FileDialog d = new FileDialog(new JFrame());
    d.setVisible(true);
    System.setProperty("apple.awt.fileDialogForDirectories", "false");
    sdk = d.getDirectory();
    return sdk;
}

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());
    }//  w  w  w.ja va2  s  .c  o 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());
    }/*  w  w w. j a  v  a2s.  com*/
    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";
    }/*from w w w.ja v  a2  s .c o  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  www. ja  v a2 s  .c o  m*/
 * @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 av  a2  s  .  c o  m*/
 * @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);/*from  w  ww  .  j  av  a 2  s  .  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;
}