diff --git a/platform/platform-api/src/com/intellij/ui/components/fields/AbstractValueInputField.java b/platform/platform-api/src/com/intellij/ui/components/fields/AbstractValueInputField.java index bf3074c7429c..a8b4859de308 100644 --- a/platform/platform-api/src/com/intellij/ui/components/fields/AbstractValueInputField.java +++ b/platform/platform-api/src/com/intellij/ui/components/fields/AbstractValueInputField.java @@ -16,7 +16,7 @@ package com.intellij.ui.components.fields; import com.intellij.openapi.options.ConfigurationException; -import com.intellij.openapi.util.text.StringUtil; +import com.intellij.openapi.util.InvalidDataException; import com.intellij.ui.components.ValidatingTextField; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -44,53 +44,39 @@ public abstract class AbstractValueInputField extends ValidatingTextField { @Override protected String validateTextOnChange(String text, DocumentEvent e) { - ParseResult result = parseValue(text); - return result.errorMessage; + try { + parseValue(text); + return null; + } + catch (InvalidDataException ex) { + return ex.getMessage(); + } } @NotNull - protected abstract ParseResult parseValue(@Nullable String text); + protected abstract T parseValue(@Nullable String text); protected abstract String valueToString(@NotNull T value); protected abstract void assertValid(@NotNull T value); public void validateContent() throws ConfigurationException { - ParseResult result = parseValue(getText()); - if (!result.isValid()) { - throw new ConfigurationException((myName != null ? myName + " " : "") + result.errorMessage); + try { + parseValue(getText()); } - } - - public class ParseResult { - private T value; - private String errorMessage; - - public ParseResult(@NotNull T value) { - this.value = value; - } - - public ParseResult(@NotNull String errorMessage) { - this.errorMessage = errorMessage; - } - - public T getValue() { - return value; - } - - public String getErrorMessage() { - return errorMessage; - } - - public boolean isValid() { - return StringUtil.isEmpty(errorMessage); + catch (InvalidDataException ex) { + throw new ConfigurationException((myName != null ? myName + " " : "") + ex.getMessage()); } } @NotNull public T getValue() { - ParseResult result = parseValue(getText()); - return result.isValid() ? result.value : myDefaultValue; + try { + return parseValue(getText()); + } + catch (InvalidDataException ex) { + return myDefaultValue; + } } public void setValue(@NotNull T newValue) { diff --git a/platform/platform-api/src/com/intellij/ui/components/fields/CommaSeparatedIntegersField.java b/platform/platform-api/src/com/intellij/ui/components/fields/CommaSeparatedIntegersField.java index 145b092d9ee7..8c1ac14d14e6 100644 --- a/platform/platform-api/src/com/intellij/ui/components/fields/CommaSeparatedIntegersField.java +++ b/platform/platform-api/src/com/intellij/ui/components/fields/CommaSeparatedIntegersField.java @@ -15,6 +15,7 @@ */ package com.intellij.ui.components.fields; +import com.intellij.openapi.util.InvalidDataException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -37,24 +38,24 @@ public class CommaSeparatedIntegersField extends AbstractValueInputField>.ParseResult parseValue(@Nullable String text) { - if (text == null || text.isEmpty()) return new ParseResult(Collections.emptyList()); + protected List parseValue(@Nullable String text) { + if (text == null || text.isEmpty()) return Collections.emptyList(); String[] chunks = text.split("\\s*,\\s*"); List values = new ArrayList<>(chunks.length); for (String chunk : chunks) { try { int value = Integer.parseInt(chunk); if (value < myMinValue || value > myMaxValue) { - return new ParseResult("Value " + value + " is out of range " + myMinValue + ".." + myMaxValue); + throw new InvalidDataException("Value " + value + " is out of range " + myMinValue + ".." + myMaxValue); } values.add(value); } catch (NumberFormatException nfe) { - return new ParseResult("Value '" + chunk + "' is not an integer number"); + throw new InvalidDataException("Value '" + chunk + "' is not an integer number"); } } Collections.sort(values); - return new ParseResult(values); + return values; } @Override diff --git a/platform/platform-api/src/com/intellij/ui/components/fields/IntegerField.java b/platform/platform-api/src/com/intellij/ui/components/fields/IntegerField.java index ed029fcf1f9b..2cd6d7797c71 100644 --- a/platform/platform-api/src/com/intellij/ui/components/fields/IntegerField.java +++ b/platform/platform-api/src/com/intellij/ui/components/fields/IntegerField.java @@ -16,6 +16,7 @@ package com.intellij.ui.components.fields; import com.intellij.openapi.application.ApplicationBundle; +import com.intellij.openapi.util.InvalidDataException; import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -63,22 +64,22 @@ public class IntegerField extends AbstractValueInputField { @NotNull @Override - protected ParseResult parseValue(@Nullable String text) { + protected Integer parseValue(@Nullable String text) { try { if (StringUtil.isEmpty(text)) { if (!myCanBeEmpty) { - return new ParseResult(ApplicationBundle.message("integer.field.value.expected")); + throw new InvalidDataException(ApplicationBundle.message("integer.field.value.expected")); } - return new ParseResult(getDefaultValue()); + return getDefaultValue(); } int value = Integer.parseInt(text); if (value < myMinValue || value > myMaxValue) { - return new ParseResult(ApplicationBundle.message("integer.field.value.out.of.range", value, myMinValue, myMaxValue)); + throw new InvalidDataException((ApplicationBundle.message("integer.field.value.out.of.range", value, myMinValue, myMaxValue))); } - return new ParseResult(value); + return value; } catch (NumberFormatException nfe) { - return new ParseResult(ApplicationBundle.message("integer.field.value.not.a.number", text)); + throw new InvalidDataException((ApplicationBundle.message("integer.field.value.not.a.number", text))); } }