All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jhotdraw8.application.action.file.RevertFileAction Maven / Gradle / Ivy

/*
 * @(#)RevertFileAction.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */
package org.jhotdraw8.application.action.file;

import javafx.event.ActionEvent;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.input.DataFormat;
import org.jhotdraw8.annotation.NonNull;
import org.jhotdraw8.annotation.Nullable;
import org.jhotdraw8.application.ApplicationLabels;
import org.jhotdraw8.application.FileBasedActivity;
import org.jhotdraw8.application.action.AbstractActivityAction;
import org.jhotdraw8.fxbase.concurrent.SimpleWorkState;
import org.jhotdraw8.fxbase.concurrent.WorkState;
import org.jhotdraw8.icollection.ChampMap;

import java.net.URI;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Lets the user write unsaved changes of the active view, then presents an
 * {@code URIChooser} and loads the selected URI into the active view.
 *
 * @author Werner Randelshofer
 */
public class RevertFileAction extends AbstractActivityAction {

    public static final String ID = "file.revert";

    /**
     * Creates a new instance.
     *
     * @param view the view
     */
    public RevertFileAction(@NonNull FileBasedActivity view) {
        super(view);
        ApplicationLabels.getResources().configureAction(this, ID);
    }

    @Override
    protected void onActionPerformed(@NonNull ActionEvent event, @NonNull FileBasedActivity activity) {
        if (isDisabled()) {
            return;
        }
        final URI uri = activity.getURI();
        final DataFormat dataFormat = activity.getDataFormat();
        if (activity.isModified()) {
            Alert alert = new Alert(Alert.AlertType.WARNING,
                    ApplicationLabels.getResources().getString("file.revert.doYouWantToRevert.message"), ButtonType.YES, ButtonType.CANCEL);
            alert.getDialogPane().setMaxWidth(640.0);
            Optional answer = alert.showAndWait();
            if (answer.isPresent() && answer.get() == ButtonType.YES) {
                doIt(activity, uri, dataFormat);
            }
        } else {
            doIt(activity, uri, dataFormat);
        }
    }

    private void doIt(@NonNull FileBasedActivity view, @Nullable URI uri, DataFormat dataFormat) {
        WorkState workState = new SimpleWorkState<>(getLabel());
        view.addDisabler(workState);

        final BiFunction handler = (actualDataFormat, throwable) -> {
            if (throwable != null) {
                Alert alert = new Alert(Alert.AlertType.ERROR, createErrorMessage(throwable));
                alert.getDialogPane().setMaxWidth(640.0);
                alert.showAndWait();
                Logger.getLogger(getClass().getName()).log(Level.WARNING, "Unexpected Exception " + throwable.getMessage(), throwable);

            }
            view.clearModified();
            view.removeDisabler(workState);
            return null;
        };

        if (uri == null) {
            view.clear().handle((ignored, throwable) -> handler.apply(null, throwable));
        } else {
            view.read(uri, dataFormat,
                            ChampMap.of(), false, workState)
                    .handle(handler);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy