Skip to content

Commit a4b52c4

Browse files
ensonickernicPanel
authored andcommitted
Add a setting for the date-format string.
Fixes #4
1 parent 1045449 commit a4b52c4

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,7 @@ An option is available to prepend the generated filename with the current date.
5555
https://user-images.githubusercontent.com/720491/178076594-a29e99ac-e81d-426d-8bca-ff4e8bfc7013.mp4
5656

5757

58-
59-
58+
Another option lets you customize the date format. The default template is
59+
`yyyy-MM-DD_` which would expand to e.g. `2022-07-31_`.
60+
All the possible patterns are docuemnted
61+
[here](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns).

src/main/java/com/kernicpanel/RandomizerExtension.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.time.LocalDate;
99
import java.time.format.DateTimeFormatter;
10+
import java.time.temporal.UnsupportedTemporalTypeException;
1011
import java.util.Arrays;
1112
import java.util.Locale;
1213
import java.util.Random;
@@ -16,6 +17,8 @@ public class RandomizerExtension extends ControllerExtension {
1617
Faker faker = new Faker();
1718

1819
private SettableBooleanValue useDate;
20+
private SettableStringValue dateFormatTemplate;
21+
private static final String DEFAULT_DATE_FORMAT_TEMPLATE = "yyyy-MM-dd_";
1922

2023
private ControllerHost host;
2124
private DocumentState documentState;
@@ -51,6 +54,16 @@ public void init() {
5154

5255
useDate =
5356
host.getPreferences().getBooleanSetting("Prepend date for filename", "Random name", true);
57+
dateFormatTemplate =
58+
host.getPreferences().getStringSetting("Format string for date prefix", "Random name", 15, DEFAULT_DATE_FORMAT_TEMPLATE);
59+
dateFormatTemplate.addValueObserver(value -> {
60+
try {
61+
LocalDate.now().format(DateTimeFormatter.ofPattern(value));
62+
} catch (IllegalArgumentException | UnsupportedTemporalTypeException e) {
63+
dateFormatTemplate.set(DEFAULT_DATE_FORMAT_TEMPLATE);
64+
host.showPopupNotification("Invalid date format template.");
65+
}
66+
});
5467

5568
documentState
5669
.getSignalSetting("Select", "Randomize browser selection", "Select random item")
@@ -98,9 +111,15 @@ private NoArgsCallback randomName() {
98111

99112
if (useDate.get()) {
100113
LocalDate dateObj = LocalDate.now();
101-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
102-
String date = dateObj.format(formatter);
103-
generatedString = date + "_" + generatedString;
114+
String formattedDate;
115+
try {
116+
formattedDate = dateObj.format(DateTimeFormatter.ofPattern(dateFormatTemplate.get()));
117+
} catch (IllegalArgumentException | UnsupportedTemporalTypeException e) {
118+
// Should not happen, unless e.g. the config data got corrupted.
119+
dateFormatTemplate.set(DEFAULT_DATE_FORMAT_TEMPLATE);
120+
formattedDate = dateObj.format(DateTimeFormatter.ofPattern(dateFormatTemplate.get()));
121+
}
122+
generatedString = formattedDate + generatedString;
104123
}
105124

106125
filenameOutput.set(generatedString.replace(" ", "_").toLowerCase(Locale.ROOT));

0 commit comments

Comments
 (0)