1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.atlassian.theplugin.idea.config.serverconfig;
18
19 import com.atlassian.theplugin.commons.ServerType;
20 import com.atlassian.theplugin.commons.Server;
21 import com.atlassian.theplugin.commons.exception.ThePluginException;
22 import com.atlassian.theplugin.commons.configuration.PluginConfiguration;
23 import com.atlassian.theplugin.commons.configuration.ProductServerConfiguration;
24 import com.atlassian.theplugin.commons.configuration.ConfigurationFactory;
25 import com.atlassian.theplugin.commons.bamboo.BambooServerFacade;
26 import com.atlassian.theplugin.commons.bamboo.BambooServerFacadeImpl;
27 import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
28 import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
29 import com.atlassian.theplugin.idea.Constants;
30 import com.atlassian.theplugin.idea.config.ContentPanel;
31 import com.atlassian.theplugin.idea.config.serverconfig.model.ServerNode;
32 import com.atlassian.theplugin.jira.JIRAServerFacade;
33 import com.atlassian.theplugin.jira.JIRAServerFacadeImpl;
34 import com.atlassian.theplugin.commons.remoteapi.RemoteApiException;
35 import com.atlassian.theplugin.util.Connector;
36 import com.atlassian.theplugin.util.PluginUtil;
37 import com.intellij.openapi.actionSystem.ActionGroup;
38 import com.intellij.openapi.actionSystem.ActionManager;
39 import com.intellij.openapi.ui.Splitter;
40 import com.intellij.openapi.util.IconLoader;
41
42 import javax.swing.*;
43 import javax.swing.text.BadLocationException;
44 import javax.swing.text.DefaultStyledDocument;
45 import javax.swing.text.Style;
46 import javax.swing.text.StyleConstants;
47 import java.awt.*;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.Map;
51
52 public final class ServerConfigPanel extends JPanel implements ContentPanel {
53 private final ServerTreePanel serverTreePanel;
54 private BlankPanel blankPanel = null;
55
56 private CardLayout editPaneCardLayout;
57 private JPanel editPane;
58 private static final String BLANK_CARD = "Blank card";
59
60 private static final float SPLIT_RATIO = 0.3f;
61 private Map<ServerType, ServerPanel> serverPanels = new HashMap<ServerType, ServerPanel>();
62
63 private transient PluginConfiguration localConfigCopy;
64 private final transient CrucibleServerFacade crucibleServerFacade;
65 private final transient BambooServerFacade bambooServerFacade;
66 private final transient JIRAServerFacade jiraServerFacade;
67 private static ServerConfigPanel instance;
68
69 private ServerConfigPanel() {
70 this.serverTreePanel = ServerTreePanel.getInstance();
71 this.crucibleServerFacade = CrucibleServerFacadeImpl.getInstance();
72 this.bambooServerFacade = BambooServerFacadeImpl.getInstance(PluginUtil.getLogger());
73 this.jiraServerFacade = JIRAServerFacadeImpl.getInstance();
74
75 this.serverTreePanel.setServerConfigPanel(this);
76 initLayout();
77 }
78
79 public static ServerConfigPanel getInstance() {
80 if (instance == null) {
81 instance = new ServerConfigPanel();
82 }
83 return instance;
84 }
85
86 private void initLayout() {
87 GridBagLayout gbl = new GridBagLayout();
88
89 setLayout(gbl);
90
91 Splitter splitter = new Splitter(false, SPLIT_RATIO);
92 splitter.setShowDividerControls(true);
93 splitter.setFirstComponent(createSelectPane());
94 splitter.setSecondComponent(createEditPane());
95 splitter.setHonorComponentsMinimumSize(true);
96
97 GridBagConstraints c = new GridBagConstraints();
98 c.fill = GridBagConstraints.BOTH;
99 c.weightx = 1;
100 c.weighty = 1;
101 c.insets = new Insets(Constants.DIALOG_MARGIN,
102 Constants.DIALOG_MARGIN,
103 Constants.DIALOG_MARGIN,
104 Constants.DIALOG_MARGIN);
105 add(splitter, c);
106 }
107
108 private JComponent createSelectPane() {
109 JPanel selectPane = new JPanel();
110 GridBagLayout gbl = new GridBagLayout();
111 selectPane.setLayout(gbl);
112 GridBagConstraints c = new GridBagConstraints();
113 c.gridx = 0;
114 c.gridy = 0;
115 c.anchor = GridBagConstraints.FIRST_LINE_START;
116 c.fill = GridBagConstraints.NONE;
117 c.ipady = 2;
118 selectPane.add(createToolbar(), c);
119 c.gridx = 0;
120 c.gridy = 1;
121 c.weightx = 1;
122 c.weighty = 1;
123 c.fill = GridBagConstraints.BOTH;
124 c.ipady = 0;
125
126
127 c.insets = new Insets(2 + 1, 2, 2, 2);
128 selectPane.add(serverTreePanel, c);
129 return selectPane;
130 }
131
132 private JComponent createToolbar() {
133 ActionManager actionManager = ActionManager.getInstance();
134 ActionGroup actionGroup = (ActionGroup) actionManager.getAction("ThePlugin.ServerConfigToolBar");
135 return actionManager.createActionToolbar("ThePluginConfig", actionGroup, true).getComponent();
136 }
137
138 private JComponent createEditPane() {
139 editPane = new JPanel();
140 editPaneCardLayout = new CardLayout();
141 editPane.setLayout(editPaneCardLayout);
142 for (int i = 0; i < ServerType.values().length; i++) {
143 ServerType serverType = ServerType.values()[i];
144 editPane.add(getServerPanel(serverType), serverType.toString());
145 }
146 editPane.add(getBlankPanel(), BLANK_CARD);
147
148 return editPane;
149 }
150
151 private JComponent getBlankPanel() {
152 if (blankPanel == null) {
153 blankPanel = new BlankPanel();
154 }
155 return blankPanel;
156 }
157
158 private JComponent getServerPanel(ServerType serverType) {
159 if (!serverPanels.containsKey(serverType)) {
160 switch (serverType) {
161 case BAMBOO_SERVER:
162 serverPanels.put(ServerType.BAMBOO_SERVER, new BambooServerConfigForm(bambooServerFacade));
163 break;
164 case CRUCIBLE_SERVER:
165 serverPanels.put(ServerType.CRUCIBLE_SERVER, new GenericServerConfigForm(new Connector() {
166 public void connect() throws ThePluginException {
167 validate();
168 try {
169 crucibleServerFacade.testServerConnection(getUrl(), getUserName(), getPassword());
170 } catch (RemoteApiException e) {
171 throw new ThePluginException(e.getMessage(), e);
172 }
173 }
174 }));
175 break;
176 case JIRA_SERVER:
177 serverPanels.put(ServerType.JIRA_SERVER, new GenericServerConfigForm(new Connector() {
178 public void connect() throws ThePluginException {
179 validate();
180 try {
181 jiraServerFacade.testServerConnection(getUrl(), getUserName(),
182 getPassword());
183 } catch (RemoteApiException e) {
184 throw new ThePluginException(e.getMessage(), e);
185 }
186 }
187 }));
188 break;
189 default:
190 break;
191 }
192 }
193 return serverPanels.get(serverType).getRootComponent();
194 }
195
196 public boolean isEnabled() {
197 return true;
198 }
199
200 public boolean isModified() {
201 if (!getLocalPluginConfigurationCopy().equals(ConfigurationFactory.getConfiguration())) {
202 return true;
203 }
204
205 for (ServerPanel entry : serverPanels.values()) {
206 if (entry != null) {
207 if (entry.isModified()) {
208 return true;
209 }
210 }
211 }
212
213 return false;
214 }
215
216 public String getTitle() {
217 return "Servers";
218 }
219
220 public void getData() {
221 if (isModified()) {
222 for (ServerType type : serverPanels.keySet()) {
223 final ProductServerConfiguration conf = getLocalPluginConfigurationCopy().getProductServers(type);
224 if (serverPanels.get(type).isModified()) {
225 if (conf.transientGetServer(serverPanels.get(type).getData()) != null) {
226 conf.storeServer(serverPanels.get(type).getData());
227 }
228 }
229 Collection<Server> s = conf.transientGetServers();
230 ConfigurationFactory.getConfiguration().getProductServers(type).setServers(s);
231 }
232
233 this.serverTreePanel.setData(getLocalPluginConfigurationCopy());
234 }
235 }
236
237
238 public void setData(PluginConfiguration config) {
239 localConfigCopy = config;
240 serverTreePanel.setData(getLocalPluginConfigurationCopy());
241 }
242
243 public void addServer(ServerType serverType) {
244 serverTreePanel.addServer(serverType);
245 }
246
247 public void removeServer() {
248 serverTreePanel.removeServer();
249 }
250
251 public void copyServer() {
252 serverTreePanel.copyServer();
253 }
254
255 public void storeServer(ServerNode serverNode) {
256 Server server = serverNode.getServer();
257 Server tempValue = serverPanels.get(serverNode.getServerType()).getData();
258 switch (serverNode.getServerType()) {
259 case BAMBOO_SERVER:
260 server.transientSetSubscribedPlans(tempValue.transientGetSubscribedPlans());
261 server.setUseFavourite(tempValue.getUseFavourite());
262 break;
263 default:
264 break;
265 }
266 server.setName(tempValue.getName());
267 server.setUserName(tempValue.getUserName());
268 server.transientSetPasswordString(tempValue.transientGetPasswordString(), tempValue.getShouldPasswordBeStored());
269 server.setEnabled(tempValue.getEnabled());
270 server.setUrlString(tempValue.getUrlString());
271 }
272
273 public void editServer(ServerType serverType, Server server) {
274 editPaneCardLayout.show(editPane, serverType.toString());
275 serverPanels.get(serverType).setData(server);
276 }
277
278 public void showEmptyPanel() {
279 editPaneCardLayout.show(editPane, BLANK_CARD);
280 }
281
282
283 static class BlankPanel extends JPanel {
284
285 public BlankPanel() {
286 initLayout();
287 }
288
289 private static final String TEXT_BEGIN = "Press the ";
290 private static final String TEXT_END = " button to define a new Server configuration.";
291
292 private void initLayout() {
293
294 setLayout(new BorderLayout());
295
296 DefaultStyledDocument doc = new DefaultStyledDocument();
297 Style s = doc.addStyle(null, null);
298 StyleConstants.setIcon(s, IconLoader.getIcon("/general/add.png"));
299 Style d = doc.addStyle(null, null);
300 StyleConstants.setFontFamily(d, getFont().getFamily());
301 StyleConstants.setFontSize(d, getFont().getSize());
302 try {
303 doc.insertString(0, TEXT_BEGIN, d);
304 doc.insertString(TEXT_BEGIN.length(), " ", s);
305 doc.insertString(TEXT_BEGIN.length() + 1, TEXT_END, d);
306 } catch (BadLocationException e) {
307 e.printStackTrace();
308 }
309 JTextPane pane = new JTextPane();
310 pane.setBackground(getBackground());
311 pane.setDocument(doc);
312 pane.setEditable(false);
313 pane.setVisible(true);
314
315 add(pane, BorderLayout.NORTH);
316 }
317
318
319 }
320
321 public PluginConfiguration getLocalPluginConfigurationCopy() {
322 return localConfigCopy;
323 }
324 }