1. /*
  2. * @(#)BasicComboBoxEditor.java 1.26 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import javax.swing.*;
  9. import javax.swing.border.Border;
  10. import javax.swing.text.AttributeSet;
  11. import javax.swing.text.BadLocationException;
  12. import javax.swing.text.PlainDocument;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import java.lang.reflect.Method;
  16. /**
  17. * The default editor for editable combo boxes. The editor is implemented as a JTextField.
  18. *
  19. * @version 1.26 12/19/03
  20. * @author Arnaud Weber
  21. * @author Mark Davidson
  22. */
  23. public class BasicComboBoxEditor implements ComboBoxEditor,FocusListener {
  24. protected JTextField editor;
  25. private Object oldValue;
  26. public BasicComboBoxEditor() {
  27. editor = new BorderlessTextField("",9);
  28. editor.setBorder(null);
  29. }
  30. public Component getEditorComponent() {
  31. return editor;
  32. }
  33. /**
  34. * Sets the item that should be edited.
  35. *
  36. * @param anObject the displayed value of the editor
  37. */
  38. public void setItem(Object anObject) {
  39. if ( anObject != null ) {
  40. editor.setText(anObject.toString());
  41. oldValue = anObject;
  42. } else {
  43. editor.setText("");
  44. }
  45. }
  46. public Object getItem() {
  47. Object newValue = editor.getText();
  48. if (oldValue != null && !(oldValue instanceof String)) {
  49. // The original value is not a string. Should return the value in it's
  50. // original type.
  51. if (newValue.equals(oldValue.toString())) {
  52. return oldValue;
  53. } else {
  54. // Must take the value from the editor and get the value and cast it to the new type.
  55. Class cls = oldValue.getClass();
  56. try {
  57. Method method = cls.getMethod("valueOf", new Class[]{String.class});
  58. newValue = method.invoke(oldValue, new Object[] { editor.getText()});
  59. } catch (Exception ex) {
  60. // Fail silently and return the newValue (a String object)
  61. }
  62. }
  63. }
  64. return newValue;
  65. }
  66. public void selectAll() {
  67. editor.selectAll();
  68. editor.requestFocus();
  69. }
  70. // This used to do something but now it doesn't. It couldn't be
  71. // removed because it would be an API change to do so.
  72. public void focusGained(FocusEvent e) {}
  73. // This used to do something but now it doesn't. It couldn't be
  74. // removed because it would be an API change to do so.
  75. public void focusLost(FocusEvent e) {}
  76. public void addActionListener(ActionListener l) {
  77. editor.addActionListener(l);
  78. }
  79. public void removeActionListener(ActionListener l) {
  80. editor.removeActionListener(l);
  81. }
  82. static class BorderlessTextField extends JTextField {
  83. public BorderlessTextField(String value,int n) {
  84. super(value,n);
  85. }
  86. // workaround for 4530952
  87. public void setText(String s) {
  88. if (getText().equals(s)) {
  89. return;
  90. }
  91. super.setText(s);
  92. }
  93. public void setBorder(Border b) {}
  94. }
  95. /**
  96. * A subclass of BasicComboBoxEditor that implements UIResource.
  97. * BasicComboBoxEditor doesn't implement UIResource
  98. * directly so that applications can safely override the
  99. * cellRenderer property with BasicListCellRenderer subclasses.
  100. * <p>
  101. * <strong>Warning:</strong>
  102. * Serialized objects of this class will not be compatible with
  103. * future Swing releases. The current serialization support is
  104. * appropriate for short term storage or RMI between applications running
  105. * the same version of Swing. As of 1.4, support for long term storage
  106. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  107. * has been added to the <code>java.beans</code> package.
  108. * Please see {@link java.beans.XMLEncoder}.
  109. */
  110. public static class UIResource extends BasicComboBoxEditor
  111. implements javax.swing.plaf.UIResource {
  112. }
  113. }