1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 package com.sun.dhcpmgr.client;
29 
30 import java.awt.*;
31 import java.awt.event.*;
32 import javax.swing.*;
33 import javax.swing.text.*;
34 import com.sun.dhcpmgr.data.Option;
35 
36 /**
37  * A text field which enforces the syntax rules for an option name.  These
38  * are all the rules for DhcptabNameField, plus a limit on the length.
39  */
40 public class OptionNameField extends DhcptabNameField {
41 
42     /**
43      * Constructs a field initialized to the provided text.  Defaults to
44      * 10 characters wide.
45      * @param text the text to display initially
46      */
OptionNameField(String text)47     public OptionNameField(String text) {
48 	this(text, 10);
49     }
50 
51     /**
52      * Constructs a field initialized to the provided text with the requested
53      * size.
54      * @param text the text to display initially
55      * @param length the length in characters the field should size itself to
56      */
OptionNameField(String text, int length)57     public OptionNameField(String text, int length) {
58 	super(text, length);
59     }
60 
createDefaultModel()61     protected Document createDefaultModel() {
62 	return new OptionNameDocument();
63     }
64 
65     /*
66      * This is the recommended way to validate input, as opposed to trapping
67      * KeyEvents because this will actually catch paste operations as well.
68      */
69     class OptionNameDocument extends DhcptabNameDocument {
insertString(int offs, String str, AttributeSet a)70 	public void insertString(int offs, String str, AttributeSet a)
71 		throws BadLocationException {
72 	    if (str != null) {
73 		if ((getLength() + str.length()) > Option.MAX_NAME_SIZE) {
74 		    throw new BadLocationException("", offs);
75 		}
76 	    }
77 	    super.insertString(offs, str, a);
78 	}
79     }
80 }
81