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  
18  package com.atlassian.theplugin.idea.jira;
19  
20  import com.atlassian.theplugin.jira.JIRAServer;
21  import com.atlassian.theplugin.jira.api.JIRAConstant;
22  import com.atlassian.theplugin.jira.api.JIRAIssue;
23  import com.atlassian.theplugin.jira.api.JIRAIssueBean;
24  import com.atlassian.theplugin.jira.api.JIRAProject;
25  import com.intellij.openapi.diagnostic.Logger;
26  import com.intellij.openapi.ui.DialogWrapper;
27  import com.intellij.openapi.ui.Messages;
28  import com.intellij.ui.ColoredListCellRenderer;
29  import com.intellij.ui.SimpleTextAttributes;
30  import com.intellij.uiDesigner.core.GridConstraints;
31  import com.intellij.uiDesigner.core.GridLayoutManager;
32  import org.jetbrains.annotations.Nullable;
33  
34  import javax.swing.*;
35  import java.awt.*;
36  import java.awt.event.ActionEvent;
37  import java.awt.event.ActionListener;
38  import java.util.List;
39  
40  public class IssueCreate extends DialogWrapper {
41      private static final Logger LOGGER = Logger.getInstance("IssueCreate");
42  
43      private JPanel mainPanel;
44      private JTextArea description;
45      private JComboBox projectComboBox;
46      private JComboBox typeComboBox;
47      private JTextField summary;
48      private JComboBox priorityComboBox;
49      private JTextField assignee;
50      private final JIRAServer jiraServer;
51  
52      public IssueCreate(JIRAServer jiraServer) {
53          super(false);
54          init();
55          this.jiraServer = jiraServer;
56          setTitle("Create JIRA Issue");
57  
58          projectComboBox.setRenderer(new ColoredListCellRenderer() {
59              protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
60                  if (value != null) {
61                      append(((JIRAProject) value).getName(), SimpleTextAttributes.SIMPLE_CELL_ATTRIBUTES);
62                  }
63              }
64          });
65  
66          typeComboBox.setRenderer(new ColoredListCellRenderer() {
67              protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
68                  if (value != null) {
69                      JIRAConstant type = (JIRAConstant) value;
70                      append(type.getName(), SimpleTextAttributes.SIMPLE_CELL_ATTRIBUTES);
71                      setIcon(CachedIconLoader.getIcon(type.getIconUrl()));
72                  }
73              }
74          });
75          typeComboBox.setEnabled(false);
76  
77          priorityComboBox.setRenderer(new ColoredListCellRenderer() {
78              protected void customizeCellRenderer(JList list, Object value, int index, boolean selected, boolean hasFocus) {
79                  if (value != null) {
80                      JIRAConstant priority = (JIRAConstant) value;
81                      append(priority.getName(), SimpleTextAttributes.SIMPLE_CELL_ATTRIBUTES);
82                      setIcon(CachedIconLoader.getIcon(priority.getIconUrl()));
83                  }
84              }
85          });
86  
87          projectComboBox.addActionListener(new ActionListener() {
88              public void actionPerformed(ActionEvent event) {
89                  JIRAProject p = (JIRAProject) projectComboBox.getSelectedItem();
90                  updateIssueTypes(p);
91              }
92          });
93          getOKAction().setEnabled(false);
94          getOKAction().putValue(Action.NAME, "Create");
95      }
96  
97      public void initData() {
98          updatePriorities();
99          updateProject();
100     }
101 
102     private void updateProject() {
103         projectComboBox.setEnabled(false);
104         getOKAction().setEnabled(false);
105 
106         new Thread(new Runnable() {
107             public void run() {
108                 final List<JIRAProject> projects = jiraServer.getProjects();
109                 EventQueue.invokeLater(new Runnable() {
110                     public void run() {
111                         addProjects(projects);
112                     }
113                 });
114             }
115         }, "atlassian-idea-plugin jira issue priorities retrieve on issue create").start();
116     }
117 
118     private void addProjects(List<JIRAProject> projects) {
119         projectComboBox.removeAllItems();
120         for (JIRAProject project : projects) {
121             if (project.getId() != JIRAServer.ANY_ID) {
122                 projectComboBox.addItem(project);
123             }
124         }
125 
126         if (jiraServer.getCurrentProject() != null) {
127             projectComboBox.setSelectedItem(jiraServer.getCurrentProject());
128         } else {
129             if (projectComboBox.getModel().getSize() > 0) {
130                 projectComboBox.setSelectedIndex(0);
131             }
132         }
133         projectComboBox.setEnabled(true);
134     }
135 
136     private void updatePriorities() {
137         priorityComboBox.setEnabled(false);
138         getOKAction().setEnabled(false);
139         new Thread(new Runnable() {
140             public void run() {
141                 final List<JIRAConstant> priorities = jiraServer.getPriorieties();
142                 EventQueue.invokeLater(new Runnable() {
143                     public void run() {
144                         addIssuePriorieties(priorities);
145                     }
146                 });
147             }
148         }, "atlassian-idea-plugin jira issue priorities retrieve on issue create").start();
149     }
150 
151     private void addIssuePriorieties(List<JIRAConstant> priorieties) {
152         priorityComboBox.removeAllItems();
153         for (JIRAConstant constant : priorieties) {
154             if (constant.getId() != JIRAServer.ANY_ID) {
155                 priorityComboBox.addItem(constant);
156             }
157         }
158         if (priorityComboBox.getModel().getSize() > 0) {
159             priorityComboBox.setSelectedIndex(priorityComboBox.getModel().getSize() / 2);
160         }
161         priorityComboBox.setEnabled(true);
162     }
163 
164     private void updateIssueTypes(final JIRAProject project) {
165         typeComboBox.setEnabled(false);
166         getOKAction().setEnabled(false);
167         new Thread(new Runnable() {
168             public void run() {
169                 jiraServer.setCurrentProject(project);
170                 final List<JIRAConstant> issueTypes = jiraServer.getIssueTypes();
171                 EventQueue.invokeLater(new Runnable() {
172                     public void run() {
173                         addIssueTypes(issueTypes);
174                     }
175                 });
176             }
177         }, "atlassian-idea-plugin jira issue types retrieve on issue create").start();
178     }
179 
180     private void addIssueTypes(List<JIRAConstant> issueTypes) {
181         typeComboBox.removeAllItems();
182         for (JIRAConstant constant : issueTypes) {
183             if (constant.getId() != JIRAServer.ANY_ID) {
184                 typeComboBox.addItem(constant);
185             }
186         }
187         typeComboBox.setEnabled(true);
188         getOKAction().setEnabled(true);
189     }
190 
191     private JIRAIssueBean issueProxy;
192 
193     JIRAIssue getJIRAIssue() {
194         return issueProxy;
195     }
196 
197     protected void doOKAction() {
198         issueProxy = new JIRAIssueBean();
199         issueProxy.setSummary(summary.getText());
200 
201         if (projectComboBox.getSelectedItem() == null) {
202             Messages.showErrorDialog(this.getContentPane(), "Project has to be selected", "Project not defined");
203             return;
204         }
205         issueProxy.setProjectKey(((JIRAProject) projectComboBox.getSelectedItem()).getKey());
206         if (typeComboBox.getSelectedItem() == null) {
207             Messages.showErrorDialog(this.getContentPane(), "Issue type has to be selected", "Issue type not defined");
208             return;
209         }
210         issueProxy.setType(((JIRAConstant) typeComboBox.getSelectedItem()));
211         issueProxy.setDescription(description.getText());
212         issueProxy.setPriority(((JIRAConstant) priorityComboBox.getSelectedItem()));
213         String assignTo = assignee.getText();
214         if (assignTo.length() > 0) {
215             issueProxy.setAssignee(assignTo);
216         }
217         super.doOKAction();
218     }
219 
220     public JComponent getPreferredFocusedComponent() {
221         return summary;
222     }
223 
224     @Nullable
225     protected JComponent createCenterPanel() {
226         return mainPanel;
227     }
228 
229     {
230 // GUI initializer generated by IntelliJ IDEA GUI Designer
231 // >>> IMPORTANT!! <<<
232 // DO NOT EDIT OR ADD ANY CODE HERE!
233         $$$setupUI$$$();
234     }
235 
236     /**
237      * Method generated by IntelliJ IDEA GUI Designer
238      * >>> IMPORTANT!! <<<
239      * DO NOT edit this method OR call it in your code!
240      *
241      * @noinspection ALL
242      */
243     private void $$$setupUI$$$() {
244         mainPanel = new JPanel();
245         mainPanel.setLayout(new GridLayoutManager(7, 6, new Insets(5, 5, 5, 5), -1, -1));
246         mainPanel.setMinimumSize(new Dimension(400, 100));
247         final JScrollPane scrollPane1 = new JScrollPane();
248         mainPanel.add(scrollPane1, new GridConstraints(5, 0, 1, 6, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
249         description = new JTextArea();
250         description.setLineWrap(true);
251         description.setMinimumSize(new Dimension(439, 120));
252         description.setPreferredSize(new Dimension(439, 120));
253         description.setWrapStyleWord(true);
254         scrollPane1.setViewportView(description);
255         final JLabel label1 = new JLabel();
256         label1.setText("Summary:");
257         mainPanel.add(label1, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
258         final JLabel label2 = new JLabel();
259         label2.setText("Project:");
260         mainPanel.add(label2, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
261         projectComboBox = new JComboBox();
262         mainPanel.add(projectComboBox, new GridConstraints(0, 1, 1, 5, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(150, -1), null, null, 0, false));
263         summary = new JTextField();
264         mainPanel.add(summary, new GridConstraints(3, 1, 1, 5, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
265         final JLabel label3 = new JLabel();
266         label3.setText("Description:");
267         mainPanel.add(label3, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
268         final JLabel label4 = new JLabel();
269         label4.setText("Assignee");
270         mainPanel.add(label4, new GridConstraints(6, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
271         assignee = new JTextField();
272         mainPanel.add(assignee, new GridConstraints(6, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
273         final JLabel label5 = new JLabel();
274         label5.setFont(new Font(label5.getFont().getName(), label5.getFont().getStyle(), 10));
275         label5.setText("Warning! This field is not validated prior to sending to JIRA");
276         mainPanel.add(label5, new GridConstraints(6, 2, 1, 4, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
277         final JLabel label6 = new JLabel();
278         label6.setText("Type:");
279         mainPanel.add(label6, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
280         typeComboBox = new JComboBox();
281         mainPanel.add(typeComboBox, new GridConstraints(1, 1, 1, 5, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(150, -1), null, null, 0, false));
282         final JLabel label7 = new JLabel();
283         label7.setText("Priority:");
284         mainPanel.add(label7, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
285         priorityComboBox = new JComboBox();
286         mainPanel.add(priorityComboBox, new GridConstraints(2, 1, 1, 5, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(150, -1), null, null, 0, false));
287         label1.setLabelFor(summary);
288         label2.setLabelFor(projectComboBox);
289         label3.setLabelFor(description);
290         label6.setLabelFor(typeComboBox);
291     }
292 
293     /**
294      * @noinspection ALL
295      */
296     public JComponent $$$getRootComponent$$$() {
297         return mainPanel;
298     }
299 }