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) 1998-1999 by Sun Microsystems, Inc.
  26  * All rights reserved.
  27  */
  28 package com.sun.dhcpmgr.ui;
  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 limits input to only those characters which can legally 
  37  * appear in a hostname as specified in RFC1123, i.e. the letters, digits
  38  * and '-'.
  39  */
  40 public class HostnameField extends JTextField {
  41 
  42     /**
  43      * Constructs an empty field, 20 characters wide.
  44      */
  45     public HostnameField() {
  46         this("");
  47     }
  48     
  49     /**
  50      * Constructs a field initialized to the provided text.  Defaults to 20
  51      * characters wide.
  52      * @param text the text to display initially
  53      */
  54     public HostnameField(String text) {
  55         this(text, 20);
  56     }
  57     
  58     /**
  59      * Constructs a field initialized to the provided text with the requested
  60      * size.
  61      * @param text the text to display initially
  62      * @param length the length in characters the field should size itself to
  63      */
  64     public HostnameField(String text, int length) {
  65         super(text, length);
  66     }
  67     
  68     protected Document createDefaultModel() {
  69         return new HostnameDocument();
  70     }
  71     
  72     /* 
  73      * This is the recommended way to validate/filter input, as opposed to
  74      * trapping KeyEvents because this will catch paste operations, too.
  75      */
  76     static class HostnameDocument extends PlainDocument {
  77         public void insertString(int offs, String str, AttributeSet a)
  78                 throws BadLocationException {
  79             if (str != null) {
  80                 char [] chars = str.toCharArray();
  81                 if ((chars.length != 0) && (getLength() == 0)) {
  82                     // First character in field must be a letter or digit
  83                     if (!Character.isLetterOrDigit(chars[0])) {
  84                         throw new BadLocationException("", offs);
  85                     }
  86                 }
  87                 // Now validate that everything we're inserting is legal
  88                 for (int i = 0; i < chars.length; ++i) {
  89                     if (!Character.isLetterOrDigit(chars[i])
  90                             && chars[i] != '-') {
  91                         throw new BadLocationException("", offs);
  92                     }
  93                 }
  94             }
  95             super.insertString(offs, str, a);
  96         }
  97     }
  98 }