Skip to content

Commit 43f88e9

Browse files
author
Mitja Leino
committed
Simplify and unify validations
1 parent 49ccd6c commit 43f88e9

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,8 @@ public void setInitializationOptionsContent(String initializationOptionsContent)
410410
}
411411

412412
@Override
413-
protected @Nullable ValidationInfo doValidate() {
414-
// Server name is not editable for existing language servers
415-
return this.languageServerPanel.getCommandLine().getValidationInfo();
413+
public @NotNull List<ValidationInfo> doValidateAll() {
414+
return languageServerPanel.doValidateAll();
416415
}
417416

418417
@Override

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

Lines changed: 8 additions & 17 deletions
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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -246,25 +246,17 @@ public ComboBox<ErrorReportingKind> getErrorReportingKindCombo() {
246246

247247
public @NotNull List<ValidationInfo> doValidateAll() {
248248
List<ValidationInfo> validations = new ArrayList<>();
249-
var serverName = getServerName();
250-
if (serverName != null) {
251-
addValidationInfo(serverName.getValidationInfo(), validations);
249+
var serverNameWidget = getServerName();
250+
if (serverNameWidget != null) {
251+
serverNameWidget.validate(validations);
252252
}
253-
var commandLine = getCommandLine();
254-
if (commandLine != null) {
255-
addValidationInfo(commandLine.getValidationInfo(), validations);
253+
var commandLineWidget = getCommandLine();
254+
if (commandLineWidget != null) {
255+
commandLineWidget.validate(validations);
256256
}
257257
return validations;
258258
}
259259

260-
private void addValidationInfo(ValidationInfo validationInfo, List<ValidationInfo> validations) {
261-
if (validationInfo == null) {
262-
return;
263-
}
264-
validations.add((validationInfo));
265-
}
266-
267-
268260
private void addValidator(JTextComponent textComponent) {
269261
textComponent.getDocument().addDocumentListener(new DocumentAdapter() {
270262
@Override

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

Lines changed: 8 additions & 12 deletions
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

Lines changed: 2 additions & 50 deletions
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)