Search in sources :

Example 1 with KeySequence

use of org.rstudio.core.client.command.KeyboardShortcut.KeySequence in project rstudio by rstudio.

the class ModifyKeyboardShortcutsWidget method applyChanges.

private void applyChanges() {
    // Build up command diffs for save after application
    final EditorKeyBindings editorBindings = EditorKeyBindings.create();
    final EditorKeyBindings appBindings = EditorKeyBindings.create();
    final EditorKeyBindings addinBindings = EditorKeyBindings.create();
    // Loop through all changes and apply based on type
    for (Map.Entry<KeyboardShortcutEntry, KeyboardShortcutEntry> entry : changes_.entrySet()) {
        KeyboardShortcutEntry newBinding = entry.getValue();
        String id = newBinding.getId();
        // Get all commands with this ID.
        List<KeyboardShortcutEntry> bindingsWithId = new ArrayList<KeyboardShortcutEntry>();
        for (KeyboardShortcutEntry binding : originalBindings_) if (binding.getId().equals(id))
            bindingsWithId.add(binding);
        // Collect all shortcuts.
        List<KeySequence> keys = new ArrayList<KeySequence>();
        for (KeyboardShortcutEntry binding : bindingsWithId) keys.add(binding.getKeySequence());
        int commandType = newBinding.getCommandType();
        if (commandType == KeyboardShortcutEntry.TYPE_RSTUDIO_COMMAND)
            appBindings.setBindings(id, keys);
        else if (commandType == KeyboardShortcutEntry.TYPE_EDITOR_COMMAND)
            editorBindings.setBindings(id, keys);
        else if (commandType == KeyboardShortcutEntry.TYPE_ADDIN)
            addinBindings.setBindings(id, keys);
    }
    // Tell satellites that they need to update bindings.
    appCommands_.addBindingsAndSave(appBindings, new CommandWithArg<EditorKeyBindings>() {

        @Override
        public void execute(EditorKeyBindings bindings) {
            events_.fireEventToAllSatellites(new RStudioKeybindingsChangedEvent(bindings));
        }
    });
    editorCommands_.addBindingsAndSave(editorBindings, new CommandWithArg<EditorKeyBindings>() {

        @Override
        public void execute(EditorKeyBindings bindings) {
            events_.fireEventToAllSatellites(new EditorKeybindingsChangedEvent(bindings));
        }
    });
    addins_.addBindingsAndSave(addinBindings, new CommandWithArg<EditorKeyBindings>() {

        @Override
        public void execute(EditorKeyBindings bindings) {
            events_.fireEvent(new AddinsKeyBindingsChangedEvent(bindings));
        }
    });
    closeDialog();
}
Also used : EditorKeyBindings(org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings) ArrayList(java.util.ArrayList) EditorKeybindingsChangedEvent(org.rstudio.core.client.events.EditorKeybindingsChangedEvent) JsArrayString(com.google.gwt.core.client.JsArrayString) RStudioKeybindingsChangedEvent(org.rstudio.core.client.events.RStudioKeybindingsChangedEvent) AddinsKeyBindingsChangedEvent(org.rstudio.studio.client.workbench.addins.AddinsKeyBindingsChangedEvent) Map(java.util.Map) HashMap(java.util.HashMap) KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence)

Example 2 with KeySequence

use of org.rstudio.core.client.command.KeyboardShortcut.KeySequence in project rstudio by rstudio.

the class ModifyKeyboardShortcutsWidget method editableTextColumn.

private Column<KeyboardShortcutEntry, String> editableTextColumn(String name, final ValueGetter<KeyboardShortcutEntry> getter) {
    EditTextCell editTextCell = new EditTextCell() {

        @Override
        public void onBrowserEvent(final Context context, final Element parent, final String value, final NativeEvent event, final ValueUpdater<String> updater) {
            // handler.
            if (event.getType().equals("keyup") && event.getKeyCode() == KeyCodes.KEY_ESCAPE) {
                parent.getFirstChildElement().blur();
                return;
            }
            super.onBrowserEvent(context, parent, value, event, updater);
        }
    };
    Column<KeyboardShortcutEntry, String> column = new Column<KeyboardShortcutEntry, String>(editTextCell) {

        @Override
        public String getValue(KeyboardShortcutEntry binding) {
            return getter.getValue(binding);
        }
    };
    column.setFieldUpdater(new FieldUpdater<KeyboardShortcutEntry, String>() {

        @Override
        public void update(int index, KeyboardShortcutEntry binding, String value) {
            KeySequence keys = KeySequence.fromShortcutString(value);
            // adding a new key sequence.
            if (keys.equals(binding.getOriginalKeySequence())) {
                changes_.remove(binding);
                binding.restoreOriginalKeySequence();
            } else {
                KeyboardShortcutEntry newBinding = new KeyboardShortcutEntry(binding.getId(), binding.getName(), keys, binding.getCommandType(), true, binding.getContext());
                changes_.put(binding, newBinding);
                binding.setKeySequence(keys);
            }
            table_.setKeyboardSelectedColumn(0);
            updateData(dataProvider_.getList());
        }
    });
    column.setSortable(true);
    table_.addColumn(column, new TextHeader(name));
    return column;
}
Also used : EditTextCell(com.google.gwt.cell.client.EditTextCell) Element(com.google.gwt.dom.client.Element) TableRowElement(com.google.gwt.dom.client.TableRowElement) TextHeader(com.google.gwt.user.cellview.client.TextHeader) JsArrayString(com.google.gwt.core.client.JsArrayString) ValueUpdater(com.google.gwt.cell.client.ValueUpdater) TextColumn(com.google.gwt.user.cellview.client.TextColumn) Column(com.google.gwt.user.cellview.client.Column) KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence) NativeEvent(com.google.gwt.dom.client.NativeEvent)

