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

The following examples show how to use java.awt.FileDialog#setMode() . 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: FileChooserBuilder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private File showFileDialog( FileDialog fileDialog, int mode ) {
    String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
    if( dirsOnly ) {
        System.setProperty("apple.awt.fileDialogForDirectories", "true"); //NOI18N
    }
    fileDialog.setMode( mode );
    fileDialog.setVisible(true);
    if( dirsOnly ) {
        if( null != oldFileDialogProp ) {
            System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
        } else {
            System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
        }
    }
    if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
        String selFile = fileDialog.getFile();
        File dir = new File( fileDialog.getDirectory() );
        return new File( dir, selFile );
    }
    return null;
}
 
Example 2
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 3
Source File: MainFrame.java    From mpcmaid with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void open() {
	final FileDialog openDialog = new FileDialog(this);
	openDialog.setDirectory(Preferences.getInstance().getOpenPath());
	openDialog.setMode(FileDialog.LOAD);
	openDialog.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;
		}
	});
	openDialog.setVisible(true);
	Preferences.getInstance().setOpenPath(openDialog.getDirectory());
	if (openDialog.getDirectory() != null && openDialog.getFile() != null) {
		String filePath = openDialog.getDirectory() + openDialog.getFile();
		System.out.println(filePath);
		final File pgmFile = new File(filePath);
		final MainFrame newFrame = new MainFrame(pgmFile);
		newFrame.show();
	}
}
 
Example 4
Source File: SamplesRunner.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void exportAsNetbeansProject(Sample sample) {
    FileDialog fileSelector = new FileDialog((JFrame)SwingUtilities.getWindowAncestor(view), "Select Destination");
    fileSelector.setMode(FileDialog.SAVE);
    fileSelector.setVisible(true);
    String selectedFile = fileSelector.getFile();
    if (selectedFile == null) {
        return;
    }
    File f = new File(new File(fileSelector.getDirectory()), selectedFile);
    System.out.println(f);
    new Thread(()->{
        try {
            sample.exportAsNetbeansProject(ctx, f);
        } catch (Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(view, ex.getMessage(), "Export Failed", JOptionPane.ERROR_MESSAGE);

        }
    }).start();
    
    
}
 
Example 5
Source File: MainFrame.java    From lizzie with GNU General Public License v3.0 5 votes vote down vote up
public void openFile() {
  JSONObject filesystem = Lizzie.config.persisted.getJSONObject("filesystem");
  FileDialog fileDialog = new FileDialog(this, resourceBundle.getString("LizzieFrame.openFile"));
  fileDialog.setLocationRelativeTo(this);
  fileDialog.setDirectory(filesystem.getString("last-folder"));
  fileDialog.setFile("*.sgf;*.gib;*.SGF;*.GIB");
  fileDialog.setMultipleMode(false);
  fileDialog.setMode(0);
  fileDialog.setVisible(true);
  File[] file = fileDialog.getFiles();
  if (file.length > 0) loadFile(file[0]);
}
 
Example 6
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 7
Source File: JWTInterceptTabController.java    From JWT4B with GNU General Public License v3.0 5 votes vote down vote up
private void radioButtonChanged(boolean cDM, boolean cRK, boolean cOS, boolean cRS, boolean cCS) {
	boolean oldRandomKey = randomKey;

	dontModify = jwtST.getRdbtnDontModify().isSelected();
	randomKey = jwtST.getRdbtnRandomKey().isSelected();
	keepOriginalSignature = jwtST.getRdbtnOriginalSignature().isSelected();
	recalculateSignature = jwtST.getRdbtnRecalculateSignature().isSelected();
	chooseSignature = jwtST.getRdbtnChooseSignature().isSelected();

	jwtST.setKeyFieldState(!keepOriginalSignature && !dontModify && !randomKey && !chooseSignature);

	if (keepOriginalSignature || dontModify) {
		jwtIM.setJWTKey("");
		jwtST.setKeyFieldValue("");
	}
	if (randomKey && !oldRandomKey) {
		generateRandomKey();
	}
	if (cCS) {
		FileDialog dialog = new FileDialog((Frame) null, "Select File to Open");
		dialog.setMode(FileDialog.LOAD);
		dialog.setVisible(true);
		if(dialog.getFile()!=null) {
			String file = dialog.getDirectory() + dialog.getFile();
			Output.output(file + " chosen.");
			String chosen = Strings.filePathToString(file);
			jwtIM.setJWTKey(chosen);
			jwtST.updateSetView(false);	
		}
	}
}
 
Example 8
Source File: FileDialogUtils.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Returns files the user selected or an empty array if the user cancelled
 * the dialog.
 */
public static File[] showOpenDialog(Frame f, String title,
		boolean allowMultipleSelection, FilenameFilter filter) {
	FileDialog fd = new FileDialog(f, title);
	fd.setMode(FileDialog.LOAD);
	if (filter != null)
		fd.setFilenameFilter(filter);
	fd.pack();
	fd.setMultipleMode(allowMultipleSelection);
	fd.setLocationRelativeTo(null);
	fd.setVisible(true);
	if (fd.getFile() == null)
		return new File[] {};
	return fd.getFiles();
}
 
Example 9
Source File: FileDialogUtils.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Show a save FileDialog.
 * 
 * @param f
 *            the frame that owns the FileDialog.
 * @param title
 *            the dialog title
 * @param filename
 *            the optional default filename shown in the file dialog.
 * @param extension
 *            the file extension ("xml", "png", etc.)
 * @return a File the user chose.
 */
