diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/LSPConsoleToolWindowPanel.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/LSPConsoleToolWindowPanel.java index 5fbc6e29e..70bf2e1d2 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/LSPConsoleToolWindowPanel.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/console/LSPConsoleToolWindowPanel.java @@ -43,6 +43,8 @@ import javax.swing.*; import javax.swing.tree.DefaultMutableTreeNode; import java.awt.*; +import java.util.HashSet; +import java.util.Set; /** * LSP consoles @@ -139,6 +141,8 @@ private class ConsoleContentPanel extends SimpleCardLayoutPanel { private ConsoleView consoleView; + private final Set settingsChangeListeners = new HashSet<>(); + public ConsoleContentPanel(DefaultMutableTreeNode key) { if (key instanceof LanguageServerTreeNode) { add(createDetailPanel((LanguageServerTreeNode) key), NAME_VIEW_DETAIL); @@ -185,6 +189,13 @@ private JComponent createDetailPanel(LanguageServerTreeNode key) { settings.setServerTrace(serverTrace); UserDefinedLanguageServerSettings.getInstance(project).setLanguageServerSettings(serverDefinition.id, settings); }); + + // Add a settings listener to keep serverTraceComboBox in sync with changes coming from the LSP settings page + // See https://github.com/redhat-developer/intellij-quarkus/issues/1062 + Runnable settingsChangeListener = createSettingsChangeListener(serverDefinition.id, serverTraceComboBox); + UserDefinedLanguageServerSettings.getInstance(getProject()).addChangeHandler(settingsChangeListener); + settingsChangeListeners.add(settingsChangeListener); + return FormBuilder.createFormBuilder() .setFormLeftIndent(10) .addComponent(createTitleComponent(serverDefinition), 1) @@ -193,6 +204,23 @@ private JComponent createDetailPanel(LanguageServerTreeNode key) { .getPanel(); } + + private Runnable createSettingsChangeListener(String id, ComboBox serverTraceComboBox) { + return new Runnable() { + @Override + public void run() { + if (isDisposed()) { + return; + } + UserDefinedLanguageServerSettings.LanguageServerDefinitionSettings settings = UserDefinedLanguageServerSettings.getInstance(project).getLanguageServerSettings(id); + ServerTrace newServerTrace = settings.getServerTrace(); + if (newServerTrace != null && !newServerTrace.equals(serverTraceComboBox.getSelectedItem())) { + serverTraceComboBox.setSelectedItem(newServerTrace); + } + } + }; + } + private JComponent createTitleComponent(LanguageServersRegistry.LanguageServerDefinition languageServerDefinition) { JLabel title = new JLabel(languageServerDefinition.getDisplayName()); String description = languageServerDefinition.description; @@ -224,6 +252,10 @@ public void showError(Throwable exception) { @Override public void dispose() { + for (Runnable settingsChangeListener : settingsChangeListeners) { + UserDefinedLanguageServerSettings.getInstance(getProject()).removeChangeHandler(settingsChangeListener); + } + settingsChangeListeners.clear(); super.dispose(); if (consoleView != null) { consoleView.dispose(); diff --git a/src/main/java/com/redhat/devtools/intellij/lsp4ij/settings/UserDefinedLanguageServerSettings.java b/src/main/java/com/redhat/devtools/intellij/lsp4ij/settings/UserDefinedLanguageServerSettings.java index a05df067d..3469637a0 100644 --- a/src/main/java/com/redhat/devtools/intellij/lsp4ij/settings/UserDefinedLanguageServerSettings.java +++ b/src/main/java/com/redhat/devtools/intellij/lsp4ij/settings/UserDefinedLanguageServerSettings.java @@ -16,12 +16,15 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.*; import com.intellij.openapi.project.Project; +import com.intellij.util.containers.ContainerUtil; import com.intellij.util.xmlb.annotations.Tag; import com.intellij.util.xmlb.annotations.XCollection; import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor; +import com.redhat.devtools.intellij.lsp4mp4ij.settings.UserDefinedMicroProfileSettings; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Map; import java.util.TreeMap; @@ -42,6 +45,8 @@ public class UserDefinedLanguageServerSettings implements PersistentStateCompone private volatile MyState myState = new MyState(); + private final List myChangeHandlers = ContainerUtil.createConcurrentList(); + public static UserDefinedLanguageServerSettings getInstance(@NotNull Project project) { return project.getService(UserDefinedLanguageServerSettings.class); } @@ -63,6 +68,7 @@ public LanguageServerDefinitionSettings getLanguageServerSettings(String languag public void setLanguageServerSettings(String languageSeverId, LanguageServerDefinitionSettings settings) { myState.myState.put(languageSeverId, settings); + fireStateChanged(); } public static class LanguageServerDefinitionSettings { @@ -108,4 +114,29 @@ public static class MyState { } + /** + * Adds the given changeHandler to the list of registered change handlers + * @param changeHandler the changeHandler to remove + */ + public void addChangeHandler(@NotNull Runnable changeHandler) { + myChangeHandlers.add(changeHandler); + } + + /** + * Removes the given changeHandler from the list of registered change handlers + * @param changeHandler the changeHandler to remove + */ + public void removeChangeHandler(@NotNull Runnable changeHandler) { + myChangeHandlers.remove(changeHandler); + } + + /** + * Notifies all registered change handlers when the state changed + */ + public void fireStateChanged() { + for (Runnable handler : myChangeHandlers) { + handler.run(); + } + } + }