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.

DurationEditor.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.durationeditor;
  23. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  24. import com.dmdirc.util.ListenerList;
  25. import java.awt.Window;
  26. import java.awt.event.ActionEvent;
  27. import java.awt.event.ActionListener;
  28. import java.awt.event.WindowAdapter;
  29. import java.awt.event.WindowEvent;
  30. import javax.swing.JButton;
  31. import javax.swing.JLabel;
  32. import javax.swing.JSpinner;
  33. import javax.swing.SpinnerNumberModel;
  34. import net.miginfocom.swing.MigLayout;
  35. /**
  36. * Duration editor component.
  37. */
  38. public class DurationEditor extends StandardDialog implements ActionListener {
  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 = 1;
  45. /** Days spinner. */
  46. private JSpinner daysSpinner;
  47. /** Hours spinner. */
  48. private JSpinner hoursSpinner;
  49. /** Minutes spinner. */
  50. private JSpinner minutesSpinner;
  51. /** Seconds spinner. */
  52. private JSpinner secondsSpinner;
  53. /** Listener list. */
  54. private final ListenerList listeners;
  55. /** Parent window. */
  56. private Window window;
  57. /**
  58. * Instantiates a new duration editor.
  59. */
  60. public DurationEditor() {
  61. this(0);
  62. }
  63. /**
  64. * Instantiates a new duration editor.
  65. *
  66. * @param window Parent window.
  67. *
  68. * @since 0.6
  69. */
  70. public DurationEditor(final Window window) {
  71. this(window, 0);
  72. }
  73. /**
  74. * Instantiates a new duration editor.
  75. *
  76. * @param duration Starting duration
  77. */
  78. public DurationEditor(final long duration) {
  79. this(null, duration);
  80. }
  81. /**
  82. * Instantiates a new duration editor.
  83. *
  84. * @param window Parent window.
  85. * @param duration Starting duration
  86. *
  87. * @since 0.6
  88. */
  89. public DurationEditor(final Window window, final long duration) {
  90. super(window, ModalityType.MODELESS);
  91. this.window = window;
  92. listeners = new ListenerList();
  93. initComponents(duration);
  94. addListeners();
  95. layoutComponents();
  96. }
  97. /**
  98. * Initialises the components.
  99. *
  100. * @param duration Duration to initialise to
  101. */
  102. private void initComponents(long duration) {
  103. orderButtons(new JButton(), new JButton());
  104. daysSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 31, 1));
  105. hoursSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 24, 1));
  106. minutesSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 60, 1));
  107. secondsSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 60, 1));
  108. daysSpinner.setValue((int) duration / 86400);
  109. duration = (duration % 86400);
  110. hoursSpinner.setValue((int) duration / 3600);
  111. duration = (duration % 3600);
  112. minutesSpinner.setValue((int) duration / 60);
  113. duration = (duration % 60);
  114. secondsSpinner.setValue((int) duration);
  115. }
  116. /**
  117. * Adds the listeners.
  118. */
  119. private void addListeners() {
  120. getOkButton().addActionListener(this);
  121. getCancelButton().addActionListener(this);
  122. setWindow(window);
  123. }
  124. /**
  125. * Lays out the components.
  126. */
  127. private void layoutComponents() {
  128. setLayout(new MigLayout("hidemode 3, pack"));
  129. add(new JLabel("Days: "), "split 8");
  130. add(daysSpinner);
  131. add(new JLabel("Hours: "));
  132. add(hoursSpinner);
  133. add(new JLabel("Minutes: "));
  134. add(minutesSpinner);
  135. add(new JLabel("Seconds: "));
  136. add(secondsSpinner, "wrap");
  137. add(getLeftButton(), "split 2, sgx button, right");
  138. add(getRightButton(), "sgx button, right");
  139. setDefaultCloseOperation(DurationEditor.DISPOSE_ON_CLOSE);
  140. }
  141. /** {@inheritDoc}
  142. *
  143. * @param e Action event
  144. */
  145. @Override
  146. public void actionPerformed(final ActionEvent e) {
  147. if (e.getSource() == getOkButton()) {
  148. fireDurationListener(getDuration());
  149. }
  150. dispose();
  151. }
  152. /**
  153. * Returns the duration currently represented by this duration editor.
  154. *
  155. * @return Current duration (in seconds)
  156. */
  157. public int getDuration() {
  158. int duration = 0;
  159. duration += ((Number) secondsSpinner.getValue()).intValue();
  160. duration += (((Number) minutesSpinner.getValue())).intValue() * 60;
  161. duration += (((Number) hoursSpinner.getValue())).intValue() * 60 * 60;
  162. duration += (((Number) daysSpinner.getValue())).intValue() * 60 * 60 *
  163. 24;
  164. return duration;
  165. }
  166. /**
  167. * Adds a DurationListener to the listener list.
  168. *
  169. * @param listener Listener to add
  170. */
  171. public void addDurationListener(final DurationListener listener) {
  172. synchronized (listeners) {
  173. if (listener == null) {
  174. return;
  175. }
  176. listeners.add(DurationListener.class, listener);
  177. }
  178. }
  179. /**
  180. * Removes a DurationListener from the listener list.
  181. *
  182. * @param listener Listener to remove
  183. */
  184. public void removeDurationListener(final DurationListener listener) {
  185. listeners.remove(DurationListener.class, listener);
  186. }
  187. /**
  188. * Fires the duration updated method on all listeners.
  189. *
  190. * @param newDuration New duration
  191. */
  192. protected void fireDurationListener(final int newDuration) {
  193. for (DurationListener listener : listeners.get(DurationListener.class)) {
  194. listener.durationUpdated(newDuration);
  195. }
  196. }
  197. /**
  198. * Sets the Parent window.
  199. *
  200. * @param window Parent window
  201. */
  202. public void setWindow(final Window window) {
  203. this.window = window;
  204. if (window != null) {
  205. window.addWindowListener(new WindowAdapter() {
  206. /** {@inheritDoc} */
  207. @Override
  208. public void windowClosed(WindowEvent e) {
  209. dispose();
  210. }
  211. });
  212. }
  213. }
  214. }