2929import java .awt .Container ;
3030import java .net .URI ;
3131import java .net .URISyntaxException ;
32+ import java .util .Objects ;
33+ import java .util .regex .Matcher ;
34+ import java .util .regex .Pattern ;
3235import javax .swing .JCheckBox ;
3336import javax .swing .JComboBox ;
3437import javax .swing .JComponent ;
@@ -45,6 +48,10 @@ public class UctSettingsEditor extends SettingsEditor<UctRunConfiguration> {
4548 private static final String LEARN_MORE_URI =
4649 "https://docs.magento.com/user-guide/getting-started.html#product-editions" ;
4750 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 );
4855
4956 private final Project project ;
5057 private String uctExecutablePath ;
@@ -126,6 +133,14 @@ protected void resetEditorFrom(final @NotNull UctRunConfiguration uctRunConfigur
126133 if (!uctRunConfiguration .getComingVersion ().isEmpty ()) {
127134 final String storedComingVersion = uctRunConfiguration .getComingVersion ();
128135 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+ }
129144 }
130145
131146 if (uctRunConfiguration .getMinIssueLevel () > 0 ) {
@@ -148,8 +163,8 @@ protected void applyEditorTo(final @NotNull UctRunConfiguration uctRunConfigurat
148163 uctRunConfiguration .setProjectRoot (projectRoot .getComponent ().getText ());
149164 uctRunConfiguration .setModulePath (modulePath .getComponent ().getText ());
150165
151- final ComboBoxItemData selectedComingVersion =
152- ( ComboBoxItemData ) comingVersion .getSelectedItem ();
166+ ComboBoxItemData selectedComingVersion
167+ = getCorrectedSelectedItem ( comingVersion .getSelectedItem () );
153168
154169 if (selectedComingVersion == null ) {
155170 uctRunConfiguration .setComingVersion ("" );
@@ -299,7 +314,7 @@ private String getStoredUctExecutablePath(
299314 */
300315 @ SuppressWarnings ("PMD.AvoidInstantiatingObjectsInLoops" )
301316 private void initializeComboboxSources () {
302- comingVersion .addItem ( new ComboBoxItemData ( "" , " Choose a target version") );
317+ comingVersion .setToolTipText ( " Choose a target version" );
303318
304319 for (final String version : SupportedVersion .getSupportedVersions ()) {
305320 comingVersion .addItem (new ComboBoxItemData (version , version ));
@@ -339,7 +354,7 @@ protected void textChanged(final @NotNull DocumentEvent event) {
339354 });
340355
341356 comingVersion .addItemListener (event -> {
342- final ComboBoxItemData selectedItem = ( ComboBoxItemData ) event .getItem ();
357+ final ComboBoxItemData selectedItem = getCorrectedSelectedItem ( event .getItem () );
343358
344359 validateComingVersionField (selectedItem );
345360 });
@@ -366,13 +381,37 @@ private void validateExecutablePathField() {
366381 * @param selectedItem ComboBoxItemData
367382 */
368383 private void validateComingVersionField (final ComboBoxItemData selectedItem ) {
384+ final Matcher matcher = MAGENTO_VERSION_PATTERN .matcher (selectedItem .getText ());
385+
369386 if (selectedItem != null && selectedItem .getKey ().isEmpty ()) {
370387 comingVersionError .setText ("Please, specify target version" );
388+ } else if (!matcher .find ()) {
389+ comingVersionError .setText ("Please, correct target version" );
371390 } else {
372391 comingVersionError .setText ("" );
373392 }
374393 }
375394
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+
376415 /**
377416 * Set selected combobox item by key.
378417 *
0 commit comments