Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TextPaneBoundedRangeModel.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.textpane;
  23. import com.dmdirc.util.collections.ListenerList;
  24. import javax.swing.BoundedRangeModel;
  25. import javax.swing.event.ChangeEvent;
  26. import javax.swing.event.ChangeListener;
  27. /**
  28. * Simple bounded range model allowing value to be anything inside min and max.
  29. */
  30. public class TextPaneBoundedRangeModel implements BoundedRangeModel {
  31. /** State change listeners. */
  32. private final ListenerList listeners = new ListenerList();
  33. /** Maximum value allowed in the range. */
  34. private int maximum = 0;
  35. /** Minimum value allowed in the range. */
  36. private int minimum = 0;
  37. /** Value inside the range. */
  38. private int value = 0;
  39. /** {@inheritDoc} */
  40. @Override
  41. public int getMinimum() {
  42. return minimum;
  43. }
  44. /** {@inheritDoc} */
  45. @Override
  46. public void setMinimum(final int newMinimum) {
  47. setRangeProperties(value, 0, newMinimum, maximum, false);
  48. }
  49. /** {@inheritDoc} */
  50. @Override
  51. public int getMaximum() {
  52. return maximum;
  53. }
  54. /** {@inheritDoc} */
  55. @Override
  56. public void setMaximum(final int newMaximum) {
  57. setRangeProperties(value, 0, minimum, newMaximum, false);
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public int getValue() {
  62. return value;
  63. }
  64. /** {@inheritDoc} */
  65. @Override
  66. public void setValue(final int newValue) {
  67. setRangeProperties(newValue, 0, minimum, maximum, false);
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void setValueIsAdjusting(final boolean b) {
  72. setRangeProperties(value, 0, minimum, maximum, false);
  73. }
  74. /** {@inheritDoc} */
  75. @Override
  76. public boolean getValueIsAdjusting() {
  77. return false;
  78. }
  79. /** {@inheritDoc} */
  80. @Override
  81. public int getExtent() {
  82. return 0;
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public void setExtent(final int newExtent) {
  87. setRangeProperties(value, 0, minimum, maximum, false);
  88. }
  89. /** {@inheritDoc} */
  90. @Override
  91. public void setRangeProperties(final int value, final int extent,
  92. final int min, final int max, final boolean adjusting) {
  93. this.value = Math.max(min, Math.min(max, value));
  94. this.minimum = Math.max(0, Math.min(max, min));
  95. this.maximum = Math.max(min, max);
  96. for (ChangeListener listener : listeners.get(
  97. ChangeListener.class)) {
  98. listener.stateChanged(new ChangeEvent(this));
  99. }
  100. }
  101. /** {@inheritDoc} */
  102. @Override
  103. public void addChangeListener(final ChangeListener x) {
  104. listeners.add(ChangeListener.class, x);
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void removeChangeListener(final ChangeListener x) {
  109. listeners.remove(ChangeListener.class, x);
  110. }
  111. }