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.idea.config.serverconfig;
18  
19  import com.atlassian.theplugin.commons.ServerType;
20  import com.atlassian.theplugin.commons.bamboo.BambooServerFacade;
21  import com.atlassian.theplugin.commons.bamboo.BambooServerFacadeImpl;
22  import com.atlassian.theplugin.commons.cfg.*;
23  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
24  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
25  import com.atlassian.theplugin.commons.fisheye.FishEyeServerFacadeImpl;
26  import com.atlassian.theplugin.idea.Constants;
27  import com.atlassian.theplugin.idea.config.serverconfig.action.AddServerAction;
28  import com.atlassian.theplugin.jira.JIRAServerFacade;
29  import com.atlassian.theplugin.jira.JIRAServerFacadeImpl;
30  import com.atlassian.theplugin.util.PluginUtil;
31  import com.intellij.openapi.actionSystem.ActionGroup;
32  import com.intellij.openapi.actionSystem.ActionManager;
33  import com.intellij.openapi.actionSystem.DataProvider;
34  import com.intellij.openapi.project.Project;
35  import com.intellij.openapi.ui.Splitter;
36  import com.intellij.openapi.util.IconLoader;
37  import org.jetbrains.annotations.NonNls;
38  import org.jetbrains.annotations.Nullable;
39  
40  import javax.swing.*;
41  import java.awt.*;
42  import java.awt.event.MouseAdapter;
43  import java.awt.event.MouseEvent;
44  import java.util.ArrayList;
45  import java.util.Collection;
46  
47  public class ServerConfigPanel extends JPanel implements DataProvider {
48  	private final ServerTreePanel serverTreePanel;
49  	private BlankPanel blankPanel;
50  
51  	private CardLayout editPaneCardLayout;
52  	private JPanel editPane;
53  	private static final String BLANK_CARD = "Blank card";
54  
55  	private static final float SPLIT_RATIO = 0.39f;
56  
57  	private Collection<ServerCfg> serverCfgs;
58  	private final BambooServerConfigForm bambooServerConfigForm;
59  	private final GenericServerConfigForm jiraServerConfigForm;
60  	private final CrucibleServerConfigForm crucibleServerConfigForm;
61  	private final GenericServerConfigForm fisheyeServerConfigFrom;
62  	private final UserCfg defaultUser;
63  	private boolean isDefaultCredentialsAsked = false;
64  
65  	public ServerConfigPanel(final Project project, final UserCfg defaultUser,
66  			ProjectConfiguration projectConfiguration,
67  			final ServerCfg selectedServer, final boolean isDefaultCredentialsAsked) {
68  		this.defaultUser = defaultUser;
69  		this.serverCfgs = projectConfiguration != null ? projectConfiguration.getServers() : new ArrayList<ServerCfg>();
70  		this.serverTreePanel = new ServerTreePanel();
71  		final CrucibleServerFacade crucibleServerFacade = CrucibleServerFacadeImpl.getInstance();
72  		final BambooServerFacade bambooServerFacade = BambooServerFacadeImpl.getInstance(PluginUtil.getLogger());
73  		final JIRAServerFacade jiraServerFacade = JIRAServerFacadeImpl.getInstance();
74  		this.isDefaultCredentialsAsked = isDefaultCredentialsAsked;
75  		final FishEyeServerFacadeImpl fishEyeServerFacade = FishEyeServerFacadeImpl.getInstance();
76  		/* required due to circular dependency unhandled by pico */
77  		this.serverTreePanel.setServerConfigPanel(this);
78  		jiraServerConfigForm = new GenericServerConfigForm(project, defaultUser, new ProductConnector(jiraServerFacade));
79  		crucibleServerConfigForm = new CrucibleServerConfigForm(project, defaultUser, crucibleServerFacade,
80  				fishEyeServerFacade);
81  		bambooServerConfigForm = new BambooServerConfigForm(project, defaultUser, bambooServerFacade);
82  		fisheyeServerConfigFrom = new GenericServerConfigForm(project, defaultUser, new ProductConnector(fishEyeServerFacade));
83  		initLayout();
84  
85  		serverTreePanel.setData(serverCfgs);
86  		// This line selects server currently selected in the main panel
87  		serverTreePanel.setSelectedServer(selectedServer);
88  	}
89  
90  
91  	public boolean isDefaultCredentialsAsked() {
92  		return isDefaultCredentialsAsked;
93  	}
94  
95  	public UserCfg getDefaultUser() {
96  		return defaultUser;
97  	}
98  
99  	public void setData(Collection<ServerCfg> aServerCfgs) {
100 		serverCfgs = aServerCfgs;
101 		serverTreePanel.setData(serverCfgs);
102 	}
103 
104 
105 	private void initLayout() {
106 		GridBagLayout gbl = new GridBagLayout();
107 
108 		setLayout(gbl);
109 
110 		Splitter splitter = new Splitter(false, SPLIT_RATIO);
111 		splitter.setShowDividerControls(false);
112 		splitter.setFirstComponent(createSelectPane());
113 		splitter.setSecondComponent(createEditPane());
114 		splitter.setHonorComponentsMinimumSize(true);
115 
116 		GridBagConstraints c = new GridBagConstraints();
117 		c.fill = GridBagConstraints.BOTH;
118 		c.weightx = 1;
119 		c.weighty = 1;
120 		c.insets = new Insets(Constants.DIALOG_MARGIN,
121 				Constants.DIALOG_MARGIN,
122 				Constants.DIALOG_MARGIN,
123 				Constants.DIALOG_MARGIN);
124 		add(splitter, c);
125 	}
126 
127 	private JComponent createSelectPane() {
128 
129 		final JPanel selectPane = new JPanel(new BorderLayout());
130 		final JPanel toolBarPanel = new JPanel(new BorderLayout());
131 		toolBarPanel.add(createToolbar(), BorderLayout.NORTH);
132 		selectPane.add(toolBarPanel, BorderLayout.NORTH);
133 		selectPane.add(serverTreePanel, BorderLayout.CENTER);
134 		selectPane.setMinimumSize(new Dimension(ServerTreePanel.WIDTH, ServerTreePanel.HEIGHT));
135 		return selectPane;
136 	}
137 
138 	protected JComponent createToolbar() {
139 		ActionManager actionManager = ActionManager.getInstance();
140 		ActionGroup actionGroup = (ActionGroup) actionManager.getAction("ThePlugin.ServerConfigToolBar");
141 		return actionManager.createActionToolbar("ThePluginConfig", actionGroup, true).getComponent();
142 	}
143 
144 	private JComponent createEditPane() {
145 		editPane = new JPanel();
146 		editPaneCardLayout = new CardLayout();
147 		editPane.setLayout(editPaneCardLayout);
148 		editPane.add(bambooServerConfigForm.getRootComponent(), "Bamboo Servers");
149 		editPane.add(jiraServerConfigForm.getRootComponent(), "JIRA Servers");
150 		editPane.add(crucibleServerConfigForm.getRootComponent(), "Crucible Servers");
151 		editPane.add(fisheyeServerConfigFrom.getRootComponent(), "FishEye Servers");
152 		editPane.add(getBlankPanel(), BLANK_CARD);
153 
154 		return editPane;
155 	}
156 
157 	private JComponent getBlankPanel() {
158 		if (blankPanel == null) {
159 			blankPanel = new BlankPanel();
160 		}
161 		return blankPanel;
162 	}
163 
164 
165 	@Override
166 	public boolean isEnabled() {
167 		return true;
168 	}
169 
170 	public String getTitle() {
171 		return "Servers";
172 	}
173 
174 	public void addServer(ServerType serverType) {
175 		serverTreePanel.addServer(serverType);
176 	}
177 
178 	public void removeServer() {
179 		serverTreePanel.removeServer();
180 	}
181 
182 	public void copyServer() {
183 		serverTreePanel.copyServer();
184 	}
185 
186 
187 	public void saveData(ServerType serverType) {
188 		switch (serverType) {
189 			case BAMBOO_SERVER:
190 				bambooServerConfigForm.saveData();
191 				break;
192 			case CRUCIBLE_SERVER:
193 				crucibleServerConfigForm.saveData();
194 				break;
195 			case JIRA_SERVER:
196 				jiraServerConfigForm.saveData();
197 				break;
198 			case FISHEYE_SERVER:
199 				fisheyeServerConfigFrom.saveData();
200 				break;
201 			default:
202 				throw new AssertionError("switch not implemented for [" + serverType + "]");
203 		}
204 	}
205 
206 	public void saveData() {
207 		for (ServerType serverType : ServerType.values()) {
208 			saveData(serverType);
209 		}
210 
211 	}
212 
213 
214 	public void editServer(ServerCfg serverCfg) {
215 		ServerType serverType = serverCfg.getServerType();
216 		editPaneCardLayout.show(editPane, serverType.toString());
217 		switch (serverType) {
218 			case BAMBOO_SERVER:
219 				BambooServerCfg bambooServerCfg = (BambooServerCfg) serverCfg;
220 				bambooServerConfigForm.saveData();
221 				bambooServerConfigForm.setData(bambooServerCfg);
222 				break;
223 			case CRUCIBLE_SERVER:
224 				CrucibleServerCfg crucibleServerCfg = (CrucibleServerCfg) serverCfg;
225 				crucibleServerConfigForm.saveData();
226 				crucibleServerConfigForm.setData(crucibleServerCfg);
227 				break;
228 			case JIRA_SERVER:
229 				jiraServerConfigForm.saveData();
230 				jiraServerConfigForm.setData(serverCfg);
231 				break;
232 			case FISHEYE_SERVER:
233 				fisheyeServerConfigFrom.saveData();
234 				fisheyeServerConfigFrom.setData(serverCfg);
235 				break;
236 			default:
237 				throw new AssertionError("switch not implemented for [" + serverType + "]");
238 		}
239 	}
240 
241 	public void showEmptyPanel() {
242 		editPaneCardLayout.show(editPane, BLANK_CARD);
243 	}
244 
245 	public void finalizeData() {
246 		bambooServerConfigForm.finalizeData();
247 		crucibleServerConfigForm.finalizeData();
248 		jiraServerConfigForm.finalizeData();
249 		fisheyeServerConfigFrom.finalizeData();
250 	}
251 
252 	public ServerCfg getSelectedServer() {
253 		return serverTreePanel.getSelectedServer();
254 	}
255 
256 
257 	private class BlankPanel extends JPanel {
258 
259 		public BlankPanel() {
260 			initLayout();
261 		}
262 
263 		private static final String TEXT_BEGIN = "Press the ";
264 		private static final String TEXT_END = " button to define a new Server configuration.";
265 
266 		private void initLayout() {
267 
268 			setLayout(new BorderLayout());
269 
270 			JPanel instructionsPanel = new JPanel(new GridBagLayout());
271 			GridBagConstraints gbc = new GridBagConstraints();
272 			gbc.gridx = 0;
273 			gbc.gridy = 0;
274 			gbc.fill = GridBagConstraints.NONE;
275 			gbc.weightx = 0.0;
276 			instructionsPanel.setOpaque(false);
277 			instructionsPanel.add(new JLabel(TEXT_BEGIN), gbc);
278 			gbc.gridx++;
279 			JLabel addServerLabel = new JLabel(IconLoader.getIcon("/general/add.png"));
280 			addServerLabel.addMouseListener(new MouseAdapter() {
281 				private Cursor oldCursor;
282 
283 				@Override
284 				public void mouseEntered(MouseEvent mouseEvent) {
285 					oldCursor = getCursor();
286 					setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
287 				}
288 
289 				@Override
290 				public void mouseExited(MouseEvent mouseEvent) {
291 					if (oldCursor != null) {
292 						setCursor(oldCursor);
293 						oldCursor = null;
294 					}
295 				}
296 
297 				@Override
298 				public void mouseClicked(MouseEvent mouseEvent) {
299 					runAddServerAction(mouseEvent);
300 				}
301 			});
302 			instructionsPanel.add(addServerLabel, gbc);
303 			gbc.gridx++;
304 			gbc.weightx = 1.0;
305 			gbc.fill = GridBagConstraints.HORIZONTAL;
306 			instructionsPanel.add(new JLabel(TEXT_END), gbc);
307 			add(instructionsPanel, BorderLayout.NORTH);
308 		}
309 
310 		private void runAddServerAction(MouseEvent mouseEvent) {
311 			ServerType type = serverTreePanel.getSelectedServerType();
312 			if (type != null) {
313 				addServer(type);
314 			} else {
315 				AddServerAction.showAddServerPopup(mouseEvent);
316 			}
317 		}
318 
319 
320 	}
321 
322 	@Nullable
323 	public Object getData(@NonNls final String dataId) {
324 		if (dataId.equals(Constants.SERVER_CONFIG_PANEL)) {
325 			return this;
326 		} else if (dataId.equals(Constants.SERVERS)) {
327 			return serverCfgs;
328 		} else {
329 			return serverTreePanel.getData(dataId);
330 		}
331 	}
332 
333 }
334