Search in sources :

Example 1 with SerializedCommandQueue

use of org.rstudio.core.client.SerializedCommandQueue in project rstudio by rstudio.

the class Projects method createNewProject.

private void createNewProject(final NewProjectResult newProject, final boolean saveChanges) {
    // This gets a little crazy. We have several pieces of asynchronous logic
    // that each may or may not need to be executed, depending on the type
    // of project being created and on whether the previous pieces of logic
    // succeed. Plus we have this ProgressIndicator that needs to be fed
    // properly.
    final ProgressIndicator indicator = globalDisplay_.getProgressIndicator("Error Creating Project");
    // Here's the command queue that will hold the various operations.
    final SerializedCommandQueue createProjectCmds = new SerializedCommandQueue();
    // WARNING: When calling addCommand, BE SURE TO PASS FALSE as the second
    // argument, to delay running of the commands until they are all
    // scheduled.
    // First, attempt to update the default project location pref
    createProjectCmds.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            UIPrefs uiPrefs = pUIPrefs_.get();
            // update default project location pref if necessary
            if ((newProject.getNewDefaultProjectLocation() != null) || (newProject.getCreateGitRepo() != uiPrefs.newProjGitInit().getValue())) {
                indicator.onProgress("Saving defaults...");
                if (newProject.getNewDefaultProjectLocation() != null) {
                    uiPrefs.defaultProjectLocation().setGlobalValue(newProject.getNewDefaultProjectLocation());
                }
                if (newProject.getCreateGitRepo() != uiPrefs.newProjGitInit().getValue()) {
                    uiPrefs.newProjGitInit().setGlobalValue(newProject.getCreateGitRepo());
                }
                if (newProject.getUsePackrat() != uiPrefs.newProjUsePackrat().getValue()) {
                    uiPrefs.newProjUsePackrat().setGlobalValue(newProject.getUsePackrat());
                }
                // call the server -- in all cases continue on with
                // creating the project (swallow errors updating the pref)
                projServer_.setUiPrefs(session_.getSessionInfo().getUiPrefs(), new VoidServerRequestCallback(indicator) {

                    @Override
                    public void onResponseReceived(Void response) {
                        continuation.execute();
                    }

                    @Override
                    public void onError(ServerError error) {
                        super.onError(error);
                        continuation.execute();
                    }
                });
            } else {
                continuation.execute();
            }
        }
    }, false);
    // Next, if necessary, clone a repo
    if (newProject.getVcsCloneOptions() != null) {
        createProjectCmds.addCommand(new SerializedCommand() {

            @Override
            public void onExecute(final Command continuation) {
                VcsCloneOptions cloneOptions = newProject.getVcsCloneOptions();
                if (cloneOptions.getVcsName().equals((VCSConstants.GIT_ID)))
                    indicator.onProgress("Cloning Git repository...");
                else
                    indicator.onProgress("Checking out SVN repository...");
                gitServer_.vcsClone(cloneOptions, new ServerRequestCallback<ConsoleProcess>() {

                    @Override
                    public void onResponseReceived(ConsoleProcess proc) {
                        final ConsoleProgressDialog consoleProgressDialog = new ConsoleProgressDialog(proc, gitServer_);
                        consoleProgressDialog.showModal();
                        proc.addProcessExitHandler(new ProcessExitEvent.Handler() {

                            @Override
                            public void onProcessExit(ProcessExitEvent event) {
                                if (event.getExitCode() == 0) {
                                    consoleProgressDialog.hide();
                                    continuation.execute();
                                } else {
                                    indicator.onCompleted();
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(ServerError error) {
                        Debug.logError(error);
                        indicator.onError(error.getUserMessage());
                    }
                });
            }
        }, false);
    }
    // Next, create the project itself -- depending on the type, this
    // could involve creating an R package, or Shiny application, and so on.
    createProjectCmds.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            // Validate the package name if we're creating a package
            if (newProject.getNewPackageOptions() != null) {
                final String packageName = newProject.getNewPackageOptions().getPackageName();
                if (!PACKAGE_NAME_PATTERN.test(packageName)) {
                    indicator.onError("Invalid package name '" + packageName + "': " + "package names must start with a letter, and contain " + "only letters and numbers.");
                    return;
                }
            }
            indicator.onProgress("Creating project...");
            if (newProject.getNewPackageOptions() == null) {
                projServer_.createProject(newProject.getProjectFile(), newProject.getNewPackageOptions(), newProject.getNewShinyAppOptions(), newProject.getProjectTemplateOptions(), new VoidServerRequestCallback(indicator) {

                    @Override
                    public void onSuccess() {
                        continuation.execute();
                    }
                });
            } else {
                String projectFile = newProject.getProjectFile();
                String packageDirectory = projectFile.substring(0, projectFile.lastIndexOf('/'));
                projServer_.packageSkeleton(newProject.getNewPackageOptions().getPackageName(), packageDirectory, newProject.getNewPackageOptions().getCodeFiles(), newProject.getNewPackageOptions().getUsingRcpp(), new ServerRequestCallback<RResult<Void>>() {

                    @Override
                    public void onResponseReceived(RResult<Void> response) {
                        if (response.failed())
                            indicator.onError(response.errorMessage());
                        else
                            continuation.execute();
                    }

                    @Override
                    public void onError(ServerError error) {
                        Debug.logError(error);
                        indicator.onError(error.getUserMessage());
                    }
                });
            }
        }
    }, false);
    // Next, initialize a git repo if requested
    if (newProject.getCreateGitRepo()) {
        createProjectCmds.addCommand(new SerializedCommand() {

            @Override
            public void onExecute(final Command continuation) {
                indicator.onProgress("Initializing git repository...");
                String projDir = FileSystemItem.createFile(newProject.getProjectFile()).getParentPathString();
                gitServer_.gitInitRepo(projDir, new VoidServerRequestCallback(indicator) {

                    @Override
                    public void onSuccess() {
                        continuation.execute();
                    }

                    @Override
                    public void onFailure() {
                        continuation.execute();
                    }
                });
            }
        }, false);
    }
    // Generate a new packrat project
    if (newProject.getUsePackrat()) {
        createProjectCmds.addCommand(new SerializedCommand() {

            @Override
            public void onExecute(final Command continuation) {
                indicator.onProgress("Initializing packrat project...");
                String projDir = FileSystemItem.createFile(newProject.getProjectFile()).getParentPathString();
                packratServer_.packratBootstrap(projDir, false, new VoidServerRequestCallback(indicator) {

                    @Override
                    public void onSuccess() {
                        continuation.execute();
                    }
                });
            }
        }, false);
    }
    if (newProject.getOpenInNewWindow()) {
        createProjectCmds.addCommand(new SerializedCommand() {

            @Override
            public void onExecute(final Command continuation) {
                FileSystemItem project = FileSystemItem.createFile(newProject.getProjectFile());
                if (Desktop.isDesktop()) {
                    Desktop.getFrame().openProjectInNewWindow(project.getPath());
                    continuation.execute();
                } else {
                    indicator.onProgress("Preparing to open project...");
                    serverOpenProjectInNewWindow(project, newProject.getRVersion(), continuation);
                }
            }
        }, false);
    }
    // If we get here, dismiss the progress indicator
    createProjectCmds.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(Command continuation) {
            indicator.onCompleted();
            if (!newProject.getOpenInNewWindow()) {
                applicationQuit_.performQuit(saveChanges, newProject.getProjectFile(), newProject.getRVersion());
            }
            continuation.execute();
        }
    }, false);
    // Now set it all in motion!
    createProjectCmds.run();
}
Also used : ConsoleProgressDialog(org.rstudio.studio.client.workbench.views.vcs.common.ConsoleProgressDialog) VcsCloneOptions(org.rstudio.studio.client.common.vcs.VcsCloneOptions) SerializedCommand(org.rstudio.core.client.SerializedCommand) SerializedCommandQueue(org.rstudio.core.client.SerializedCommandQueue) ServerError(org.rstudio.studio.client.server.ServerError) FileSystemItem(org.rstudio.core.client.files.FileSystemItem) Command(com.google.gwt.user.client.Command) SerializedCommand(org.rstudio.core.client.SerializedCommand) ProgressIndicator(org.rstudio.core.client.widget.ProgressIndicator) ConsoleProcess(org.rstudio.studio.client.common.console.ConsoleProcess) VoidServerRequestCallback(org.rstudio.studio.client.server.VoidServerRequestCallback) VoidServerRequestCallback(org.rstudio.studio.client.server.VoidServerRequestCallback) ServerRequestCallback(org.rstudio.studio.client.server.ServerRequestCallback) UIPrefs(org.rstudio.studio.client.workbench.prefs.model.UIPrefs) Void(org.rstudio.studio.client.server.Void) ProcessExitEvent(org.rstudio.studio.client.common.console.ProcessExitEvent) RResult(org.rstudio.studio.client.server.remote.RResult)

