1 7 package java.awt; 8 9 import java.awt.peer.TextFieldPeer; 10 import java.awt.event.*; 11 import java.util.EventListener ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectInputStream ; 14 import java.io.IOException ; 15 import javax.accessibility.*; 16 17 18 80 public class TextField extends TextComponent { 81 82 92 int columns; 93 94 105 char echoChar; 106 107 transient ActionListener actionListener; 108 109 private static final String base = "textfield"; 110 private static int nameCounter = 0; 111 112 115 private static final long serialVersionUID = -2966288784432217853L; 116 117 120 private static native void initIDs(); 121 122 static { 123 124 Toolkit.loadLibraries(); 125 if (!GraphicsEnvironment.isHeadless()) { 126 initIDs(); 127 } 128 } 129 130 136 public TextField() throws HeadlessException { 137 this("", 0); 138 } 139 140 149 public TextField(String text) throws HeadlessException { 150 this(text, (text != null) ? text.length() : 0); 151 } 152 153 164 public TextField(int columns) throws HeadlessException { 165 this("", columns); 166 } 167 168 183 public TextField(String text, int columns) throws HeadlessException { 184 super(text); 185 this.columns = (columns >= 0) ? columns : 0; 186 } 187 188 192 String constructComponentName() { 193 synchronized (getClass()) { 194 return base + nameCounter++; 195 } 196 } 197 198 202 public void addNotify() { 203 synchronized (getTreeLock()) { 204 if (peer == null) 205 peer = getToolkit().createTextField(this); 206 super.addNotify(); 207 } 208 } 209 210 222 public char getEchoChar() { 223 return echoChar; 224 } 225 226 239 public void setEchoChar(char c) { 240 setEchoCharacter(c); 241 } 242 243 247 @Deprecated 248 public synchronized void setEchoCharacter(char c) { 249 if (echoChar != c) { 250 echoChar = c; 251 TextFieldPeer peer = (TextFieldPeer)this.peer; 252 if (peer != null) { 253 peer.setEchoCharacter(c); 254 } 255 } 256 } 257 258 264 public void setText(String t) { 265 super.setText(t); 266 267 if (valid) { 269 invalidate(); 270 } 271 } 272 273 286 public boolean echoCharIsSet() { 287 return echoChar != 0; 288 } 289 290 297 public int getColumns() { 298 return columns; 299 } 300 301 311 public synchronized void setColumns(int columns) { 312 int oldVal = this.columns; 313 if (columns < 0) { 314 throw new IllegalArgumentException ("columns less than zero."); 315 } 316 if (columns != oldVal) { 317 this.columns = columns; 318 invalidate(); 319 } 320 } 321 322 331 public Dimension getPreferredSize(int columns) { 332 return preferredSize(columns); 333 } 334 335 339 @Deprecated 340 public Dimension preferredSize(int columns) { 341 synchronized (getTreeLock()) { 342 TextFieldPeer peer = (TextFieldPeer)this.peer; 343 return (peer != null) ? 344 peer.preferredSize(columns) : 345 super.preferredSize(); 346 } 347 } 348 349 355 public Dimension getPreferredSize() { 356 return preferredSize(); 357 } 358 359 363 @Deprecated 364 public Dimension preferredSize() { 365 synchronized (getTreeLock()) { 366 return (columns > 0) ? 367 preferredSize(columns) : 368 super.preferredSize(); 369 } 370 } 371 372 379 public Dimension getMinimumSize(int columns) { 380 return minimumSize(columns); 381 } 382 383 387 @Deprecated 388 public Dimension minimumSize(int columns) { 389 synchronized (getTreeLock()) { 390 TextFieldPeer peer = (TextFieldPeer)this.peer; 391 return (peer != null) ? 392 peer.minimumSize(columns) : 393 super.minimumSize(); 394 } 395 } 396 397 403 public Dimension getMinimumSize() { 404 return minimumSize(); 405 } 406 407 411 @Deprecated 412 public Dimension minimumSize() { 413 synchronized (getTreeLock()) { 414 return (columns > 0) ? 415 minimumSize(columns) : 416 super.minimumSize(); 417 } 418 } 419 420 431 public synchronized void addActionListener(ActionListener l) { 432 if (l == null) { 433 return; 434 } 435 actionListener = AWTEventMulticaster.add(actionListener, l); 436 newEventsOnly = true; 437 } 438 439 450 public synchronized void removeActionListener(ActionListener l) { 451 if (l == null) { 452 return; 453 } 454 actionListener = AWTEventMulticaster.remove(actionListener, l); 455 } 456 457 470 public synchronized ActionListener[] getActionListeners() { 471 return (ActionListener[])(getListeners(ActionListener.class)); 472 } 473 474 507 public <T extends EventListener > T[] getListeners(Class <T> listenerType) { 508 EventListener l = null; 509 if (listenerType == ActionListener.class) { 510 l = actionListener; 511 } else { 512 return super.getListeners(listenerType); 513 } 514 return AWTEventMulticaster.getListeners(l, listenerType); 515 } 516 517 boolean eventEnabled(AWTEvent e) { 519 if (e.id == ActionEvent.ACTION_PERFORMED) { 520 if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 || 521 actionListener != null) { 522 return true; 523 } 524 return false; 525 } 526 return super.eventEnabled(e); 527 } 528 529 544 protected void processEvent(AWTEvent e) { 545 if (e instanceof ActionEvent) { 546 processActionEvent((ActionEvent)e); 547 return; 548 } 549 super.processEvent(e); 550 } 551 552 575 protected void processActionEvent(ActionEvent e) { 576 ActionListener listener = actionListener; 577 if (listener != null) { 578 listener.actionPerformed(e); 579 } 580 } 581 582 591 protected String paramString() { 592 String str = super.paramString(); 593 if (echoChar != 0) { 594 str += ",echo=" + echoChar; 595 } 596 return str; 597 } 598 599 600 603 608 private int textFieldSerializedDataVersion = 1; 609 610 625 private void writeObject(ObjectOutputStream s) 626 throws IOException 627 { 628 s.defaultWriteObject(); 629 630 AWTEventMulticaster.save(s, actionListenerK, actionListener); 631 s.writeObject(null); 632 } 633 634 647 private void readObject(ObjectInputStream s) 648 throws ClassNotFoundException , IOException , HeadlessException 649 { 650 s.defaultReadObject(); 652 653 if (columns < 0) { 655 columns = 0; 656 } 657 658 Object keyOrNull; 660 while(null != (keyOrNull = s.readObject())) { 661 String key = ((String )keyOrNull).intern(); 662 663 if (actionListenerK == key) { 664 addActionListener((ActionListener)(s.readObject())); 665 } else { 666 s.readObject(); 668 } 669 } 670 } 671 672 673 677 678 687 public AccessibleContext getAccessibleContext() { 688 if (accessibleContext == null) { 689 accessibleContext = new AccessibleAWTTextField(); 690 } 691 return accessibleContext; 692 } 693 694 699 protected class AccessibleAWTTextField extends AccessibleAWTTextComponent 700 { 701 704 private static final long serialVersionUID = 6219164359235943158L; 705 706 713 public AccessibleStateSet getAccessibleStateSet() { 714 AccessibleStateSet states = super.getAccessibleStateSet(); 715 states.add(AccessibleState.SINGLE_LINE); 716 return states; 717 } 718 } 719 720 } 721 | Popular Tags |