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