Search in sources :

Example 21 with ScheduledCommand

use of com.google.gwt.core.client.Scheduler.ScheduledCommand in project rstudio by rstudio.

the class Source method onSelection.

public void onSelection(SelectionEvent<Integer> event) {
    if (activeEditor_ != null)
        activeEditor_.onDeactivate();
    activeEditor_ = null;
    if (event.getSelectedItem() >= 0) {
        activeEditor_ = editors_.get(event.getSelectedItem());
        activeEditor_.onActivate();
        // let any listeners know this tab was activated
        events_.fireEvent(new DocTabActivatedEvent(activeEditor_.getPath(), activeEditor_.getId()));
        // event
        if (initialized_ && !isDebugSelectionPending()) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                public void execute() {
                    if (activeEditor_ != null)
                        activeEditor_.focus();
                }
            });
        } else if (isDebugSelectionPending()) {
            // we're debugging, so send focus to the console instead of the 
            // editor
            commands_.activateConsole().execute();
            clearPendingDebugSelection();
        }
    }
    if (initialized_)
        manageCommands();
}
Also used : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand)

Example 22 with ScheduledCommand

use of com.google.gwt.core.client.Scheduler.ScheduledCommand in project rstudio by rstudio.

the class Source method doOpenSourceFile.

private void doOpenSourceFile(final FileSystemItem file, final TextFileType fileType, final FilePosition position, final String pattern, final int navMethod, final boolean forceHighlightMode) {
    // if the navigation should happen in another window, do that instead
    NavigationResult navResult = windowManager_.navigateToFile(file, position, navMethod);
    // we navigated externally, just skip this
    if (navResult.getType() == NavigationResult.RESULT_NAVIGATED)
        return;
    // we're about to open in this window--if it's the main window, focus it
    if (SourceWindowManager.isMainSourceWindow() && Desktop.isDesktop())
        Desktop.getFrame().bringMainFrameToFront();
    final boolean isDebugNavigation = navMethod == NavigationMethods.DEBUG_STEP || navMethod == NavigationMethods.DEBUG_END;
    final CommandWithArg<EditingTarget> editingTargetAction = new CommandWithArg<EditingTarget>() {

        @Override
        public void execute(EditingTarget target) {
            if (position != null) {
                SourcePosition endPosition = null;
                if (isDebugNavigation) {
                    DebugFilePosition filePos = (DebugFilePosition) position.cast();
                    endPosition = SourcePosition.create(filePos.getEndLine() - 1, filePos.getEndColumn() + 1);
                    if (Desktop.isDesktop() && navMethod != NavigationMethods.DEBUG_END)
                        Desktop.getFrame().bringMainFrameToFront();
                }
                navigate(target, SourcePosition.create(position.getLine() - 1, position.getColumn() - 1), endPosition);
            } else if (pattern != null) {
                Position pos = target.search(pattern);
                if (pos != null) {
                    navigate(target, SourcePosition.create(pos.getRow(), 0), null);
                }
            }
        }

        private void navigate(final EditingTarget target, final SourcePosition srcPosition, final SourcePosition srcEndPosition) {
            Scheduler.get().scheduleDeferred(new ScheduledCommand() {

                @Override
                public void execute() {
                    if (navMethod == NavigationMethods.DEBUG_STEP) {
                        target.highlightDebugLocation(srcPosition, srcEndPosition, true);
                    } else if (navMethod == NavigationMethods.DEBUG_END) {
                        target.endDebugHighlighting();
                    } else {
                        // force highlight mode if requested
                        if (forceHighlightMode)
                            target.forceLineHighlighting();
                        // now navigate to the new position
                        boolean highlight = navMethod == NavigationMethods.HIGHLIGHT_LINE && !uiPrefs_.highlightSelectedLine().getValue();
                        target.navigateToPosition(srcPosition, false, highlight);
                    }
                }
            });
        }
    };
    if (navResult.getType() == NavigationResult.RESULT_RELOCATE) {
        server_.getSourceDocument(navResult.getDocId(), new ServerRequestCallback<SourceDocument>() {

            @Override
            public void onResponseReceived(final SourceDocument doc) {
                editingTargetAction.execute(addTab(doc, OPEN_REPLAY));
            }

            @Override
            public void onError(ServerError error) {
                globalDisplay_.showErrorMessage("Document Tab Move Failed", "Couldn't move the tab to this window: \n" + error.getMessage());
            }
        });
        return;
    }
    final CommandWithArg<FileSystemItem> action = new CommandWithArg<FileSystemItem>() {

        @Override
        public void execute(FileSystemItem file) {
            openFile(file, fileType, editingTargetAction);
        }
    };
    // highlight in place.
    if (isDebugNavigation) {
        setPendingDebugSelection();
        for (int i = 0; i < editors_.size(); i++) {
            EditingTarget target = editors_.get(i);
            String path = target.getPath();
            if (path != null && path.equalsIgnoreCase(file.getPath())) {
                // the file's open; just update its highlighting 
                if (navMethod == NavigationMethods.DEBUG_END) {
                    target.endDebugHighlighting();
                } else {
                    view_.selectTab(i);
                    editingTargetAction.execute(target);
                }
                return;
            }
        }
        // open a file just to turn off debug highlighting in the file!
        if (navMethod == NavigationMethods.DEBUG_END)
            return;
    }
    // Warning: event.getFile() can be null (e.g. new Sweave document)
    if (file != null && file.getLength() < 0) {
        // If the file has no size info, stat the file from the server. This
        // is to prevent us from opening large files accidentally.
        server_.stat(file.getPath(), new ServerRequestCallback<FileSystemItem>() {

            @Override
            public void onResponseReceived(FileSystemItem response) {
                action.execute(response);
            }

            @Override
            public void onError(ServerError error) {
                // Couldn't stat the file? Proceed anyway. If the file doesn't
                // exist, we'll let the downstream code be the one to show the
                // error.
                action.execute(file);
            }
        });
    } else {
        action.execute(file);
    }
}
Also used : SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) Position(org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position) ServerError(org.rstudio.studio.client.server.ServerError) TextEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.text.TextEditingTarget) EditingTarget(org.rstudio.studio.client.workbench.views.source.editors.EditingTarget) DataEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.data.DataEditingTarget) CodeBrowserEditingTarget(org.rstudio.studio.client.workbench.views.source.editors.codebrowser.CodeBrowserEditingTarget) SourceDocument(org.rstudio.studio.client.workbench.views.source.model.SourceDocument) JSONString(com.google.gwt.json.client.JSONString) JsArrayString(com.google.gwt.core.client.JsArrayString) FileSystemItem(org.rstudio.core.client.files.FileSystemItem) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) NavigationResult(org.rstudio.studio.client.workbench.views.source.SourceWindowManager.NavigationResult)

