Skip to content

Commit 2de6a9f

Browse files
author
Mitja Leino
committed
Simplify and unify validations
1 parent 6c8f0c0 commit 2de6a9f

File tree

5 files changed

+26
-96
lines changed

5 files changed

+26
-96
lines changed

src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerView.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,8 @@ public void setInitializationOptionsContent(String initializationOptionsContent)
420420
}
421421

422422
@Override
423-
protected @Nullable ValidationInfo doValidate() {
424-
// Server name is not editable for existing language servers
425-
return this.languageServerPanel.getCommandLine().getValidationInfo();
423+
public @NotNull List<ValidationInfo> doValidateAll() {
424+
return languageServerPanel.doValidateAll();
426425
}
427426

428427
@Override

src/main/java/com/redhat/devtools/lsp4ij/settings/ui/CommandLineWidget.java

+8-17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.redhat.devtools.lsp4ij.LanguageServerBundle;
2121

2222
import javax.swing.border.Border;
23+
import java.util.List;
2324

2425
/**
2526
* Command line widget used to fill the command to start a language
@@ -36,28 +37,18 @@ public CommandLineWidget() {
3637
super.setFont(JBFont.regular());
3738
super.getEmptyText().setText(LanguageServerBundle.message("language.server.command.emptyText"));
3839
this.normalBorder = this.getBorder();
39-
addListeners(this);
4040
}
4141

4242
@Override
43-
public void validateInput() {
44-
if (isValid()) {
45-
this.setBorder(normalBorder);
46-
} else {
43+
public void validate(List<ValidationInfo> validations) {
44+
boolean valid = true;
45+
if (getDocument() != null && getText().isBlank()) {
4746
setErrorBorder(this);
47+
valid = false;
48+
validations.add(new ValidationInfo(errorMessage, this));
4849
}
49-
}
50-
51-
@Override
52-
public ValidationInfo getValidationInfo() {
53-
if (isValid()) {
54-
return null;
50+
if (valid) {
51+
this.setBorder(normalBorder);
5552
}
56-
return new ValidationInfo(errorMessage, this);
57-
}
58-
59-
@Override
60-
public boolean isValid() {
61-
return getDocument() != null && !getText().isBlank();
6253
}
6354
}

src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java

+6-14
Original file line numberDiff line numberDiff line change
@@ -254,25 +254,17 @@ public ComboBox<ErrorReportingKind> getErrorReportingKindCombo() {
254254

255255
public @NotNull List<ValidationInfo> doValidateAll() {
256256
List<ValidationInfo> validations = new ArrayList<>();
257-
var serverName = getServerName();
258-
if (serverName != null) {
259-
addValidationInfo(serverName.getValidationInfo(), validations);
257+
var serverNameWidget = getServerName();
258+
if (serverNameWidget != null) {
259+
serverNameWidget.validate(validations);
260260
}
261-
var commandLine = getCommandLine();
262-
if (commandLine != null) {
263-
addValidationInfo(commandLine.getValidationInfo(), validations);
261+
var commandLineWidget = getCommandLine();
262+
if (commandLineWidget != null) {
263+
commandLineWidget.validate(validations);
264264
}
265265
return validations;
266266
}
267267

268-
private void addValidationInfo(ValidationInfo validationInfo, List<ValidationInfo> validations) {
269-
if (validationInfo == null) {
270-
return;
271-
}
272-
validations.add((validationInfo));
273-
}
274-
275-
276268
private void addValidator(JTextComponent textComponent) {
277269
textComponent.getDocument().addDocumentListener(new DocumentAdapter() {
278270
@Override

src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ServerNameWidget.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.redhat.devtools.lsp4ij.LanguageServerBundle;
1717

1818
import javax.swing.border.Border;
19+
import java.util.List;
1920

2021
/**
2122
* Server name widget that contains the server name when creating a new LS configuration
@@ -26,24 +27,19 @@ public class ServerNameWidget extends JBTextField implements ValidatableConsoleW
2627

2728
public ServerNameWidget() {
2829
this.originalBorder = this.getBorder();
29-
addListeners(this);
3030
}
3131

3232
@Override
33-
public void validateInput() {
34-
if (isValid()) {
35-
this.setBorder(originalBorder);
36-
} else {
33+
public void validate(List<ValidationInfo> validations) {
34+
boolean isValid = true;
35+
if (getDocument() != null && getText().isBlank()) {
3736
setErrorBorder(this);
37+
isValid = false;
38+
validations.add(new ValidationInfo(errorMessage, this));
3839
}
39-
}
40-
41-
@Override
42-
public ValidationInfo getValidationInfo() {
43-
if (isValid()) {
44-
return null;
40+
if (isValid) {
41+
this.setBorder(originalBorder);
4542
}
46-
return new ValidationInfo(errorMessage, this);
4743
}
4844

4945
@Override

src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableConsoleWidget.java

+2-50
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@
1212
package com.redhat.devtools.lsp4ij.settings.ui;
1313

1414
import com.intellij.openapi.ui.ValidationInfo;
15-
import com.intellij.ui.DocumentAdapter;
1615
import com.intellij.ui.JBColor;
17-
import com.intellij.ui.components.JBTextArea;
18-
import com.intellij.ui.components.JBTextField;
1916
import com.intellij.util.ui.JBUI;
20-
import org.jetbrains.annotations.NotNull;
2117

2218
import javax.swing.*;
23-
import javax.swing.event.DocumentEvent;
19+
import java.util.List;
2420
import java.awt.*;
25-
import java.awt.event.FocusAdapter;
26-
import java.awt.event.FocusEvent;
2721

2822
/**
2923
* A shared interface meant to simplify creating validatable components used in
@@ -40,47 +34,5 @@ default void setErrorBorder(JComponent jComponent) {
4034
jComponent.setBorder(JBUI.Borders.customLine(color, 1));
4135
}
4236

43-
/**
44-
* Add listeners that handle input validation. The first instance of validation should run on focus
45-
* and then on each change
46-
* @param jComponent interface implementor (e.g. addListeners(this);)
47-
*/
48-
default void addListeners(JComponent jComponent) {
49-
jComponent.addFocusListener(new FocusAdapter() {
50-
@Override
51-
public void focusGained(FocusEvent e) {
52-
validateInput();
53-
super.focusGained(e);
54-
}
55-
});
56-
57-
DocumentAdapter adapter = new DocumentAdapter() {
58-
@Override
59-
protected void textChanged(@NotNull DocumentEvent e) {
60-
validateInput();
61-
}
62-
};
63-
if (jComponent instanceof JBTextField jbTextField) {
64-
jbTextField.getDocument().addDocumentListener(adapter);
65-
} else if (jComponent instanceof JBTextArea jbTextArea) {
66-
jbTextArea.getDocument().addDocumentListener(adapter);
67-
}
68-
}
69-
70-
/**
71-
* Overridable method that implements the field validation
72-
* Handles updating the style of the component for error/valid
73-
*/
74-
void validateInput();
75-
76-
/**
77-
* Overridable method that returns a ValidationInfo, is the component is not valid
78-
* @return a ValidationInfo element if component validation fails, null if component is valid
79-
*/
80-
ValidationInfo getValidationInfo();
81-
82-
/**
83-
* Overridable method that has to implement the validation check
84-
*/
85-
boolean isValid();
37+
void validate(List<ValidationInfo> validations);
8638
}

0 commit comments

Comments
 (0)