001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.soa.ui.form;
020:
021: import javax.swing.text.AttributeSet;
022: import javax.swing.text.BadLocationException;
023: import javax.swing.text.PlainDocument;
024:
025: /**
026: *
027: * @author nk160297
028: */
029: public class RangeIntegerDocument extends PlainDocument {
030:
031: int myMinValue;
032: int myMaxValue;
033:
034: public RangeIntegerDocument(int minValue, int maxValue) {
035: super ();
036: myMinValue = minValue;
037: myMaxValue = maxValue;
038: }
039:
040: public void replace(int offset, int length, String text,
041: AttributeSet attrs) throws BadLocationException {
042: //
043: String fullText = getText(0, getLength());
044: StringBuffer sb = new StringBuffer(Math.max(fullText.length(),
045: 10));
046: sb.append(fullText.substring(0, offset));
047: sb.append(text);
048: sb.append(fullText.substring(offset + length));
049: //
050: if (sb.length() == 0) {
051: super .replace(offset, length, text, attrs);
052: } else {
053: try {
054: int newValue = Integer.parseInt(sb.toString());
055: //
056: if (newValue >= myMinValue && newValue <= myMaxValue) {
057: super .replace(offset, length, text, attrs);
058: }
059: } catch (NumberFormatException ex) {
060: // do nothing
061: }
062: }
063: }
064:
065: public void remove(int offset, int length)
066: throws BadLocationException {
067: //
068: String fullText = getText(0, getLength());
069: StringBuffer sb = new StringBuffer(fullText.length());
070: sb.append(fullText.substring(0, offset));
071: sb.append(fullText.substring(offset + length));
072: //
073: if (sb.length() == 0) {
074: super .remove(offset, length);
075: } else {
076: try {
077: int newValue = Integer.parseInt(sb.toString());
078: //
079: if (newValue >= myMinValue && newValue <= myMaxValue) {
080: super .remove(offset, length);
081: }
082: } catch (NumberFormatException ex) {
083: // do nothing
084: }
085: }
086: }
087:
088: public void insertString(int offset, String str, AttributeSet a)
089: throws BadLocationException {
090: //
091: String fullText = getText(0, getLength());
092: StringBuffer sb = new StringBuffer(fullText.length()
093: + str.length());
094: sb.append(fullText.substring(0, offset));
095: sb.append(str);
096: sb.append(fullText.substring(offset));
097: //
098: if (sb.length() == 0) {
099: super .insertString(offset, str, a);
100: } else {
101: try {
102: int newValue = Integer.parseInt(sb.toString());
103: //
104: if (newValue >= myMinValue && newValue <= myMaxValue) {
105: super .insertString(offset, str, a);
106: }
107: } catch (NumberFormatException ex) {
108: // do nothing
109: }
110: }
111: }
112:
113: }
|