001: /*
002: * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed WITHOUT ANY WARRANTY; and without the
010: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: * See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License
014: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
015: * or write to:
016: *
017: * Free Software Foundation, Inc.,
018: * 59 Temple Place - Suite 330,
019: * Boston, MA USA 02111-1307
020: *
021: *
022: * MaskedPlainDocument.java
023: *
024: * Created on May 7, 2007, 11:46 AM
025: *
026: * To change this template, choose Tools | Template Manager
027: * and open the template in the editor.
028: */
029:
030: package it.businesslogic.ireport.gui.sheet;
031:
032: import javax.swing.text.AttributeSet;
033: import javax.swing.text.BadLocationException;
034: import javax.swing.text.PlainDocument;
035:
036: /**
037: *
038: * @author gtoffoli
039: */
040: public class MaskedPlainDocument extends PlainDocument {
041:
042: public static final int NO_MAX_LENGTH = 0;
043:
044: public static final String COLOR_MASK = "(#?)(([0-9]|[a-f]|[A-F]){0,6})";
045: private String mask = null;
046: private int maxLength = 0;
047:
048: /**
049: * Create a MaskedPlainDocument based on the given regex espression
050: */
051: public MaskedPlainDocument(String mask, int maxLength) {
052: this .mask = mask;
053: this .maxLength = maxLength;
054: }
055:
056: /**
057: * Same as MaskedPlainDocument(String mask, NO_MAX_LENGTH)
058: *
059: */
060: public MaskedPlainDocument(String mask) {
061: this (mask, NO_MAX_LENGTH);
062: }
063:
064: public void insertString(int offset, String str, AttributeSet attr)
065: throws BadLocationException {
066:
067: if (str == null)
068: return;
069: if (maxLength > 0 && offset >= maxLength) {
070: return;
071: }
072: // does the insertion exceed the max length
073: if (maxLength > 0 && str.length() > maxLength) {
074: str = str.substring(0, maxLength);
075: }
076:
077: // Create the final string...
078: try {
079:
080: String currentString = getText(0, offset);
081: currentString += str;
082:
083: //System.out.println("Getting text " + offset + " ==> " + getLength());
084:
085: if (offset < getLength()) {
086: currentString += getText(offset, getLength() - offset);
087: }
088: // remove the last '\n' if any...
089: //if (currentString.endsWith("\n")) currentString = currentString.substring(0, currentString.length()-1);
090: currentString = currentString.trim();
091: //System.out.println(currentString + " " + currentString.matches(mask) + " ==> " + mask);
092:
093: if (currentString.matches(mask)) {
094: str = str.toUpperCase();
095: super .insertString(offset, str, attr);
096: }
097: } catch (Exception ex) {
098: ex.printStackTrace();
099: }
100: }
101:
102: public String getMask() {
103: return mask;
104: }
105:
106: public void setMask(String mask) {
107: this .mask = mask;
108: }
109:
110: public int getMaxLength() {
111: return maxLength;
112: }
113:
114: public void setMaxLength(int maxLength) {
115: this.maxLength = maxLength;
116: }
117:
118: }
|