1. /*
  2. * @(#)JIntegerTextField.java 1.7 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.colorchooser;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11. import javax.swing.event.*;
  12. import javax.swing.text.*;
  13. /**
  14. * A text field which takes integer values
  15. *
  16. * @version 1.7 11/29/01
  17. * @author Steve Wilson
  18. */
  19. class JIntegerTextField extends JTextField {
  20. public JIntegerTextField( int min, int max, int initialValue ) {
  21. super( new NumericDocument(min, max), initialValue+"", String.valueOf(max).length()+1 );
  22. installKeyboardActions();
  23. }
  24. protected void installKeyboardActions() {
  25. KeyStroke upKey = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
  26. KeyStroke downKey = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
  27. KeyStroke numUpKey = KeyStroke.getKeyStroke("KP_UP");
  28. KeyStroke numDownKey = KeyStroke.getKeyStroke("KP_DOWN");
  29. registerKeyboardAction(new ValueDelta(1), numUpKey, JComponent.WHEN_FOCUSED);
  30. registerKeyboardAction(new ValueDelta(-1), numDownKey, JComponent.WHEN_FOCUSED);
  31. registerKeyboardAction(new ValueDelta(1), upKey, JComponent.WHEN_FOCUSED);
  32. registerKeyboardAction(new ValueDelta(-1), downKey, JComponent.WHEN_FOCUSED);
  33. }
  34. public int getIntegerValue() {
  35. return ((NumericDocument)getDocument()).getIntegerValue();
  36. }
  37. public void setText(String s) {
  38. NumericDocument doc = (NumericDocument)getDocument();
  39. int oldValue = doc.currentVal;
  40. try {
  41. doc.currentVal = doc.parse(s);
  42. } catch (Exception e) {
  43. Toolkit.getDefaultToolkit().beep();
  44. return;
  45. }
  46. if (oldValue != doc.currentVal) {
  47. doc.checkingEnabled = false;
  48. super.setText(s);
  49. doc.checkingEnabled = true;
  50. }
  51. }
  52. class ValueDelta implements ActionListener {
  53. int delta;
  54. public ValueDelta(int delta) {
  55. this.delta = delta;
  56. }
  57. public void actionPerformed(ActionEvent e) {
  58. NumericDocument doc = (NumericDocument)getDocument();
  59. int min = doc.min;
  60. int max = doc.max;
  61. int value = getIntegerValue();
  62. value += delta;
  63. if ( value < min) {
  64. value = max;
  65. } else if (value > max) {
  66. value = min;
  67. }
  68. setText(String.valueOf(value));
  69. }
  70. }
  71. }
  72. class NumericDocument extends PlainDocument {
  73. int min;
  74. int max;
  75. int currentVal = 0;
  76. boolean checkingEnabled = true;
  77. public NumericDocument(int min, int max) {
  78. this.min = min;
  79. this.max = max;
  80. }
  81. public int getIntegerValue() {
  82. return currentVal;
  83. }
  84. public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
  85. if (str == null) {
  86. return;
  87. }
  88. if (!checkingEnabled) {
  89. super.insertString(offs, str, a);
  90. return;
  91. }
  92. String proposedResult = null;
  93. if (getLength() == 0) {
  94. proposedResult = str;
  95. } else {
  96. StringBuffer currentBuffer = new StringBuffer( getText(0, getLength()) );
  97. currentBuffer.insert(offs, str);
  98. proposedResult = currentBuffer.toString();
  99. }
  100. try {
  101. currentVal = parse(proposedResult);
  102. super.insertString(offs, str, a);
  103. } catch (Exception e){
  104. Toolkit.getDefaultToolkit().beep();
  105. }
  106. }
  107. public void remove(int offs, int len) throws BadLocationException {
  108. if (!checkingEnabled) {
  109. super.remove(offs, len);
  110. return;
  111. }
  112. String currentText = getText(0, getLength());
  113. String beforeOffset = currentText.substring(0, offs);
  114. String afterOffset = currentText.substring(len + offs, currentText.length());
  115. String proposedResult = beforeOffset + afterOffset;
  116. try {
  117. currentVal = parse(proposedResult);
  118. super.remove(offs, len);
  119. } catch (Exception e) {
  120. Toolkit.getDefaultToolkit().beep();
  121. }
  122. }
  123. public int parse(String proposedResult) throws NumberFormatException{
  124. int value = 0;
  125. if ( proposedResult.length() != 0) {
  126. value = Integer.parseInt(proposedResult);
  127. }
  128. if ( value >= min && value <= max) {
  129. return value;
  130. } else {
  131. throw new NumberFormatException();
  132. }
  133. }
  134. }