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  package com.atlassian.theplugin.idea.config;
17  
18  import com.atlassian.theplugin.commons.UiTaskExecutor;
19  import com.atlassian.theplugin.commons.bamboo.BambooServerFacade;
20  import com.atlassian.theplugin.commons.cfg.ProjectConfiguration;
21  import com.atlassian.theplugin.commons.cfg.ServerCfg;
22  import com.atlassian.theplugin.commons.cfg.UserCfg;
23  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
24  import com.atlassian.theplugin.commons.fisheye.FishEyeServerFacade;
25  import com.atlassian.theplugin.configuration.WorkspaceConfigurationBean;
26  import com.atlassian.theplugin.idea.AboutForm;
27  import com.atlassian.theplugin.idea.Constants;
28  import com.atlassian.theplugin.idea.IdeaHelper;
29  import com.atlassian.theplugin.idea.config.serverconfig.ServerConfigPanel;
30  import com.atlassian.theplugin.jira.JIRAServerFacade;
31  import com.intellij.openapi.application.ApplicationManager;
32  import com.intellij.openapi.application.ModalityState;
33  import com.intellij.openapi.project.Project;
34  import com.intellij.openapi.ui.DialogWrapper;
35  import com.intellij.openapi.ui.Messages;
36  import org.jetbrains.annotations.NotNull;
37  
38  import javax.swing.*;
39  import javax.swing.event.ChangeEvent;
40  import javax.swing.event.ChangeListener;
41  import java.awt.*;
42  
43  public class ProjectConfigurationPanel extends JPanel {
44  	private final FooterPanel footerPanel = new FooterPanel();
45  	private final JTabbedPane contentPanel = new JTabbedPane();
46  	private final ServerConfigPanel serverConfigPanel;
47  	private final ProjectDefaultsConfigurationPanel defaultsConfigurationPanel;
48  	private final AboutForm aboutBox;
49  
50  	private Project project;
51  	private ProjectConfiguration projectConfiguration;
52  	private UserCfg defaultCredentials;
53  	private boolean defaultCredentialsAsked;
54      private WorkspaceConfigurationBean projectConfigurationBean;
55  
56      private static final int WIDTH = 800;
57  
58  	private static final int HEIGHT = 700;
59  
60  	public ProjectConfiguration getProjectConfiguration() {
61  		serverConfigPanel.saveData();
62  		return projectConfiguration;
63  	}
64  
65  	public UserCfg getDefaultCredentials() {
66  		return serverConfigPanel.getDefaultUser();
67  	}
68  
69  	public ProjectConfigurationPanel(@NotNull final Project project,
70                                       @NotNull final ProjectConfiguration projectConfiguration,
71                                       @NotNull final CrucibleServerFacade crucibleServerFacade,
72                                       @NotNull final FishEyeServerFacade fishEyeServerFacade,
73                                       final BambooServerFacade bambooServerFacade,
74                                       final JIRAServerFacade jiraServerFacade,
75                                       @NotNull final UiTaskExecutor uiTaskExecutor, final ServerCfg selectedServer,
76                                       /*final IntelliJProjectCfgManager projectCfgManager, */
77                                       @NotNull final UserCfg defaultCredentials,
78                                       final boolean defaultCredentialsAsked,
79                                       WorkspaceConfigurationBean projectConfigurationBean) {
80  		this.project = project;
81  		this.projectConfiguration = projectConfiguration;
82  		this.defaultCredentials = defaultCredentials;
83  		this.defaultCredentialsAsked = defaultCredentialsAsked;
84          this.projectConfigurationBean = projectConfigurationBean;
85          serverConfigPanel = new ServerConfigPanel(project, defaultCredentials,
86  				projectConfiguration, selectedServer, defaultCredentialsAsked);
87  		defaultsConfigurationPanel = new ProjectDefaultsConfigurationPanel(project, projectConfiguration, crucibleServerFacade,
88  				fishEyeServerFacade, bambooServerFacade, jiraServerFacade, uiTaskExecutor, defaultCredentials);
89  		aboutBox = new AboutForm();
90  
91  		initLayout();
92  	}
93  
94  	public boolean isDefaultCredentialsAsked() {
95  		return this.defaultCredentialsAsked;
96  	}
97  
98  	private void initLayout() {
99  		setLayout(new BorderLayout());
100 		setMinimumSize(new Dimension(WIDTH, HEIGHT));
101 		setPreferredSize(getMinimumSize());
102 
103 		contentPanel.setOpaque(true);
104 		contentPanel.setBackground(new Color(Constants.BG_COLOR_R, Constants.BG_COLOR_G, Constants.BG_COLOR_B));
105 		contentPanel.getModel().addChangeListener(new ChangeListener() {
106 			public void stateChanged(final ChangeEvent e) {
107 				if (contentPanel.getSelectedComponent() == defaultsConfigurationPanel) {
108 					defaultsConfigurationPanel.setData(projectConfiguration);
109 				}
110 			}
111 		});
112 
113 		// add servers tab
114 		contentPanel.add(serverConfigPanel.getTitle(), serverConfigPanel);
115 		contentPanel.add("Defaults", defaultsConfigurationPanel);
116 		contentPanel.add("About", aboutBox.getRootPane());
117 
118 		add(contentPanel, BorderLayout.CENTER);
119 		add(footerPanel, BorderLayout.SOUTH);
120 	}
121 
122 	public void saveData(boolean finalizeData) {
123 		if (finalizeData) {
124 			serverConfigPanel.finalizeData();
125 		}
126 		serverConfigPanel.saveData();
127 		if (!projectConfiguration.isDefaultFishEyeServerValid()) {
128 			projectConfiguration.setDefaultFishEyeServerId(null);
129             projectConfigurationBean.setDefaultFishEyeServerAsked(false);
130 			Messages.showInfoMessage(this, "Default FishEye server settings have been cleared.", "Information");
131 		}
132 		if (!projectConfiguration.isDefaultCrucibleServerValid()) {
133 			projectConfiguration.setDefaultCrucibleServerId(null);
134 			Messages.showInfoMessage(this, "Default Crucible server settings have been cleared.", "Information");
135 		}
136 
137 		if (!projectConfiguration.isDefaultJiraServerValid()) {
138 			projectConfiguration.setDefaultJiraServerId(null);
139             projectConfigurationBean.setDefaultJiraServerAsked(false);
140 			Messages.showInfoMessage(this, "Default JIRA server settings have been cleared.", "Information");
141 		}
142 
143 
144 	}
145 
146 
147 	public void setData(ProjectConfiguration aProjectConfiguration) {
148 		projectConfiguration = aProjectConfiguration;
149 		serverConfigPanel.setData(projectConfiguration.getServers());
150 		defaultsConfigurationPanel.setData(projectConfiguration);
151 	}
152 
153 	public void askForDefaultCredentials() {
154 		final ServerCfg serverCfg = serverConfigPanel.getSelectedServer();
155 		final boolean alreadyExists =
156 				null != IdeaHelper.getProjectCfgManager(project)
157                         .getProjectConfiguration().getServerCfg(serverCfg.getServerId());
158 
159 		ApplicationManager.getApplication().executeOnPooledThread(new Runnable() {
160 			public void run() {
161 
162 				final ModalityState modalityState = ModalityState.stateForComponent(ProjectConfigurationPanel.this);
163 				ApplicationManager.getApplication().invokeLater(new Runnable() {
164 					public void run() {
165 
166 						if (!alreadyExists && !defaultCredentialsAsked
167 								&& (defaultCredentials == null
168 								|| (defaultCredentials.getPassword().equals("")
169 								&& defaultCredentials.getPassword().equals(""))
170 								&& serverCfg.getUsername().length() > 0)) {
171 							int answer = Messages.showYesNoDialog(project,
172 									"<html>Do you want to set server <b>" + serverCfg.getName()
173 											+ "</b> <i>username</i> and <i>password</i>"
174 											+ " as default credentials for the Atlassian IntelliJ Connector?</html>",
175 									"Set as default",
176 									Messages.getQuestionIcon());
177 							if (answer == DialogWrapper.OK_EXIT_CODE) {
178 								UserCfg credentials = new UserCfg(serverCfg.getUsername(),
179 										serverCfg.getPassword(), true);
180 								IdeaHelper.getProjectCfgManager(project).setDefaultCredentials(credentials);
181 								defaultsConfigurationPanel.setDefaultCredentials(credentials);
182 
183 
184 							}
185 							IdeaHelper.getProjectCfgManager(project).setDefaultCredentialsAsked(true);
186 							ProjectConfigurationPanel.this.defaultCredentialsAsked = true;
187 						}
188 					}
189 				}, modalityState);
190 			}
191 		});
192 	}
193 }