1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.atlassian.theplugin.idea;
17
18 import com.atlassian.theplugin.ConnectionWrapper;
19 import com.atlassian.theplugin.commons.remoteapi.ServerData;
20 import com.atlassian.theplugin.idea.ui.DialogWithDetails;
21 import com.atlassian.theplugin.util.Connector;
22 import com.atlassian.theplugin.util.PluginUtil;
23 import com.intellij.openapi.progress.ProgressIndicator;
24 import com.intellij.openapi.progress.Task;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.ui.Messages;
27 import static com.intellij.openapi.ui.Messages.showMessageDialog;
28 import org.apache.log4j.Category;
29 import org.jetbrains.annotations.NotNull;
30
31 import java.awt.*;
32
33
34
35
36 public class TestConnectionTask extends Task.Modal {
37
38 private static final int CHECK_CANCEL_INTERVAL = 500;
39 private final Category log = Category.getInstance(TestConnectionTask.class);
40 private final ConnectionWrapper testConnector;
41 private final TestConnectionProcessor processor;
42 private final ServerData serverData;
43 private boolean showOkMessage = true;
44 private boolean showErrorMessage = true;
45
46
47
48 public TestConnectionTask(Project currentProject, final Connector connectionTester,
49 ServerData serverData, final TestConnectionProcessor processor,
50 String title, boolean canBeCanceled, boolean showOkMessage, boolean showErrorMessage) {
51 this(currentProject, connectionTester, serverData, processor, title, canBeCanceled);
52 this.showOkMessage = showOkMessage;
53 this.showErrorMessage = showErrorMessage;
54 }
55
56 public TestConnectionTask(Project currentProject, final Connector connectionTester,
57 ServerData serverData, final TestConnectionProcessor processor,
58 String title, boolean canBeCanceled) {
59 super(currentProject, title, canBeCanceled);
60 this.processor = processor;
61 this.serverData = serverData;
62 testConnector = new ConnectionWrapper(connectionTester, serverData, "test thread");
63
64 }
65
66 @Override
67 public void run(@NotNull ProgressIndicator indicator) {
68
69 if (indicator == null) {
70 PluginUtil.getLogger().error("Progress Indicator is null in TestConnectionTask!!!");
71 System.out.println("Progress Indicator is null in TestConnectionTask!!!");
72 } else {
73 indicator.setText("Connecting...");
74 indicator.setFraction(0);
75 indicator.setIndeterminate(true);
76 }
77 testConnector.start();
78
79 while (testConnector.getConnectionState() == ConnectionWrapper.ConnectionState.NOT_FINISHED) {
80 try {
81 if (indicator.isCanceled()) {
82 testConnector.setInterrupted();
83
84 break;
85 } else {
86 java.lang.Thread.sleep(CHECK_CANCEL_INTERVAL);
87 }
88 } catch (InterruptedException e) {
89 log.info(e.getMessage());
90 }
91 }
92
93 ConnectionWrapper.ConnectionState state = testConnector.getConnectionState();
94 processor.setConnectionResult(state);
95 switch (testConnector.getConnectionState()) {
96 case FAILED:
97 EventQueue.invokeLater(new Runnable() {
98 public void run() {
99 if (showErrorMessage) {
100 DialogWithDetails.showExceptionDialog(getProject(),
101 serverData.getName() + " : " + testConnector.getErrorMessage(),
102 testConnector.getException(), HelpUrl.getHelpUrl(Constants.HELP_TEST_CONNECTION));
103 }
104 processor.onError(testConnector.getErrorMessage());
105 }
106 });
107 break;
108 case INTERUPTED:
109 log.debug("Cancel was pressed during 'Test Connection' operation");
110 break;
111 case SUCCEEDED:
112 EventQueue.invokeLater(new Runnable() {
113 public void run() {
114 if (showOkMessage) {
115 showMessageDialog(getProject(), "Connected successfully", "Connection OK",
116 Messages.getInformationIcon());
117 }
118 processor.onSuccess();
119 }
120 });
121 break;
122 default:
123 log.warn("Unexpected 'Test Connection' thread state: "
124 + testConnector.getConnectionState().toString());
125 }
126 }
127 }
128