Java JTextComponent Key addKeyBindings(final JTextComponent comp)

Here you can find the source of addKeyBindings(final JTextComponent comp)

Description

_more_

License

Open Source License

Parameter

Parameter Description
comp _more_

Declaration

public static void addKeyBindings(final JTextComponent comp) 

Method Source Code

//package com.java2s;
/*// w  ww. j  a va 2s.  c  o  m
 * Copyright 1997-2016 Unidata Program Center/University Corporation for
 * Atmospheric Research, P.O. Box 3000, Boulder, CO 80307,
 * support@unidata.ucar.edu.
 * 
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JTextArea;

import javax.swing.text.JTextComponent;

public class Main {
    /**
     * _more_
     *
     * @param comp _more_
     */
    public static void addKeyBindings(final JTextComponent comp) {
        //TODO Make this into a KeyMap
        comp.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent e) {
                if (!e.isControlDown()) {
                    return;
                }
                int pos = comp.getCaretPosition();

                if (e.getKeyCode() == e.VK_B) {
                    if (comp.getCaretPosition() > 0) {
                        comp.setCaretPosition(pos - 1);
                    }
                    return;
                }
                if (e.getKeyCode() == e.VK_F) {
                    if (comp.getCaretPosition() < comp.getText().length()) {
                        comp.setCaretPosition(pos + 1);
                    }
                    return;
                }
                if (e.getKeyCode() == e.VK_E) {
                    String t = comp.getText();
                    int endPos = t.indexOf("\n", pos);
                    if (endPos >= 0) {
                        comp.setCaretPosition(endPos);
                    } else {
                        comp.setCaretPosition(t.length());
                    }
                }

                if ((e.getKeyCode() == e.VK_O) && (comp instanceof JTextArea)) {
                    String t = comp.getText();
                    t = t.substring(0, pos) + "\n" + t.substring(pos);
                    comp.setText(t);
                    comp.setCaretPosition(pos);
                }

                if (false && (comp instanceof JTextArea) && (e.getKeyCode() == e.VK_P)) {
                    String t = comp.getText();
                    char c;
                    int cnt = 0;
                    while ((--pos >= 0) && (c = t.charAt(pos)) != '\n') {
                        cnt++;
                    }

                    comp.setCaretPosition(pos - cnt);
                }

                if (e.getKeyCode() == e.VK_K) {
                    String t = comp.getText();
                    if (pos >= t.length()) {
                        return;
                    }
                    int endPos = t.indexOf("\n", pos);
                    if (endPos == pos) {
                        t = t.substring(0, pos) + t.substring(endPos + 1);
                    } else if (endPos > pos) {
                        t = t.substring(0, pos) + "\n" + t.substring(endPos + 1);
                    } else {
                        t = t.substring(0, pos);
                    }
                    comp.setText(t);
                    comp.setCaretPosition(pos);
                }

                if (e.getKeyCode() == e.VK_D) {
                    String t = comp.getText();
                    if (pos >= t.length()) {
                        return;
                    }
                    if (pos > 0) {
                        t = t.substring(0, pos) + t.substring(pos + 1);
                    } else {
                        t = t.substring(pos + 1);
                    }
                    comp.setText(t);
                    if ((pos >= 0) && (pos < t.length())) {
                        comp.setCaretPosition(pos);
                    }
                }
            }
        });
    }
}

Related

  1. sendKeyPress(JTextComponent target, int v_key, int modifiers)