Beispiel #1
0
    @Override
    public void keyPressed(KeyEvent evt) {
      if (evt.isConsumed()) return;
      Component comp = getFocusOwner();
      if (evt.getKeyCode() == KeyEvent.VK_ENTER && enterEnabled) {
        while (comp != null) {
          if (comp instanceof JComboBox) {
            JComboBox<?> combo = (JComboBox<?>) comp;
            if (combo.isEditable()) {
              Object selected = combo.getEditor().getItem();
              if (selected != null) combo.setSelectedItem(selected);
            }

            if (combo.isPopupVisible()) {
              evt.consume();
              combo.setPopupVisible(false);
            }
            return;
          }
          // TODO: add other classes that need custom key handling here.
          comp = comp.getParent();
        }
        evt.consume();
        ok();
      } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE || isCloseBufferShortcut(evt)) {
        evt.consume();
        if (comp instanceof JComboBox) {
          JComboBox<?> combo = (JComboBox<?>) comp;
          if (combo.isPopupVisible()) combo.setPopupVisible(false);
          else cancel();
        } else cancel();
      }
    }
Beispiel #2
0
 /**
  * Processes key stroke events such as mnemonics and accelerators.
  *
  * @param evt the key event to be processed
  */
 protected void processKeyEvent(KeyEvent evt) {
   MenuSelectionManager.defaultManager().processKeyEvent(evt);
   if (evt.isConsumed()) {
     return;
   }
   super.processKeyEvent(evt);
 }
  /**
   * When a MenuElement receives an event from a KeyListener, it should never process the event
   * directly. Instead all MenuElements should call this method with the event.
   *
   * @param e a KeyEvent object
   */
  public void processKeyEvent(KeyEvent e) {
    MenuElement[] sel2 = new MenuElement[0];
    sel2 = selection.toArray(sel2);
    int selSize = sel2.length;
    MenuElement[] path;

    if (selSize < 1) {
      return;
    }

    for (int i = selSize - 1; i >= 0; i--) {
      MenuElement elem = sel2[i];
      MenuElement[] subs = elem.getSubElements();
      path = null;

      for (int j = 0; j < subs.length; j++) {
        if (subs[j] == null
            || !subs[j].getComponent().isShowing()
            || !subs[j].getComponent().isEnabled()) {
          continue;
        }

        if (path == null) {
          path = new MenuElement[i + 2];
          System.arraycopy(sel2, 0, path, 0, i + 1);
        }
        path[i + 1] = subs[j];
        subs[j].processKeyEvent(e, path, this);
        if (e.isConsumed()) {
          return;
        }
      }
    }

    // finally dispatch event to the first component in path
    path = new MenuElement[1];
    path[0] = sel2[0];
    path[0].processKeyEvent(e, path, this);
    if (e.isConsumed()) {
      return;
    }
  }
  @Override
  public void keyPressed(KeyEvent evt) {
    multicaster.keyPressed(evt);
    if (!evt.isConsumed()) {
      super.keyPressed(evt);

      // Forward key presses to the handler
      if (dragLocation != null) {
        multicaster.trackStep(anchor, dragLocation, evt.getModifiersEx(), getView());
      }
    }
  }
