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
35 /**
36 * A text field which enforces the syntax rules for the name field in the
37 * dhcptab. At present, all characters must be printable ASCII, but not the
38 * comment introduction character '#'.
39 */
40 public class DhcptabNameField extends JTextField {
41
42 /**
43 * Constructs a field initialized to the provided text. Defaults to
44 * 20 characters wide.
45 * @param text the text to display initially
46 */
47 public DhcptabNameField(String text) {
48 this(text, 20);
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 */
57 public DhcptabNameField(String text, int length) {
58 super(text, length);
59 }
60
61 protected Document createDefaultModel() {
62 return new DhcptabNameDocument();
63 }
64 }
65
66 /**
67 * This is the recommended way to validate input, as opposed to trapping
68 * KeyEvents because this will actually catch paste operations as well.
69 */
70 class DhcptabNameDocument extends PlainDocument {
71 public void insertString(int offs, String str, AttributeSet a)
72 throws BadLocationException {
73 if (str != null) {
74 char [] chars = str.toCharArray();
75 for (int i = 0; i < chars.length; ++i) {
76 // Must be a printable ASCII char, but not the comment character
77 if (chars[i] < ' ' || chars[i] > '~' || chars[i] == '#') {
78 throw new BadLocationException("", offs);
79 }
80 }
81 }
82 super.insertString(offs, str, a);
83 }
84 }