/** * Set up key bindings and focus listener for the FieldEditor. * * @param component */ public void setupJTextComponent(final JComponent component, final AutoCompleteListener acl) { // Here we add focus listeners to the component. The funny code is because we need // to guarantee that the AutoCompleteListener - if used - is called before fieldListener // on a focus lost event. The AutoCompleteListener is responsible for removing any // current suggestion when focus is lost, and this must be done before fieldListener // stores the current edit. Swing doesn't guarantee the order of execution of event // listeners, so we handle this by only adding the AutoCompleteListener and telling // it to call fieldListener afterwards. If no AutoCompleteListener is used, we // add the fieldListener normally. if (acl != null) { component.addKeyListener(acl); component.addFocusListener(acl); acl.setNextFocusListener(fieldListener); } else component.addFocusListener(fieldListener); InputMap im = component.getInputMap(JComponent.WHEN_FOCUSED); ActionMap am = component.getActionMap(); im.put(Globals.prefs.getKey("Entry editor, previous entry"), "prev"); am.put("prev", parent.prevEntryAction); im.put(Globals.prefs.getKey("Entry editor, next entry"), "next"); am.put("next", parent.nextEntryAction); im.put(Globals.prefs.getKey("Entry editor, store field"), "store"); am.put("store", parent.storeFieldAction); im.put(Globals.prefs.getKey("Entry editor, next panel"), "right"); im.put(Globals.prefs.getKey("Entry editor, next panel 2"), "right"); am.put("left", parent.switchLeftAction); im.put(Globals.prefs.getKey("Entry editor, previous panel"), "left"); im.put(Globals.prefs.getKey("Entry editor, previous panel 2"), "left"); am.put("right", parent.switchRightAction); im.put(Globals.prefs.getKey("Help"), "help"); am.put("help", parent.helpAction); im.put(Globals.prefs.getKey("Save database"), "save"); am.put("save", parent.saveDatabaseAction); im.put(Globals.prefs.getKey("Next tab"), "nexttab"); am.put("nexttab", parent.frame.nextTab); im.put(Globals.prefs.getKey("Previous tab"), "prevtab"); am.put("prevtab", parent.frame.prevTab); try { HashSet<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>( component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); keys.clear(); keys.add(AWTKeyStroke.getAWTKeyStroke("pressed TAB")); component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys); keys = new HashSet<AWTKeyStroke>( component.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)); keys.clear(); keys.add(KeyStroke.getKeyStroke("shift pressed TAB")); component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keys); } catch (Throwable t) { System.err.println(t); } }
public boolean postProcessKeyEvent(KeyEvent e) { // Processing events only if we are in the focused window but // we are not focus owner since otherwise we will get // duplicate shortcut events in the client - one is from // activate_accelerator, another from forwarded event // FIXME: This is probably an incompatibility, protocol // doesn't say anything about disable accelerators when client // is focused. XWindowPeer parent = getToplevelXWindow(); if (parent == null || !((Window) parent.getTarget()).isFocused() || target.isFocusOwner()) { return false; } boolean result = false; if (xembedLog.isLoggable(PlatformLogger.FINER)) xembedLog.finer("Post-processing event " + e); // Process ACCELERATORS AWTKeyStroke stroke = AWTKeyStroke.getAWTKeyStrokeForEvent(e); long accel_id = 0; boolean exists = false; synchronized (ACCEL_LOCK) { exists = accel_lookup.containsKey(stroke); if (exists) { accel_id = accel_lookup.get(stroke).longValue(); } } if (exists) { if (xembedLog.isLoggable(PlatformLogger.FINE)) xembedLog.fine("Activating accelerator " + accel_id); xembed.sendMessage( xembed.handle, XEMBED_ACTIVATE_ACCELERATOR, accel_id, 0, 0); // FIXME: How about overloaded? result = true; } // Process Grabs, unofficial GTK feature exists = false; GrabbedKey key = new GrabbedKey(e); synchronized (GRAB_LOCK) { exists = grabbed_keys.contains(key); } if (exists) { if (xembedLog.isLoggable(PlatformLogger.FINE)) xembedLog.fine("Forwarding grabbed key " + e); forwardKeyEvent(e); result = true; } return result; }
public void pularCampoComEnter() { // Colocando enter para pular de campo HashSet backup = new HashSet(this.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); HashSet conj = (HashSet) backup.clone(); conj.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_ENTER, 0)); this.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, conj); // nao muda de foco nmo botao enter ao pressionar enter jBPesquisar.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, backup); jBMostrarTodos.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, backup); }
/** * 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; }