Java Code Examples for java.awt.FileDialog#getDirectory()

The following examples show how to use java.awt.FileDialog#getDirectory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ApplicationPanel.java    From EchoSim with Apache License 2.0 6 votes vote down vote up
protected void doUtteranceLoadFile()
{
    FileDialog fd = new FileDialog(getFrame(), "Load Utterance File", FileDialog.LOAD);
    fd.setDirectory(RuntimeLogic.getProp("utterance.file.dir"));
    fd.setFile(RuntimeLogic.getProp("utterance.file.file"));
    fd.setVisible(true);
    if (fd.getDirectory() == null)
        return;
    String intentFile = fd.getDirectory()+System.getProperty("file.separator")+fd.getFile();
    if ((intentFile == null) || (intentFile.length() == 0))
        return;
    try
    {
        RuntimeLogic.readUtterances(mRuntime, (new File(intentFile)).toURI());
        RuntimeLogic.setProp("utterance.file.dir", fd.getDirectory());
        RuntimeLogic.setProp("utterance.file.file", fd.getFile());
    }
    catch (IOException e)
    {
        JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+intentFile, JOptionPane.ERROR_MESSAGE);
    }
}
 
Example 2
Source File: FileUtil.java    From desktop with GNU General Public License v3.0 6 votes vote down vote up
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";
    }

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

    String path = dialog.getDirectory() + dialog.getFile();
    return new File(path);
}
 
Example 3
Source File: ScriptPanel.java    From EchoSim with Apache License 2.0 6 votes vote down vote up
private void doSaveDisk()
{
    FileDialog fd = new FileDialog(getFrame(), "Save Script", FileDialog.SAVE);
    fd.setDirectory(RuntimeLogic.getProp("script.file.dir"));
    fd.setFile(RuntimeLogic.getProp("script.file.file"));
    fd.setVisible(true);
    if (fd.getDirectory() == null)
        return;
    String historyFile = fd.getDirectory()+System.getProperty("file.separator")+fd.getFile();
    if ((historyFile == null) || (historyFile.length() == 0))
        return;
    try
    {
        ScriptLogic.saveScript(mRuntime, new File(historyFile));
        RuntimeLogic.setProp("script.file.dir", fd.getDirectory());
        RuntimeLogic.setProp("script.file.file", fd.getFile());
    }
    catch (IOException e)
    {
        JOptionPane.showMessageDialog(this, e.getLocalizedMessage(), "Error reading "+historyFile, JOptionPane.ERROR_MESSAGE);
    }
}
 
Example 4
Source File: SaveGameFileChooser.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Displays a file chooser dialog for the user to select the file to which the current game should
 * be saved.
 *
 * @param frame The owner of the file chooser dialog; may be {@code null}.
 * @return The file to which the current game should be saved or {@code null} if the user
 *     cancelled the operation.
 */
public static File getSaveGameLocation(final Frame frame, final GameData gameData) {
  final FileDialog fileDialog = new FileDialog(frame);
  fileDialog.setMode(FileDialog.SAVE);
  fileDialog.setDirectory(ClientSetting.saveGamesFolderPath.getValueOrThrow().toString());
  fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
  fileDialog.setFile(getSaveGameName(gameData));

  fileDialog.setVisible(true);
  final String fileName = fileDialog.getFile();
  if (fileName == null) {
    return null;
  }

  // If the user selects a filename that already exists,
  // the AWT Dialog will ask the user for confirmation
  return new File(fileDialog.getDirectory(), GameDataFileUtils.addExtensionIfAbsent(fileName));
}
 
Example 5
Source File: Notate.java    From jmg with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Dialog to import a jm XML file
 */
 
 public void openJMXML() {
    FileDialog loadjmxml = new FileDialog(this, "Select a jMusic XML score file.", FileDialog.LOAD);
    loadjmxml.setDirectory( lastDirectory );
    loadjmxml.show();
    String fileName = loadjmxml.getFile();
    if (fileName != null) {
        Score s = new Score();
        lastDirectory = loadjmxml.getDirectory(); 
        Read.xml(s, lastDirectory + fileName);
        setNewScore(s);
    }
}
 
Example 6
Source File: FileUtils.java    From toxiclibs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Displays a standard AWT file dialog for choosing a file for loading or
 * saving.
 * 
 * @param frame
 *            parent frame
 * @param title
 *            dialog title
 * @param path
 *            base directory (or null)
 * @param formats
 *            an array of allowed file extensions (or null to allow all)
 * @param mode
 *            either FileUtils.LOAD or FileUtils.SAVE
 * @return path to chosen file or null, if user has canceled
 */
