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.crucible;
18  
19  import com.atlassian.theplugin.commons.crucible.api.model.CommentBean;
20  import com.atlassian.theplugin.commons.crucible.api.model.ReviewAdapter;
21  import com.atlassian.theplugin.idea.Constants;
22  import com.atlassian.theplugin.idea.ui.DialogWithDetails;
23  import com.intellij.openapi.project.Project;
24  import com.intellij.openapi.ui.DialogWrapper;
25  import com.intellij.ui.HyperlinkLabel;
26  import com.intellij.uiDesigner.core.GridConstraints;
27  import com.intellij.uiDesigner.core.GridLayoutManager;
28  import com.intellij.uiDesigner.core.Spacer;
29  import org.jetbrains.annotations.Nullable;
30  
31  import javax.swing.*;
32  import javax.swing.event.HyperlinkEvent;
33  import javax.swing.event.HyperlinkListener;
34  import java.awt.*;
35  import java.awt.event.ActionEvent;
36  import java.awt.event.ActionListener;
37  
38  
39  public class CommentEditForm extends DialogWrapper {
40      private JPanel rootComponent;
41      private JTextArea commentText;
42      private JScrollPane commentPane;
43      private JCheckBox defectCheckBox;
44      private JButton postButton;
45      private JButton saveAsDraftButton;
46      private JButton cancelButton;
47      private JPanel comboPanel;
48      private JPanel toolPanel;
49      private JPanel errorPanel;
50      private HyperlinkLabel errorLabel;
51  
52  
53      private boolean saveAsDraft = false;
54      private final CommentBean comment;
55      private Throwable lastError;
56  
57      public CommentEditForm(final Project project, final ReviewAdapter review, final CommentBean comment) {
58  
59          super(project, false);
60          this.comment = comment;
61  
62  
63          $$$setupUI$$$();
64          init();
65  
66          this.errorPanel.setVisible(false);
67          errorPanel.add(new JLabel("Comment submission failed - "));
68          errorLabel = new HyperlinkLabel("click here for details");
69          errorLabel.setOpaque(false);
70          errorLabel.addHyperlinkListener(new HyperlinkListener() {
71              public void hyperlinkUpdate(HyperlinkEvent e) {
72                  if (lastError != null) {
73                      DialogWithDetails.showExceptionDialog(project, lastError.getMessage(), lastError);
74                  }
75              }
76          });
77          errorPanel.add(errorLabel);
78  
79          comboPanel.setLayout(new FlowLayout());
80  
81          final CrucibleReviewMetricsCombos combos = new CrucibleReviewMetricsCombos(comment.getCustomFields(),
82                  review.getMetricDefinitions(),
83                  comboPanel);
84  
85          postButton.setAction(getOKAction());
86          postButton.setMnemonic('P');
87          saveAsDraftButton.setAction(getDraftAction());
88          saveAsDraftButton.setMnemonic('D');
89          cancelButton.setAction(getCancelAction());
90  
91  
92          commentText.setText(comment.getMessage());
93  
94          defectCheckBox.addActionListener(new ActionListener() {
95  
96              public void actionPerformed(ActionEvent event) {
97                  combos.showMetricCombos(defectCheckBox.isSelected());
98                  pack();
99              }
100         });
101 
102         if (comment.isDefectRaised()) {
103             defectCheckBox.setSelected(true);
104             combos.showMetricCombos(true);
105         } else {
106             defectCheckBox.setSelected(false);
107             combos.showMetricCombos(false);
108         }
109 
110         if (comment.isReply()) {
111             defectCheckBox.setVisible(false);
112             if (comment.getPermId() != null) {
113                 setTitle("Edit Reply");
114             } else {
115                 setTitle("Add Reply");
116             }
117         } else {
118             if (comment.getPermId() != null) {
119                 setTitle("Edit Comment");
120             } else {
121                 setTitle("Add Comment");
122             }
123         }
124 
125         if (comment.getPermId() != null) {
126             if (comment.isDraft()) {
127                 saveAsDraftButton.setVisible(true);
128             } else {
129                 saveAsDraftButton.setVisible(false);
130             }
131 
132         } else {
133             saveAsDraftButton.setVisible(true);
134         }
135 
136         getOKAction().putValue(Action.NAME, "Post");
137     }
138 
139     public CommentEditForm(Project project, final ReviewAdapter review, final CommentBean data, final Throwable error) {
140         this(project, review, data);
141         if (error != null) {
142             errorPanel.setBackground(Constants.FAIL_COLOR);
143             errorPanel.setVisible(true);
144             lastError = error;
145         }
146     }
147 
148     public JComponent getPreferredFocusedComponent() {
149         return commentText;
150     }
151 
152     public JComponent getRootComponent() {
153         return rootComponent;
154     }
155 
156     @Nullable
157     protected JComponent createCenterPanel() {
158         return getRootComponent();
159     }
160 
161 
162     protected void doOKAction() {
163         comment.setDraft(saveAsDraft);
164         comment.setDefectRaised(defectCheckBox.isSelected());
165         comment.setMessage(commentText.getText());
166         super.doOKAction();
167     }
168 
169     @Override
170     protected Action[] createActions() {
171         return new Action[0];
172     }
173 
174     public Action getDraftAction() {
175         return draftAction;
176     }
177 
178     private Action draftAction = new AbstractAction() {
179         {
180             putValue(Action.NAME, "Save as Draft");
181         }
182 
183         public void actionPerformed(ActionEvent e) {
184             saveAsDraft = true;
185             doOKAction();
186         }
187     };
188 
189     private void createUIComponents() {
190     }
191 
192     /**
193      * Method generated by IntelliJ IDEA GUI Designer
194      * >>> IMPORTANT!! <<<
195      * DO NOT edit this method OR call it in your code!
196      *
197      * @noinspection ALL
198      */
199     private void $$$setupUI$$$() {
200         rootComponent = new JPanel();
201         rootComponent.setLayout(new GridLayoutManager(3, 1, new Insets(0, 0, 0, 0), -1, -1));
202         rootComponent.setMinimumSize(new Dimension(650, 300));
203         commentPane = new JScrollPane();
204         rootComponent.add(commentPane, new GridConstraints(1, 0, 1, 1, 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));
205         commentText = new JTextArea();
206         commentText.setLineWrap(true);
207         commentText.setText("");
208         commentText.setWrapStyleWord(true);
209         commentPane.setViewportView(commentText);
210         toolPanel = new JPanel();
211         toolPanel.setLayout(new GridLayoutManager(1, 6, new Insets(0, 0, 0, 0), -1, -1));
212         rootComponent.add(toolPanel, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
213         defectCheckBox = new JCheckBox();
214         defectCheckBox.setText("Defect");
215         toolPanel.add(defectCheckBox, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
216         postButton = new JButton();
217         postButton.setText("Post");
218         toolPanel.add(postButton, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
219         saveAsDraftButton = new JButton();
220         saveAsDraftButton.setText("Save as draft");
221         toolPanel.add(saveAsDraftButton, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
222         cancelButton = new JButton();
223         cancelButton.setText("Cancel");
224         toolPanel.add(cancelButton, new GridConstraints(0, 5, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
225         final Spacer spacer1 = new Spacer();
226         toolPanel.add(spacer1, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
227         comboPanel = new JPanel();
228         comboPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
229         toolPanel.add(comboPanel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
230         errorPanel = new JPanel();
231         errorPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
232         rootComponent.add(errorPanel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
233     }
234 
235     /**
236      * @noinspection ALL
237      */
238     public JComponent $$$getRootComponent$$$() {
239         return rootComponent;
240     }
241 }