Example 2 with SerializedCommandQueue

use of org.rstudio.core.client.SerializedCommandQueue in project rstudio by rstudio.

the class ModifyKeyboardShortcutsWidget method collectShortcuts.

private void collectShortcuts() {
    final List<KeyboardShortcutEntry> bindings = new ArrayList<KeyboardShortcutEntry>();
    SerializedCommandQueue queue = new SerializedCommandQueue();
    // Load addins discovered as part of package exports. This registers
    // the addin, with the actual keybinding to be registered later,
    // if discovered.
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            RAddins rAddins = addins_.getRAddins();
            for (String key : JsUtil.asIterable(rAddins.keys())) {
                RAddin addin = rAddins.get(key);
                bindings.add(new KeyboardShortcutEntry(addin.getPackage() + "::" + addin.getBinding(), addin.getName(), new KeySequence(), KeyboardShortcutEntry.TYPE_ADDIN, false, AppCommand.Context.Addin));
            }
            continuation.execute();
        }
    });
    // Load saved addin bindings
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            addins_.loadBindings(new CommandWithArg<EditorKeyBindings>() {

                @Override
                public void execute(EditorKeyBindings addinBindings) {
                    for (String commandId : addinBindings.iterableKeys()) {
                        EditorKeyBinding addinBinding = addinBindings.get(commandId);
                        for (KeyboardShortcutEntry binding : bindings) {
                            if (binding.getId() == commandId) {
                                List<KeySequence> keys = addinBinding.getKeyBindings();
                                if (keys.size() >= 1)
                                    binding.setDefaultKeySequence(keys.get(0));
                                if (keys.size() >= 2) {
                                    for (int i = 1; i < keys.size(); i++) {
                                        bindings.add(new KeyboardShortcutEntry(binding.getId(), binding.getName(), keys.get(i), KeyboardShortcutEntry.TYPE_ADDIN, false, AppCommand.Context.Addin));
                                    }
                                }
                            }
                        }
                    }
                    continuation.execute();
                }
            });
        }
    });
    // Ace loading command
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            // Ace Commands
            JsArray<AceCommand> aceCommands = editorCommands_.getCommands();
            for (int i = 0; i < aceCommands.length(); i++) {
                AceCommand command = aceCommands.get(i);
                JsArrayString shortcuts = command.getBindingsForCurrentPlatform();
                if (shortcuts != null) {
                    String id = command.getInternalName();
                    String name = command.getDisplayName();
                    boolean custom = command.isCustomBinding();
                    for (int j = 0; j < shortcuts.length(); j++) {
                        String shortcut = shortcuts.get(j);
                        KeySequence keys = KeySequence.fromShortcutString(shortcut);
                        int type = KeyboardShortcutEntry.TYPE_EDITOR_COMMAND;
                        bindings.add(new KeyboardShortcutEntry(id, name, keys, type, custom, AppCommand.Context.Editor));
                    }
                }
            }
            continuation.execute();
        }
    });
    // RStudio commands
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            // RStudio Commands
            appCommands_.loadBindings(new CommandWithArg<EditorKeyBindings>() {

                @Override
                public void execute(final EditorKeyBindings customBindings) {
                    Map<String, AppCommand> commands = commands_.getCommands();
                    for (Map.Entry<String, AppCommand> entry : commands.entrySet()) {
                        AppCommand command = entry.getValue();
                        if (isExcludedCommand(command))
                            continue;
                        String id = command.getId();
                        String name = getAppCommandName(command);
                        int type = KeyboardShortcutEntry.TYPE_RSTUDIO_COMMAND;
                        boolean isCustom = customBindings.hasKey(id);
                        List<KeySequence> keySequences = new ArrayList<KeySequence>();
                        if (isCustom)
                            keySequences = customBindings.get(id).getKeyBindings();
                        else
                            keySequences.add(command.getKeySequence());
                        for (KeySequence keys : keySequences) {
                            KeyboardShortcutEntry binding = new KeyboardShortcutEntry(id, name, keys, type, isCustom, command.getContext());
                            bindings.add(binding);
                        }
                    }
                    continuation.execute();
                }
            });
        }
    });
    // Sort and finish up
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            Collections.sort(bindings, new Comparator<KeyboardShortcutEntry>() {

                @Override
                public int compare(KeyboardShortcutEntry o1, KeyboardShortcutEntry o2) {
                    if (o1.getContext() != o2.getContext())
                        return o1.getContext().compareTo(o2.getContext());
                    return o1.getName().compareTo(o2.getName());
                }
            });
            originalBindings_ = bindings;
            updateData(bindings);
            continuation.execute();
        }
    });
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(Command continuation) {
            if (initialFilterText_ != null) {
                filterWidget_.setText(initialFilterText_);
                filter();
            }
            continuation.execute();
        }
    });
    // Exhaust the queue
    queue.run();
}
Also used : SerializedCommand(org.rstudio.core.client.SerializedCommand) RAddins(org.rstudio.studio.client.workbench.addins.Addins.RAddins) SerializedCommandQueue(org.rstudio.core.client.SerializedCommandQueue) ArrayList(java.util.ArrayList) JsArrayString(com.google.gwt.core.client.JsArrayString) CommandWithArg(org.rstudio.core.client.CommandWithArg) Comparator(java.util.Comparator) EditorKeyBinding(org.rstudio.core.client.command.EditorCommandManager.EditorKeyBinding) EditorKeyBindings(org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings) JsArray(com.google.gwt.core.client.JsArray) RAddin(org.rstudio.studio.client.workbench.addins.Addins.RAddin) JsArrayString(com.google.gwt.core.client.JsArrayString) Command(com.google.gwt.user.client.Command) SerializedCommand(org.rstudio.core.client.SerializedCommand) AceCommand(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceCommand) AceCommand(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceCommand) KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with SerializedCommandQueue

