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.bamboo.BambooServerFacade;
21 import com.atlassian.theplugin.commons.bamboo.BambooServerFacadeImpl;
22 import com.atlassian.theplugin.commons.cfg.BambooServerCfg;
23 import com.atlassian.theplugin.commons.cfg.ServerCfg;
24 import com.atlassian.theplugin.commons.crucible.CrucibleServerFacade;
25 import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
26 import com.atlassian.theplugin.idea.Constants;
27 import com.atlassian.theplugin.jira.JIRAServerFacade;
28 import com.atlassian.theplugin.jira.JIRAServerFacadeImpl;
29 import com.atlassian.theplugin.util.PluginUtil;
30 import com.intellij.openapi.actionSystem.ActionGroup;
31 import com.intellij.openapi.actionSystem.ActionManager;
32 import com.intellij.openapi.actionSystem.DataProvider;
33 import com.intellij.openapi.project.Project;
34 import com.intellij.openapi.ui.Splitter;
35 import com.intellij.openapi.util.IconLoader;
36 import org.jetbrains.annotations.NonNls;
37 import org.jetbrains.annotations.Nullable;
38
39 import javax.swing.*;
40 import javax.swing.text.BadLocationException;
41 import javax.swing.text.DefaultStyledDocument;
42 import javax.swing.text.Style;
43 import javax.swing.text.StyleConstants;
44 import java.awt.*;
45 import java.util.Collection;
46
47 public class ServerConfigPanel extends JPanel implements DataProvider {
48 private final ServerTreePanel serverTreePanel;
49 private BlankPanel blankPanel = null;
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.3f;
56
57 private final Project project;
58 private Collection<ServerCfg> serverCfgs;
59 private BambooServerConfigForm bambooServerConfigForm;
60 private GenericServerConfigForm jiraServerConfigForm;
61 private GenericServerConfigForm crucibleServerConfigForm;
62
63 public ServerConfigPanel(Project project, Collection<ServerCfg> serverCfgs) {
64 this.project = project;
65 this.serverCfgs = serverCfgs;
66 this.serverTreePanel = new ServerTreePanel();
67 final CrucibleServerFacade crucibleServerFacade = CrucibleServerFacadeImpl.getInstance();
68 final BambooServerFacade bambooServerFacade = BambooServerFacadeImpl.getInstance(PluginUtil.getLogger());
69 final JIRAServerFacade jiraServerFacade = JIRAServerFacadeImpl.getInstance();
70
71 this.serverTreePanel.setServerConfigPanel(this);
72 jiraServerConfigForm = new GenericServerConfigForm(project, new ProductConnector(jiraServerFacade));
73 crucibleServerConfigForm = new GenericServerConfigForm(project, new ProductConnector(crucibleServerFacade));
74 bambooServerConfigForm = new BambooServerConfigForm(project, bambooServerFacade);
75 initLayout();
76
77 serverTreePanel.setData(serverCfgs);
78
79 }
80
81
82 public void setData(Collection<ServerCfg> aServerCfgs) {
83 serverCfgs = aServerCfgs;
84 serverTreePanel.setData(serverCfgs);
85 }
86
87
88 private void initLayout() {
89 GridBagLayout gbl = new GridBagLayout();
90
91 setLayout(gbl);
92
93 Splitter splitter = new Splitter(false, SPLIT_RATIO);
94 splitter.setShowDividerControls(true);
95 splitter.setFirstComponent(createSelectPane());
96 splitter.setSecondComponent(createEditPane());
97 splitter.setHonorComponentsMinimumSize(true);
98
99 GridBagConstraints c = new GridBagConstraints();
100 c.fill = GridBagConstraints.BOTH;
101 c.weightx = 1;
102 c.weighty = 1;
103 c.insets = new Insets(Constants.DIALOG_MARGIN,
104 Constants.DIALOG_MARGIN,
105 Constants.DIALOG_MARGIN,
106 Constants.DIALOG_MARGIN);
107 add(splitter, c);
108 }
109
110 private JComponent createSelectPane() {
111
112 final JPanel selectPane = new JPanel(new BorderLayout());
113 final JPanel toolBarPanel = new JPanel(new BorderLayout());
114 toolBarPanel.add(createToolbar(), BorderLayout.NORTH);
115 selectPane.add(toolBarPanel, BorderLayout.NORTH);
116 selectPane.add(serverTreePanel, BorderLayout.CENTER);
117 return selectPane;
118 }
119
120 protected JComponent createToolbar() {
121 ActionManager actionManager = ActionManager.getInstance();
122 ActionGroup actionGroup = (ActionGroup) actionManager.getAction("ThePlugin.ServerConfigToolBar");
123 return actionManager.createActionToolbar("ThePluginConfig", actionGroup, true).getComponent();
124 }
125
126 private JComponent createEditPane() {
127 editPane = new JPanel();
128 editPaneCardLayout = new CardLayout();
129 editPane.setLayout(editPaneCardLayout);
130 editPane.add(bambooServerConfigForm.getRootComponent(), "Bamboo Servers");
131 editPane.add(jiraServerConfigForm.getRootComponent(), "JIRA Servers");
132 editPane.add(crucibleServerConfigForm.getRootComponent(), "Crucible Servers");
133 editPane.add(getBlankPanel(), BLANK_CARD);
134
135 return editPane;
136 }
137
138 private JComponent getBlankPanel() {
139 if (blankPanel == null) {
140 blankPanel = new BlankPanel();
141 }
142 return blankPanel;
143 }
144
145
146 @Override
147 public boolean isEnabled() {
148 return true;
149 }
150
151 public String getTitle() {
152 return "Servers";
153 }
154
155 public void addServer(ServerType serverType) {
156 serverTreePanel.addServer(serverType);
157 }
158
159 public void removeServer() {
160 serverTreePanel.removeServer();
161 }
162
163 public void copyServer() {
164 serverTreePanel.copyServer();
165 }
166
167
168 public void saveData(ServerType serverType) {
169 switch (serverType) {
170 case BAMBOO_SERVER:
171 bambooServerConfigForm.saveData();
172 break;
173 case CRUCIBLE_SERVER:
174 crucibleServerConfigForm.saveData();
175 break;
176 case JIRA_SERVER:
177 jiraServerConfigForm.saveData();
178 break;
179 default:
180 throw new AssertionError("switch not implemented for [" + serverType + "]");
181 }
182 }
183
184 public void saveData() {
185 for (ServerType serverType : ServerType.values()) {
186 saveData(serverType);
187 }
188 }
189
190
191 public void editServer(ServerCfg serverCfg) {
192 ServerType serverType = serverCfg.getServerType();
193 editPaneCardLayout.show(editPane, serverType.toString());
194 switch (serverType) {
195 case BAMBOO_SERVER:
196 BambooServerCfg bambooServerCfg = (BambooServerCfg) serverCfg;
197 bambooServerConfigForm.saveData();
198 bambooServerConfigForm.setData(bambooServerCfg);
199 break;
200 case CRUCIBLE_SERVER:
201 crucibleServerConfigForm.saveData();
202 crucibleServerConfigForm.setData(serverCfg);
203 break;
204 case JIRA_SERVER:
205 jiraServerConfigForm.saveData();
206 jiraServerConfigForm.setData(serverCfg);
207 break;
208 default:
209 throw new AssertionError("switch not implemented for [" + serverType + "]");
210 }
211 }
212
213 public void showEmptyPanel() {
214 editPaneCardLayout.show(editPane, BLANK_CARD);
215 }
216
217 public void finalizeData() {
218 bambooServerConfigForm.finalizeData();
219 crucibleServerConfigForm.finalizeData();
220 jiraServerConfigForm.finalizeData();
221 }
222
223
224 static class BlankPanel extends JPanel {
225
226 public BlankPanel() {
227 initLayout();
228 }
229
230 private static final String TEXT_BEGIN = "Press the ";
231 private static final String TEXT_END = " button to define a new Server configuration.";
232
233 private void initLayout() {
234
235 setLayout(new BorderLayout());
236
237 DefaultStyledDocument doc = new DefaultStyledDocument();
238 Style s = doc.addStyle(null, null);
239 StyleConstants.setIcon(s, IconLoader.getIcon("/general/add.png"));
240 Style d = doc.addStyle(null, null);
241 StyleConstants.setFontFamily(d, getFont().getFamily());
242 StyleConstants.setFontSize(d, getFont().getSize());
243 try {
244 doc.insertString(0, TEXT_BEGIN, d);
245 doc.insertString(TEXT_BEGIN.length(), " ", s);
246 doc.insertString(TEXT_BEGIN.length() + 1, TEXT_END, d);
247 } catch (BadLocationException e) {
248 PluginUtil.getLogger().error(e);
249 }
250 JTextPane pane = new JTextPane();
251 pane.setBackground(getBackground());
252 pane.setDocument(doc);
253 pane.setEditable(false);
254 pane.setVisible(true);
255
256 add(pane, BorderLayout.NORTH);
257 }
258
259
260 }
261
262 @Nullable
263 public Object getData(@NonNls final String dataId) {
264 if (dataId.equals(Constants.SERVER_CONFIG_PANEL)) {
265 return this;
266 } else if (dataId.equals(Constants.SERVERS)) {
267 return serverCfgs;
268 } else {
269 return serverTreePanel.getData(dataId);
270 }
271 }
272
273 }
274