Example 23 with ScheduledCommand

use of com.google.gwt.core.client.Scheduler.ScheduledCommand in project rstudio by rstudio.

the class AceEditor method doSetCode.

private void doSetCode(String code, boolean preserveCursorPosition) {
    // Filter out Escape characters that might have snuck in from an old
    // bug in 0.95. We can choose to remove this when 0.95 ships, hopefully
    // any documents that would be affected by this will be gone by then.
    code = code.replaceAll("", "");
    // Normalize newlines -- convert all of '\r', '\r\n', '\n\r' to '\n'.
    code = StringUtil.normalizeNewLines(code);
    final AceEditorNative ed = widget_.getEditor();
    if (preserveCursorPosition) {
        final Position cursorPos;
        final int scrollTop, scrollLeft;
        cursorPos = ed.getSession().getSelection().getCursor();
        scrollTop = ed.getRenderer().getScrollTop();
        scrollLeft = ed.getRenderer().getScrollLeft();
        // Setting the value directly on the document prevents undo/redo
        // stack from being blown away
        widget_.getEditor().getSession().getDocument().setValue(code);
        ed.getSession().getSelection().moveCursorTo(cursorPos.getRow(), cursorPos.getColumn(), false);
        ed.getRenderer().scrollToY(scrollTop);
        ed.getRenderer().scrollToX(scrollLeft);
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {

            @Override
            public void execute() {
                ed.getRenderer().scrollToY(scrollTop);
                ed.getRenderer().scrollToX(scrollLeft);
            }
        });
    } else {
        ed.getSession().setValue(code);
        ed.getSession().getSelection().moveCursorTo(0, 0, false);
    }
}
Also used : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) InputEditorPosition(org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition) SourcePosition(org.rstudio.studio.client.workbench.views.source.model.SourcePosition) Breakpoint(org.rstudio.studio.client.common.debugging.model.Breakpoint)