Beispiel #5
0
  public final boolean dispatch(KeyEvent event) {
    if (event.getID() != KeyEvent.KEY_PRESSED && event.getID() != KeyEvent.KEY_RELEASED) {
      return false;
    }

    if (event.getID() == KeyEvent.KEY_PRESSED) {
      final KeyStroke stroke =
          KeyStroke.getKeyStroke(event.getKeyCode(), event.getModifiers(), false);
      if (proceedKeyEvent(event, stroke)) return false;
    }

    if (event.getID() == KeyEvent.KEY_RELEASED) {
      final KeyStroke stroke =
          KeyStroke.getKeyStroke(event.getKeyCode(), event.getModifiers(), true);
      return proceedKeyEvent(event, stroke);
    }

    myMnemonicsSearch.process(event);
    mySpeedSearch.process(event);

    if (event.isConsumed()) return true;
    process(event);
    return event.isConsumed();
  }
  /**
   * This method is called when the focused component (and none of its ancestors) want the key
   * event. This will look up the keystroke to see if any chidren (or subchildren) of the specified
   * container want a crack at the event. If one of them wants it, then it will "DO-THE-RIGHT-THING"
   */
  public boolean fireKeyboardAction(KeyEvent e, boolean pressed, Container topAncestor) {

    if (e.isConsumed()) {
      System.out.println("Acquired pre-used event!");
      Thread.dumpStack();
    }

    // There may be two keystrokes associated with a low-level key event;
    // in this case a keystroke made of an extended key code has a priority.
    KeyStroke ks;
    KeyStroke ksE = null;

    if (e.getID() == KeyEvent.KEY_TYPED) {
      ks = KeyStroke.getKeyStroke(e.getKeyChar());
    } else {
      if (e.getKeyCode() != e.getExtendedKeyCode()) {
        ksE = KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed);
      }
      ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed);
    }

    Hashtable keyMap = containerMap.get(topAncestor);
    if (keyMap != null) { // this container isn't registered, so bail

      Object tmp = null;
      // extended code has priority
      if (ksE != null) {
        tmp = keyMap.get(ksE);
        if (tmp != null) {
          ks = ksE;
        }
      }
      if (tmp == null) {
        tmp = keyMap.get(ks);
      }

      if (tmp == null) {
        // don't do anything
      } else if (tmp instanceof JComponent) {
        JComponent c = (JComponent) tmp;
        if (c.isShowing() && c.isEnabled()) { // only give it out if enabled and visible
          fireBinding(c, ks, e, pressed);
        }
      } else if (tmp instanceof Vector) { // more than one comp registered for this
        Vector v = (Vector) tmp;
        // There is no well defined order for WHEN_IN_FOCUSED_WINDOW
        // bindings, but we give precedence to those bindings just
        // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW
        // bindings are accessed before those of the JRootPane (they
        // both have a WHEN_IN_FOCUSED_WINDOW binding for enter).
        for (int counter = v.size() - 1; counter >= 0; counter--) {
          JComponent c = (JComponent) v.elementAt(counter);
          // System.out.println("Trying collision: " + c + " vector = "+ v.size());
          if (c.isShowing() && c.isEnabled()) { // don't want to give these out
            fireBinding(c, ks, e, pressed);
            if (e.isConsumed()) return true;
          }
        }
      } else {
        System.out.println("Unexpected condition in fireKeyboardAction " + tmp);
        // This means that tmp wasn't null, a JComponent, or a Vector.  What is it?
        Thread.dumpStack();
      }
    }

    if (e.isConsumed()) {
      return true;
    }
    // if no one else handled it, then give the menus a crack
    // The're handled differently.  The key is to let any JMenuBars
    // process the event
    if (keyMap != null) {
      Vector v = (Vector) keyMap.get(JMenuBar.class);
      if (v != null) {
        Enumeration iter = v.elements();
        while (iter.hasMoreElements()) {
          JMenuBar mb = (JMenuBar) iter.nextElement();
          if (mb.isShowing() && mb.isEnabled()) { // don't want to give these out
            boolean extended = (ksE != null) && !ksE.equals(ks);
            if (extended) {
              fireBinding(mb, ksE, e, pressed);
            }
            if (!extended || !e.isConsumed()) {
              fireBinding(mb, ks, e, pressed);
            }
            if (e.isConsumed()) {
              return true;
            }
          }
        }
      }
    }

    return e.isConsumed();
  }
 @Override
 protected void processKeyEvent(KeyEvent evt) {
   if (evt.getID() == KeyEvent.KEY_PRESSED) {
     ActionContext ac = VFSBrowser.getActionContext();
     int row = parentDirectories.getSelectedIndex();
     switch (evt.getKeyCode()) {
       case KeyEvent.VK_DOWN:
         evt.consume();
         if (row < parentDirectories.getSize().height - 1)
           parentDirectories.setSelectedIndex(++row);
         break;
       case KeyEvent.VK_LEFT:
         if ((evt.getModifiers() & InputEvent.ALT_MASK) > 0) {
           evt.consume();
           browser.previousDirectory();
         } else super.processEvent(evt);
         break;
       case KeyEvent.VK_RIGHT:
         if ((evt.getModifiers() & InputEvent.ALT_MASK) > 0) {
           evt.consume();
           browser.nextDirectory();
         } else super.processEvent(evt);
         break;
       case KeyEvent.VK_TAB:
         evt.consume();
         if ((evt.getModifiers() & InputEvent.SHIFT_MASK) > 0) browser.focusOnDefaultComponent();
         else table.requestFocus();
         break;
       case KeyEvent.VK_UP:
         evt.consume();
         if (row > 0) {
           parentDirectories.setSelectedIndex(--row);
         }
         break;
       case KeyEvent.VK_BACK_SPACE:
         evt.consume();
         EditAction up = ac.getAction("vfs.browser.up");
         ac.invokeAction(evt, up);
         break;
       case KeyEvent.VK_F5:
         evt.consume();
         EditAction reload = ac.getAction("vfs.browser.reload");
         ac.invokeAction(evt, reload);
         break;
       case KeyEvent.VK_ENTER:
         evt.consume();
         if (row != -1) {
           // basically the same handling as in ParentMouseHandler#mouseReleased
           Object obj = parentDirectories.getModel().getElementAt(row);
           if (obj instanceof VFSFile) {
             VFSFile dirEntry = (VFSFile) obj;
             browser.setDirectory(dirEntry.getPath());
             if (browser.getMode() == VFSBrowser.BROWSER) focusOnFileView();
           }
         }
         break;
         /* These actions don't work because they look at the EntryTable for the current selected
         * 	item. We need actions that look at the parentDirectoryList item instead.
         *
         			case KeyEvent.VK_DELETE:
         				evt.consume();
         				ea = ac.getAction("vfs.browser.delete");
         				ac.invokeAction(evt, ea);
         				break;
         			case KeyEvent.CTRL_MASK | KeyEvent.VK_N:
         				evt.consume();
         				ea = ac.getAction("vfs.browser.new-file");
         				ac.invokeAction(evt, ea);
         				break;
         			case KeyEvent.VK_INSERT:
         				evt.consume();
         				ea = ac.getAction("vfs.browser.new-directory");
         				ac.invokeAction(evt, ea);
         				break; */
     }
   } else if (evt.getID() == KeyEvent.KEY_TYPED) {
     if (evt.isControlDown() || evt.isAltDown() || evt.isMetaDown()) {
       evt.consume();
       return;
     }
     switch (evt.getKeyChar()) {
       case '~':
         evt.consume();
         if (browser.getMode() == VFSBrowser.BROWSER)
           browser.setDirectory(System.getProperty("user.home"));
         break;
       case '/':
         evt.consume();
         if (browser.getMode() == VFSBrowser.BROWSER) browser.rootDirectory();
         break;
       case '-':
         evt.consume();
         if (browser.getMode() == VFSBrowser.BROWSER) {
           browser.setDirectory(browser.getView().getBuffer().getDirectory());
         }
         break;
     }
   }
   if (!evt.isConsumed()) super.processKeyEvent(evt);
 }
  /**
   * Need this method to detect when the focus may have chance to leave the focus cycle root which
   * is EmbeddedFrame. Mostly, the code here is copied from
   * DefaultKeyboardFocusManager.processKeyEvent with some minor modifications.
   */
  public boolean dispatchKeyEvent(KeyEvent e) {

    // We can't guarantee that this is called on the same AppContext as EmbeddedFrame
    // belongs to. That's why we can't use public methods to find current focus cycle
    // root. Instead, we access KFM's private field directly.
    if (currentCycleRoot == null) {
      currentCycleRoot =
          (Field)
              AccessController.doPrivileged(
                  new PrivilegedAction() {
                    public Object run() {
                      try {
                        Field unaccessibleRoot =
                            KeyboardFocusManager.class.getDeclaredField("currentFocusCycleRoot");
                        if (unaccessibleRoot != null) {
                          unaccessibleRoot.setAccessible(true);
                        }
                        return unaccessibleRoot;
                      } catch (NoSuchFieldException e1) {
                        assert false;
                      } catch (SecurityException e2) {
                        assert false;
                      }
                      return null;
                    }
                  });
    }

    Container currentRoot = null;
    if (currentCycleRoot != null) {
      try {
        // The field is static, so we can pass null to Field.get() as the argument.
        currentRoot = (Container) currentCycleRoot.get(null);
      } catch (IllegalAccessException e3) {
        // This is impossible: currentCycleRoot would be null if setAccessible failed.
        assert false;
      }
    }

    // if we are not in EmbeddedFrame's cycle, we should not try to leave.
    if (this != currentRoot) {
      return false;
    }

    // KEY_TYPED events cannot be focus traversal keys
    if (e.getID() == KeyEvent.KEY_TYPED) {
      return false;
    }

    if (!getFocusTraversalKeysEnabled() || e.isConsumed()) {
      return false;
    }

    AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e);
    Set toTest;
    Component currentFocused = e.getComponent();

    Component last = getFocusTraversalPolicy().getLastComponent(this);
    toTest = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
    if (toTest.contains(stroke) && (currentFocused == last || last == null)) {
      if (traverseOut(FORWARD)) {
        e.consume();
        return true;
      }
    }

    Component first = getFocusTraversalPolicy().getFirstComponent(this);
    toTest = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
    if (toTest.contains(stroke) && (currentFocused == first || first == null)) {
      if (traverseOut(BACKWARD)) {
        e.consume();
        return true;
      }
    }
    return false;
  }
 /** @see IFigure#handleKeyReleased(KeyEvent) */
 public void handleKeyReleased(KeyEvent event) {
   Iterator iter = eventListeners.getListeners(KeyListener.class);
   while (!event.isConsumed() && iter.hasNext()) ((KeyListener) iter.next()).keyReleased(event);
 }