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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2006-2013 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
  41. * structure is changed (or anything else that would prevent serialized
  42. * objects being unserialized with the new 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,
  93. * false if it's not.
  94. *
  95. * @return true if the toggle button is selected, otherwise false
  96. */
  97. public boolean isSelected() {
  98. return checkbox.isSelected();
  99. }
  100. /**
  101. * Sets the state of the button.
  102. *
  103. * @param selected true if the button is selected, otherwise false
  104. */
  105. public void setSelected(final boolean selected) {
  106. checkbox.setSelected(selected);
  107. }
  108. /**
  109. * Returns the current value of the model, typically this value is
  110. * displayed by the editor. If the user has changed the value displayed
  111. * by the editor it is possible for the model's value to differ from that
  112. * of the editor, refer to the class level javadoc for examples of how to
  113. * deal with this.
  114. *
  115. * This method simply delegates to the model. It is equivalent to:
  116. *
  117. * getModel().getValue()
  118. *
  119. * @return The current value
  120. */
  121. public Object getValue() {
  122. return spinner.getValue();
  123. }
  124. /**
  125. * Changes current value of the model, typically this value is displayed
  126. * by the editor. If the SpinnerModel implementation doesn't support
  127. * the specified value then an IllegalArgumentException is thrown.
  128. * This method simply delegates to the model. It is equivalent to:
  129. *
  130. * getModel().setValue(value)
  131. *
  132. * @param value Value to set
  133. */
  134. public void setValue(final Object value) {
  135. spinner.setValue(value);
  136. }
  137. /**
  138. * Returns the SpinnerModel that defines this spinners sequence of values.
  139. *
  140. * @return the value of the model property
  141. */
  142. public SpinnerModel getModel() {
  143. return spinner.getModel();
  144. }
  145. /**
  146. * Changes the model that represents the value of this spinner. If the
  147. * editor property has not been explicitly set, the editor property
  148. * is (implicitly) set after the "model" PropertyChangeEvent has been
  149. * fired. The editor property is set to the value returned by createEditor,
  150. * as in:
  151. *
  152. * setEditor(createEditor(model));
  153. *
  154. * @param model the new SpinnerModel
  155. */
  156. public void setModel(final SpinnerModel model) {
  157. spinner.setModel(model);
  158. }
  159. /**
  160. * Adds a change listener to this optional spinner.
  161. *
  162. * @param listener Listener to add
  163. */
  164. public void addChangeListener(final ChangeListener listener) {
  165. listeners.add(ChangeListener.class, listener);
  166. }
  167. /**
  168. * Removes a change listener from this optional spinner.
  169. *
  170. * @param listener Listener to remove
  171. */
  172. public void removeChangeListener(final ChangeListener listener) {
  173. listeners.remove(ChangeListener.class, listener);
  174. }
  175. /** Fires change listeners. */
  176. private void fireChangeListener() {
  177. for (ChangeListener listener : listeners.get(ChangeListener.class)) {
  178. listener.stateChanged(new ChangeEvent(this));
  179. }
  180. }
  181. /** {@inheritDoc} */
  182. @Override
  183. public void stateChanged(final ChangeEvent e) {
  184. fireChangeListener();
  185. }
  186. /**
  187. * {@inheritDoc}
  188. *
  189. * @param e Action event
  190. */
  191. @Override
  192. public void actionPerformed(final ActionEvent e) {
  193. fireChangeListener();
  194. spinner.setEnabled(checkbox.isSelected());
  195. }
  196. }