Example 24 with ScheduledCommand

use of com.google.gwt.core.client.Scheduler.ScheduledCommand in project rstudio by rstudio.

the class ShortcutManager method handleKeyDown.

private boolean handleKeyDown(NativeEvent event) {
    // generated by Qt when commands executed from menu.
    if (activeEditEventType_ != EditEvent.TYPE_NONE) {
        final int type = activeEditEventType_;
        Scheduler.get().scheduleDeferred(new ScheduledCommand() {

            @Override
            public void execute() {
                events_.fireEvent(new EditEvent(false, type));
            }
        });
        activeEditEventType_ = EditEvent.TYPE_NONE;
        keyBuffer_.clear();
        return false;
    }
    // modal dialogs)
    if (!isEnabled())
        return false;
    // Don't dispatch on bare modifier keypresses.
    if (KeyboardHelper.isModifierKey(event.getKeyCode()))
        return false;
    // practice, this is better than breaking copy + paste everywhere else...
    if (isEditKeyCombination(event)) {
        Element target = Element.as(event.getEventTarget());
        AceEditorNative editor = AceEditorNative.getEditor(target);
        if (editor == null) {
            keyBuffer_.clear();
            return false;
        }
    }
    // Escape key should always clear the keybuffer.
    if (event.getKeyCode() == KeyCodes.KEY_ESCAPE) {
        keyBuffer_.clear();
        return false;
    }
    int keyCode = event.getKeyCode();
    int modifier = KeyboardShortcut.getModifierValue(event);
    // since we have code wired to that expectation
    if (keyCode == 173)
        keyCode = 189;
    KeyCombination keyCombination = new KeyCombination(keyCode, modifier);
    // is focused.
    if (keyCombination.getKeyCode() == KeyCodes.KEY_F && keyCombination.getModifier() == KeyboardShortcut.CTRL) {
        Element target = Element.as(event.getEventTarget());
        AceEditorNative editor = AceEditorNative.getEditor(target);
        if (editor != null && editor.isVimModeOn()) {
            keyBuffer_.clear();
            return false;
        }
    }
    // Bail if this is an ignored key combination.
    if (isIgnoredKeyCombination(keyCombination)) {
        keyBuffer_.clear();
        return false;
    }
    keyBuffer_.add(keyCombination);
    // Loop through all active key maps, and attempt to find an active
    // binding. 'pending' is used to indicate whether there are any bindings
    // following the current state of the keybuffer.
    boolean pending = false;
    for (Map.Entry<KeyMapType, KeyMap> entry : keyMaps_.entrySet()) {
        KeyMap map = entry.getValue();
        CommandBinding binding = map.getActiveBinding(keyBuffer_);
        if (binding != null) {
            keyBuffer_.clear();
            if (XTermWidget.isXTerm(Element.as(event.getEventTarget()))) {
                if (binding.getId() == "consoleClear") {
                    // special case; we expect users will try to use Ctrl+L to
                    // clear the terminal, and don't want that to actually
                    // clear the currently hidden console instead
                    event.stopPropagation();
                    commands_.clearTerminalScrollbackBuffer().execute();
                    return false;
                } else if (binding.getId() == "closeSourceDoc") {
                    // when focus is in the terminal and let terminal see keys
                    return false;
                }
                if (binding.getContext() != AppCommand.Context.Workbench && binding.getContext() != AppCommand.Context.Addin && binding.getContext() != AppCommand.Context.PackageDevelopment) {
                    // Let terminal see the keyboard input and don't execute command.
                    return false;
                }
            }
            event.stopPropagation();
            binding.execute();
            return true;
        }
        if (map.isPrefix(keyBuffer_))
            pending = true;
    }
    if (!(pending || isPrefixForEditor(keyCombination, event)))
        keyBuffer_.clear();
    // 'I', and some other cases)
    if (!keyBuffer_.isEmpty()) {
        KeyCombination keys = keyBuffer_.get(keyBuffer_.size() - 1);
        if (keys.getModifier() == KeyboardShortcut.NONE)
            keyBuffer_.clear();
    }
    return false;
}
Also used : Element(com.google.gwt.dom.client.Element) KeyCombination(org.rstudio.core.client.command.KeyboardShortcut.KeyCombination) CommandBinding(org.rstudio.core.client.command.KeyMap.CommandBinding) AceEditorNative(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceEditorNative) ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) EditEvent(org.rstudio.studio.client.events.EditEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) KeyMapType(org.rstudio.core.client.command.KeyMap.KeyMapType)