public static String showFileDialog(final Frame frame, final String title,
        String path, final String[] formats, final int mode) {
    String fileID = null;
    FileDialog fd = new FileDialog(frame, title, mode);
    if (path != null) {
        fd.setDirectory(path);
    }
    if (formats != null) {
        fd.setFilenameFilter(new FilenameFilter() {

            public boolean accept(File dir, String name) {
                boolean isAccepted = false;
                for (String ext : formats) {
                    if (name.indexOf(ext) != -1) {
                        isAccepted = true;
                        break;
                    }
                }
                return isAccepted;
            }
        });
    }
    fd.setVisible(true);
    if (fd.getFile() != null) {
        fileID = fd.getFile();
        fileID = fd.getDirectory() + fileID;
    }
    return fileID;
}
 
Example 7
Source File: CustomSettingsDialog.java    From swingsane with Apache License 2.0 5 votes vote down vote up
private File getLoadFile(String filename, String extension) {
  FileDialog fd = new FileDialog((JDialog) getRootPane().getTopLevelAncestor(),
      Localizer.localize("LoadScannerOptionsFromFileTitle"), FileDialog.LOAD);
  fd.setDirectory(".");
  FilenameExtensionFilter filter = new FilenameExtensionFilter();
  filter.addExtension("xml");
  fd.setFilenameFilter(filter);
  fd.setModal(true);
  fd.setVisible(true);
  if (fd.getFile() == null) {
    return null;
  }
  return new File(fd.getDirectory() + File.separator + fd.getFile());
}
 
Example 8
Source File: MainFrame.java    From mpcmaid with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void saveAs() {
	final FileDialog saveDialog = new FileDialog(this);
	saveDialog.setDirectory(Preferences.getInstance().getSavePath());
	saveDialog.setMode(FileDialog.SAVE);
	saveDialog.setFilenameFilter(new FilenameFilter() {
		public boolean accept(File dir, String name) {
			String[] supportedFiles = { "PGM", "pgm" };
			for (int i = 0; i < supportedFiles.length; i++) {
				if (name.endsWith(supportedFiles[i])) {
					return true;
				}
			}
			return false;
		}
	});
	saveDialog.setVisible(true);
	Preferences.getInstance().setSavePath(saveDialog.getDirectory());
	String filename = saveDialog.getFile();
	if (saveDialog.getDirectory() != null && filename != null) {
		if (!filename.toUpperCase().endsWith(".PGM")) {
			filename += ".PGM";
		}
		String filePath = saveDialog.getDirectory() + filename;
		System.out.println(filePath);
		final File file = new File(filePath);
		setPgmFile(file);
		program.save(pgmFile);
	}
}
 
Example 9
Source File: Ted.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Asks for a file name. then saves the current content of editor pane to the file.
 */
private void doSaveAs() {
    FileDialog fileDialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
    fileDialog.show();
    if (fileDialog.getFile() == null)
        return;
    fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();

    doSave(fileName);
}
 
Example 10
Source File: Ted.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** This method is called when File -> Open menu item is invoked.
 * It displays a dialog to choose the file to be opened and edited.
 * @param evt ActionEvent instance passed from actionPerformed event.
 */
private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
    FileDialog fileDialog = new FileDialog(this, "Open...", FileDialog.LOAD);
    fileDialog.show();
    if (fileDialog.getFile() == null)
        return;
    fileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();

    FileInputStream fis = null;
    String str = null;
    try {
        fis = new FileInputStream(fileName);
        int size = fis.available();
        byte[] bytes = new byte [size];
        fis.read(bytes);
        str = new String(bytes);
    } catch (IOException e) {
    } finally {
        try {
            fis.close();
        } catch (IOException e2) {
        }
    }

    if (str != null)
        textBox.setText(str);
}
 
