@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(); } }
/** Parse clicks to cancel the recording if we get a click that's not in the JList (or ESC). */ protected boolean parseClick(AWTEvent event) { if (isFinished()) { return false; } // FIXME add key-based activation/termination? boolean consumed = true; if (combo == null) { combo = getComboBox(event); listener = new ActionListener() { public void actionPerformed(ActionEvent ev) { index = combo.getSelectedIndex(); if (!combo.isPopupVisible()) { combo.removeActionListener(listener); setFinished(true); } } }; combo.addActionListener(listener); setStatus("Waiting for selection"); } else if (event.getID() == KeyEvent.KEY_RELEASED && (((KeyEvent) event).getKeyCode() == KeyEvent.VK_SPACE || ((KeyEvent) event).getKeyCode() == KeyEvent.VK_ENTER)) { index = combo.getSelectedIndex(); setFinished(true); } // Cancel via click somewhere else else if (event.getID() == MouseEvent.MOUSE_PRESSED && !AWT.isOnPopup((Component) event.getSource()) && combo != getComboBox(event)) { setFinished(true); consumed = false; } // Cancel via ESC key else if (event.getID() == KeyEvent.KEY_RELEASED && ((KeyEvent) event).getKeyCode() == KeyEvent.VK_ESCAPE) { setStatus("Selection canceled"); setFinished(true); } else { Log.debug("Event ignored"); } if (list == null && combo.isPopupVisible()) list = tester.findComboList(combo); if (isFinished()) { combo.removeActionListener(listener); listener = null; } return consumed; }
public boolean canClose() { return !myCombo.isPopupVisible(); }