public static File showSaveDialog(Frame f, String title, String filename,
		String extension) {
	if (extension.startsWith("."))
		extension = extension.substring(1);

	FileDialog fd = new FileDialog(f, title);
	fd.setMode(FileDialog.SAVE);
	if (filename != null)
		fd.setFile(filename);
	fd.setFilenameFilter(new SuffixFilenameFilter(extension));
	fd.pack();
	fd.setLocationRelativeTo(null);
	fd.setVisible(true);

	String s = fd.getFile();
	if (s == null)
		return null;

	if (s.toLowerCase().endsWith("." + extension)) {
		return new File(fd.getDirectory() + s);
	}

	// TODO: show a 'are you sure you want to replace' dialog here, if we
	// change the filename
	// native FileDialogs don't always show the right warning dialog IF the
	// file extension
	// isn't present

	return new File(fd.getDirectory() + fd.getFile() + "." + extension);
}
 
Example 10
Source File: CFileChooser.java    From binnavi with Apache License 2.0 5 votes vote down vote up
private static int showNativeFileDialog(final JFileChooser chooser) {
  final FileDialog result = new FileDialog((Frame) chooser.getParent());

  result.setDirectory(chooser.getCurrentDirectory().getPath());

  final File selected = chooser.getSelectedFile();
  result.setFile(selected == null ? "" : selected.getPath());
  result.setFilenameFilter(new FilenameFilter() {
    @Override
    public boolean accept(final File dir, final String name) {
      return chooser.getFileFilter().accept(new File(dir.getPath() + File.pathSeparator + name));
    }
  });

  if (chooser.getDialogType() == SAVE_DIALOG) {
    result.setMode(FileDialog.SAVE);
  } else {
    // The native dialog only support Open and Save
    result.setMode(FileDialog.LOAD);
  }

  if (chooser.getFileSelectionMode() == DIRECTORIES_ONLY) {
    System.setProperty("apple.awt.fileDialogForDirectories", "true");
  }

  // Display dialog
  result.setVisible(true);

  System.setProperty("apple.awt.fileDialogForDirectories", "false");
  if (result.getFile() == null) {
    return CANCEL_OPTION;
  }

  final String selectedDir = result.getDirectory();
  chooser
      .setSelectedFile(new File(FileUtils.ensureTrailingSlash(selectedDir) + result.getFile()));
  return APPROVE_OPTION;
}
 
Example 11
Source File: GameFileSelector.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Opens up a UI pop-up allowing user to select a game file. Returns nothing if user closes the
 * pop-up.
 */
public Optional<File> selectGameFile(final Frame owner) {
  final FileDialog fileDialog = new FileDialog(owner);
  fileDialog.setMode(FileDialog.LOAD);
  fileDialog.setDirectory(ClientSetting.saveGamesFolderPath.getValueOrThrow().toString());
  fileDialog.setFilenameFilter((dir, name) -> GameDataFileUtils.isCandidateFileName(name));
  fileDialog.setVisible(true);

  // FileDialog.getFiles() always returns an array
  // of 1 or 0 items, because FileDialog.multipleMode is false by default
  return Arrays.stream(fileDialog.getFiles()).findAny().map(this::mapFileResult);
}
 
Example 12
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 13
Source File: Export.java    From BART with MIT License 5 votes vote down vote up
private File chooseFile() {
    JFrame mainFrame = (JFrame) WindowManager.getDefault().getMainWindow();
    FileDialog fileDialog = new FileDialog(mainFrame, new File(System.getProperty("user.home")).toString());
    fileDialog.setTitle("Export changes in cvs file");
    fileDialog.setFile("expected.csv");
    fileDialog.setMode(FileDialog.SAVE);
    fileDialog.setFilenameFilter(new ExtFileFilter("csv"));
    fileDialog.setVisible(true);
    String filename = fileDialog.getFile();
    if (filename == null){
        return null;
    }
    String dir = fileDialog.getDirectory();
    return new File(dir + File.separator + filename);
}
 
Example 14
Source File: OpenFileAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc} Displays a file chooser dialog
 * and opens the selected files.
 */
@Override
public void actionPerformed(ActionEvent e) {
    if (running) {
        return;
    }
    try {
        running = true;
        JFileChooser chooser = prepareFileChooser();
        File[] files;
        try {
            if( Boolean.getBoolean("nb.native.filechooser") ) { //NOI18N
                String oldFileDialogProp = System.getProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                System.setProperty("apple.awt.fileDialogForDirectories", "false"); //NOI18N
                FileDialog fileDialog = new FileDialog(WindowManager.getDefault().getMainWindow());
                fileDialog.setMode(FileDialog.LOAD);
                fileDialog.setDirectory(getCurrentDirectory().getAbsolutePath());
                fileDialog.setTitle(chooser.getDialogTitle());
                fileDialog.setVisible(true);
                if( null != oldFileDialogProp ) {
                    System.setProperty("apple.awt.fileDialogForDirectories", oldFileDialogProp); //NOI18N
                } else {
                    System.clearProperty("apple.awt.fileDialogForDirectories"); //NOI18N
                }

                if( fileDialog.getDirectory() != null && fileDialog.getFile() != null ) {
                    String selFile = fileDialog.getFile();
                    File dir = new File( fileDialog.getDirectory() );
                    files = new File[] { new File( dir, selFile ) };
                    currentDirectory = dir;
                } else {
                    throw new UserCancelException();
                }
            } else {
                files = chooseFilesToOpen(chooser);
                currentDirectory = chooser.getCurrentDirectory();
                if (chooser.getFileFilter() != null) { // #227187
                    currentFileFilter =
                            chooser.getFileFilter().getDescription();
                }
            }
        } catch (UserCancelException ex) {
            return;
        }
        for (int i = 0; i < files.length; i++) {
            OpenFile.openFile(files[i], -1);
        }
    } finally {
        running = false;
    }
}