/** Code completion. */ private void complete() { if (selected()) return; // find first character final int caret = editor.pos(), startPos = editor.completionStart(); final String prefix = string(substring(editor.text(), startPos, caret)); if (prefix.isEmpty()) return; // find insertion candidates final TreeMap<String, String> tmp = new TreeMap<>(); for (final Entry<String, String> entry : REPLACE.entrySet()) { final String key = entry.getKey(); if (key.startsWith(prefix)) tmp.put(key, entry.getValue()); } if (tmp.size() == 1) { // insert single candidate complete(tmp.values().iterator().next(), startPos); } else if (!tmp.isEmpty()) { // show popup menu final JPopupMenu pm = new JPopupMenu(); final ActionListener al = new ActionListener() { @Override public void actionPerformed(final ActionEvent ae) { complete(ae.getActionCommand().replaceAll("^.*?\\] ", ""), startPos); } }; for (final Entry<String, String> entry : tmp.entrySet()) { final JMenuItem mi = new JMenuItem("[" + entry.getKey() + "] " + entry.getValue()); pm.add(mi); mi.addActionListener(al); } pm.addSeparator(); final JMenuItem mi = new JMenuItem(Text.INPUT + Text.COLS + prefix); mi.setEnabled(false); pm.add(mi); final int[] cursor = rend.cursor(); pm.show(this, cursor[0], cursor[1]); // highlight first entry final MenuElement[] me = {pm, (JMenuItem) pm.getComponent(0)}; MenuSelectionManager.defaultManager().setSelectedPath(me); } }
/** * @return <code>true</code> if and only if the passed event is already dispatched by the <code> * IdeKeyEventDispatcher</code> and there is no need for any other processing of the event. */ public boolean dispatchKeyEvent(final KeyEvent e) { if (myDisposed) return false; if (e.isConsumed()) { return false; } // http://www.jetbrains.net/jira/browse/IDEADEV-12372 if (e.getKeyCode() == KeyEvent.VK_CONTROL) { if (e.getID() == KeyEvent.KEY_PRESSED) { myLeftCtrlPressed = e.getKeyLocation() == KeyEvent.KEY_LOCATION_LEFT; } else if (e.getID() == KeyEvent.KEY_RELEASED) { myLeftCtrlPressed = false; } } else if (e.getKeyCode() == KeyEvent.VK_ALT) { if (e.getID() == KeyEvent.KEY_PRESSED) { myRightAltPressed = e.getKeyLocation() == KeyEvent.KEY_LOCATION_RIGHT; } else if (e.getID() == KeyEvent.KEY_RELEASED) { myRightAltPressed = false; } } KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); Component focusOwner = focusManager.getFocusOwner(); // shortcuts should not work in shortcut setup fields if (focusOwner instanceof ShortcutTextField) { return false; } if (focusOwner instanceof JTextComponent && ((JTextComponent) focusOwner).isEditable()) { if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED && e.getKeyChar() != KeyEvent.VK_ESCAPE) { MacUIUtil.hideCursor(); } } MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager(); MenuElement[] selectedPath = menuSelectionManager.getSelectedPath(); if (selectedPath.length > 0) { if (!(selectedPath[0] instanceof ComboPopup)) { // The following couple of lines of code is a PATCH!!! // It is needed to ignore ENTER KEY_TYPED events which sometimes can reach editor when an // action // is invoked from main menu via Enter key. setState(KeyState.STATE_PROCESSED); setPressedWasProcessed(true); return false; } } // Keymap shortcuts (i.e. not local shortcuts) should work only in: // - main frame // - floating focusedWindow // - when there's an editor in contexts Window focusedWindow = focusManager.getFocusedWindow(); boolean isModalContext = focusedWindow != null && isModalContext(focusedWindow); final DataManager dataManager = DataManager.getInstance(); if (dataManager == null) return false; DataContext dataContext = dataManager.getDataContext(); myContext.setDataContext(dataContext); myContext.setFocusOwner(focusOwner); myContext.setModalContext(isModalContext); myContext.setInputEvent(e); try { if (getState() == KeyState.STATE_INIT) { return inInitState(); } else if (getState() == KeyState.STATE_PROCESSED) { return inProcessedState(); } else if (getState() == KeyState.STATE_WAIT_FOR_SECOND_KEYSTROKE) { return inWaitForSecondStrokeState(); } else if (getState() == KeyState.STATE_SECOND_STROKE_IN_PROGRESS) { return inSecondStrokeInProgressState(); } else if (getState() == KeyState.STATE_KEY_GESTURE_PROCESSOR) { return myKeyGestureProcessor.process(); } else { throw new IllegalStateException("state = " + getState()); } } finally { myContext.clear(); } }