001: // IPTemplateAttributeEditor.java
002: // $Id: IPTextField.java,v 1.7 2000/08/16 21:37:56 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1997.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.widgets;
007:
008: import java.awt.AWTEventMulticaster;
009: import java.awt.Component;
010: import java.awt.Container;
011: import java.awt.Dimension;
012: import java.awt.GridLayout;
013: import java.awt.Panel;
014: import java.awt.TextComponent;
015: import java.awt.TextField;
016:
017: import java.awt.event.ActionEvent;
018: import java.awt.event.ActionListener;
019: import java.awt.event.ComponentEvent;
020: import java.awt.event.InputEvent;
021: import java.awt.event.KeyAdapter;
022: import java.awt.event.KeyEvent;
023: import java.awt.event.TextEvent;
024: import java.awt.event.TextListener;
025:
026: import java.util.EventObject;
027: import java.util.StringTokenizer;
028:
029: /**
030: * IPTextField :
031: * @author Benoit Mahe <bmahe@sophia.inria.fr>
032: */
033:
034: public class IPTextField extends Panel implements TextEditable {
035:
036: class IntegerListener extends KeyAdapter {
037:
038: IPTextField comp = null;
039:
040: // filter the non-numeric char
041: public void keyPressed(KeyEvent ke) {
042: if (ke.getKeyCode() == KeyEvent.VK_DELETE
043: || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE
044: || ke.getKeyCode() == KeyEvent.VK_UP
045: || ke.getKeyCode() == KeyEvent.VK_DOWN
046: || ke.getKeyCode() == KeyEvent.VK_LEFT
047: || ke.getKeyCode() == KeyEvent.VK_RIGHT)
048: return;
049: //I'm sure of that
050: TextField target = (TextField) ke.getComponent();
051: if (ke.getKeyChar() == '*') {
052: ke.consume();
053: target.setText("*");
054: requestFocusOnNextField(target);
055: } else if (ke.getKeyCode() == KeyEvent.VK_SPACE) {
056: ke.consume();
057: requestFocusOnNextField(target);
058: } else if (ke.getKeyCode() == KeyEvent.VK_TAB
059: || ke.getKeyCode() == KeyEvent.VK_SPACE
060: || ke.getKeyCode() == KeyEvent.VK_ENTER) {
061: requestFocusOnNextField(target);
062: } else if (!(ke.getKeyChar() >= '0' && ke.getKeyChar() <= '9')) {
063: ke.consume();
064: }
065: }
066:
067: void requestFocusOnNextField(TextField target) {
068: if (target == comp.fields[0])
069: comp.fields[1].requestFocus();
070: else if (target == comp.fields[1])
071: comp.fields[2].requestFocus();
072: else if (target == comp.fields[2])
073: comp.fields[3].requestFocus();
074: else if (target == comp.fields[3]) {
075: comp.fireActionEvent();
076: comp.fields[0].requestFocus();
077: comp.fields[0].setText("");
078: comp.fields[1].setText("");
079: comp.fields[2].setText("");
080: comp.fields[3].setText("");
081: }
082: }
083:
084: IntegerListener(IPTextField comp) {
085: this .comp = comp;
086: }
087:
088: }
089:
090: class IPListener implements TextListener {
091:
092: IPTextField comp = null;
093:
094: public void textValueChanged(TextEvent e) {
095: // I'm sure of that
096: TextField target = (TextField) e.getSource();
097: String IP = target.getText();
098:
099: if (IP.length() < 3)
100: return;
101: if (IP.length() == 4) {
102: target.setText(IP.substring(3));
103: return;
104: }
105:
106: try {
107: short ip = Short.parseShort(IP);
108: if (ip > 255) {
109: target.setText("");
110: return;
111: }
112: } catch (NumberFormatException ex) {
113: target.setText("");
114: return;
115: }
116: if (target == comp.fields[0])
117: comp.fields[1].requestFocus();
118: else if (target == comp.fields[1])
119: comp.fields[2].requestFocus();
120: else if (target == comp.fields[2])
121: comp.fields[3].requestFocus();
122: }
123:
124: IPListener(IPTextField comp) {
125: this .comp = comp;
126: }
127: }
128:
129: //////// The IPTextField itself
130:
131: protected TextField[] fields = null;
132: transient ActionListener actionListener;
133: String command = "";
134:
135: /**
136: * Adds the specified action listener to recieve action events from
137: * this IPTextField.
138: * @param al - the action listener.
139: */
140: public synchronized void addActionListener(ActionListener al) {
141: actionListener = AWTEventMulticaster.add(actionListener, al);
142: }
143:
144: /**
145: * Removes the specified action listener so that it no longer receives
146: * action events from IPTextField.
147: * @param al - the action listener.
148: */
149: public synchronized void removeActionListener(ActionListener al) {
150: actionListener = AWTEventMulticaster.remove(actionListener, al);
151: }
152:
153: /**
154: * fire a new ActionEvent and process it, if some listeners are listening
155: */
156:
157: protected void fireActionEvent() {
158: if (actionListener != null) {
159: ActionEvent ae = new ActionEvent(this ,
160: ActionEvent.ACTION_PERFORMED, command);
161: actionListener.actionPerformed(ae);
162: }
163: }
164:
165: private String getFieldValue(TextField field) {
166: String svalue = field.getText();
167: if (svalue == null)
168: return "0";
169: if (svalue.length() == 0)
170: return "0";
171: else
172: return svalue;
173: }
174:
175: /**
176: * Gets the text that is presented by this IPTextField.
177: */
178: public String getText() {
179: return (getFieldValue(fields[0]) + "."
180: + getFieldValue(fields[1]) + "."
181: + getFieldValue(fields[2]) + "." + getFieldValue(fields[3]));
182: }
183:
184: /**
185: * Sets the text that is presented by this IPTextField to be the specified
186: * text.
187: * @param text - the new text
188: */
189: public void setText(String IPT) {
190: StringTokenizer st = new StringTokenizer(IPT, ".");
191: int i = 0;
192: while (i < 4 && st.hasMoreTokens())
193: fields[i++].setText(st.nextToken());
194: }
195:
196: /**
197: * Check if the current text value and the default value are different.
198: */
199: public boolean updated() {
200: return (!getText().equals("0.0.0.0"));
201: }
202:
203: /**
204: * Sets the text at its default value
205: */
206: public void setDefault() {
207: fields[0].setText("");
208: fields[1].setText("");
209: fields[2].setText("");
210: fields[3].setText("");
211: }
212:
213: public Dimension getMinimumSize() {
214: return new Dimension(4 * fields[0].getMinimumSize().width,
215: fields[0].getMinimumSize().height);
216: }
217:
218: public Dimension getPreferredSize() {
219: return getMinimumSize();
220: }
221:
222: public IPTextField() {
223: IntegerListener intlistener = new IntegerListener(this );
224: IPListener iplistener = new IPListener(this );
225:
226: fields = new TextField[4];
227: for (int i = 0; i < fields.length; i++) {
228: fields[i] = new TextField(3);
229: fields[i].addTextListener(iplistener);
230: fields[i].addKeyListener(intlistener);
231: }
232: setLayout(new GridLayout(1, 4));
233: add(fields[0]);
234: add(fields[1]);
235: add(fields[2]);
236: add(fields[3]);
237:
238: }
239:
240: }
|