1 7 package javax.swing; 8 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.beans.*; 12 import javax.swing.text.*; 13 import javax.swing.plaf.*; 14 import javax.swing.event.*; 15 import javax.accessibility.*; 16 17 import java.io.ObjectOutputStream ; 18 import java.io.ObjectInputStream ; 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 22 140 public class JTextField extends JTextComponent implements SwingConstants { 141 142 147 public JTextField() { 148 this(null, null, 0); 149 } 150 151 158 public JTextField(String text) { 159 this(null, text, 0); 160 } 161 162 173 public JTextField(int columns) { 174 this(null, null, columns); 175 } 176 177 187 public JTextField(String text, int columns) { 188 this(null, text, columns); 189 } 190 191 207 public JTextField(Document doc, String text, int columns) { 208 if (columns < 0) { 209 throw new IllegalArgumentException ("columns less than zero."); 210 } 211 visibility = new DefaultBoundedRangeModel (); 212 visibility.addChangeListener(new ScrollRepainter()); 213 this.columns = columns; 214 if (doc == null) { 215 doc = createDefaultModel(); 216 } 217 setDocument(doc); 218 if (text != null) { 219 setText(text); 220 } 221 } 222 223 230 public String getUIClassID() { 231 return uiClassID; 232 } 233 234 235 248 public void setDocument(Document doc) { 249 if (doc != null) { 250 doc.putProperty("filterNewlines", Boolean.TRUE); 251 } 252 super.setDocument(doc); 253 } 254 255 268 public boolean isValidateRoot() { 269 Component parent = getParent(); 270 if (parent instanceof JViewport ) { 271 return false; 272 } 273 return true; 274 } 275 276 277 290 public int getHorizontalAlignment() { 291 return horizontalAlignment; 292 } 293 294 319 public void setHorizontalAlignment(int alignment) { 320 if (alignment == horizontalAlignment) return; 321 int oldValue = horizontalAlignment; 322 if ((alignment == LEFT) || (alignment == CENTER) || 323 (alignment == RIGHT)|| (alignment == LEADING) || 324 (alignment == TRAILING)) { 325 horizontalAlignment = alignment; 326 } else { 327 throw new IllegalArgumentException ("horizontalAlignment"); 328 } 329 firePropertyChange("horizontalAlignment", oldValue, horizontalAlignment); 330 invalidate(); 331 repaint(); 332 } 333 334 341 protected Document createDefaultModel() { 342 return new PlainDocument(); 343 } 344 345 350 public int getColumns() { 351 return columns; 352 } 353 354 364 public void setColumns(int columns) { 365 int oldVal = this.columns; 366 if (columns < 0) { 367 throw new IllegalArgumentException ("columns less than zero."); 368 } 369 if (columns != oldVal) { 370 this.columns = columns; 371 invalidate(); 372 } 373 } 374 375 385 protected int getColumnWidth() { 386 if (columnWidth == 0) { 387 FontMetrics metrics = getFontMetrics(getFont()); 388 columnWidth = metrics.charWidth('m'); 389 } 390 return columnWidth; 391 } 392 393 401 public Dimension getPreferredSize() { 402 Dimension size = super.getPreferredSize(); 403 if (columns != 0) { 404 Insets insets = getInsets(); 405 size.width = columns * getColumnWidth() + 406 insets.left + insets.right; 407 } 408 return size; 409 } 410 411 418 public void setFont(Font f) { 419 super.setFont(f); 420 columnWidth = 0; 421 } 422 423 429 public synchronized void addActionListener(ActionListener l) { 430 listenerList.add(ActionListener.class, l); 431 } 432 433 439 public synchronized void removeActionListener(ActionListener l) { 440 if ((l != null) && (getAction() == l)) { 441 setAction(null); 442 } else { 443 listenerList.remove(ActionListener.class, l); 444 } 445 } 446 447 455 public synchronized ActionListener[] getActionListeners() { 456 return (ActionListener[])listenerList.getListeners( 457 ActionListener.class); 458 } 459 460 468 protected void fireActionPerformed() { 469 Object [] listeners = listenerList.getListenerList(); 471 int modifiers = 0; 472 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 473 if (currentEvent instanceof InputEvent) { 474 modifiers = ((InputEvent)currentEvent).getModifiers(); 475 } else if (currentEvent instanceof ActionEvent) { 476 modifiers = ((ActionEvent)currentEvent).getModifiers(); 477 } 478 ActionEvent e = 479 new ActionEvent(this, ActionEvent.ACTION_PERFORMED, 480 (command != null) ? command : getText(), 481 EventQueue.getMostRecentEventTime(), modifiers); 482 483 for (int i = listeners.length-2; i>=0; i-=2) { 486 if (listeners[i]==ActionListener.class) { 487 ((ActionListener)listeners[i+1]).actionPerformed(e); 488 } 489 } 490 } 491 492 497 public void setActionCommand(String command) { 498 this.command = command; 499 } 500 501 private Action action; 502 private PropertyChangeListener actionPropertyChangeListener; 503 504 534 public void setAction(Action a) { 535 Action oldValue = getAction(); 536 if (action==null || !action.equals(a)) { 537 action = a; 538 if (oldValue!=null) { 539 removeActionListener(oldValue); 540 oldValue.removePropertyChangeListener(actionPropertyChangeListener); 541 actionPropertyChangeListener = null; 542 } 543 configurePropertiesFromAction(action); 544 if (action!=null) { 545 if (!isListener(ActionListener.class, action)) { 547 addActionListener(action); 548 } 549 actionPropertyChangeListener = createActionPropertyChangeListener(action); 551 action.addPropertyChangeListener(actionPropertyChangeListener); 552 } 553 firePropertyChange("action", oldValue, action); 554 revalidate(); 555 repaint(); 556 } 557 } 558 559 private boolean isListener(Class c, ActionListener a) { 560 boolean isListener = false; 561 Object [] listeners = listenerList.getListenerList(); 562 for (int i = listeners.length-2; i>=0; i-=2) { 563 if (listeners[i]==c && listeners[i+1]==a) { 564 isListener=true; 565 } 566 } 567 return isListener; 568 } 569 570 581 public Action getAction() { 582 return action; 583 } 584 585 599 protected void configurePropertiesFromAction(Action a) { 600 setEnabled((a!=null?a.isEnabled():true)); 601 setToolTipText((a!=null?(String )a.getValue(Action.SHORT_DESCRIPTION):null)); 602 } 603 604 626 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 627 return new AbstractActionPropertyChangeListener (this, a) { 628 public void propertyChange(PropertyChangeEvent e) { 629 String propertyName = e.getPropertyName(); 630 JTextField textField = (JTextField )getTarget(); 631 if (textField == null) { Action action = (Action )e.getSource(); 633 action.removePropertyChangeListener(this); 634 } else { 635 if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) { 636 String text = (String ) e.getNewValue(); 637 textField.setToolTipText(text); 638 } else if (propertyName.equals("enabled")) { 639 Boolean enabledState = (Boolean ) e.getNewValue(); 640 textField.setEnabled(enabledState.booleanValue()); 641 textField.repaint(); 642 } 643 } 644 } 645 }; 646 } 647 648 657 public Action [] getActions() { 658 return TextAction.augmentList(super.getActions(), defaultActions); 659 } 660 661 667 public void postActionEvent() { 668 fireActionPerformed(); 669 } 670 671 673 687 public BoundedRangeModel getHorizontalVisibility() { 688 return visibility; 689 } 690 691 696 public int getScrollOffset() { 697 return visibility.getValue(); 698 } 699 700 705 public void setScrollOffset(int scrollOffset) { 706 visibility.setValue(scrollOffset); 707 } 708 709 714 public void scrollRectToVisible(Rectangle r) { 715 Insets i = getInsets(); 717 int x0 = r.x + visibility.getValue() - i.left; 718 int x1 = x0 + r.width; 719 if (x0 < visibility.getValue()) { 720 visibility.setValue(x0); 722 } else if(x1 > visibility.getValue() + visibility.getExtent()) { 723 visibility.setValue(x1 - visibility.getExtent()); 725 } 726 } 727 728 732 boolean hasActionListener() { 733 Object [] listeners = listenerList.getListenerList(); 735 for (int i = listeners.length-2; i>=0; i-=2) { 738 if (listeners[i]==ActionListener.class) { 739 return true; 740 } 741 } 742 return false; 743 } 744 745 747 752 public static final String notifyAction = "notify-field-accept"; 753 754 private BoundedRangeModel visibility; 755 private int horizontalAlignment = LEADING; 756 private int columns; 757 private int columnWidth; 758 private String command; 759 760 private static final Action [] defaultActions = { 761 new NotifyAction() 762 }; 763 764 768 private static final String uiClassID = "TextFieldUI"; 769 770 772 static class NotifyAction extends TextAction { 774 775 NotifyAction() { 776 super(notifyAction); 777 } 778 779 public void actionPerformed(ActionEvent e) { 780 JTextComponent target = getFocusedComponent(); 781 if (target instanceof JTextField ) { 782 JTextField field = (JTextField ) target; 783 field.postActionEvent(); 784 } 785 } 786 787 public boolean isEnabled() { 788 JTextComponent target = getFocusedComponent(); 789 if (target instanceof JTextField ) { 790 return ((JTextField )target).hasActionListener(); 791 } 792 return false; 793 } 794 } 795 796 class ScrollRepainter implements ChangeListener, Serializable { 797 798 public void stateChanged(ChangeEvent e) { 799 repaint(); 800 } 801 802 } 803 804 805 810 private void writeObject(ObjectOutputStream s) throws IOException { 811 s.defaultWriteObject(); 812 if (getUIClassID().equals(uiClassID)) { 813 byte count = JComponent.getWriteObjCounter(this); 814 JComponent.setWriteObjCounter(this, --count); 815 if (count == 0 && ui != null) { 816 ui.installUI(this); 817 } 818 } 819 } 820 821 822 831 protected String paramString() { 832 String horizontalAlignmentString; 833 if (horizontalAlignment == LEFT) { 834 horizontalAlignmentString = "LEFT"; 835 } else if (horizontalAlignment == CENTER) { 836 horizontalAlignmentString = "CENTER"; 837 } else if (horizontalAlignment == RIGHT) { 838 horizontalAlignmentString = "RIGHT"; 839 } else if (horizontalAlignment == LEADING) { 840 horizontalAlignmentString = "LEADING"; 841 } else if (horizontalAlignment == TRAILING) { 842 horizontalAlignmentString = "TRAILING"; 843 } else horizontalAlignmentString = ""; 844 String commandString = (command != null ? 845 command : ""); 846 847 return super.paramString() + 848 ",columns=" + columns + 849 ",columnWidth=" + columnWidth + 850 ",command=" + commandString + 851 ",horizontalAlignment=" + horizontalAlignmentString; 852 } 853 854 855 859 860 871 public AccessibleContext getAccessibleContext() { 872 if (accessibleContext == null) { 873 accessibleContext = new AccessibleJTextField(); 874 } 875 return accessibleContext; 876 } 877 878 893 protected class AccessibleJTextField extends AccessibleJTextComponent { 894 895 902 public AccessibleStateSet getAccessibleStateSet() { 903 AccessibleStateSet states = super.getAccessibleStateSet(); 904 states.add(AccessibleState.SINGLE_LINE); 905 return states; 906 } 907 } 908 } 909 | Popular Tags |