/** * DataCleaner (community edition) * Copyright (C) 2014 Neopost - Customer Information Management * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program 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 distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.datacleaner.util; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.text.PlainDocument; /** * A {@link Document} used for holding a single character. */ public class SingleCharacterDocument extends PlainDocument { private static final long serialVersionUID = 1L; @Override public void insertString(final int offs, final String str, final AttributeSet a) throws BadLocationException { if (str == null) { return; } final String newText = getText(0, getLength()) + str; final int length = newText.length(); if (length == 1) { super.insertString(offs, str, a); } else if (length == 2) { if ('\\' == newText.charAt(0)) { // 2 characters are only allowed if the first letter is a // backslash (escape char) super.insertString(offs, str, a); } } } }