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.jira;
17  
18  import com.atlassian.theplugin.idea.IdeaHelper;
19  import com.atlassian.theplugin.idea.jira.controls.*;
20  import com.atlassian.theplugin.idea.ui.ScrollablePanel;
21  import com.atlassian.theplugin.jira.JiraActionFieldType;
22  import com.atlassian.theplugin.jira.api.JIRAActionField;
23  import com.atlassian.theplugin.jira.api.JIRAIssue;
24  import com.atlassian.theplugin.jira.model.JIRAServerModel;
25  import com.intellij.openapi.project.Project;
26  import com.intellij.openapi.ui.DialogWrapper;
27  import com.intellij.uiDesigner.core.GridLayoutManager;
28  import com.jgoodies.forms.layout.CellConstraints;
29  import com.jgoodies.forms.layout.FormLayout;
30  import org.apache.commons.lang.StringUtils;
31  import org.jetbrains.annotations.Nullable;
32  
33  import javax.swing.*;
34  import java.awt.*;
35  import java.util.ArrayList;
36  import java.util.Collection;
37  import java.util.HashMap;
38  import java.util.List;
39  
40  /**
41   * @author Jacek Jaroczynski
42   */
43  public class PerformIssueActionForm extends DialogWrapper implements FreezeListener {
44      private JPanel root;
45      private JPanel contentPanel;
46      private Project project;
47      private JIRAIssue issue;
48      private List<JIRAActionField> fields;
49      private HashMap<String, Boolean> fieldsStatus = new HashMap<String, Boolean>();
50      private List<ActionFieldEditor> createdFieldEditors = new ArrayList<ActionFieldEditor>();
51      private int semaphore = 0;
52      private CommentTextArea commentTextArea;
53  
54      public PerformIssueActionForm(final Project project, final JIRAIssue issue, final List<JIRAActionField> fields,
55                                    final String name) {
56  
57          super(project, true);
58          this.project = project;
59          this.issue = issue;
60          this.fields = fields;
61  
62          setupUI();
63  
64          init();
65          pack();
66          createContent(fields);
67  
68          setTitle(name);
69          root.setMinimumSize(new Dimension(600, 300));
70          root.setPreferredSize(new Dimension(600, 300));
71  //		getOKAction().putValue(Action.NAME, name);
72      }
73  
74      private void createContent(final List<JIRAActionField> fieldList) {
75  
76          String columns = "3dlu, right:pref, 3dlu, fill:1dlu:grow, 3dlu";
77          String rows = "3dlu";
78  
79          Collection<JIRAActionField> sortedFieldList = JiraActionFieldType.sortFieldList(fieldList);
80  
81          JIRAServerModel jiraServerModel = IdeaHelper.getJIRAServerModel(project);
82  
83          List<ActionFieldEditor> editors = new ArrayList<ActionFieldEditor>();
84          List<String> unsupportedFields = new ArrayList<String>();
85  
86          for (JIRAActionField field : sortedFieldList) {
87  
88              ActionFieldEditor editor = null;
89              String row = null;
90  
91              switch (JiraActionFieldType.getFiledTypeForFieldId(field)) {
92                  case SUMMARY:
93                      editor = new FieldTextField(issue.getSummary(), field);
94                      row = ", pref, 3dlu";
95                      break;
96                  case DESCRIPTION:
97                      // we use wiki markup version from field (not html version from issue)
98                      editor = new FieldTextArea(field.getValues().get(0), field);
99                      row = ", fill:pref:grow, 3dlu";
100                     break;
101                 case ISSUE_TYPE:
102                     editor = new FieldIssueType(jiraServerModel, issue, field, this);
103                     row = ", p, 3dlu";
104                     break;
105                 case RESOLUTION:
106                     editor = new FieldResolution(jiraServerModel, issue, field, this);
107                     row = ", p, 3dlu";
108                     break;
109                 case ASSIGNEE:
110                     editor = new FieldUser(issue.getAssigneeId(), field);
111                     row = ", p, 3dlu";
112                     break;
113                 case PRIORITY:
114                     editor = new FieldPriority(jiraServerModel, issue, field, this);
115                     row = ", p, 3dlu";
116                     break;
117                 case VERSIONS:
118                     editor = new FieldAffectsVersion(jiraServerModel, issue, field, this);
119                     row = ", p, 3dlu";
120                     break;
121                 case FIX_VERSIONS:
122                     editor = new FieldFixForVersion(jiraServerModel, issue, field, this);
123                     row = ", p, 3dlu";
124                     break;
125                 case COMPONENTS:
126                     editor = new FieldComponents(jiraServerModel, issue, field, this);
127                     row = ", p, 3dlu";
128                     break;
129                 case REPORTER:
130                     editor = new FieldUser(issue.getReporterId(), field);
131                     row = ", p, 3dlu";
132                     break;
133                 case ENVIRONMENT:
134                     editor = new FieldTextArea(field.getValues().get(0), field);
135                     row = ", fill:pref:grow, 3dlu";
136                     break;
137                 case TIMETRACKING:
138                     editor = new FieldTimeTracking(field.getValues().get(0), issue, field, this);
139                     row = ", p, 3dlu";
140                     break;
141                 case DUE_DATE:
142                     String content = "";
143                     if (field.getValues() != null && field.getValues().size() > 0) {
144                         content = field.getValues().get(0);
145                     }
146                     editor = new FieldDueDate(content, field, this);
147                     row = ", p, 3dlu";
148                     break;
149                 case UNSUPPORTED:
150                 default:
151                     unsupportedFields.add(field.getName());
152                     break;
153             }
154 
155             if (editor != null && row != null) {
156                 editors.add(editor);
157                 rows += row;
158             }
159         }
160 
161         rows += ", fill:pref:grow, 3dlu";    // Comments text area
162 
163         if (!unsupportedFields.isEmpty()) {
164             rows += ", pref, 3dlu";    // warning status line about not handled
165         }
166 
167         contentPanel.setLayout(new FormLayout(columns, rows));
168         final CellConstraints cc = new CellConstraints();
169 
170         int y = 2;
171 
172         for (ActionFieldEditor editor : editors) {
173             final JLabel label = new JLabel(editor.getFieldName() + ":");
174             contentPanel.add(label, cc.xy(2, y, CellConstraints.RIGHT, CellConstraints.TOP));
175             contentPanel.add(editor.getComponent(), cc.xy(4, y));
176             createdFieldEditors.add(editor);
177             y += 2;
178         }
179 
180         final JLabel label = new JLabel("Comment :");
181         contentPanel.add(label, cc.xy(2, y, CellConstraints.RIGHT, CellConstraints.TOP));
182         // todo create field for Comments
183         commentTextArea = new CommentTextArea();
184         contentPanel.add(commentTextArea, cc.xy(4, y));
185 
186         y += 2;
187 
188         if (!unsupportedFields.isEmpty()) {
189             String warning = "Unsupported fields (original values copied): ";
190             warning += StringUtils.join(unsupportedFields, ", ");
191             contentPanel.add(new JLabel(warning), cc.xyw(2, y, 3, CellConstraints.LEFT, CellConstraints.CENTER));
192         }
193 
194         SwingUtilities.invokeLater(new Runnable() {
195             public void run() {
196                 contentPanel.validate();
197             }
198         });
199     }
200 
201     public List<JIRAActionField> getFields() {
202 
203         List<JIRAActionField> ret = new ArrayList<JIRAActionField>();
204 
205         ret.addAll(fields);
206 
207         for (ActionFieldEditor editor : createdFieldEditors) {
208             if (ret.contains(editor.getEditedFieldValue())) {
209                 ret.remove(editor.getEditedFieldValue());
210             }
211             ret.add(editor.getEditedFieldValue());
212         }
213 
214         return ret;
215     }
216 
217     public String getComment() {
218         return commentTextArea.getComment();
219     }
220 
221     protected void doOKAction() {
222         super.doOKAction();
223     }
224 
225     @Nullable
226     protected JComponent createCenterPanel() {
227         return root;
228     }
229 
230     public void freeze() {
231         semaphore++;
232         getOKAction().setEnabled(false);
233         root.validate();
234     }
235 
236     public void unfreeze() {
237         semaphore--;
238         if (semaphore == 0) {
239             getOKAction().setEnabled(true);
240             root.validate();
241         }
242     }
243 
244     public void fieldSyntaxError(final String fieldName) {
245         if (fieldsStatus.containsKey(fieldName)) {
246             if (!fieldsStatus.get(fieldName)) {
247                 fieldsStatus.put(fieldName, true);
248                 semaphore++;
249             }
250 
251         } else {
252             fieldsStatus.put(fieldName, true);
253             semaphore++;
254         }
255 
256         if (semaphore > 0) {
257             getOKAction().setEnabled(false);
258         }
259     }
260 
261     public void fieldSyntaxOk(final String fieldName) {
262         if (fieldsStatus.containsKey(fieldName)) {
263             if (fieldsStatus.get(fieldName)) {
264                 fieldsStatus.put(fieldName, false);
265                 semaphore--;
266             }
267         }
268 
269         if (semaphore == 0) {
270             getOKAction().setEnabled(true);
271         }
272     }
273 
274     private void setupUI() {
275         root = new JPanel();
276         root.setOpaque(false);
277         root.setLayout(new BorderLayout(0, 0));
278         final JScrollPane scroll = new JScrollPane();
279         scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
280         scroll.setOpaque(false);
281         scroll.getViewport().setOpaque(false);
282         root.add(scroll, BorderLayout.CENTER);
283         contentPanel = new ScrollablePanel();
284         scroll.setViewportView(contentPanel);
285     }
286 
287     {
288 // GUI initializer generated by IntelliJ IDEA GUI Designer
289 // >>> IMPORTANT!! <<<
290 // DO NOT EDIT OR ADD ANY CODE HERE!
291         $$$setupUI$$$();
292     }
293 
294     /**
295      * Method generated by IntelliJ IDEA GUI Designer
296      * >>> IMPORTANT!! <<<
297      * DO NOT edit this method OR call it in your code!
298      *
299      * @noinspection ALL
300      */
301     private void $$$setupUI$$$() {
302         root = new JPanel();
303         root.setLayout(new BorderLayout(0, 0));
304         final JScrollPane scrollPane1 = new JScrollPane();
305         scrollPane1.setHorizontalScrollBarPolicy(31);
306         root.add(scrollPane1, BorderLayout.CENTER);
307         contentPanel = new JPanel();
308         contentPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
309         scrollPane1.setViewportView(contentPanel);
310     }
311 
312     /**
313      * @noinspection ALL
314      */
315     public JComponent $$$getRootComponent$$$() {
316         return root;
317     }
318 }