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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 1999 by Sun Microsystems, Inc.
26  * All rights reserved.
27  *
28  * pmTextField.java
29  * Extension of JTextField which accepts only 8-bit-ASCII.
30  */
31 
32 package com.sun.admin.pm.client;
33 
34 import java.awt.*;
35 import java.awt.event.*;
36 import javax.swing.*;
37 import javax.swing.text.*;
38 
39 public class pmTextField extends JTextField {
pmTextField(int n)40     public pmTextField(int n) {
41         this(null, n);
42     }
43 
pmTextField(String s)44     public pmTextField(String s) {
45         this(s, 0);
46     }
47 
pmTextField(String s, int n)48     public pmTextField(String s, int n) {
49         super(s, n);
50     }
51 
createDefaultModel()52     protected Document createDefaultModel() {
53         return new pmFilterDoc();
54     }
55 
56     /*
57      * This doc implementation will disallow insertion of a
58      * string containing any characters which are non-8-bit-ascii.
59      */
60     private class pmFilterDoc extends PlainDocument {
insertString(int offset, String str, AttributeSet a)61         public void insertString(int offset, String str, AttributeSet a)
62             throws BadLocationException {
63             int i, c;
64             char[] buf = str.toCharArray();
65 
66             for (i = 0; i < buf.length; i++) {
67                 c = (new Character(buf[i])).charValue();
68                 if (c > 0x00ff)
69                     break;
70             }
71             if (i == buf.length)
72                 super.insertString(offset, str, a);
73             else
74                 Toolkit.getDefaultToolkit().beep();
75     }
76     }
77 
main(String args[])78     public static void main(String args[]) {
79         JFrame f = new JFrame();
80         f.getContentPane().add(new pmTextField(20));
81         f.pack();
82         f.setVisible(true);
83         f.repaint();
84     }
85 
86 }
87