Skip to content

Commit 9c3f189

Browse files
authored
Merge pull request #4354 from Flanker32/hanxiao/dispatch
Fix show messages dialog will throw exception in non-dispatch thread
2 parents 45e12a3 + 8ce0901 commit 9c3f189

File tree

1 file changed

+34
-12
lines changed
  • PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/helpers

1 file changed

+34
-12
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/src/com/microsoft/intellij/helpers/UIHelperImpl.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@
8888
import java.text.DecimalFormat;
8989
import java.util.HashMap;
9090
import java.util.Map;
91+
import java.util.concurrent.ExecutionException;
92+
import java.util.concurrent.FutureTask;
93+
import java.util.concurrent.RunnableFuture;
94+
import java.util.function.Supplier;
9195

9296
import static com.microsoft.azuretools.core.mvp.model.springcloud.SpringCloudIdHelper.getSubscriptionId;
9397
import static com.microsoft.intellij.helpers.arm.DeploymentPropertyViewProvider.TYPE;
@@ -151,13 +155,17 @@ public void run() {
151155
@Override
152156
public boolean showConfirmation(@NotNull String message, @NotNull String title, @NotNull String[] options,
153157
String defaultOption) {
154-
return Messages.showDialog(message, title, options, ArrayUtils.indexOf(options, defaultOption), null) == 0;
158+
return showConfirmation(null, message, title, options, defaultOption);
155159
}
156160

157161
@Override
158162
public boolean showConfirmation(@NotNull Component node, @NotNull String message, @NotNull String title, @NotNull String[] options, String defaultOption) {
159-
return Messages.showDialog(node, message, title, options, ArrayUtils.indexOf(options, defaultOption), null)
160-
== 0;
163+
return runFromDispatchThread(() -> 0 == Messages.showDialog(node,
164+
message,
165+
title,
166+
options,
167+
ArrayUtils.indexOf(options, defaultOption),
168+
null));
161169
}
162170

163171
@Override
@@ -719,27 +727,41 @@ public static String readableFileSize(long size) {
719727

720728
@Override
721729
public void showMessageDialog(Component component, String message, String title, Icon icon) {
722-
Messages.showMessageDialog(component, message, title, icon);
730+
DefaultLoader.getIdeHelper().invokeLater(() -> Messages.showMessageDialog(component, message, title, icon));
723731
}
724732

725733
@Override
726734
public int showConfirmDialog(Component component, String message, String title, String[] options,
727735
String defaultOption, Icon icon) {
728-
return Messages.showDialog(component,
729-
message,
730-
title,
731-
options,
732-
ArrayUtils.indexOf(options, defaultOption),
733-
icon);
736+
return runFromDispatchThread(() -> Messages.showDialog(component,
737+
message,
738+
title,
739+
options,
740+
ArrayUtils.indexOf(options, defaultOption),
741+
icon));
734742
}
735743

736744
@Override
737745
public boolean showYesNoDialog(Component component, String message, String title, Icon icon) {
738-
return Messages.showYesNoDialog(component, message, title, icon) == Messages.YES;
746+
return runFromDispatchThread(() -> Messages.showYesNoDialog(component, message, title, icon)
747+
== Messages.YES);
739748
}
740749

741750
@Override
742751
public String showInputDialog(Component component, String message, String title, Icon icon) {
743-
return Messages.showInputDialog(component, message, title, icon);
752+
return runFromDispatchThread(() -> Messages.showInputDialog(component, message, title, icon));
753+
}
754+
755+
private static <T> T runFromDispatchThread(Supplier<T> supplier) {
756+
if (ApplicationManager.getApplication().isDispatchThread()) {
757+
return supplier.get();
758+
}
759+
RunnableFuture<T> runnableFuture = new FutureTask<>(() -> supplier.get());
760+
ApplicationManager.getApplication().invokeLater(runnableFuture);
761+
try {
762+
return runnableFuture.get();
763+
} catch (InterruptedException | ExecutionException e) {
764+
return null;
765+
}
744766
}
745767
}

0 commit comments

Comments
 (0)