File Source: DurationEditor.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.durationeditor.DurationEditor__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.addons.ui_swing.components.StandardDialog;
26 import com.dmdirc.util.ListenerList;
27
28 import java.awt.Window;
29 import java.awt.event.ActionEvent;
30 import java.awt.event.ActionListener;
31 import java.awt.event.WindowAdapter;
32 import java.awt.event.WindowEvent;
33
34 import javax.swing.JButton;
35 import javax.swing.JLabel;
36 import javax.swing.JSpinner;
37 import javax.swing.SpinnerNumberModel;
38
39 import net.miginfocom.swing.MigLayout;
40
41 /**
42 * Duration editor component.
43 */
44 public class DurationEditor extends StandardDialog implements ActionListener {
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 /** Days spinner. */
53 private JSpinner daysSpinner;
54 /** Hours spinner. */
55 private JSpinner hoursSpinner;
56 /** Minutes spinner. */
57 private JSpinner minutesSpinner;
58 /** Seconds spinner. */
59 private JSpinner secondsSpinner;
60 /** Listener list. */
61 private final ListenerList listeners;
62 /** Parent window. */
63 private Window window;
64
65 /**
66 * Instantiates a new duration editor.
67 */
68 public DurationEditor() {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationEditor()
*
* Postconditions:
* this.cancelButton == One-of{&new JButton(initComponents#2), &new JButton(initComponents#1)}
* this.cancelButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.daysSpinner == &new JSpinner(initComponents#3)
* this.hoursSpinner == &new JSpinner(initComponents#5)
* this.listeners == &new ListenerList(DurationEditor#1)
* this.minutesSpinner == &new JSpinner(initComponents#7)
* this.okButton == One-of{&new JButton(initComponents#1), &new JButton(initComponents#2)}
* this.okButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.secondsSpinner == &new JSpinner(initComponents#9)
* this.window == null
* ...
*/
69 this(0);
70 }
71
72 /**
73 * Instantiates a new duration editor.
74 *
75 * @param window Parent window.
76 *
77 * @since 0.6
78 */
79 public DurationEditor(final Window window) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationEditor(Window)
*
* Postconditions:
* this.cancelButton == One-of{&new JButton(initComponents#2), &new JButton(initComponents#1)}
* this.cancelButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.daysSpinner == &new JSpinner(initComponents#3)
* this.hoursSpinner == &new JSpinner(initComponents#5)
* this.listeners == &new ListenerList(DurationEditor#1)
* this.minutesSpinner == &new JSpinner(initComponents#7)
* this.okButton == One-of{&new JButton(initComponents#1), &new JButton(initComponents#2)}
* this.okButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.secondsSpinner == &new JSpinner(initComponents#9)
* this.window == window
* ...
*/
80 this(window, 0);
81 }
82
83 /**
84 * Instantiates a new duration editor.
85 *
86 * @param duration Starting duration
87 */
88 public DurationEditor(final long duration) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationEditor(long)
*
* Preconditions:
* duration in {-185_542_587_273_599..371_085_174_374_399}
*
* Postconditions:
* this.cancelButton == One-of{&new JButton(initComponents#2), &new JButton(initComponents#1)}
* this.cancelButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.daysSpinner == &new JSpinner(initComponents#3)
* this.hoursSpinner == &new JSpinner(initComponents#5)
* this.listeners == &new ListenerList(DurationEditor#1)
* this.minutesSpinner == &new JSpinner(initComponents#7)
* this.okButton == One-of{&new JButton(initComponents#1), &new JButton(initComponents#2)}
* this.okButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.secondsSpinner == &new JSpinner(initComponents#9)
* this.window == null
* ...
*/
89 this(null, duration);
90 }
91
92 /**
93 * Instantiates a new duration editor.
94 *
95 * @param window Parent window.
96 * @param duration Starting duration
97 *
98 * @since 0.6
99 */
100 public DurationEditor(final Window window, final long duration) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationEditor(Window, long)
*
* Preconditions:
* duration in {-185_542_587_273_599..371_085_174_374_399}
*
* Presumptions:
* init'ed(java.awt.Dialog$ModalityType.MODELESS)
*
* Postconditions:
* this.cancelButton == One-of{&new JButton(initComponents#2), &new JButton(initComponents#1)}
* this.cancelButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.daysSpinner == &new JSpinner(initComponents#3)
* this.hoursSpinner == &new JSpinner(initComponents#5)
* this.listeners == &new ListenerList(DurationEditor#1)
* this.minutesSpinner == &new JSpinner(initComponents#7)
* this.okButton == One-of{&new JButton(initComponents#1), &new JButton(initComponents#2)}
* this.okButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.secondsSpinner == &new JSpinner(initComponents#9)
* this.window == window
* ...
*/
101 super(window, ModalityType.MODELESS);
102
103 this.window = window;
104
105 listeners = new ListenerList();
106
107 initComponents(duration);
108 addListeners();
109 layoutComponents();
110 }
111
112 /**
113 * Initialises the components.
114 *
115 * @param duration Duration to initialise to
116 */
117 private void initComponents(long duration) {
/*
P/P * Method: void initComponents(long)
*
* Preconditions:
* duration in {-185_542_587_273_599..371_085_174_374_399}
*
* Postconditions:
* this.cancelButton == One-of{&new JButton(initComponents#2), &new JButton(initComponents#1)}
* this.cancelButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.daysSpinner == &new JSpinner(initComponents#3)
* this.hoursSpinner == &new JSpinner(initComponents#5)
* this.minutesSpinner == &new JSpinner(initComponents#7)
* this.okButton == One-of{&new JButton(initComponents#1), &new JButton(initComponents#2)}
* this.okButton in Addr_Set{&new JButton(initComponents#1),&new JButton(initComponents#2)}
* this.secondsSpinner == &new JSpinner(initComponents#9)
* new JButton(initComponents#1) num objects == 1
* new JButton(initComponents#2) num objects == 1
* ...
*/
118 orderButtons(new JButton(), new JButton());
119 daysSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 31, 1));
120 hoursSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 24, 1));
121 minutesSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 60, 1));
122 secondsSpinner = new JSpinner(new SpinnerNumberModel(0, 0, 60, 1));
123
124 daysSpinner.setValue((int) duration / 86400);
125 duration = (duration % 86400);
126 hoursSpinner.setValue((int) duration / 3600);
127 duration = (duration % 3600);
128 minutesSpinner.setValue((int) duration / 60);
129 duration = (duration % 60);
130 secondsSpinner.setValue((int) duration);
131 }
132
133 /**
134 * Adds the listeners.
135 */
136 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* init'ed(this.window)
* this.cancelButton != null
* this.okButton != null
*
* Postconditions:
* this.window == old this.window
* init'ed(this.window)
*/
137 getOkButton().addActionListener(this);
138 getCancelButton().addActionListener(this);
139
140 setWindow(window);
141 }
142
143 /**
144 * Lays out the components.
145 */
146 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.daysSpinner)
* init'ed(this.hoursSpinner)
* init'ed(this.minutesSpinner)
* init'ed(this.secondsSpinner)
* (soft) init'ed(this.cancelButton)
* (soft) init'ed(this.okButton)
*/
147 setLayout(new MigLayout("hidemode 3, pack"));
148
149 add(new JLabel("Days: "), "split 8");
150 add(daysSpinner);
151 add(new JLabel("Hours: "));
152 add(hoursSpinner);
153 add(new JLabel("Minutes: "));
154 add(minutesSpinner);
155 add(new JLabel("Seconds: "));
156 add(secondsSpinner, "wrap");
157 add(getLeftButton(), "split 2, sgx button, right");
158 add(getRightButton(), "sgx button, right");
159
160 setDefaultCloseOperation(DurationEditor.DISPOSE_ON_CLOSE);
161 pack();
162 setResizable(false);
163 setVisible(true);
164 }
165
166 /** {@inheritDoc}
167 *
168 * @param e Action event
169 */
170 @Override
171 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* init'ed(this.okButton)
* (soft) this.daysSpinner != null
* (soft) this.hoursSpinner != null
* (soft) this.listeners != null
* (soft) this.minutesSpinner != null
* (soft) this.secondsSpinner != null
*/
172 if (e.getSource() == getOkButton()) {
173 fireDurationListener(getDuration());
174 }
175 dispose();
176 }
177
178 /**
179 * Returns the duration currently represented by this duration editor.
180 *
181 * @return Current duration (in seconds)
182 */
183 public int getDuration() {
/*
P/P * Method: int getDuration()
*
* Preconditions:
* this.daysSpinner != null
* this.hoursSpinner != null
* this.minutesSpinner != null
* this.secondsSpinner != null
*
* Presumptions:
* java.lang.Number:intValue(...)@187 in {-107_374_182..107_374_182}
* java.lang.Number:intValue(...)@187*60 + java.lang.Number:intValue(...)@186 in {-231..232-1}
* java.lang.Number:intValue(...)@187*60 + java.lang.Number:intValue(...)@186 + java.lang.Number:intValue(...)@188*3_600 in {-231..232-1}
* java.lang.Number:intValue(...)@187*60 + java.lang.Number:intValue(...)@186 + java.lang.Number:intValue(...)@188*3_600 + java.lang.Number:intValue(...)@189*86_400 in {-231..232-1}
* java.lang.Number:intValue(...)@188 in {-1_789_569..1_789_569}
* ...
*
* Postconditions:
* init'ed(return_value)
*/
184 int duration = 0;
185
186 duration += ((Number) secondsSpinner.getValue()).intValue();
187 duration += (((Number) minutesSpinner.getValue())).intValue() * 60;
188 duration += (((Number) hoursSpinner.getValue())).intValue() * 60 * 60;
189 duration += (((Number) daysSpinner.getValue())).intValue() * 60 * 60 *
190 24;
191
192 return duration;
193 }
194
195 /**
196 * Adds a DurationListener to the listener list.
197 *
198 * @param listener Listener to add
199 */
200 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}
*/
201 synchronized (listeners) {
202 if (listener == null) {
203 return;
204 }
205 listeners.add(DurationListener.class, listener);
206 }
207 }
208
209 /**
210 * Removes a DurationListener from the listener list.
211 *
212 * @param listener Listener to remove
213 */
214 public void removeDurationListener(final DurationListener listener) {
/*
P/P * Method: void removeDurationListener(DurationListener)
*
* Preconditions:
* this.listeners != null
*/
215 listeners.remove(DurationListener.class, listener);
216 }
217
218 /**
219 * Fires the duration updated method on all listeners.
220 *
221 * @param newDuration New duration
222 */
223 protected void fireDurationListener(final int newDuration) {
/*
P/P * Method: void fireDurationListener(int)
*
* Preconditions:
* this.listeners != null
*
* Presumptions:
* com.dmdirc.util.ListenerList:get(...)@224 != null
* java.util.Iterator:next(...)@224 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@224: {0}, {1}
*/
224 for (DurationListener listener : listeners.get(DurationListener.class)) {
225 listener.durationUpdated(newDuration);
226 }
227 }
228
229 /**
230 * Sets the Parent window.
231 *
232 * @param window Parent window
233 */
234 public void setWindow(final Window window) {
/*
P/P * Method: void setWindow(Window)
*
* Postconditions:
* this.window == window
* init'ed(this.window)
*
* Test Vectors:
* window: Addr_Set{null}, Inverse{null}
*/
235 this.window = window;
236
237 if (window != null) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.durationeditor.DurationEditor$1(DurationEditor)
*/
238 window.addWindowListener(new WindowAdapter() {
239
240 /** {@inheritDoc} */
241 @Override
242 public void windowClosed(WindowEvent e) {
/*
P/P * Method: void windowClosed(WindowEvent)
*/
243 dispose();
244 }
245 });
246 }
247 }
248 }
SofCheck Inspector Build Version : 2.17854
| DurationEditor.java |
2009-Jun-25 01:54:24 |
| DurationEditor.class |
2009-Sep-02 17:04:15 |
| DurationEditor$1.class |
2009-Sep-02 17:04:15 |