Example usage for javax.swing InputMap size

List of usage examples for javax.swing InputMap size

Introduction

In this page you can find the example usage for javax.swing InputMap size.

Prototype

public int size() 

Source Link

Document

Returns the number of KeyStroke bindings.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();

    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");

    JButton component = new JButton("button");

    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);

    System.out.println(inputMap.size());

}

From source file:Main.java

/**
 * install focus forward and backward//from   ww w  .j  a  v a2  s.  co  m
 */
public static void installDefaultFocusHandling(Container c) {
    // focus TAB
    HashSet<KeyStroke> set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

    // focus shift-TAB
    set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
    c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);

    // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work
    // correctly
    if (c instanceof JComponent) {
        JComponent jc = (JComponent) c;
        InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED);
        if (inputMapWhenFocused.size() == 0) {
            inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED),
                    "swingDummyFocusKey");
        }
    }
}

From source file:org.omegat.gui.shortcuts.PropertiesShortcutsTest.java

/**
 * Test of bindKeyStrokes method, of class PropertiesShortcuts.
 *///from  w w  w . j a v  a2s  .  co m
@Test
public void testBindKeyStrokes_InputMap_ObjectArr() {
    // bind
    InputMap inputMap = new InputMap();
    shotcuts.bindKeyStrokes(inputMap, TEST_SAVE, TEST_CUT, TEST_USER_1);

    // test map size
    long expSize = 3;
    long size = inputMap.size();
    assertEquals(expSize, size);

    // test keys
    KeyStroke[] expResults = new KeyStroke[] { CTRL_S, CTRL_X, CTRL_P };
    KeyStroke[] results = inputMap.keys();
    assertArrayEquals(expResults, results);

    // test entry1 exists
    Object expResult = TEST_SAVE;
    Object result = inputMap.get(CTRL_S);
    assertEquals(expResult, result);

    // test entry2 exists
    expResult = TEST_CUT;
    result = inputMap.get(CTRL_X);
    assertEquals(expResult, result);

    // test entry3 exists
    expResult = TEST_USER_1;
    result = inputMap.get(CTRL_P);
    assertEquals(expResult, result);

    // test remove entry with null shortcut
    inputMap.put(CTRL_D, TEST_DELETE); // put target
    expResult = TEST_DELETE;
    result = inputMap.get(CTRL_D);
    assertEquals(expResult, result); // target exists before remove
    shotcuts.bindKeyStrokes(inputMap, TEST_DELETE); // key to be removed as null
    result = inputMap.get(CTRL_D);
    assertNull(result); // target will be null after removed

    // test map size again
    expSize = 3;
    size = inputMap.size();
    assertEquals(expSize, size);

    // ensure no affect for entry1 after removing
    expResult = TEST_SAVE;
    result = inputMap.get(CTRL_S);
    assertEquals(expResult, result);

    // ensure no affect for entry2 after removing
    expResult = TEST_CUT;
    result = inputMap.get(CTRL_X);
    assertEquals(expResult, result);

    // ensure no affect for entry3 after removing
    expResult = TEST_USER_1;
    result = inputMap.get(CTRL_P);
    assertEquals(expResult, result);
}

From source file:org.wings.plaf.css.InputMapScriptListener.java

public static void install(SComponent component) {
    ScriptListener[] scriptListeners = component.getScriptListeners();

    for (int i = 0; i < scriptListeners.length; i++) {
        ScriptListener scriptListener = scriptListeners[i];
        if (scriptListener instanceof InputMapScriptListener)
            component.removeScriptListener(scriptListener);
    }/*from  w ww . j  av a  2 s  .  co m*/

    InputMap inputMap = component.getInputMap();
    if (inputMap.size() == 0)
        return; // we're done

    StringBuffer pressed = new StringBuffer();
    StringBuffer typed = new StringBuffer();
    StringBuffer released = new StringBuffer();
    KeyStroke[] keyStrokes = inputMap.keys();

    for (int i = 0; i < keyStrokes.length; i++) {
        KeyStroke keyStroke = keyStrokes[i];
        Object binding = inputMap.get(keyStroke);

        /*
        writeMatch(typed, keyStroke);
        writeRequest(typed, binding);
        */

        switch (keyStroke.getKeyEventType()) {
        case KeyEvent.KEY_PRESSED:
            writeMatch(pressed, keyStroke);
            writeRequest(pressed, binding);
            break;
        case KeyEvent.KEY_TYPED:
            writeMatch(typed, keyStroke);
            writeRequest(typed, binding);
            log.debug("typed binding = " + binding);
            break;
        case KeyEvent.KEY_RELEASED:
            writeMatch(released, keyStroke);
            writeRequest(released, binding);
            log.debug("released binding = " + binding);
            break;
        }
    }

    if (pressed.length() > 0)
        component.addScriptListener(
                new InputMapScriptListener("onkeydown", "return pressed_" + component.getName() + "(event)",
                        "function pressed_" + component.getName() + "(event) {\n  "
                                + "event = getEvent(event);\n  " + pressed.toString() + "  return true;\n}\n"));
    if (typed.length() > 0)
        component.addScriptListener(
                new InputMapScriptListener("onkeypress", "return typed_" + component.getName() + "(event)",
                        "function typed_" + component.getName() + "(event) {\n  "
                                + "event = getEvent(event);\n  " + typed.toString() + "  return true;\n}\n"));
    if (released.length() > 0)
        component.addScriptListener(
                new InputMapScriptListener("onkeyup", "return released_" + component.getName() + "(event)",
                        "function released_" + component.getName() + "(event) {\n"
                                + "event = getEvent(event);\n  " + released.toString()
                                + "  return true;\n}\n"));
}

