您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

OptionalJSpinner.java 6.5KB

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