View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Shows a dialog for each Bamboo server that has not the password set.
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  		// todo make sure the config listener is unregistered / not added every time missignpasswordhandler is created
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 }