Skip to content

Commit 6aa1167

Browse files
committed
change environments in background
Signed-off-by: shalom <[email protected]>
1 parent a702b00 commit 6aa1167

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

ide-common/src/main/java/org/digma/intellij/plugin/analytics/AnalyticsService.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.intellij.openapi.Disposable;
55
import com.intellij.openapi.diagnostic.Logger;
6+
import com.intellij.openapi.progress.ProgressIndicator;
7+
import com.intellij.openapi.progress.Task;
68
import com.intellij.openapi.project.Project;
79
import org.digma.intellij.plugin.log.Log;
810
import org.digma.intellij.plugin.model.rest.errordetails.CodeObjectErrorDetails;
@@ -20,18 +22,14 @@
2022
import org.jetbrains.annotations.Nullable;
2123

2224
import javax.net.ssl.SSLException;
25+
import javax.swing.*;
2326
import java.io.Closeable;
2427
import java.io.IOException;
2528
import java.lang.reflect.InvocationHandler;
2629
import java.lang.reflect.InvocationTargetException;
2730
import java.lang.reflect.Method;
2831
import java.net.ConnectException;
29-
import java.util.ArrayList;
30-
import java.util.Arrays;
31-
import java.util.HashMap;
32-
import java.util.List;
33-
import java.util.Map;
34-
import java.util.Objects;
32+
import java.util.*;
3533
import java.util.stream.Collectors;
3634

3735
public class AnalyticsService implements Disposable {
@@ -77,7 +75,7 @@ public Environment getEnvironment() {
7775
}
7876

7977
//just replace the client and do not fire any events
80-
private void replaceClient(String url, String token) {
78+
private synchronized void replaceClient(String url, String token) {
8179
if (analyticsProviderProxy != null) {
8280
try {
8381
analyticsProviderProxy.close();
@@ -92,14 +90,31 @@ private void replaceClient(String url, String token) {
9290

9391
private void replaceClientAndFireChange(String url, String token) {
9492

95-
replaceClient(url,token);
93+
replaceClient(url, token);
9694

97-
List<String> envs = getEnvironments();
98-
if (envs == null) {
99-
envs = new ArrayList<>();
95+
var r = new Runnable() {
96+
@Override
97+
public void run() {
98+
List<String> envs = getEnvironments();
99+
if (envs == null) {
100+
envs = new ArrayList<>();
101+
}
102+
103+
environment.replaceEnvironmentsList(envs);
104+
}
105+
};
106+
107+
if (SwingUtilities.isEventDispatchThread()) {
108+
new Task.Backgroundable(project, "Digma: Environments list changed...") {
109+
@Override
110+
public void run(@NotNull ProgressIndicator indicator) {
111+
r.run();
112+
}
113+
}.queue();
114+
} else {
115+
r.run();
100116
}
101117

102-
environment.replaceEnvironmentsList(envs);
103118
}
104119

105120

ide-common/src/main/java/org/digma/intellij/plugin/analytics/Environment.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ public void setCurrent(String newEnv) {
6868
this.current = newEnv;
6969
persistenceData.setCurrentEnv(newEnv);
7070

71-
notifyEnvironmentChanged(oldEnv, newEnv);
71+
if (SwingUtilities.isEventDispatchThread()) {
72+
new Task.Backgroundable(project, "Digma: Environment Changed...") {
73+
@Override
74+
public void run(@NotNull ProgressIndicator indicator) {
75+
notifyEnvironmentChanged(oldEnv, newEnv);
76+
}
77+
}.queue();
78+
} else {
79+
notifyEnvironmentChanged(oldEnv, newEnv);
80+
}
81+
82+
7283
}
7384

7485

src/main/kotlin/org/digma/intellij/plugin/ui/common/EnvironmentsPanel.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import java.util.*
2424
import java.util.function.Function
2525
import javax.swing.Icon
2626
import javax.swing.JComponent
27+
import javax.swing.SwingUtilities
2728

2829
//need to remember we have two instances of this panel , one for the insights tab and one for the errors tab.
2930
//both instances need to be in sync with the selected button and the environments list.
@@ -47,7 +48,13 @@ class EnvironmentsPanel(
4748
//need to change also in the errors tab, and vice versa.
4849
val messageBusConnection = project.messageBus.connect()
4950
messageBusConnection.subscribe(EnvironmentChanged.ENVIRONMENT_CHANGED_TOPIC, EnvironmentChanged {
50-
select(it)
51+
if (SwingUtilities.isEventDispatchThread()) {
52+
select(it)
53+
} else {
54+
SwingUtilities.invokeLater {
55+
select(it)
56+
}
57+
}
5158
})
5259
Disposer.register(project, messageBusConnection)
5360
}

0 commit comments

Comments
 (0)