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 import java.net.InetAddress;
35
36 import com.sun.dhcpmgr.data.IPAddress;
37 import com.sun.dhcpmgr.data.ValidationException;
38
39 /**
40 * A text field which limits input to only those characters which can legally
41 * appear in an IP address, i.e. the digits and '.'.
42 */
43 public class IPAddressField extends JTextField {
44
45 /**
46 * Constructs an empty field.
47 */
48 public IPAddressField() {
49 this("");
50 }
51
52 /**
53 * Constructs a field initialized to the provided text.
54 * @param text the text to display initially
55 */
56 public IPAddressField(String text) {
57 super(text, 15);
58 }
59
60 /**
61 * Sets the value to a provided IP address.
62 * @param addr an <code>InetAddress</code> to display
63 */
64 public void setValue(InetAddress addr) {
65 if (addr == null) {
66 setText("");
67 } else {
68 setText(addr.getHostAddress());
69 }
70 }
71
72 /**
73 * Sets the value to the provided IP address. This is our special
74 * <code>IPAddress</code> class.
75 * @param addr a <code>IPAddress</code> to display
76 */
77 public void setValue(IPAddress addr) {
78 if (addr == null) {
79 setText("");
80 } else {
81 setText(addr.getHostAddress());
82 }
83 }
84
85 /**
86 * Return the current value as an <code>IPAddress</code>.
87 * @return the current value as an <code>IPAddress</code>, or null if the
88 * current text is not a valid IP address.
89 */
90 public IPAddress getValue() {
91 IPAddress a = null;
92 try {
93 a = new IPAddress(getText());
94 } catch (ValidationException e) {
95 // Do nothing
96 }
97 return a;
98 }
99
100 protected Document createDefaultModel() {
101 return new IPAddressDocument();
102 }
103
104 /*
105 * This is the recommended way to validate/filter input, as opposed to
106 * trapping KeyEvents because this will catch paste operations, too.
107 */
108 static class IPAddressDocument extends PlainDocument {
109 public void insertString(int offs, String str, AttributeSet a)
110 throws BadLocationException {
111 if (str != null) {
112 char [] chars = str.toCharArray();
113 if ((getLength() + chars.length) > 15) {
114 // IP addresses are limited to 15 characters, period.
115 throw new BadLocationException("", offs);
116 }
117 for (int i = 0; i < chars.length; ++i) {
118 if (!Character.isDigit(chars[i]) && chars[i] != '.') {
119 throw new BadLocationException("", offs);
120 }
121 }
122 }
123 super.insertString(offs, str, a);
124 }
125 }
126 }