Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OptionalJSpinner.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components;
  18. import com.dmdirc.util.collections.ListenerList;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import javax.swing.JCheckBox;
  22. import javax.swing.JPanel;
  23. import javax.swing.JSpinner;
  24. import javax.swing.SpinnerModel;
  25. import javax.swing.SpinnerNumberModel;
  26. import javax.swing.event.ChangeEvent;
  27. import javax.swing.event.ChangeListener;
  28. import net.miginfocom.swing.MigLayout;
  29. /**
  30. * Optional JSpinner component. Composite JSpinner, JCheckbox component.
  31. */
  32. public class OptionalJSpinner extends JPanel implements ActionListener,
  33. ChangeListener {
  34. /** A version number for this class. */
  35. private static final long serialVersionUID = -2867331420063503447L;
  36. /** Composite spinner. */
  37. private final JSpinner spinner;
  38. /** Composite checkbox. */
  39. private final JCheckBox checkbox;
  40. /** Our listeners. */
  41. private final ListenerList listeners;
  42. /**
  43. * Creates a new optional JSpinner with a default number model.
  44. */
  45. public OptionalJSpinner() {
  46. this(new SpinnerNumberModel());
  47. }
  48. /**
  49. * Creates a new optional JSpinner with a default number model.
  50. *
  51. * @param enabled Initial selected state
  52. */
  53. public OptionalJSpinner(final boolean enabled) {
  54. this(new SpinnerNumberModel(), enabled);
  55. }
  56. /**
  57. * Creates a new optional SPinner with a default number model.
  58. *
  59. * @param model Model to show
  60. */
  61. public OptionalJSpinner(final SpinnerModel model) {
  62. this(model, true);
  63. }
  64. /**
  65. * Creates a new optional SPinner with a default number model.
  66. *
  67. * @param model Model to show
  68. * @param enabled Initial selected state
  69. */
  70. public OptionalJSpinner(final SpinnerModel model, final boolean enabled) {
  71. checkbox = new JCheckBox("", enabled);
  72. spinner = new JSpinner(model);
  73. listeners = new ListenerList();
  74. setLayout(new MigLayout("fill"));
  75. add(checkbox, "");
  76. add(spinner, "growx, pushx");
  77. spinner.addChangeListener(this);
  78. checkbox.addActionListener(this);
  79. spinner.setEnabled(checkbox.isSelected());
  80. }
  81. /**
  82. * Returns the state of the button. True if the toggle button is selected, false if it's not.
  83. *
  84. * @return true if the toggle button is selected, otherwise false
  85. */
  86. public boolean isSelected() {
  87. return checkbox.isSelected();
  88. }
  89. /**
  90. * Sets the state of the button.
  91. *
  92. * @param selected true if the button is selected, otherwise false
  93. */
  94. public void setSelected(final boolean selected) {
  95. checkbox.setSelected(selected);
  96. }
  97. /**
  98. * Returns the current value of the model, typically this value is displayed by the editor. If
  99. * the user has changed the value displayed by the editor it is possible for the model's value
  100. * to differ from that of the editor, refer to the class level javadoc for examples of how to
  101. * deal with this.
  102. *
  103. * This method simply delegates to the model. It is equivalent to:
  104. *
  105. * getModel().getValue()
  106. *
  107. * @return The current value
  108. */
  109. public Object getValue() {
  110. return spinner.getValue();
  111. }
  112. /**
  113. * Changes current value of the model, typically this value is displayed by the editor. If the
  114. * SpinnerModel implementation doesn't support the specified value then an
  115. * IllegalArgumentException is thrown. This method simply delegates to the model. It is
  116. * equivalent to:
  117. *
  118. * getModel().setValue(value)
  119. *
  120. * @param value Value to set
  121. */
  122. public void setValue(final Object value) {
  123. spinner.setValue(value);
  124. }
  125. /**
  126. * Returns the SpinnerModel that defines this spinners sequence of values.
  127. *
  128. * @return the value of the model property
  129. */
  130. public SpinnerModel getModel() {
  131. return spinner.getModel();
  132. }
  133. /**
  134. * Changes the model that represents the value of this spinner. If the editor property has not
  135. * been explicitly set, the editor property is (implicitly) set after the "model"
  136. * PropertyChangeEvent has been fired. The editor property is set to the value returned by
  137. * createEditor, as in:
  138. *
  139. * setEditor(createEditor(model));
  140. *
  141. * @param model the new SpinnerModel
  142. */
  143. public void setModel(final SpinnerModel model) {
  144. spinner.setModel(model);
  145. }
  146. /**
  147. * Adds a change listener to this optional spinner.
  148. *
  149. * @param listener Listener to add
  150. */
  151. public void addChangeListener(final ChangeListener listener) {
  152. listeners.add(ChangeListener.class, listener);
  153. }
  154. /**
  155. * Removes a change listener from this optional spinner.
  156. *
  157. * @param listener Listener to remove
  158. */
  159. public void removeChangeListener(final ChangeListener listener) {
  160. listeners.remove(ChangeListener.class, listener);
  161. }
  162. /** Fires change listeners. */
  163. private void fireChangeListener() {
  164. for (ChangeListener listener : listeners.get(ChangeListener.class)) {
  165. listener.stateChanged(new ChangeEvent(this));
  166. }
  167. }
  168. @Override
  169. public void stateChanged(final ChangeEvent e) {
  170. fireChangeListener();
  171. }
  172. @Override
  173. public void actionPerformed(final ActionEvent e) {
  174. fireChangeListener();
  175. spinner.setEnabled(checkbox.isSelected());
  176. }
  177. }