29
29
import java .awt .Container ;
30
30
import java .net .URI ;
31
31
import java .net .URISyntaxException ;
32
+ import java .util .Objects ;
33
+ import java .util .regex .Matcher ;
34
+ import java .util .regex .Pattern ;
32
35
import javax .swing .JCheckBox ;
33
36
import javax .swing .JComboBox ;
34
37
import javax .swing .JComponent ;
@@ -45,6 +48,10 @@ public class UctSettingsEditor extends SettingsEditor<UctRunConfiguration> {
45
48
private static final String LEARN_MORE_URI =
46
49
"https://docs.magento.com/user-guide/getting-started.html#product-editions" ;
47
50
private static final String LEARN_MORE_TEXT = "Learn more. " ;
51
+ @ SuppressWarnings ("checkstyle:LineLength" )
52
+ private static final String MAGENTO_VERSION_REGEX = "^(0|[1-9]\\ d*)\\ .(0|[1-9]\\ d*)\\ .(0|[1-9]\\ d*)(?:-((?:0|[1-9]\\ d*|\\ d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\ .(?:0|[1-9]\\ d*|\\ d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\ +([0-9a-zA-Z-]+(?:\\ .[0-9a-zA-Z-]+)*))?$" ;
53
+ public static final Pattern MAGENTO_VERSION_PATTERN
54
+ = Pattern .compile (MAGENTO_VERSION_REGEX );
48
55
49
56
private final Project project ;
50
57
private String uctExecutablePath ;
@@ -126,6 +133,14 @@ protected void resetEditorFrom(final @NotNull UctRunConfiguration uctRunConfigur
126
133
if (!uctRunConfiguration .getComingVersion ().isEmpty ()) {
127
134
final String storedComingVersion = uctRunConfiguration .getComingVersion ();
128
135
setSelectedValueByItsKey (comingVersion , storedComingVersion );
136
+
137
+ if (!Objects .requireNonNull (
138
+ comingVersion .getSelectedItem ()).toString ().equals (storedComingVersion )) {
139
+ final ComboBoxItemData customVersion
140
+ = new ComboBoxItemData (storedComingVersion , storedComingVersion );
141
+ comingVersion .addItem (customVersion );
142
+ comingVersion .setSelectedItem (customVersion );
143
+ }
129
144
}
130
145
131
146
if (uctRunConfiguration .getMinIssueLevel () > 0 ) {
@@ -148,8 +163,8 @@ protected void applyEditorTo(final @NotNull UctRunConfiguration uctRunConfigurat
148
163
uctRunConfiguration .setProjectRoot (projectRoot .getComponent ().getText ());
149
164
uctRunConfiguration .setModulePath (modulePath .getComponent ().getText ());
150
165
151
- final ComboBoxItemData selectedComingVersion =
152
- ( ComboBoxItemData ) comingVersion .getSelectedItem ();
166
+ final ComboBoxItemData selectedComingVersion
167
+ = getCorrectedSelectedItem ( comingVersion .getSelectedItem () );
153
168
154
169
if (selectedComingVersion == null ) {
155
170
uctRunConfiguration .setComingVersion ("" );
@@ -299,7 +314,7 @@ private String getStoredUctExecutablePath(
299
314
*/
300
315
@ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
301
316
private void initializeComboboxSources () {
302
- comingVersion .addItem ( new ComboBoxItemData ( "" , " Choose a target version") );
317
+ comingVersion .setToolTipText ( " Choose a target version" );
303
318
304
319
for (final String version : SupportedVersion .getSupportedVersions ()) {
305
320
comingVersion .addItem (new ComboBoxItemData (version , version ));
@@ -339,7 +354,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
339
354
});
340
355
341
356
comingVersion .addItemListener (event -> {
342
- final ComboBoxItemData selectedItem = ( ComboBoxItemData ) event .getItem ();
357
+ final ComboBoxItemData selectedItem = getCorrectedSelectedItem ( event .getItem () );
343
358
344
359
validateComingVersionField (selectedItem );
345
360
});
@@ -366,13 +381,37 @@ private void validateExecutablePathField() {
366
381
* @param selectedItem ComboBoxItemData
367
382
*/
368
383
private void validateComingVersionField (final ComboBoxItemData selectedItem ) {
384
+ final Matcher matcher = MAGENTO_VERSION_PATTERN .matcher (selectedItem .getText ());
385
+
369
386
if (selectedItem != null && selectedItem .getKey ().isEmpty ()) {
370
387
comingVersionError .setText ("Please, specify target version" );
388
+ } else if (!matcher .find ()) { // NOPMD
389
+ comingVersionError .setText ("Please, correct target version" );
371
390
} else {
372
391
comingVersionError .setText ("" );
373
392
}
374
393
}
375
394
395
+ /**
396
+ * Get existing item or select and convert custom version.
397
+ *
398
+ * @param selectedItem String|ComboBoxItemData
399
+ * @return ComboBoxItemData
400
+ */
401
+ private ComboBoxItemData getCorrectedSelectedItem (final Object selectedItem ) {
402
+ ComboBoxItemData selectedComingVersion ;
403
+
404
+ if (selectedItem instanceof ComboBoxItemData ) {
405
+ selectedComingVersion = (ComboBoxItemData ) selectedItem ;
406
+ } else {
407
+ final String customSelectedVersion = selectedItem .toString ();
408
+ selectedComingVersion
409
+ = new ComboBoxItemData (customSelectedVersion , customSelectedVersion );
410
+ }
411
+
412
+ return selectedComingVersion ;
413
+ }
414
+
376
415
/**
377
416
* Set selected combobox item by key.
378
417
*
0 commit comments