diff --git a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java index 4bbb431db..e44b2ab1c 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/launching/ui/NewLanguageServerDialog.java @@ -7,6 +7,7 @@ * * Contributors: * Red Hat, Inc. - initial API and implementation + * Mitja Leino - Extend ValidatableDialog for validations ******************************************************************************/ package com.redhat.devtools.lsp4ij.launching.ui; diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerView.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerView.java index 9948527d7..e2950b028 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerView.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/LanguageServerView.java @@ -10,7 +10,7 @@ * * Contributors: * Red Hat Inc. - initial API and implementation - * Mitja Leino - Implement DialogWrapper for validations + * Mitja Leino - Extend ValidatableDialog for validations *******************************************************************************/ package com.redhat.devtools.lsp4ij.settings; @@ -19,7 +19,6 @@ import com.intellij.openapi.fileTypes.FileNameMatcher; import com.intellij.openapi.fileTypes.FileType; import com.intellij.openapi.project.Project; -import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.ValidationInfo; import com.intellij.ui.IdeBorderFactory; import com.intellij.util.ui.FormBuilder; diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/CommandLineWidget.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/CommandLineWidget.java index 82c22f42c..2a96caa44 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/CommandLineWidget.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/CommandLineWidget.java @@ -20,6 +20,7 @@ import com.redhat.devtools.lsp4ij.LanguageServerBundle; import javax.swing.border.Border; +import java.util.ArrayList; import java.util.List; /** @@ -41,14 +42,17 @@ public CommandLineWidget() { @Override public void validate(List validations) { - boolean valid = true; + List widgetValidations = new ArrayList<>(); + if (getDocument() != null && getText().isBlank()) { - setErrorBorder(this); - valid = false; - validations.add(new ValidationInfo(errorMessage, this)); + widgetValidations.add(new ValidationInfo(errorMessage, this)); } - if (valid) { + + if (widgetValidations.isEmpty()) { this.setBorder(normalBorder); + } else { + validations.addAll(widgetValidations); + setErrorBorder(this); } } } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java index 7b54f626a..e8528216d 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/LanguageServerPanel.java @@ -7,6 +7,7 @@ * * Contributors: * Red Hat, Inc. - initial API and implementation + * Mitja Leino - Add DialogWrapper for validations ******************************************************************************/ package com.redhat.devtools.lsp4ij.settings.ui; @@ -84,11 +85,14 @@ private void createUI(FormBuilder builder, JComponent description, EditionMode m addDebugTab(tabbedPane, mode); // Add validation - var serverName = getServerName(); - if (serverName != null) { - addValidator(serverName); + var serverNameWidget = getServerName(); + if (serverNameWidget != null) { + addValidator(serverNameWidget); + } + var commandLineWidget = getCommandLine(); + if (commandLineWidget != null) { + addValidator(getCommandLine()); } - addValidator(getCommandLine()); } private void addServerTab(JBTabbedPane tabbedPane, JComponent description, EditionMode mode) { diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ServerNameWidget.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ServerNameWidget.java index b9e9ecde5..6b2d3c089 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ServerNameWidget.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ServerNameWidget.java @@ -16,6 +16,7 @@ import com.redhat.devtools.lsp4ij.LanguageServerBundle; import javax.swing.border.Border; +import java.util.ArrayList; import java.util.List; /** @@ -31,14 +32,17 @@ public ServerNameWidget() { @Override public void validate(List validations) { - boolean isValid = true; + List widgetValidations = new ArrayList<>(); + if (getDocument() != null && getText().isBlank()) { - setErrorBorder(this); - isValid = false; - validations.add(new ValidationInfo(errorMessage, this)); + widgetValidations.add(new ValidationInfo(errorMessage, this)); } - if (isValid) { + + if (widgetValidations.isEmpty()) { this.setBorder(originalBorder); + } else { + validations.addAll(widgetValidations); + setErrorBorder(this); } } diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableConsoleWidget.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableConsoleWidget.java index e67031371..ab815af39 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableConsoleWidget.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableConsoleWidget.java @@ -34,5 +34,10 @@ default void setErrorBorder(JComponent jComponent) { jComponent.setBorder(JBUI.Borders.customLine(color, 1)); } + /** + * Runs validations on the widget and handles border styling + * @param validations the dialog wrapper validation list, + * adds the validations to the list if there are any errors + */ void validate(List validations); } \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableDialog.java b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableDialog.java index b38cfbf11..c9852f892 100644 --- a/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableDialog.java +++ b/src/main/java/com/redhat/devtools/lsp4ij/settings/ui/ValidatableDialog.java @@ -1,10 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat Inc. and others. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Mitja Leino - Initial API and implementation + *******************************************************************************/ package com.redhat.devtools.lsp4ij.settings.ui; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.DialogWrapper; +/** + * Shareable component for shared validations using DialogWrapper + */ public abstract class ValidatableDialog extends DialogWrapper { - public ValidatableDialog(Project project) { + protected ValidatableDialog(Project project) { super(project); }