private void removeTabIfPossilbe(KeyEvent event, JTextComponent component) {
        try {
            Document doc = component.getDocument();
            int caretPostion = component.getCaretPosition();
            int lineStartIndex = doc.getText(0, caretPostion).lastIndexOf('\n') + 1;
            lineStartIndex = lineStartIndex < 0 ? 0 : lineStartIndex;
            lineStartIndex = lineStartIndex >= doc.getLength() ? doc.getLength() - 1 : lineStartIndex;
            int scanEndIndex = lineStartIndex + 4 <= doc.getLength() ? lineStartIndex + 4 : doc.getLength();

            for (int i = 0; i < 4 && i + lineStartIndex < scanEndIndex; i++) {
                if (doc.getText(lineStartIndex, 1).matches(" ")) {
                    doc.remove(lineStartIndex, 1);
                } else if (doc.getText(lineStartIndex, 1).matches("\t")) {
                    doc.remove(lineStartIndex, 1);
                    break;
                } else {
                    break;
                }
            }

            event.consume();
        } catch (BadLocationException ex) {
            System.out.println("Could not insert a tab: " + ex.getMessage());
        }
    }