You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OptionalJSpinner.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.components;
  23. import com.dmdirc.util.collections.ListenerList;
  24. import java.awt.event.ActionEvent;
  25. import java.awt.event.ActionListener;
  26. import javax.swing.JCheckBox;
  27. import javax.swing.JPanel;
  28. import javax.swing.JSpinner;
  29. import javax.swing.SpinnerModel;
  30. import javax.swing.SpinnerNumberModel;
  31. import javax.swing.event.ChangeEvent;
  32. import javax.swing.event.ChangeListener;
  33. import net.miginfocom.swing.MigLayout;
  34. /**
  35. * Optional JSpinner component. Composite JSpinner, JCheckbox component.
  36. */
  37. public class OptionalJSpinner extends JPanel implements ActionListener,
  38. ChangeListener {
  39. /**
  40. * A version number for this class. It should be changed whenever the class structure is changed
  41. * (or anything else that would prevent serialized objects being unserialized with the new
  42. * class).
  43. */
  44. private static final long serialVersionUID = -2867331420063503447L;
  45. /** Composite spinner. */
  46. private final JSpinner spinner;
  47. /** Composite checkbox. */
  48. private final JCheckBox checkbox;
  49. /** Our listeners. */
  50. private final ListenerList listeners;
  51. /**
  52. * Creates a new optional JSpinner with a default number model.
  53. */
  54. public OptionalJSpinner() {
  55. this(new SpinnerNumberModel());
  56. }
  57. /**
  58. * Creates a new optional JSpinner with a default number model.
  59. *
  60. * @param enabled Initial selected state
  61. */
  62. public OptionalJSpinner(final boolean enabled) {
  63. this(new SpinnerNumberModel(), enabled);
  64. }
  65. /**
  66. * Creates a new optional SPinner with a default number model.
  67. *
  68. * @param model Model to show
  69. */
  70. public OptionalJSpinner(final SpinnerModel model) {
  71. this(model, true);
  72. }
  73. /**
  74. * Creates a new optional SPinner with a default number model.
  75. *
  76. * @param model Model to show
  77. * @param enabled Initial selected state
  78. */
  79. public OptionalJSpinner(final SpinnerModel model, final boolean enabled) {
  80. super();
  81. checkbox = new JCheckBox("", enabled);
  82. spinner = new JSpinner(model);
  83. listeners = new ListenerList();
  84. setLayout(new MigLayout("fill"));
  85. add(checkbox, "");
  86. add(spinner, "growx, pushx");
  87. spinner.addChangeListener(this);
  88. checkbox.addActionListener(this);
  89. spinner.setEnabled(checkbox.isSelected());
  90. }
  91. /**
  92. * Returns the state of the button. True if the toggle button is selected, false if it's not.
  93. *
  94. * @return true if the toggle button is selected, otherwise false
  95. */
  96. public boolean isSelected() {
  97. return checkbox.isSelected();
  98. }
  99. /**
  100. * Sets the state of the button.
  101. *
  102. * @param selected true if the button is selected, otherwise false
  103. */
  104. public void setSelected(final boolean selected) {
  105. checkbox.setSelected(selected);
  106. }
  107. /**
  108. * Returns the current value of the model, typically this value is displayed by the editor. If
  109. * the user has changed the value displayed by the editor it is possible for the model's value
  110. * to differ from that of the editor, refer to the class level javadoc for examples of how to
  111. * deal with this.
  112. *
  113. * This method simply delegates to the model. It is equivalent to:
  114. *
  115. * getModel().getValue()
  116. *
  117. * @return The current value
  118. */
  119. public Object getValue() {
  120. return spinner.getValue();
  121. }
  122. /**
  123. * Changes current value of the model, typically this value is displayed by the editor. If the
  124. * SpinnerModel implementation doesn't support the specified value then an
  125. * IllegalArgumentException is thrown. This method simply delegates to the model. It is
  126. * equivalent to:
  127. *
  128. * getModel().setValue(value)
  129. *
  130. * @param value Value to set
  131. */
  132. public void setValue(final Object value) {
  133. spinner.setValue(value);
  134. }
  135. /**
  136. * Returns the SpinnerModel that defines this spinners sequence of values.
  137. *
  138. * @return the value of the model property
  139. */
  140. public SpinnerModel getModel() {
  141. return spinner.getModel();
  142. }
  143. /**
  144. * Changes the model that represents the value of this spinner. If the editor property has not
  145. * been explicitly set, the editor property is (implicitly) set after the "model"
  146. * PropertyChangeEvent has been fired. The editor property is set to the value returned by
  147. * createEditor, as in:
  148. *
  149. * setEditor(createEditor(model));
  150. *
  151. * @param model the new SpinnerModel
  152. */
  153. public void setModel(final SpinnerModel model) {
  154. spinner.setModel(model);
  155. }
  156. /**
  157. * Adds a change listener to this optional spinner.
  158. *
  159. * @param listener Listener to add
  160. */
  161. public void addChangeListener(final ChangeListener listener) {
  162. listeners.add(ChangeListener.class, listener);
  163. }
  164. /**
  165. * Removes a change listener from this optional spinner.
  166. *
  167. * @param listener Listener to remove
  168. */
  169. public void removeChangeListener(final ChangeListener listener) {
  170. listeners.remove(ChangeListener.class, listener);
  171. }
  172. /** Fires change listeners. */
  173. private void fireChangeListener() {
  174. for (ChangeListener listener : listeners.get(ChangeListener.class)) {
  175. listener.stateChanged(new ChangeEvent(this));
  176. }
  177. }
  178. /** {@inheritDoc} */
  179. @Override
  180. public void stateChanged(final ChangeEvent e) {
  181. fireChangeListener();
  182. }
  183. /**
  184. * {@inheritDoc}
  185. *
  186. * @param e Action event
  187. */
  188. @Override
  189. public void actionPerformed(final ActionEvent e) {
  190. fireChangeListener();
  191. spinner.setEnabled(checkbox.isSelected());
  192. }
  193. }