1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
package org.openslx.dozmod.gui.helper;
import java.awt.Component;
import java.util.Date;
import javax.swing.Box;
import javax.swing.DefaultBoundedRangeModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.apache.log4j.Logger;
import org.openslx.dozmod.gui.control.QLabel;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.util.FormatHelper;
public class ExpiryDateChooser {
private static final Logger LOGGER = Logger.getLogger(ExpiryDateChooser.class);
/**
* Helper to ask the user for the number of days to extend a date with. If
* startingDate is given, it will preview the starting date + selected
* number of days. If startingDate is null, no preview will be done.
* Returns the numbers of days selected.
*
* @param parent component to center the dialog on.
* @param starting date to add the user selected duration on to. Only needed
* for live preview of the future date.
* @return the number of days the user selected.
*/
public static int askFutureExpiryDuration(final Component parent, final Date startingDate) {
ExpiryDateChooserWindow win = new ExpiryDateChooserWindow(parent);
int ret = win.query(startingDate);
if (ret <= 0
|| ret > Session.getSatelliteConfig().maxImageValidityDays) {
LOGGER.error("Invalid date returned after asking user extension duration: "
+ ret);
return -1;
}
return ret;
}
}
/**
* Internal layout class.
*/
class ExpiryDateChooserWindow extends JPanel {
/**
* Version for serialization.
*/
private static final long serialVersionUID = 7300013126671050102L;
public static final int DEFAULT_EXTENSION_DURATION = 14;
private final JPanel pane = new JPanel();
private final GridManager grdPane = new GridManager(pane, 3, true);
private final JSlider sldDaysToExtend = new JSlider(JSlider.HORIZONTAL);
private final QLabel lblNewDate = new QLabel();
private final QLabel lblNewDateLabel = new QLabel(I18n.HELPER.getString("ExpiryDateChooser.Label.newDate.text"));
private final Component parent;
public ExpiryDateChooserWindow(final Component parent) {
this.parent = parent;
grdPane.add(new QLabel(I18n.HELPER.getString("ExpiryDateChooser.Label.daysToExtend.text")), 3)
.fill(true, true)
.expand(true, true);
grdPane.nextRow();
sldDaysToExtend.setModel(new DefaultBoundedRangeModel(DEFAULT_EXTENSION_DURATION, 0, 1,
Session.getSatelliteConfig().maxImageValidityDays));
grdPane.add(sldDaysToExtend, 3).fill(true, false).expand(true, false);
grdPane.nextRow();
grdPane.add(lblNewDateLabel);
grdPane.add(Box.createGlue()).fill(true, false).expand(true, false);
grdPane.add(lblNewDate).fill(true, true).expand(true, true);
grdPane.finish(true);
}
public int query(final Date startingDate) {
// show bottom date preview if only one version was selected
lblNewDate.setText(startingDate == null ? ""
: FormatHelper.longDate(
DateTimeHelper.addDaysTo(startingDate, DEFAULT_EXTENSION_DURATION)));
sldDaysToExtend.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (startingDate == null)
return;
Date current = DateTimeHelper.addDaysTo(startingDate, sldDaysToExtend.getValue());
lblNewDate.setText(FormatHelper.longDate(current));
}
});
sldDaysToExtend.setValue(DEFAULT_EXTENSION_DURATION);
int ret = JOptionPane.showConfirmDialog(parent, pane,
I18n.HELPER.getString("ExpiryDateChooser.ConfirmDialog.ret.title"), JOptionPane.OK_CANCEL_OPTION);
if (ret != 0)
return -1;
return sldDaysToExtend.getValue();
}
}
|