Example 3 with KeySequence

use of org.rstudio.core.client.command.KeyboardShortcut.KeySequence in project rstudio by rstudio.

the class ShortcutManager method register.

public void register(int m1, int k1, int m2, int k2, AppCommand command, String groupName, String title, String disableModes) {
    KeySequence sequence = new KeySequence();
    sequence.add(k1, m1);
    sequence.add(k2, m2);
    register(sequence, command, groupName, title, disableModes);
}
Also used : KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence)

Example 4 with KeySequence

use of org.rstudio.core.client.command.KeyboardShortcut.KeySequence in project rstudio by rstudio.

the class UserCommandManager method onRegisterUserCommand.

private void onRegisterUserCommand(RegisterUserCommandEvent event) {
    final String name = event.getData().getName();
    JsArrayString shortcutStrings = event.getData().getShortcuts();
    for (int i = 0; i < shortcutStrings.length(); i++) {
        String shortcutString = shortcutStrings.get(i);
        KeySequence sequence = KeySequence.fromShortcutString(shortcutString);
        assert sequence != null : "Failed to parse string '" + shortcutString + "'";
        KeyboardShortcut shortcut = new KeyboardShortcut(sequence);
        UserCommand command = new UserCommand(name, new Command() {

            @Override
            public void execute() {
                events_.fireEvent(new ExecuteUserCommandEvent(name));
            }
        });
        commandMap_.put(shortcut, command);
    }
}
Also used : Command(com.google.gwt.user.client.Command) ExecuteUserCommandEvent(org.rstudio.studio.client.server.remote.ExecuteUserCommandEvent) JsArrayString(com.google.gwt.core.client.JsArrayString) JsArrayString(com.google.gwt.core.client.JsArrayString) KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence)

Example 5 with KeySequence

use of org.rstudio.core.client.command.KeyboardShortcut.KeySequence in project rstudio by rstudio.

the class ModifyKeyboardShortcutsWidget method sort.

private void sort(List<KeyboardShortcutEntry> data, final int column, final boolean ascending) {
    Collections.sort(data, new Comparator<KeyboardShortcutEntry>() {

        @Override
        public int compare(KeyboardShortcutEntry o1, KeyboardShortcutEntry o2) {
            int result = 0;
            if (column == 0) {
                result = o1.getName().compareTo(o2.getName());
            } else if (column == 1) {
                KeySequence k1 = o1.getKeySequence();
                KeySequence k2 = o2.getKeySequence();
                if (k1 == null && k2 == null)
                    result = 0;
                else if (k1 == null)
                    result = 1;
                else if (k2 == null)
                    result = -1;
                else
                    result = k1.toString().compareTo(k2.toString());
            } else if (column == 2) {
                result = o1.getContext().toString().compareTo(o2.getContext().toString());
            }
            return ascending ? result : -result;
        }
    });
}
Also used : KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence)

Aggregations

KeySequence (org.rstudio.core.client.command.KeyboardShortcut.KeySequence)11 JsArrayString (com.google.gwt.core.client.JsArrayString)5 ArrayList (java.util.ArrayList)3 RAddin (org.rstudio.studio.client.workbench.addins.Addins.RAddin)3 TableRowElement (com.google.gwt.dom.client.TableRowElement)2 Command (com.google.gwt.user.client.Command)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EditorKeyBindings (org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings)2 RAddins (org.rstudio.studio.client.workbench.addins.Addins.RAddins)2 EditTextCell (com.google.gwt.cell.client.EditTextCell)1 ValueUpdater (com.google.gwt.cell.client.ValueUpdater)1 JsArray (com.google.gwt.core.client.JsArray)1 Element (com.google.gwt.dom.client.Element)1 NativeEvent (com.google.gwt.dom.client.NativeEvent)1 Column (com.google.gwt.user.cellview.client.Column)1 TextColumn (com.google.gwt.user.cellview.client.TextColumn)1 TextHeader (com.google.gwt.user.cellview.client.TextHeader)1 Comparator (java.util.Comparator)1 List (java.util.List)1