1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.theplugin.remoteapi;
18
19 import com.atlassian.theplugin.cfg.CfgUtil;
20 import com.atlassian.theplugin.commons.cfg.ConfigurationListenerAdapter;
21 import com.atlassian.theplugin.commons.cfg.ServerCfg;
22 import com.atlassian.theplugin.commons.cfg.ServerId;
23 import com.atlassian.theplugin.commons.remoteapi.ProductServerFacade;
24 import com.atlassian.theplugin.commons.util.MiscUtil;
25 import com.atlassian.theplugin.idea.IdeaHelper;
26 import com.atlassian.theplugin.idea.PasswordDialog;
27 import com.atlassian.theplugin.idea.ThePluginApplicationComponent;
28 import com.atlassian.theplugin.idea.config.ProjectCfgManagerImpl;
29 import com.atlassian.theplugin.util.PluginUtil;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.ui.Messages;
32
33 import javax.swing.*;
34 import static javax.swing.JOptionPane.OK_CANCEL_OPTION;
35 import static javax.swing.JOptionPane.PLAIN_MESSAGE;
36 import java.util.Set;
37
38
39
40
41 public class MissingPasswordHandler implements MissingPasswordHandlerQueue.Handler {
42
43 private static boolean isDialogShown = false;
44
45 private final ProductServerFacade serverFacade;
46 private final ProjectCfgManagerImpl projectCfgManager;
47 private final Project project;
48 private final Set<ServerId> serversWithoutPassword = MiscUtil.buildHashSet();
49 private boolean shouldStop;
50
51
52 public MissingPasswordHandler(ProductServerFacade serverFacade, final ProjectCfgManagerImpl projectCfgManager,
53 final Project project) {
54 this.serverFacade = serverFacade;
55 this.projectCfgManager = projectCfgManager;
56 this.project = project;
57
58 projectCfgManager.getCfgManager().
59 addProjectConfigurationListener(CfgUtil.getProjectId(project), new LocalConfigurationListener());
60 }
61
62 private synchronized boolean shouldStop() {
63 return shouldStop;
64 }
65
66 public void go() {
67
68 if (!isDialogShown && !shouldStop()) {
69
70 isDialogShown = true;
71 boolean wasCanceled = false;
72
73 for (ServerCfg server : projectCfgManager.getAllEnabledServers(serverFacade.getServerType())) {
74 if (server.isComplete() || serversWithoutPassword.contains(server.getServerId())
75 || server.isUseDefaultCredentials()) {
76 continue;
77 }
78 PasswordDialog dialog = new PasswordDialog(server, serverFacade, project);
79 dialog.pack();
80 JPanel panel = dialog.getPasswordPanel();
81
82 int answer = JOptionPane.showConfirmDialog(JOptionPane.getRootFrame(), panel,
83 PluginUtil.getInstance().getName(), OK_CANCEL_OPTION, PLAIN_MESSAGE);
84
85 if (answer == JOptionPane.OK_OPTION) {
86 String password = dialog.getPasswordString();
87 Boolean shouldPasswordBeStored = dialog.getShouldPasswordBeStored();
88 server.setPassword(password);
89 server.setPasswordStored(shouldPasswordBeStored);
90 server.setUsername(dialog.getUserName());
91 } else {
92 wasCanceled = true;
93 serversWithoutPassword.add(server.getServerId());
94 }
95 }
96 ThePluginApplicationComponent appComponent = IdeaHelper.getAppComponent();
97 appComponent.rescheduleStatusCheckers(true);
98
99 if (wasCanceled) {
100 Messages.showMessageDialog(
101 "You can always change password by changing plugin settings (Preferences | IDE Settings | "
102 + PluginUtil.getInstance().getName() + ")", "Information", Messages.getInformationIcon());
103 }
104 isDialogShown = false;
105 }
106
107 }
108
109 private class LocalConfigurationListener extends ConfigurationListenerAdapter {
110 @Override
111 public void projectUnregistered() {
112 shouldStop = true;
113 }
114 }
115 }