Example #1
0
  /**
   * register keystrokes here which are for the WHEN_IN_FOCUSED_WINDOW case. Other types of
   * keystrokes will be handled by walking the hierarchy That simplifies some potentially hairy
   * stuff.
   */
  public void registerKeyStroke(KeyStroke k, JComponent c) {
    Container topContainer = getTopAncestor(c);
    if (topContainer == null) {
      return;
    }
    Hashtable keyMap = containerMap.get(topContainer);

    if (keyMap == null) { // lazy evaluate one
      keyMap = registerNewTopContainer(topContainer);
    }

    Object tmp = keyMap.get(k);
    if (tmp == null) {
      keyMap.put(k, c);
    } else if (tmp instanceof Vector) { // if there's a Vector there then add to it.
      Vector v = (Vector) tmp;
      if (!v.contains(c)) { // only add if this keystroke isn't registered for this component
        v.addElement(c);
      }
    } else if (tmp instanceof JComponent) {
      // if a JComponent is there then remove it and replace it with a vector
      // Then add the old compoennt and the new compoent to the vector
      // then insert the vector in the table
      if (tmp != c) { // this means this is already registered for this component, no need to dup
        Vector<JComponent> v = new Vector<JComponent>();
        v.addElement((JComponent) tmp);
        v.addElement(c);
        keyMap.put(k, v);
      }
    } else {
      System.out.println("Unexpected condition in registerKeyStroke");
      Thread.dumpStack();
    }

    componentKeyStrokeMap.put(new ComponentKeyStrokePair(c, k), topContainer);

    // Check for EmbeddedFrame case, they know how to process accelerators even
    // when focus is not in Java
    if (topContainer instanceof EmbeddedFrame) {
      ((EmbeddedFrame) topContainer).registerAccelerator(k);
    }
  }