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.

DurationDisplay.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.durationeditor;
  23. import com.dmdirc.addons.ui_swing.UIUtilities;
  24. import com.dmdirc.ui.IconManager;
  25. import com.dmdirc.util.DateUtils;
  26. import com.dmdirc.util.collections.ListenerList;
  27. import java.awt.Insets;
  28. import java.awt.Window;
  29. import java.awt.event.ActionEvent;
  30. import java.awt.event.ActionListener;
  31. import javax.swing.JButton;
  32. import javax.swing.JLabel;
  33. import javax.swing.JPanel;
  34. import net.miginfocom.swing.MigLayout;
  35. /**
  36. * Duration display and edit component.
  37. */
  38. public class DurationDisplay extends JPanel implements ActionListener,
  39. DurationListener {
  40. /** A version number for this class. */
  41. private static final long serialVersionUID = 1;
  42. /** Listener list. */
  43. private final ListenerList listeners;
  44. /** Icon manager to change icon on editor window. */
  45. private final IconManager iconManager;
  46. /** Current duration. */
  47. private int duration;
  48. /** Duration label. */
  49. private JLabel durationLabel;
  50. /** Edit button. */
  51. private JButton button;
  52. /** Parent window. */
  53. private Window window;
  54. /**
  55. * Initialises a new duration display of 0 milliseconds.
  56. *
  57. * @param iconManager Icon manager used to change window icon
  58. */
  59. public DurationDisplay(final IconManager iconManager) {
  60. this(iconManager, 0);
  61. }
  62. /**
  63. * Instantiates a new duration display.
  64. *
  65. * @param iconManager Icon manager used to change window icon
  66. * @param duration Starting duration
  67. */
  68. public DurationDisplay(final IconManager iconManager, final long duration) {
  69. this(null, iconManager, duration);
  70. }
  71. /**
  72. * Initialises a new duration display showing the specified millisecond duration.
  73. *
  74. * @param window Parent window.
  75. * @param iconManager Icon manager used to change window icon
  76. * @param duration Duration to display in milliseconds
  77. *
  78. * @since 0.6
  79. */
  80. public DurationDisplay(final Window window, final IconManager iconManager, final long duration) {
  81. super();
  82. this.window = window;
  83. this.iconManager = iconManager;
  84. this.duration = (int) (duration / 1000);
  85. listeners = new ListenerList();
  86. initComponents();
  87. addListeners();
  88. layoutComponents();
  89. }
  90. /**
  91. * Initialises and lays out the components.
  92. */
  93. private void initComponents() {
  94. button = new JButton("Edit");
  95. durationLabel = new JLabel();
  96. if (duration == 0) {
  97. durationLabel.setText("0 Seconds");
  98. } else {
  99. durationLabel.setText(DateUtils.formatDuration(duration));
  100. }
  101. if (UIUtilities.isWindowsUI()) {
  102. button.setMargin(new Insets(2, 4, 2, 4));
  103. } else {
  104. button.setMargin(new Insets(0, 2, 0, 2));
  105. }
  106. }
  107. /**
  108. * Adds listeners to the components.
  109. */
  110. private void addListeners() {
  111. button.addActionListener(this);
  112. }
  113. /**
  114. * Lays out the components.
  115. */
  116. private void layoutComponents() {
  117. setLayout(new MigLayout("ins 0, fill"));
  118. add(durationLabel, "growx, pushx");
  119. add(button, "");
  120. }
  121. /**
  122. * {@inheritDoc}
  123. *
  124. * @param e Action event
  125. */
  126. @Override
  127. public void actionPerformed(final ActionEvent e) {
  128. final DurationEditor editor = new DurationEditor(window, iconManager, duration);
  129. editor.display(window);
  130. editor.addDurationListener(this);
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. @Override
  136. public void durationUpdated(final int newDuration) {
  137. duration = newDuration;
  138. if (duration == 0) {
  139. durationLabel.setText("0 Seconds");
  140. } else {
  141. durationLabel.setText(DateUtils.formatDuration(duration));
  142. }
  143. fireDurationListener(newDuration * 1000);
  144. }
  145. /**
  146. * Returns the duration of this display in milliseconds.
  147. *
  148. * @return Displayed duration in milliseconds
  149. */
  150. public long getDuration() {
  151. return duration * 1000;
  152. }
  153. /**
  154. * Adds a DurationListener to the listener list.
  155. *
  156. * @param listener Listener to add
  157. */
  158. public void addDurationListener(final DurationListener listener) {
  159. if (listener == null) {
  160. return;
  161. }
  162. listeners.add(DurationListener.class, listener);
  163. }
  164. /**
  165. * Removes a DurationListener from the listener list.
  166. *
  167. * @param listener Listener to remove
  168. */
  169. public void removeDurationListener(final DurationListener listener) {
  170. listeners.remove(DurationListener.class, listener);
  171. }
  172. /**
  173. * Fires the duration updated method on all listeners.
  174. *
  175. * @param newDuration New duration
  176. */
  177. protected void fireDurationListener(final int newDuration) {
  178. for (final DurationListener listener : listeners.get(DurationListener.class)) {
  179. listener.durationUpdated(newDuration);
  180. }
  181. }
  182. /**
  183. * Sets the Parent window.
  184. *
  185. * @param window Parent window
  186. */
  187. public void setWindow(final Window window) {
  188. this.window = window;
  189. }
  190. }