Java Code Examples for javax.swing.text.JTextComponent#getKeymap()

The following examples show how to use javax.swing.text.JTextComponent#getKeymap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompletionScrollPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null && editorActionName != null) {
        Action a = component.getActionMap().get(editorActionName);
        Keymap km = component.getKeymap();
        if (a != null && km != null) {
            KeyStroke[] keys = km.getKeyStrokesForAction(a);
            if (keys != null && keys.length > 0) {
                ret = keys;
            }
        }
    }
    return ret;
}
 
Example 2
Source File: DocumentationScrollPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null) {
        TextUI componentUI = component.getUI();
        Keymap km = component.getKeymap();
        if (componentUI != null && km != null) {
            EditorKit kit = componentUI.getEditorKit(component);
            if (kit instanceof BaseKit) {
                 Action a = ((BaseKit)kit).getActionByName(editorActionName);
                if (a != null) {
                    KeyStroke[] keys = km.getKeyStrokesForAction(a);
                    if (keys != null && keys.length > 0) {
                        ret = keys;
                    }
                }
            }
        }
    }
    return ret;
}
 
Example 3
Source File: DocumentationScrollPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null) {
        TextUI componentUI = component.getUI();
        Keymap km = component.getKeymap();
        if (componentUI != null && km != null) {
            EditorKit kit = componentUI.getEditorKit(component);
            if (kit instanceof BaseKit) {
                Action a = ((BaseKit)kit).getActionByName(editorActionName);
                if (a != null) {
                    KeyStroke[] keys = km.getKeyStrokesForAction(a);
                    if (keys != null && keys.length > 0) {
                        ret = keys;
                    }
                }
            }
        }
    }
    return ret;
}
 
Example 4
Source File: DocumentationScrollPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null) {
        TextUI componentUI = component.getUI();
        Keymap km = component.getKeymap();
        if (componentUI != null && km != null) {
            EditorKit kit = componentUI.getEditorKit(component);
            if (kit instanceof BaseKit) {
                Action a = ((BaseKit)kit).getActionByName(editorActionName);
                if (a != null) {
                    KeyStroke[] keys = km.getKeyStrokesForAction(a);
                    if (keys != null && keys.length > 0) {
                        ret = keys;
                    }
                }
            }
        }
    }
    return ret;
}
 
Example 5
Source File: CslEditorKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target) {
    // Try to get the accelerator
    Keymap km = target.getKeymap();

    if (km != null) {
        KeyStroke[] keys = km.getKeyStrokesForAction(a);

        if ((keys != null) && (keys.length > 0)) {
            item.setAccelerator(keys[0]);
        } else if (a != null) {
            KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);

            if (ks != null) {
                item.setAccelerator(ks);
            }
        }
    }
}
 
Example 6
Source File: CompletionScrollPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null && editorActionName != null) {
        Action a = component.getActionMap().get(editorActionName);
        Keymap km = component.getKeymap();
        if (a != null && km != null) {
            KeyStroke[] keys = km.getKeyStrokesForAction(a);
            if (keys != null && keys.length > 0) {
                ret = keys;
            }
        }
    }
    return ret;
}
 
Example 7
Source File: DocumentationScrollPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey, JTextComponent component) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null) {
        TextUI componentUI = component.getUI();
        Keymap km = component.getKeymap();
        if (componentUI != null && km != null) {
            EditorKit kit = componentUI.getEditorKit(component);
            if (kit instanceof BaseKit) {
                 Action a = ((BaseKit)kit).getActionByName(editorActionName);
                if (a != null) {
                    KeyStroke[] keys = km.getKeyStrokesForAction(a);
                    if (keys != null && keys.length > 0) {
                        ret = keys;
                    }
                }
            }
        }
    }
    return ret;
}
 