From source file:org.wings.plaf.css.PrefixAndSuffixDelegate.java

public void writePrefix(Device device, SComponent component) throws IOException {
    final SDimension prefSize = component.getPreferredSize();
    final StringBuffer cssInlineStyle = new StringBuffer();

    Utils.printDebugNewline(device, component);
    Utils.printDebug(device, "<!-- ").print(component.getName()).print(" -->");

    //------------------------ OUTER DIV

    // This is the outer DIV element of a component
    // it is responsible for Postioning (i.e. it take up all free space around to i.e. center
    // the inner div inside this free space
    device.print("<div");
    if (component.getStyle() != null && component.getStyle().length() > 0) {
        Utils.optAttribute(device, "class", component.getStyle() + "_Box");
    }/*www .  ja v  a 2s  . c  om*/
    Utils.optAttribute(device, "id", component.getName());
    if (component instanceof DragSource) {
        cssInlineStyle.append("position:relative;");
    }

    // if sizes are spec'd in percentages, we need the outer box to have full size...
    final boolean isHeightPercentage = prefSize != null && prefSize.getHeightUnit() != null
            && prefSize.getHeightUnit().indexOf("%") != -1;
    final boolean isWidthPercentage = prefSize != null && prefSize.getWidthUnit() != null
            && prefSize.getWidthUnit().indexOf("%") != -1;
    // special case of special case: if the component with relative size is vertically aligned, we must avoid 100% heigth
    final boolean isVAligned = (component.getVerticalAlignment() == SConstants.CENTER
            || component.getVerticalAlignment() == SConstants.BOTTOM);
    if (isHeightPercentage && isVAligned == false) {
        cssInlineStyle.append("height:100%;");
    }
    if (isWidthPercentage) {
        cssInlineStyle.append("width:100%;");
    }

    // Output collected inline CSS style
    Utils.optAttribute(device, "style", cssInlineStyle);
    device.print(">"); // div

    //------------------------ INNER DIV

    // This is the inner DIV around each component.
    // It is responsible for component size, and other styles.
    device.print("<div");
    Utils.optAttribute(device, "id", component.getName() + "_i");
    //id=\"").print(component.getName()).print("\"");
    // Special handling: Mark Titled Borders for styling
    if (component.getBorder() instanceof STitledBorder) {
        Utils.optAttribute(device, "class", component.getStyle() + " STitledBorder");
    } else {
        Utils.optAttribute(device, "class", component.getStyle());
    }
    Utils.optAttribute(device, "style", Utils.generateCSSInlinePreferredSize(prefSize).toString());

    if (component instanceof LowLevelEventListener) {
        LowLevelEventListener lowLevelEventListener = (LowLevelEventListener) component;
        device.print(" eid=\"").print(lowLevelEventListener.getEncodedLowLevelEventId()).print("\"");
    }

    // Tooltip handling
    final String toolTip = component.getToolTipText();
    if (toolTip != null) {
        device.print(" onmouseover=\"return makeTrue(domTT_activate(this, event, 'content', '").print(toolTip)
                .print("', 'predefined', 'default'));\"");
    }

    // Key bindings
    InputMap inputMap = component.getInputMap();
    if (inputMap != null && inputMap.size() > 0) {
        if (false == (inputMap instanceof VersionedInputMap)) {
            log.debug("inputMap = " + inputMap);
            inputMap = new VersionedInputMap(inputMap);
            component.setInputMap(inputMap);
        }

        final VersionedInputMap versionedInputMap = (VersionedInputMap) inputMap;
        final Integer inputMapVersion = (Integer) component.getClientProperty("inputMapVersion");
        if (inputMapVersion == null || versionedInputMap.getVersion() != inputMapVersion.intValue()) {
            log.debug("inputMapVersion = " + inputMapVersion);
            InputMapScriptListener.install(component);
            component.putClientProperty("inputMapVersion", new Integer(versionedInputMap.getVersion()));
        }
    }

    // Component popup menu
    final SPopupMenu menu = component.getComponentPopupMenu();
    if (menu != null) {
        final String componentId = menu.getName();
        final String popupId = componentId + "_pop";
        device.print(" onContextMenu=\"javascript:return wpm_menuPopup(event, '");
        device.print(popupId);
        device.print("');\" onMouseDown=\"javascript:return wpm_menuPopup(event, '");
        device.print(popupId);
        device.print("');\"");
    }

    device.print(">"); // div

    // Special handling: Render title of STitledBorder
    if (component.getBorder() instanceof STitledBorder) {
        STitledBorder titledBorder = (STitledBorder) component.getBorder();
        device.print("<div class=\"STitledBorderLegend\" style=\"");
        titledBorder.getTitleAttributes().write(device);
        device.print("\">");
        device.print(titledBorder.getTitle());
        device.print("</div>");
    }

    component.fireRenderEvent(SComponent.START_RENDERING);
}