File Source: DurationDisplay.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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
23 package com.dmdirc.addons.ui_swing.components.durationeditor;
24
25 import com.dmdirc.ui.messages.Formatter;
26 import com.dmdirc.addons.ui_swing.UIUtilities;
27 import com.dmdirc.util.ListenerList;
28
29 import java.awt.Insets;
30 import java.awt.Window;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33
34 import javax.swing.JButton;
35 import javax.swing.JLabel;
36 import javax.swing.JPanel;
37
38 import net.miginfocom.swing.MigLayout;
39
40 /**
41 * Duration display and edit component.
42 */
43 public class DurationDisplay extends JPanel implements ActionListener,
44 DurationListener {
45
46 /**
47 * A version number for this class. It should be changed whenever the class
48 * structure is changed (or anything else that would prevent serialized
49 * objects being unserialized with the new class).
50 */
51 private static final long serialVersionUID = 1;
52 /** Current duration. */
53 private int duration;
54 /** Duration label. */
55 private JLabel durationLabel;
56 /** Edit button. */
57 private JButton button;
58 /** Listener list. */
59 private final ListenerList listeners;
60 /** Parent window. */
61 private Window window;
62
63 /**
64 * Initialises a new duration display of 0 milliseconds.
65 */
66 public DurationDisplay() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay()
*
* Postconditions:
* this.button == &new JButton(initComponents#1)
* init'ed(this.duration)
* this.durationLabel == &new JLabel(initComponents#2)
* this.listeners == &new ListenerList(DurationDisplay#1)
* this.window == null
* new JButton(initComponents#1) num objects == 1
* new JLabel(initComponents#2) num objects == 1
* new ListenerList(DurationDisplay#1) num objects == 1
*/
67 this(0);
68 }
69
70 /**
71 * Instantiates a new duration display.
72 *
73 * @param window Parent window.
74 *
75 * @since 0.6
76 */
77 public DurationDisplay(final Window window) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay(Window)
*
* Postconditions:
* this.button == &new JButton(initComponents#1)
* init'ed(this.duration)
* this.durationLabel == &new JLabel(initComponents#2)
* this.listeners == &new ListenerList(DurationDisplay#1)
* this.window == window
* init'ed(this.window)
* new JButton(initComponents#1) num objects == 1
* new JLabel(initComponents#2) num objects == 1
* new ListenerList(DurationDisplay#1) num objects == 1
*/
78 this(window, 0);
79 }
80
81 /**
82 * Instantiates a new duration display.
83 *
84 * @param duration Starting duration
85 */
86 public DurationDisplay(final long duration) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay(long)
*
* Postconditions:
* this.button == &new JButton(initComponents#1)
* init'ed(this.duration)
* this.durationLabel == &new JLabel(initComponents#2)
* this.listeners == &new ListenerList(DurationDisplay#1)
* this.window == null
* new JButton(initComponents#1) num objects == 1
* new JLabel(initComponents#2) num objects == 1
* new ListenerList(DurationDisplay#1) num objects == 1
*/
87 this(null, duration);
88 }
89
90 /**
91 * Initialises a new duration display showing the specified millisecond duration.
92 *
93 * @param window Parent window.
94 * @param duration Duration to display in milliseconds
95 *
96 * @since 0.6
97 */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationDisplay(Window, long)
*
* Presumptions:
* java.lang.Long:valueOf(...)@100 != null
*
* Postconditions:
* this.button == &new JButton(initComponents#1)
* init'ed(this.duration)
* this.durationLabel == &new JLabel(initComponents#2)
* this.listeners == &new ListenerList(DurationDisplay#1)
* this.window == window
* init'ed(this.window)
* new JButton(initComponents#1) num objects == 1
* new JLabel(initComponents#2) num objects == 1
* new ListenerList(DurationDisplay#1) num objects == 1
*/
98 public DurationDisplay(final Window window, final long duration) {
99 this.window = window;
100 this.duration = Long.valueOf(duration / 1000).intValue();
101 listeners = new ListenerList();
102
103 initComponents();
104 addListeners();
105 layoutComponents();
106 }
107
108 /**
109 * Initliases and lays out the components.
110 */
111 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Preconditions:
* init'ed(this.duration)
*
* Postconditions:
* this.button == &new JButton(initComponents#1)
* this.durationLabel == &new JLabel(initComponents#2)
* new JButton(initComponents#1) num objects == 1
* new JLabel(initComponents#2) num objects == 1
*
* Test Vectors:
* this.duration: {-231..-1, 1..232-1}, {0}
*/
112 button = new JButton("Edit");
113 durationLabel = new JLabel();
114 if (duration == 0) {
115 durationLabel.setText("0 Seconds");
116 } else {
117 durationLabel.setText(Formatter.formatDuration(duration));
118 }
119
120 if (UIUtilities.isWindowsUI()) {
121 button.setMargin(new Insets(2, 4, 2, 4));
122 } else {
123 button.setMargin(new Insets(0, 2, 0, 2));
124 }
125 }
126
127 /**
128 * Adds listeners to the components.
129 */
130 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.button != null
*/
131 button.addActionListener(this);
132 }
133
134 /**
135 * Lays out the components.
136 */
137 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.button)
* init'ed(this.durationLabel)
*/
138 setLayout(new MigLayout("ins 0, fill"));
139
140 add(durationLabel, "growx, pushx");
141 add(button, "");
142 }
143
144 /**
145 * {@inheritDoc}
146 *
147 * @param e Action event
148 */
149 @Override
150 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* init'ed(this.duration)
* init'ed(this.window)
*/
151 DurationEditor editor = new DurationEditor(window, duration);
152 editor.setLocationRelativeTo(this);
153 editor.addDurationListener(this);
154 }
155
156 /**
157 * {@inheritDoc}
158 */
159 @Override
160 public void durationUpdated(final int newDuration) {
/*
P/P * Method: void durationUpdated(int)
*
* Preconditions:
* newDuration in {-2_147_483..4_294_967}
* this.durationLabel != null
* this.listeners != null
*
* Postconditions:
* this.duration == newDuration
* this.duration in {-2_147_483..4_294_967}
*
* Test Vectors:
* newDuration: {-2_147_483..-1, 1..4_294_967}, {0}
*/
161 duration = newDuration;
162 if (duration == 0) {
163 durationLabel.setText("0 Seconds");
164 } else {
165 durationLabel.setText(Formatter.formatDuration(duration));
166 }
167 fireDurationListener(newDuration * 1000);
168 }
169
170 /**
171 * Returns the duration of this display in milliseconds.
172 *
173 * @return Displayed duration in milliseconds
174 */
175 public long getDuration() {
/*
P/P * Method: long getDuration()
*
* Preconditions:
* init'ed(this.duration)
*
* Postconditions:
* return_value == this.duration*1_000
* return_value in {-2_147_483_648_000..4_294_967_295_000}
*/
176 return duration * 1000;
177 }
178
179 /**
180 * Adds a DurationListener to the listener list.
181 *
182 * @param listener Listener to add
183 */
184 public void addDurationListener(final DurationListener listener) {
/*
P/P * Method: void addDurationListener(DurationListener)
*
* Preconditions:
* (soft) this.listeners != null
*
* Test Vectors:
* listener: Inverse{null}, Addr_Set{null}
*/
185 synchronized (listeners) {
186 if (listener == null) {
187 return;
188 }
189 listeners.add(DurationListener.class, listener);
190 }
191 }
192
193 /**
194 * Removes a DurationListener from the listener list.
195 *
196 * @param listener Listener to remove
197 */
198 public void removeDurationListener(final DurationListener listener) {
/*
P/P * Method: void removeDurationListener(DurationListener)
*
* Preconditions:
* this.listeners != null
*/
199 listeners.remove(DurationListener.class, listener);
200 }
201
202 /**
203 * Fires the duration updated method on all listeners.
204 *
205 * @param newDuration New duration
206 */
207 protected void fireDurationListener(final int newDuration) {
/*
P/P * Method: void fireDurationListener(int)
*
* Preconditions:
* this.listeners != null
*
* Presumptions:
* com.dmdirc.util.ListenerList:get(...)@208 != null
* java.util.Iterator:next(...)@208 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@208: {0}, {1}
*/
208 for (DurationListener listener : listeners.get(DurationListener.class)) {
209 listener.durationUpdated(newDuration);
210 }
211 }
212
213 /**
214 * Sets the Parent window.
215 *
216 * @param window Parent window
217 */
218 public void setWindow(final Window window) {
/*
P/P * Method: void setWindow(Window)
*
* Postconditions:
* this.window == window
* init'ed(this.window)
*/
219 this.window = window;
220 }
221 }
SofCheck Inspector Build Version : 2.17854
| DurationDisplay.java |
2009-Jun-25 01:54:24 |
| DurationDisplay.class |
2009-Sep-02 17:04:15 |