diff options
author | Jonathan Bauer | 2018-11-27 17:45:34 +0100 |
---|---|---|
committer | Jonathan Bauer | 2018-11-27 17:45:34 +0100 |
commit | 5674f7e63f599ae71d54e50c1bdb53ef11b4d186 (patch) | |
tree | d83b58530e9ec9181c9f479d555596b753d2ce45 /dozentenmodul | |
parent | [client] fill new form fields on netshare selection (diff) | |
download | tutor-module-5674f7e63f599ae71d54e50c1bdb53ef11b4d186.tar.gz tutor-module-5674f7e63f599ae71d54e50c1bdb53ef11b4d186.tar.xz tutor-module-5674f7e63f599ae71d54e50c1bdb53ef11b4d186.zip |
[client] allow user to choose how long an image is to be extended for
Diffstat (limited to 'dozentenmodul')
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/ExpiryDateChooser.java | 97 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java | 25 |
2 files changed, 119 insertions, 3 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/ExpiryDateChooser.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/ExpiryDateChooser.java new file mode 100644 index 00000000..a4810d24 --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/ExpiryDateChooser.java @@ -0,0 +1,97 @@ +package org.openslx.dozmod.gui.helper; + +import java.awt.Component; +import java.util.Calendar; +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); + + public final static int DEFAULT_EXTENSION_DURATION = 14; + + private final static JPanel pane = new JPanel(); + private final static GridManager grdPane = new GridManager(pane, 3, true); + private final static JSlider sldDaysToExtend = new JSlider(JSlider.HORIZONTAL); + private final static QLabel lblNewDate = new QLabel(); + private final static QLabel lblNewDateLabel = new QLabel(); + private final static String txtNewDateLevel = "Neues Ablaufdatum: "; + static { + grdPane.add(new QLabel("Geben Sie ein um wieviele Tage diese Version(en) verlängert werden soll:"), 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); + } + + /** + * 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 askFutureExpiryDate(final Component parent, final Date startingDate) { + final Calendar startingDayMidnight = Calendar.getInstance(); + if (startingDate != null) { + startingDayMidnight.setTime(startingDate); + startingDayMidnight.set(Calendar.HOUR_OF_DAY, 23); + startingDayMidnight.set(Calendar.MINUTE, 59); + startingDayMidnight.set(Calendar.SECOND, 59); + lblNewDate.setText(FormatHelper.longDate( + DateTimeHelper.addDaysTo(startingDayMidnight.getTime(), DEFAULT_EXTENSION_DURATION))); + lblNewDateLabel.setText(txtNewDateLevel); + } + sldDaysToExtend.setValue(DEFAULT_EXTENSION_DURATION); + sldDaysToExtend.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + if (startingDate != null) { + Date newExpiryDate = DateTimeHelper.addDaysTo(startingDayMidnight.getTime(), + sldDaysToExtend.getValue()); + lblNewDate.setText(FormatHelper.longDate(newExpiryDate)); + } + } + }); + + int ret = JOptionPane.showConfirmDialog(parent, pane, "Ablaufdatum verlängern", + JOptionPane.OK_CANCEL_OPTION); + if (ret != 0) { + LOGGER.info("Canceled by user."); + return -1; + } + if (sldDaysToExtend.getValue() <= 0 + || sldDaysToExtend.getValue() > Session.getSatelliteConfig().maxImageValidityDays) { + LOGGER.error("Invalid date returned after asking user extension duration: " + + sldDaysToExtend.getValue()); + return -1; + } + return sldDaysToExtend.getValue(); + } +}
\ No newline at end of file diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java index 23f8ed8c..20e5c30c 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java @@ -14,6 +14,7 @@ import java.awt.event.WindowEvent; import java.nio.ByteBuffer; import java.util.Collections; import java.util.Comparator; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -44,6 +45,7 @@ import org.openslx.dozmod.gui.MainWindow; import org.openslx.dozmod.gui.changemonitor.AbstractControlWrapper; import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor; import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor.TextNotEmptyConstraint; +import org.openslx.dozmod.gui.helper.ExpiryDateChooser; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.helper.PopupMenu; import org.openslx.dozmod.gui.helper.UiFeedback; @@ -667,16 +669,34 @@ public class ImageDetailsWindow extends ImageDetailsWindowLayout implements UiFe /** * Extends the expiration date for given image versions to current date - * plus the configured maximum image validity days. + * plus the user-supplied duration. * * @param versions to extend the validity of */ private void extendVersionExpiry(List<ImageVersionDetails> versions) { + int daysToExtend = -1; + if (versions.size() > 1) { + // more than one version given, ask the user once and use the value for all versions. + daysToExtend = ExpiryDateChooser.askFutureExpiryDate(this, null); + if (daysToExtend == -1) + return; + } int count = 0; for (ImageVersionDetails img : versions) { + long currentExpireDate = img.expireTime; + if (new Date(currentExpireDate).before(new Date(Util.unixTime()))) { + // already expired, use today as starting date for the extension + currentExpireDate = Util.unixTime(); + } + if (daysToExtend == -1) { + daysToExtend = ExpiryDateChooser.askFutureExpiryDate(this, new Date(currentExpireDate * 1000)); + if (daysToExtend == -1) + return; + } + currentExpireDate += daysToExtend * 86400L; try { ThriftManager.getSatClient().setImageVersionExpiry(Session.getSatelliteToken(), img.versionId, - Util.unixTime() + Session.getSatelliteConfig().maxImageValidityDays * 86400); + currentExpireDate); count++; } catch (TException e) { ThriftError.showMessage(this, LOGGER, e, @@ -787,7 +807,6 @@ public class ImageDetailsWindow extends ImageDetailsWindowLayout implements UiFe changeMonitor.reset(); // finally do show it all setVisible(true); - } /** |