use of org.rstudio.core.client.SerializedCommandQueue in project rstudio by rstudio.

the class WorkbenchScreen method prefetch.

private void prefetch() {
    final SerializedCommandQueue prefetchQueue = new SerializedCommandQueue();
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        public void execute() {
            onPaneSizesChanged();
        }
    });
    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

        public void execute() {
            for (final WorkbenchTab tab : paneManager_.getAllTabs()) prefetchQueue.addCommand(new SerializedCommand() {

                public void onExecute(Command continuation) {
                    tab.prefetch(continuation);
                }
            });
            prefetchQueue.addCommand(new SerializedCommand() {

                public void onExecute(Command continuation) {
                    ApplicationEndedPopupPanel.prefetch(continuation);
                }
            });
            prefetchQueue.addCommand(new SerializedCommand() {

                public void onExecute(Command continuation) {
                    edit_.forceLoad(true, continuation);
                }
            });
            prefetchQueue.addCommand(new SerializedCommand() {

                public void onExecute(Command continuation) {
                    optionsLoader_.forceLoad(true, continuation);
                }
            });
        }
    });
}
Also used : SerializedCommand(org.rstudio.core.client.SerializedCommand) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) SerializedCommand(org.rstudio.core.client.SerializedCommand) Command(com.google.gwt.user.client.Command) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) TimeBufferedCommand(org.rstudio.core.client.TimeBufferedCommand) SerializedCommandQueue(org.rstudio.core.client.SerializedCommandQueue)

Aggregations

Command (com.google.gwt.user.client.Command)3 SerializedCommand (org.rstudio.core.client.SerializedCommand)3 SerializedCommandQueue (org.rstudio.core.client.SerializedCommandQueue)3 JsArray (com.google.gwt.core.client.JsArray)1 JsArrayString (com.google.gwt.core.client.JsArrayString)1 ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CommandWithArg (org.rstudio.core.client.CommandWithArg)1 TimeBufferedCommand (org.rstudio.core.client.TimeBufferedCommand)1 EditorKeyBinding (org.rstudio.core.client.command.EditorCommandManager.EditorKeyBinding)1 EditorKeyBindings (org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings)1 KeySequence (org.rstudio.core.client.command.KeyboardShortcut.KeySequence)1 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)1 ProgressIndicator (org.rstudio.core.client.widget.ProgressIndicator)1 ConsoleProcess (org.rstudio.studio.client.common.console.ConsoleProcess)1 ProcessExitEvent (org.rstudio.studio.client.common.console.ProcessExitEvent)1 VcsCloneOptions (org.rstudio.studio.client.common.vcs.VcsCloneOptions)1