public MainPanel() { super(new BorderLayout()); InputMap im = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); KeyStroke stab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK); KeyStroke senter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_DOWN_MASK); im.put(tab, im.get(enter)); im.put(stab, im.get(senter)); final Color orgColor = table.getSelectionBackground(); final Color tflColor = this.getBackground(); table.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) { table.setSelectionForeground(Color.WHITE); table.setSelectionBackground(orgColor); } @Override public void focusLost(FocusEvent e) { table.setSelectionForeground(Color.BLACK); table.setSelectionBackground(tflColor); } }); table.setComponentPopupMenu(new TablePopupMenu()); add(new JScrollPane(table)); setPreferredSize(new Dimension(320, 240)); }
public void loadBindingMap(InputMap im, ActionMap am) { results.append("Bound Actions\n"); String unboundActions = ""; String unboundInputKeys = ""; Hashtable mi = buildReverseMap(im); Object[] k = am.allKeys(); if (k != null) { for (int i = 0; i < k.length; i++) { if (mi.containsKey(k[i])) { results.append(" " + getActionName(k[i])); results.append(";" + mi.get(k[i]) + "\n"); } else { unboundActions += (" " + getActionName(k[i]) + "\n"); } } results.append("\nUnbound Actions\n\n"); results.append(unboundActions); } results.append("\nUnbound InputMap Entries\n"); k = im.allKeys(); if (k != null) { for (int i = 0; i < k.length; i++) { KeyStroke key = (KeyStroke) k[i]; Object actionKey = im.get(key); if (am.get(actionKey) == null) { results.append(" " + im.get((KeyStroke) k[i]) + ": " + k[i] + "\n"); } } } }
private boolean proceedKeyEvent(KeyEvent event, KeyStroke stroke) { if (myInputMap.get(stroke) != null) { final Action action = myActionMap.get(myInputMap.get(stroke)); if (action != null && action.isEnabled()) { action.actionPerformed( new ActionEvent( getContent(), event.getID(), "", event.getWhen(), event.getModifiers())); return true; } } return false; }
/* * Aqui eu sobrescrevo o comportamento do ENTER na tabela, por default ele * vai para a proxima linha, mas neste caso quero que pegue o valor da atual * para exibir o dados. */ public void alteraComportamentEnterTabela() { tabela.setSelectionMode(0); // somente uma linha pode ser selecionada InputMap im = tabela.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); im.put(enter, im.get(KeyStroke.getKeyStroke(KeyEvent.VK_GREATER, 0))); Action enterAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JTable tabela = (JTable) e.getSource(); } }; tabela.getActionMap().put(im.get(enter), enterAction); }
// --------> // original code: // ftp://ftp.oreilly.de/pub/examples/english_examples/jswing2/code/goodies/Mapper.java // modified by terai // private Hashtable<Object, ArrayList<KeyStroke>> buildReverseMap(InputMap im) { // Hashtable<Object, ArrayList<KeyStroke>> h = new Hashtable<>(); // if (Objects.isNull(im.allKeys())) { // return h; // } // for (KeyStroke ks: im.allKeys()) { // Object name = im.get(ks); // if (h.containsKey(name)) { // h.get(name).add(ks); // } else { // ArrayList<KeyStroke> keylist = new ArrayList<>(); // keylist.add(ks); // h.put(name, keylist); // } // } // return h; // } private void loadBindingMap(Integer focusType, InputMap im, ActionMap am) { if (Objects.isNull(im.allKeys())) { return; } ActionMap tmpAm = new ActionMap(); for (Object actionMapKey : am.allKeys()) { tmpAm.put(actionMapKey, am.get(actionMapKey)); } for (KeyStroke ks : im.allKeys()) { Object actionMapKey = im.get(ks); Action action = am.get(actionMapKey); if (Objects.isNull(action)) { model.addBinding(new Binding(focusType, "____" + actionMapKey.toString(), ks.toString())); } else { model.addBinding(new Binding(focusType, actionMapKey.toString(), ks.toString())); } tmpAm.remove(actionMapKey); } if (Objects.isNull(tmpAm.allKeys())) { return; } for (Object actionMapKey : tmpAm.allKeys()) { model.addBinding(new Binding(focusType, actionMapKey.toString(), "")); } }
public void alteraComportamentoEnterTabela() { /* * Aqui eu sobrescrevo o comportamento do ENTER na tabela, por default * ele vai para a proxima linha, mas neste caso quero que pegue o valor * da atual para exibir o dados. */ tabela.setSelectionMode(0); // somente uma linha pode ser selecionada InputMap im = tabela.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); im.put(enter, im.get(KeyStroke.getKeyStroke(KeyEvent.VK_GREATER, 0))); Action enterAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JTable tabela = (JTable) e.getSource(); } }; tabela.getActionMap().put(im.get(enter), enterAction); }
public void loadInputMap(InputMap im, String indent) { KeyStroke[] k = im.allKeys(); if (k == null) { results.append(indent + "No InputMap defined\n"); } else { results.append(indent + "\nInputMap (" + k.length + " local keys)\n"); } if (k != null) { for (int i = 0; i < k.length; i++) { results.append(indent + " Key: " + k[i] + ", binding: " + im.get(k[i]) + "\n"); } } }
/** * Registers the keystroke of the given action as "command" of the given component. * * <p>This code is based on the Sulky-tools, found at <http://github.com/huxi/sulky>. * * @param aComponent the component that should react on the keystroke, cannot be <code>null</code> * ; * @param aAction the action of the keystroke, cannot be <code>null</code>; * @param aCommandName the name of the command to register the keystore under. */ public static void registerKeystroke( final JComponent aComponent, final Action aAction, final String aCommandName) { final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY); if (keyStroke == null) { return; } InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = aComponent.getActionMap(); inputMap.put(keyStroke, aCommandName); actionMap.put(aCommandName, aAction); inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED); Object value = inputMap.get(keyStroke); if (value != null) { inputMap.put(keyStroke, aCommandName); } inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); value = inputMap.get(keyStroke); if (value != null) { inputMap.put(keyStroke, aCommandName); } }
private Hashtable buildReverseMap(InputMap im) { KeyStroke k[] = im.allKeys(); Hashtable h = new Hashtable(); if (k != null) { for (int i = 0; i < k.length; i++) { Object nk = im.get(k[i]); Object cv = h.get(nk); if (h.containsKey(nk)) { ((Vector) cv).add(k[i]); } else { Vector v = new Vector(); v.add(k[i]); h.put(nk, v); } } } return h; }
// {{{ _preprocessKeyEvent() method private KeyEvent _preprocessKeyEvent(KeyEvent evt) { if (view.isClosed()) return null; Component focusOwner = view.getFocusOwner(); if (focusOwner instanceof JComponent) { JComponent comp = (JComponent) focusOwner; InputMap map = comp.getInputMap(); ActionMap am = comp.getActionMap(); if (map != null && am != null && comp.isEnabled()) { KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(evt); Object binding = map.get(keyStroke); if (binding != null && am.get(binding) != null) { return null; } } } if (focusOwner instanceof JTextComponent) { // fix for the bug where key events in JTextComponents // inside views are also handled by the input handler if (evt.getID() == KeyEvent.KEY_PRESSED) { switch (evt.getKeyCode()) { case KeyEvent.VK_ENTER: case KeyEvent.VK_TAB: case KeyEvent.VK_BACK_SPACE: case KeyEvent.VK_SPACE: return null; } } } if (evt.isConsumed()) return null; if (Debug.DUMP_KEY_EVENTS) { Log.log(Log.DEBUG, this, "Key event (preprocessing) : " + AbstractInputHandler.toString(evt)); } return KeyEventWorkaround.processKeyEvent(evt); } // }}}
@SuppressWarnings("HardCodedStringLiteral") private void processListSelection(final KeyEvent e) { if (togglePopup(e)) return; if (!isPopupShowing()) return; final InputMap map = myPathTextField.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); if (map != null) { final Object object = map.get(KeyStroke.getKeyStrokeForEvent(e)); if (object instanceof Action) { final Action action = (Action) object; if (action.isEnabled()) { action.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "action")); e.consume(); return; } } } final Object action = getAction(e, myList); if ("selectNextRow".equals(action)) { if (ensureSelectionExists()) { ListScrollingUtil.moveDown(myList, e.getModifiersEx()); } } else if ("selectPreviousRow".equals(action)) { ListScrollingUtil.moveUp(myList, e.getModifiersEx()); } else if ("scrollDown".equals(action)) { ListScrollingUtil.movePageDown(myList); } else if ("scrollUp".equals(action)) { ListScrollingUtil.movePageUp(myList); } else if (getSelectedFileFromCompletionPopup() != null && (e.getKeyCode() == KeyEvent.VK_ENTER || e.getKeyCode() == KeyEvent.VK_TAB) && e.getModifiers() == 0) { hideCurrentPopup(); e.consume(); processChosenFromCompletion(e.getKeyCode() == KeyEvent.VK_TAB); } }
public FileTextFieldImpl( final JTextField field, Finder finder, LookupFilter filter, Map<String, String> macroMap, final Disposable parent) { myPathTextField = field; myMacroMap = new TreeMap<String, String>(); myMacroMap.putAll(macroMap); final InputMap listMap = (InputMap) UIManager.getDefaults().get("List.focusInputMap"); final KeyStroke[] listKeys = listMap.keys(); myDisabledTextActions = new HashSet<Action>(); for (KeyStroke eachListStroke : listKeys) { final String listActionID = (String) listMap.get(eachListStroke); if ("selectNextRow".equals(listActionID) || "selectPreviousRow".equals(listActionID)) { final Object textActionID = field.getInputMap().get(eachListStroke); if (textActionID != null) { final Action textAction = field.getActionMap().get(textActionID); if (textAction != null) { myDisabledTextActions.add(textAction); } } } } final FileTextFieldImpl assigned = (FileTextFieldImpl) myPathTextField.getClientProperty(KEY); if (assigned != null) { assigned.myFinder = finder; assigned.myFilter = filter; return; } myPathTextField.putClientProperty(KEY, this); final boolean headless = ApplicationManager.getApplication().isUnitTestMode(); myUiUpdater = new MergingUpdateQueue("FileTextField.UiUpdater", 200, false, myPathTextField); if (!headless) { new UiNotifyConnector(myPathTextField, myUiUpdater); } myFinder = finder; myFilter = filter; myFileSpitRegExp = myFinder.getSeparator().replaceAll("\\\\", "\\\\\\\\"); myPathTextField .getDocument() .addDocumentListener( new DocumentListener() { public void insertUpdate(final DocumentEvent e) { processTextChanged(); } public void removeUpdate(final DocumentEvent e) { processTextChanged(); } public void changedUpdate(final DocumentEvent e) { processTextChanged(); } }); myPathTextField.addKeyListener( new KeyAdapter() { public void keyPressed(final KeyEvent e) { processListSelection(e); } }); myPathTextField.addFocusListener( new FocusAdapter() { public void focusLost(final FocusEvent e) { closePopup(); } }); myCancelAction = new CancelAction(); new LazyUiDisposable<FileTextFieldImpl>(parent, field, this) { protected void initialize( @NotNull Disposable parent, @NotNull FileTextFieldImpl child, @Nullable Project project) { Disposer.register(child, myUiUpdater); } }; }
protected String getActionForKeyStroke(final KeyStroke keyStroke) { return (String) myInputMap.get(keyStroke); }