Example 11
Source File: MiniEdit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Invoker for the saveas action */
public void saveAs() {
    Doc doc = getSelectedDoc();
    if (doc == null) {
        throw new NullPointerException ("no doc");
    }
    

    FileDialog fd = new FileDialog (MiniEdit.this, "Save as");
    fd.setMode(fd.SAVE);
    fd.show();
    if (fd.getFile() != null) {
        File nue = new File (fd.getDirectory() + fd.getFile());
        try {
            boolean success = nue.createNewFile();
            if (success) {
                FileWriter w = new FileWriter (nue);
                doc.getTextPane().write(w);
                file = nue;
                documentTabs.setTitleAt(documentTabs.getSelectedIndex(), nue.getName());
                documentTabs.setToolTipTextAt(documentTabs.getSelectedIndex(), nue.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
Example 12
Source File: Ted.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void jMenuItem4ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
    // Add your handling code here:
    FileDialog fileDialog = new FileDialog (this, "Open...", FileDialog.LOAD);
    fileDialog.show ();
    if (fileDialog.getFile () == null)
        return;
    fileName = fileDialog.getDirectory () + File.separator + fileDialog.getFile ();

    FileInputStream fis = null;
    String str = null;
    try {
        fis = new FileInputStream (fileName);
        int size = fis.available ();
        byte[] bytes = new byte [size];
        fis.read (bytes);
        str = new String (bytes);
    } catch (IOException e) {
    } finally {
        try {
            fis.close ();
        } catch (IOException e2) {
        }
    }

    if (str != null)
        textBox.setText (str);
}
 
Example 13
Source File: Notate.java    From jmg with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Dialog to import a jm file
 */
 
 public void openJM() {
    FileDialog loadjm = new FileDialog(this, "Select a jm score file.", FileDialog.LOAD);
    loadjm.setDirectory( lastDirectory );
    loadjm.show();
    String fileName = loadjm.getFile();
    if (fileName != null) {
        Score s = new Score();
        lastDirectory = loadjm.getDirectory();  
        Read.jm(s, lastDirectory + fileName);
        setNewScore(s);
    }
}
 
Example 14
Source File: RBridge.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String rChooseFile(Rengine re, int newFile) {
    FileDialog fd = new FileDialog(Wandora.getWandora(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE);
    fd.setVisible(true);
    String res=null;
    if (fd.getDirectory()!=null) res=fd.getDirectory();
    if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile());
    return res;
}
 
Example 15
Source File: rtest.java    From RDMP1 with GNU General Public License v2.0 5 votes vote down vote up
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.show();
	String res = null;
	if (fd.getDirectory() != null)
		res = fd.getDirectory();
	if (fd.getFile() != null)
		res = (res == null) ? fd.getFile() : (res + fd.getFile());
	return res;
}
 
Example 16
Source File: FileChooser.java    From CQL with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 *
 * @param title
 * @param mode
 * @param filter
 *
 * @return
 */
private static File chooseFileAWT(String title, Mode mode, FileFilter filter) {
	Easik e = Easik.getInstance();
	EasikSettings s = e.getSettings();
	FileDialog dialog = new FileDialog(e.getFrame(), title,
			(mode == Mode.SAVE) ? FileDialog.SAVE : FileDialog.LOAD);

	dialog.setDirectory(s.getDefaultFolder());

	if ((mode == Mode.DIRECTORY) && EasikConstants.RUNNING_ON_MAC) {
		System.setProperty("apple.awt.fileDialogForDirectories", "true");
	} else if (filter != null) {
		dialog.setFilenameFilter(filter);
	}

	// Show the dialog (this blocks until the user is done)
	dialog.setVisible(true);

	if ((mode == Mode.DIRECTORY) && EasikConstants.RUNNING_ON_MAC) {
		System.setProperty("apple.awt.fileDialogForDirectories", "false");
	}

	String filename = dialog.getFile();

	if (filename == null) {
		return null;
	}

	File selected = new File(dialog.getDirectory(), filename);

	if (mode != Mode.DIRECTORY) {
		s.setProperty("folder_last", selected.getParentFile().getAbsolutePath());
	}

	return selected;
}
 
Example 17
Source File: PolicyTool.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * perform SAVE AS
 */
void displaySaveAsDialog(int nextEvent) {

    // pop up a dialog box for the user to enter a filename.
    FileDialog fd = new FileDialog
            (tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
    fd.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            e.getWindow().setVisible(false);
        }
    });
    fd.setVisible(true);

    // see if the user hit cancel
    if (fd.getFile() == null ||
        fd.getFile().equals(""))
        return;

    // get the entered filename
    File saveAsFile = new File(fd.getDirectory(), fd.getFile());
    String filename = saveAsFile.getPath();
    fd.dispose();

    try {
        // save the policy entries to a file
        tool.savePolicy(filename);

        // display status
        MessageFormat form = new MessageFormat(PolicyTool.getMessage
                ("Policy.successfully.written.to.filename"));
        Object[] source = {filename};
        tw.displayStatusDialog(null, form.format(source));

        // display the new policy filename
        JTextField newFilename = (JTextField)tw.getComponent
                        (ToolWindow.MW_FILENAME_TEXTFIELD);
        newFilename.setText(filename);
        tw.setVisible(true);

        // now continue with the originally requested command
        // (QUIT, NEW, or OPEN)
        userSaveContinue(tool, tw, this, nextEvent);

    } catch (FileNotFoundException fnfe) {
        if (filename == null || filename.equals("")) {
            tw.displayErrorDialog(null, new FileNotFoundException
                        (PolicyTool.getMessage("null.filename")));
        } else {
            tw.displayErrorDialog(null, fnfe);
        }
    } catch (Exception ee) {
        tw.displayErrorDialog(null, ee);
    }
}
 
Example 18
Source File: PolicyTool.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * perform SAVE AS
 */
void displaySaveAsDialog(int nextEvent) {

    // pop up a dialog box for the user to enter a filename.
    FileDialog fd = new FileDialog
            (tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
    fd.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            e.getWindow().setVisible(false);
        }
    });
    fd.setVisible(true);

    // see if the user hit cancel
    if (fd.getFile() == null ||
        fd.getFile().equals(""))
        return;

    // get the entered filename
    File saveAsFile = new File(fd.getDirectory(), fd.getFile());
    String filename = saveAsFile.getPath();
    fd.dispose();

    try {
        // save the policy entries to a file
        tool.savePolicy(filename);

        // display status
        MessageFormat form = new MessageFormat(PolicyTool.getMessage
                ("Policy.successfully.written.to.filename"));
        Object[] source = {filename};
        tw.displayStatusDialog(null, form.format(source));

        // display the new policy filename
        JTextField newFilename = (JTextField)tw.getComponent
                        (ToolWindow.MW_FILENAME_TEXTFIELD);
        newFilename.setText(filename);
        tw.setVisible(true);

        // now continue with the originally requested command
        // (QUIT, NEW, or OPEN)
        userSaveContinue(tool, tw, this, nextEvent);

    } catch (FileNotFoundException fnfe) {
        if (filename == null || filename.equals("")) {
            tw.displayErrorDialog(null, new FileNotFoundException
                        (PolicyTool.getMessage("null.filename")));
        } else {
            tw.displayErrorDialog(null, fnfe);
        }
    } catch (Exception ee) {
        tw.displayErrorDialog(null, ee);
    }
}
 
