Search in sources :

Example 11 with FileChooser

use of javafx.stage.FileChooser in project jabref by JabRef.

the class ImportFormats method getImportAction.

/**
     * Create an AbstractAction for performing an Import operation.
     * @param frame The JabRefFrame of this JabRef instance.
     * @param openInNew Indicate whether the action should open into a new database or
     *  into the currently open one.
     * @return The action.
     */
public static AbstractAction getImportAction(JabRefFrame frame, boolean openInNew) {
    class ImportAction extends MnemonicAwareAction {

        private final boolean newDatabase;

        public ImportAction(boolean newDatabase) {
            this.newDatabase = newDatabase;
            if (newDatabase) {
                putValue(Action.NAME, Localization.menuTitle("Import into new library"));
                putValue(Action.ACCELERATOR_KEY, Globals.getKeyPrefs().getKey(KeyBinding.IMPORT_INTO_NEW_DATABASE));
            } else {
                putValue(Action.NAME, Localization.menuTitle("Import into current library"));
                putValue(Action.ACCELERATOR_KEY, Globals.getKeyPrefs().getKey(KeyBinding.IMPORT_INTO_CURRENT_DATABASE));
            }
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            SortedSet<Importer> importers = Globals.IMPORT_FORMAT_READER.getImportFormats();
            List<FileExtensions> extensions = importers.stream().map(Importer::getExtensions).collect(Collectors.toList());
            FileChooser.ExtensionFilter allImports = ImportFileFilter.convert(Localization.lang("Available import formats"), importers);
            FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder().addExtensionFilters(extensions).withInitialDirectory(Globals.prefs.get(JabRefPreferences.IMPORT_WORKING_DIRECTORY)).build();
            DialogService ds = new FXDialogService();
            FileChooser fs = ds.getConfiguredFileChooser(fileDialogConfiguration);
            fs.setSelectedExtensionFilter(allImports);
            File f = DefaultTaskExecutor.runInJavaFXThread(() -> fs.showOpenDialog(null));
            Optional<Path> selectedFile = Optional.ofNullable(f).map(File::toPath);
            FileChooser.ExtensionFilter selectedExtension = fs.getSelectedExtensionFilter();
            // Add file filter for all supported types
            selectedFile.ifPresent(file -> {
                try {
                    if (!Files.exists(file)) {
                        JOptionPane.showMessageDialog(frame, Localization.lang("File not found") + ": '" + file.getFileName() + "'.", Localization.lang("Import"), JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                    Optional<Importer> format = ImportFileFilter.convert(selectedExtension, importers);
                    ImportMenuItem importMenu = new ImportMenuItem(frame, newDatabase, format.orElse(null));
                    importMenu.automatedImport(Collections.singletonList(file.toString()));
                    Globals.prefs.put(JabRefPreferences.IMPORT_WORKING_DIRECTORY, file.getParent().toString());
                } catch (Exception ex) {
                    LOGGER.warn("Cannot import file", ex);
                }
            });
        }
    }
    return new ImportAction(openInNew);
}
Also used : Path(java.nio.file.Path) DialogService(org.jabref.gui.DialogService) FXDialogService(org.jabref.gui.FXDialogService) ActionEvent(java.awt.event.ActionEvent) FileDialogConfiguration(org.jabref.gui.util.FileDialogConfiguration) FXDialogService(org.jabref.gui.FXDialogService) MnemonicAwareAction(org.jabref.gui.actions.MnemonicAwareAction) FileExtensions(org.jabref.logic.util.FileExtensions) FileChooser(javafx.stage.FileChooser) File(java.io.File) Importer(org.jabref.logic.importer.Importer)

Example 12 with FileChooser

use of javafx.stage.FileChooser in project bitsquare by bitsquare.

the class TraderDisputeView method onOpenAttachment.

private void onOpenAttachment(Attachment attachment) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Save file to disk");
    fileChooser.setInitialFileName(attachment.getFileName());
    /* if (Utilities.isUnix())
            fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));*/
    File file = fileChooser.showSaveDialog(stage);
    if (file != null) {
        try (FileOutputStream fileOutputStream = new FileOutputStream(file.getAbsolutePath())) {
            fileOutputStream.write(attachment.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileChooser(javafx.stage.FileChooser) IOException(java.io.IOException) File(java.io.File)

Example 13 with FileChooser

use of javafx.stage.FileChooser in project bitsquare by bitsquare.

the class GUIUtil method exportCSV.

public static <T> void exportCSV(String fileName, CSVEntryConverter<T> headerConverter, CSVEntryConverter<T> contentConverter, T emptyItem, List<T> list, Stage stage) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setInitialFileName(fileName);
    File file = fileChooser.showSaveDialog(stage);
    try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file, false), Charsets.UTF_8)) {
        CSVWriter<T> headerWriter = new CSVWriterBuilder<T>(outputStreamWriter).strategy(CSVStrategy.UK_DEFAULT).entryConverter(headerConverter).build();
        headerWriter.write(emptyItem);
        CSVWriter<T> contentWriter = new CSVWriterBuilder<T>(outputStreamWriter).strategy(CSVStrategy.UK_DEFAULT).entryConverter(contentConverter).build();
        contentWriter.writeAll(list);
    } catch (RuntimeException | IOException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        new Popup().error("Exporting to CSV failed because of an error.\n" + "Error = " + e.getMessage());
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Popup(io.bitsquare.gui.main.overlays.popups.Popup) FileChooser(javafx.stage.FileChooser) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File)

Example 14 with FileChooser

use of javafx.stage.FileChooser in project Gargoyle by callakrsos.

the class DialogUtil method showFileSaveDialog.

public static File showFileSaveDialog(final Window ownerWindow, Consumer<FileChooser> option) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    installDefaultPath(fileChooser);
    option.accept(fileChooser);
    File result = fileChooser.showSaveDialog(ownerWindow);
    if (result != null)
        applyLastPath(result.getParentFile());
    return result;
}
Also used : FileChooser(javafx.stage.FileChooser) File(java.io.File)

Example 15 with FileChooser

use of javafx.stage.FileChooser in project Gargoyle by callakrsos.

the class DialogUtil method showMultiFileDialog.

/**
	 * 멀티 파일 다이얼로그 오픈
	 *
	 * @Date 2015. 10. 12.
	 * @param ownerWindow
	 * @param option
	 * @return
	 * @User KYJ
	 */
public static List<File> showMultiFileDialog(final Window ownerWindow, Consumer<FileChooser> option) {
    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Open Resource File");
    installDefaultPath(fileChooser);
    option.accept(fileChooser);
    List<File> files = fileChooser.showOpenMultipleDialog(ownerWindow);
    if (files == null || files.isEmpty())
        return Collections.emptyList();
    applyLastPath(files.get(files.size() - 1));
    return files;
}
Also used : FileChooser(javafx.stage.FileChooser) File(java.io.File)

Aggregations

FileChooser (javafx.stage.FileChooser)64 File (java.io.File)59 Preferences (java.util.prefs.Preferences)21 IOException (java.io.IOException)17 ResourceBundle (java.util.ResourceBundle)11 FXML (javafx.fxml.FXML)11 FileOutputStream (java.io.FileOutputStream)5 ArrayList (java.util.ArrayList)5 Alert (javafx.scene.control.Alert)5 Button (javafx.scene.control.Button)4 Popup (io.bitsquare.gui.main.overlays.popups.Popup)3 List (java.util.List)3 GargoyleFileAlreadyExistException (com.kyj.fx.voeditor.visual.exceptions.GargoyleFileAlreadyExistException)2 Palette (fractal.Palette)2 Path (java.nio.file.Path)2 ObservableList (javafx.collections.ObservableList)2 FilteredList (javafx.collections.transformation.FilteredList)2 SortedList (javafx.collections.transformation.SortedList)2 Task (javafx.concurrent.Task)2 Insets (javafx.geometry.Insets)2