Search in sources :

Example 1 with Result

use of org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog.Result in project rstudio by rstudio.

the class ApplicationQuit method handleUnsavedChanges.

public static void handleUnsavedChanges(final int saveAction, String caption, boolean forceSaveAll, final SourceShim sourceShim, final WorkbenchContext workbenchContext, final UnsavedChangesTarget globalEnvTarget, final QuitContext quitContext) {
    // see what the unsaved changes situation is and prompt accordingly
    ArrayList<UnsavedChangesTarget> unsavedSourceDocs = sourceShim.getUnsavedChanges(Source.TYPE_FILE_BACKED);
    // force save all
    if (forceSaveAll) {
        // save all unsaved documents and then quit
        sourceShim.handleUnsavedChangesBeforeExit(unsavedSourceDocs, new Command() {

            @Override
            public void execute() {
                boolean saveChanges = saveAction != SaveAction.NOSAVE;
                quitContext.onReadyToQuit(saveChanges);
            }
        });
        return;
    } else // no unsaved changes at all
    if (saveAction != SaveAction.SAVEASK && unsavedSourceDocs.size() == 0) {
        // define quit operation
        final Operation quitOperation = new Operation() {

            public void execute() {
                quitContext.onReadyToQuit(saveAction == SaveAction.SAVE);
            }
        };
        // if this is a quit session then we always prompt
        if (ApplicationAction.isQuit()) {
            RStudioGinjector.INSTANCE.getGlobalDisplay().showYesNoMessage(MessageDialog.QUESTION, caption, "Are you sure you want to quit the R session?", quitOperation, true);
        } else {
            quitOperation.execute();
        }
        return;
    }
    // just an unsaved environment
    if (unsavedSourceDocs.size() == 0 && workbenchContext != null) {
        // confirm quit and do it
        String prompt = "Save workspace image to " + workbenchContext.getREnvironmentPath() + "?";
        RStudioGinjector.INSTANCE.getGlobalDisplay().showYesNoMessage(GlobalDisplay.MSG_QUESTION, caption, prompt, true, new Operation() {

            public void execute() {
                quitContext.onReadyToQuit(true);
            }
        }, new Operation() {

            public void execute() {
                quitContext.onReadyToQuit(false);
            }
        }, new Operation() {

            public void execute() {
            }
        }, "Save", "Don't Save", true);
    } else // must be from the main window in web mode)
    if (saveAction != SaveAction.SAVEASK && unsavedSourceDocs.size() == 1 && (Desktop.isDesktop() || !(unsavedSourceDocs.get(0) instanceof UnsavedChangesItem))) {
        sourceShim.saveWithPrompt(unsavedSourceDocs.get(0), sourceShim.revertUnsavedChangesBeforeExitCommand(new Command() {

            @Override
            public void execute() {
                quitContext.onReadyToQuit(saveAction == SaveAction.SAVE);
            }
        }), null);
    } else // multiple save targets
    {
        ArrayList<UnsavedChangesTarget> unsaved = new ArrayList<UnsavedChangesTarget>();
        if (saveAction == SaveAction.SAVEASK && globalEnvTarget != null)
            unsaved.add(globalEnvTarget);
        unsaved.addAll(unsavedSourceDocs);
        new UnsavedChangesDialog(caption, unsaved, new OperationWithInput<UnsavedChangesDialog.Result>() {

            @Override
            public void execute(Result result) {
                ArrayList<UnsavedChangesTarget> saveTargets = result.getSaveTargets();
                // remote global env target from list (if specified) and 
                // compute the saveChanges value
                boolean saveGlobalEnv = saveAction == SaveAction.SAVE;
                if (saveAction == SaveAction.SAVEASK && globalEnvTarget != null)
                    saveGlobalEnv = saveTargets.remove(globalEnvTarget);
                final boolean saveChanges = saveGlobalEnv;
                // save specified documents and then quit
                sourceShim.handleUnsavedChangesBeforeExit(saveTargets, new Command() {

                    @Override
                    public void execute() {
                        quitContext.onReadyToQuit(saveChanges);
                    }
                });
            }
        }, // no cancel operation
        null).showModal();
    }
}
Also used : UnsavedChangesItem(org.rstudio.studio.client.workbench.model.UnsavedChangesItem) UnsavedChangesDialog(org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog) UnsavedChangesTarget(org.rstudio.studio.client.workbench.model.UnsavedChangesTarget) Command(com.google.gwt.user.client.Command) RepeatingCommand(com.google.gwt.core.client.Scheduler.RepeatingCommand) ArrayList(java.util.ArrayList) Operation(org.rstudio.core.client.widget.Operation) Result(org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog.Result)

Example 2 with Result

use of org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog.Result in project rstudio by rstudio.

the class SourceBuildHelper method withSaveFilesBeforeCommand.

private void withSaveFilesBeforeCommand(final Command command, final Command cancelCommand, String commandSource) {
    if (uiPrefs_.saveAllBeforeBuild().getValue()) {
        sourceShim_.saveAllUnsaved(command);
    } else {
        String alwaysSaveOption = !uiPrefs_.saveAllBeforeBuild().getValue() ? "Always save files before build" : null;
        ArrayList<UnsavedChangesTarget> unsavedSourceDocs = sourceShim_.getUnsavedChanges(Source.TYPE_FILE_BACKED);
        if (unsavedSourceDocs.size() > 0) {
            new UnsavedChangesDialog(commandSource, alwaysSaveOption, unsavedSourceDocs, new OperationWithInput<UnsavedChangesDialog.Result>() {

                @Override
                public void execute(Result result) {
                    if (result.getAlwaysSave()) {
                        uiPrefs_.saveAllBeforeBuild().setGlobalValue(true);
                        uiPrefs_.writeUIPrefs();
                    }
                    sourceShim_.handleUnsavedChangesBeforeExit(result.getSaveTargets(), command);
                }
            }, cancelCommand).showModal();
        } else {
            command.execute();
        }
    }
}
Also used : UnsavedChangesDialog(org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog) UnsavedChangesTarget(org.rstudio.studio.client.workbench.model.UnsavedChangesTarget) OperationWithInput(org.rstudio.core.client.widget.OperationWithInput) Result(org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog.Result)

Aggregations

UnsavedChangesTarget (org.rstudio.studio.client.workbench.model.UnsavedChangesTarget)2 UnsavedChangesDialog (org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog)2 Result (org.rstudio.studio.client.workbench.ui.unsaved.UnsavedChangesDialog.Result)2 RepeatingCommand (com.google.gwt.core.client.Scheduler.RepeatingCommand)1 Command (com.google.gwt.user.client.Command)1 ArrayList (java.util.ArrayList)1 Operation (org.rstudio.core.client.widget.Operation)1 OperationWithInput (org.rstudio.core.client.widget.OperationWithInput)1 UnsavedChangesItem (org.rstudio.studio.client.workbench.model.UnsavedChangesItem)1