Example 19
Source File: PolicyTool.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * perform SAVE AS
 */
void displaySaveAsDialog(int nextEvent) {

    // pop up a dialog box for the user to enter a filename.
    FileDialog fd = new FileDialog
            (tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
    fd.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            e.getWindow().setVisible(false);
        }
    });
    fd.setVisible(true);

    // see if the user hit cancel
    if (fd.getFile() == null ||
        fd.getFile().equals(""))
        return;

    // get the entered filename
    File saveAsFile = new File(fd.getDirectory(), fd.getFile());
    String filename = saveAsFile.getPath();
    fd.dispose();

    try {
        // save the policy entries to a file
        tool.savePolicy(filename);

        // display status
        MessageFormat form = new MessageFormat(PolicyTool.getMessage
                ("Policy.successfully.written.to.filename"));
        Object[] source = {filename};
        tw.displayStatusDialog(null, form.format(source));

        // display the new policy filename
        JTextField newFilename = (JTextField)tw.getComponent
                        (ToolWindow.MW_FILENAME_TEXTFIELD);
        newFilename.setText(filename);
        tw.setVisible(true);

        // now continue with the originally requested command
        // (QUIT, NEW, or OPEN)
        userSaveContinue(tool, tw, this, nextEvent);

    } catch (FileNotFoundException fnfe) {
        if (filename == null || filename.equals("")) {
            tw.displayErrorDialog(null, new FileNotFoundException
                        (PolicyTool.getMessage("null.filename")));
        } else {
            tw.displayErrorDialog(null, fnfe);
        }
    } catch (Exception ee) {
        tw.displayErrorDialog(null, ee);
    }
}
 
Example 20
Source File: PolicyTool.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * perform SAVE AS
 */
void displaySaveAsDialog(int nextEvent) {

    // pop up a dialog box for the user to enter a filename.
    FileDialog fd = new FileDialog
            (tw, PolicyTool.getMessage("Save.As"), FileDialog.SAVE);
    fd.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            e.getWindow().setVisible(false);
        }
    });
    fd.setVisible(true);

    // see if the user hit cancel
    if (fd.getFile() == null ||
        fd.getFile().equals(""))
        return;

    // get the entered filename
    File saveAsFile = new File(fd.getDirectory(), fd.getFile());
    String filename = saveAsFile.getPath();
    fd.dispose();

    try {
        // save the policy entries to a file
        tool.savePolicy(filename);

        // display status
        MessageFormat form = new MessageFormat(PolicyTool.getMessage
                ("Policy.successfully.written.to.filename"));
        Object[] source = {filename};
        tw.displayStatusDialog(null, form.format(source));

        // display the new policy filename
        JTextField newFilename = (JTextField)tw.getComponent
                        (ToolWindow.MW_FILENAME_TEXTFIELD);
        newFilename.setText(filename);
        tw.setVisible(true);

        // now continue with the originally requested command
        // (QUIT, NEW, or OPEN)
        userSaveContinue(tool, tw, this, nextEvent);

    } catch (FileNotFoundException fnfe) {
        if (filename == null || filename.equals("")) {
            tw.displayErrorDialog(null, new FileNotFoundException
                        (PolicyTool.getMessage("null.filename")));
        } else {
            tw.displayErrorDialog(null, fnfe);
        }
    } catch (Exception ee) {
        tw.displayErrorDialog(null, ee);
    }
}