Java Code Examples for javax.swing.text.JTextComponent#getInputMap()

The following examples show how to use javax.swing.text.JTextComponent#getInputMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
protected TextComponentPopupMenu(JTextComponent textComponent) {
  super();
  add(cutAction);
  add(copyAction);
  add(pasteAction);
  add(deleteAction);
  addSeparator();
  add(undoAction);
  add(redoAction);
  textComponent.getDocument().addUndoableEditListener(manager);
  textComponent.getActionMap().put("undo", undoAction);
  textComponent.getActionMap().put("redo", redoAction);
  InputMap imap = textComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "undo");
  imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "redo");
  // Java 10:
  // imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()), "undo");
  // imap.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()), "redo");
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static void initUndoRedo(JTextComponent tc) {
  String undoAction = "undo";
  String redoAction = "redo";

  UndoManager manager = new UndoManager();
  tc.getDocument().addUndoableEditListener(manager);
  tc.getActionMap().put(undoAction, new UndoAction(manager));
  tc.getActionMap().put(redoAction, new RedoAction(manager));

  int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
  // Java 10: int modifiers = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
  InputMap im = tc.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
  im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, modifiers), undoAction);
  im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, modifiers | InputEvent.SHIFT_DOWN_MASK), redoAction);
  im.put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, modifiers), redoAction);
}
 
Example 3
Source File: AnimationAutoCompletion.java    From 3Dscript with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Installs this auto-completion on a text component. If this
 * {@link AnimationAutoCompletion} is already installed on another text component,
 * it is uninstalled first.
 *
 * @param c The text component.
 * @see #uninstall()
 */
@Override
public void install(JTextComponent c) {

	if (textComponent != null) {
		uninstall();
	}

	this.textComponent = c;
	installTriggerKey(getTriggerKey());

	// Install the function completion key, if there is one.
	// NOTE: We cannot do this if the start char is ' ' (e.g. just a space
	// between the function name and parameters) because it overrides
	// RSTA's special space action. It seems KeyStorke.getKeyStroke(' ')
	// hoses ctrl+space, shift+space, etc., even though I think it
	// shouldn't...
	char start = provider.getParameterListStart();
	if (start != 0 && start != ' ') {
		InputMap im = c.getInputMap();
		ActionMap am = c.getActionMap();
		KeyStroke ks = KeyStroke.getKeyStroke(start);
		oldParenKey = im.get(ks);
		im.put(ks, PARAM_COMPLETE_KEY);
		oldParenAction = am.get(PARAM_COMPLETE_KEY);
		am.put(PARAM_COMPLETE_KEY, new ParameterizedCompletionStartAction(
				start));
	}

	textComponentListener.addTo(this.textComponent);
	// In case textComponent is already in a window...
	textComponentListener.hierarchyChanged(null);

	if (isAutoActivationEnabled()) {
		autoActivationListener.addTo(this.textComponent);
	}

	UIManager.addPropertyChangeListener(lafListener);
	updateUI(); // In case there have been changes since we uninstalled

}