001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.util;
028:
029: import java.util.ArrayList;
030: import java.util.StringTokenizer;
031:
032: import javax.swing.text.AttributeSet;
033: import javax.swing.text.BadLocationException;
034: import javax.swing.text.PlainDocument;
035:
036: public class NumericListDocument extends PlainDocument {
037: /**
038: * Default constructor creates a NumericListDocument holding a single zero value.
039: **/
040: public NumericListDocument() {
041: try {
042: insertString(0, "0", null);
043: } catch (BadLocationException e) {
044: }
045: }
046:
047: /**
048: * Insert a string into the document. The string is checked to
049: * insure that it is a comma-separated or space separated list of numbers
050: * @param offset the location in the document where insertion is to occur.
051: * @param s the string to insert -- all characters must be decimal digits.
052: * @param attrs the set of attributes fo the inserted characters.
053: **/
054: public void insertString(int offset, String s, AttributeSet attrs)
055: throws BadLocationException {
056: for (int i = 0, n = s.length(); i < n; i++) {
057: char c = s.charAt(i);
058: if ((c < '0' || c > '9') && c != ',' && c != ' ') {
059: throw new IllegalArgumentException(
060: "not a digit or separator");
061: }
062: }
063: super .insertString(offset, s, attrs);
064: }
065:
066: /**
067: * Replace the current value in the document with a new value.
068: * @param value the new value to insert.
069: **/
070: public void setValue(int value) {
071: int[] v = { value };
072: setValues(v);
073: }
074:
075: public void setValues(int[] values) {
076: try {
077: remove(0, getLength());
078: StringBuffer buf = new StringBuffer();
079: for (int i = 0; i < values.length; i++) {
080: buf.append(", ");
081: buf.append(values[i]);
082: }
083: insertString(0, buf.substring(2), null);
084: } catch (BadLocationException e) {
085: }
086: }
087:
088: /**
089: * Get the current value in the document. Converts the string in
090: * the document to a number.
091: * @return the value in the buffer as an int.
092: **/
093: public int[] getValues() {
094: try {
095: ArrayList strings = new ArrayList();
096: String text = getText(0, getLength());
097: StringTokenizer tokens = new StringTokenizer(text, ", ");
098: while (tokens.hasMoreTokens()) {
099: strings.add(tokens.nextToken());
100: }
101: int[] result = new int[strings.size()];
102: for (int i = 0; i < result.length; i++) {
103: result[i] = Integer.parseInt((String) strings.get(i));
104: }
105: return result;
106: } catch (BadLocationException e) {
107: return new int[0];
108: }
109: }
110: }
|