Example 8
Source File: NbEditorKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target){
    // Try to get the accelerator
    Keymap km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() :
            target.getKeymap();
    if (km != null) {
        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        if (keys != null && keys.length > 0) {
            boolean added = false;
            for (int i = 0; i<keys.length; i++){
                if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) ||
                    keys[i].getKeyCode() == KeyEvent.VK_ADD){
                    item.setMnemonic(keys[i].getKeyCode());
                    added = true;
                    break;
                }
            }
            if (added == false) item.setMnemonic(keys[0].getKeyCode());
        }
    }
}
 
Example 9
Source File: ScrollCompletionPane.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Attempt to find the editor keystroke for the given editor action. */
private KeyStroke[] findEditorKeys(JTextComponent component, String editorActionName, KeyStroke defaultKey) {
    // This method is implemented due to the issue
    // #25715 - Attempt to search keymap for the keybinding that logically corresponds to the action
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    if (component != null) {
        Action a = component.getActionMap().get(editorActionName);
        Keymap km = component.getKeymap();
        if (a != null && km != null) {
            KeyStroke[] keys = km.getKeyStrokesForAction(a);
            if (keys != null && keys.length > 0) {
                ret = keys;
            }
        }
    }
    return ret;
}
 
Example 10
Source File: MainMenuAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Adds accelerators to given JMenuItem taken from the action */
protected static void addAccelerators(Action a, JMenuItem item, JTextComponent target){
    if (target == null || a==null || item==null) return;
    
    // get accelerators from kitAction
    Action kitAction = getActionByName((String)a.getValue(Action.NAME));
    if (kitAction!=null) a = kitAction;
    // Try to get the accelerator, TopComponent action could be obsoleted
    Keymap km = target.getKeymap();

    if (km != null) {
        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        KeyStroke itemAccelerator = item.getAccelerator();
        
        if (keys != null && keys.length > 0) {
            if (itemAccelerator==null || !itemAccelerator.equals(keys[0])){
                item.setAccelerator(keys[0]);
            }
        }else{
            if (itemAccelerator!=null && kitAction!=null){
                item.setAccelerator(null);
            }
        }
    }
}
 
Example 11
Source File: NbEditorKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void addAcceleretors(Action a, JMenuItem item, JTextComponent target) {
    // Try to get the accelerator
    Keymap km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() :
            target.getKeymap();
    if (km != null) {
        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        if (keys != null && keys.length > 0) {
            boolean added = false;
            for (int i = 0; i<keys.length; i++){
                if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) ||
                    keys[i].getKeyCode() == KeyEvent.VK_ADD){
                    item.setMnemonic(keys[i].getKeyCode());
                    added = true;
                    break;
                }
            }
            if (added == false) {
                item.setMnemonic(keys[0].getKeyCode());
            }
        }else if (a!=null){
            KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
            if (ks!=null) {
                item.setMnemonic(ks.getKeyCode());
            }
        }
    }
}
 
Example 12
Source File: NbEditorToolBar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Attempt to find the editor keystroke for the given action. */
private KeyStroke[] findEditorKeys(String editorActionName, KeyStroke defaultKey) {
    KeyStroke[] ret = new KeyStroke[] { defaultKey };
    JTextComponent comp = getComponent();
    if (editorActionName != null && comp != null) {
        TextUI textUI = comp.getUI();
        Keymap km = comp.getKeymap();
        if (textUI != null && km != null) {
            EditorKit kit = textUI.getEditorKit(comp);
            if (kit instanceof BaseKit) {
                Action a = ((BaseKit)kit).getActionByName(editorActionName);
                if (a != null) {
                    KeyStroke[] keys = km.getKeyStrokesForAction(a);
                    if (keys != null && keys.length > 0) {
                        ret = keys;
                    } else {
                        // try kit's keymap
                        Keymap km2 = ((BaseKit)kit).getKeymap();
                        KeyStroke[] keys2 = km2.getKeyStrokesForAction(a);
                        if (keys2 != null && keys2.length > 0) {
                            ret = keys2;
                        }                            
                    }
                }
            }
        }
    }
    return ret;
}