Use InvalidDataException (cleaner API)

This commit is contained in:
Rustam Vishnyakov
2017-09-11 18:59:17 +03:00
parent 92854f337f
commit bd3d6321d7
3 changed files with 32 additions and 44 deletions
@@ -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<T> 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) {
@@ -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<List<In
@NotNull
@Override
protected AbstractValueInputField<List<Integer>>.ParseResult parseValue(@Nullable String text) {
if (text == null || text.isEmpty()) return new ParseResult(Collections.emptyList());
protected List<Integer> parseValue(@Nullable String text) {
if (text == null || text.isEmpty()) return Collections.emptyList();
String[] chunks = text.split("\\s*,\\s*");
List<Integer> 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
@@ -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<Integer> {
@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)));
}
}