Example 25 with ScheduledCommand

use of com.google.gwt.core.client.Scheduler.ScheduledCommand in project rstudio by rstudio.

the class DualWindowLayoutPanel method replaceWindows.

public void replaceWindows(LogicalWindow windowA, LogicalWindow windowB) {
    unhookEvents();
    windowA_ = windowA;
    windowB_ = windowB;
    hookEvents();
    layout_.setWidgets(new Widget[] { windowA_.getNormal(), windowA_.getMinimized(), windowB_.getNormal(), windowB_.getMinimized() });
    Scheduler.get().scheduleFinally(new ScheduledCommand() {

        public void execute() {
            windowA_.onWindowStateChange(new WindowStateChangeEvent(NORMAL));
        }
    });
}
Also used : ScheduledCommand(com.google.gwt.core.client.Scheduler.ScheduledCommand) WindowStateChangeEvent(org.rstudio.core.client.events.WindowStateChangeEvent)

Aggregations

ScheduledCommand (com.google.gwt.core.client.Scheduler.ScheduledCommand)104 Position (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Position)7 Element (com.google.gwt.dom.client.Element)6 Range (org.rstudio.studio.client.workbench.views.source.editors.text.ace.Range)6 JsArray (com.google.gwt.core.client.JsArray)5 Command (com.google.gwt.user.client.Command)5 ServerError (org.rstudio.studio.client.server.ServerError)5 JsArrayString (com.google.gwt.core.client.JsArrayString)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 FileSystemItem (org.rstudio.core.client.files.FileSystemItem)4 RepeatingCommand (com.google.gwt.core.client.Scheduler.RepeatingCommand)3 ClickEvent (com.google.gwt.event.dom.client.ClickEvent)3 Event (com.google.gwt.user.client.Event)3 NativePreviewHandler (com.google.gwt.user.client.Event.NativePreviewHandler)3 Timer (com.google.gwt.user.client.Timer)3 WindowEx (org.rstudio.core.client.dom.WindowEx)3 SourcePosition (org.rstudio.studio.client.workbench.views.source.model.SourcePosition)3 ErrorDialog (com.google.gerrit.client.ErrorDialog)2 OnEditEnabler (com.google.gerrit.client.ui.OnEditEnabler)2