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.*;
20  import com.intellij.openapi.project.Project;
21  import com.intellij.openapi.ui.DialogWrapper;
22  import com.intellij.uiDesigner.core.GridConstraints;
23  import com.intellij.uiDesigner.core.GridLayoutManager;
24  import com.intellij.uiDesigner.core.Spacer;
25  import org.jetbrains.annotations.Nullable;
26  
27  import javax.swing.*;
28  import javax.swing.Action;
29  import java.awt.*;
30  import java.awt.event.ActionEvent;
31  import java.awt.event.ActionListener;
32  import java.util.List;
33  import java.util.ArrayList;
34  import java.util.Map;
35  import java.util.HashMap;
36  
37  
38  public class CommentEditForm extends DialogWrapper {
39  	private JPanel rootComponent;
40  	private JTextArea commentText;
41  	private JScrollPane commentPane;
42  	private JCheckBox defectCheckBox;
43  	private JButton postButton;
44  	private JButton saveAsDraftButton;
45  	private JButton cancelButton;
46  	private JPanel comboPanel;
47  	private JPanel toolPanel;
48  
49  	private final Review review;
50  	private final CommentBean comment;
51  
52  	private boolean saveAsDraft = false;
53  	private static final String RANK_FIELD = "Ranking";
54  	private static final String CLASSIFICATION_FIELD = "Classification";
55  
56  	private Map<String, JComboBox> combos = new HashMap<String, JComboBox>();
57  
58  	public CommentEditForm(Project project, final Review review, final CommentBean comment, List<CustomFieldDef> metrics) {
59  		super(project, false);
60  
61  		this.review = review;
62  		this.comment = comment;
63  
64  		$$$setupUI$$$();
65  		init();
66  
67  		comboPanel.setLayout(new FlowLayout());
68  
69  		for (CustomFieldDef metric : metrics) {
70  			final JLabel label = new JLabel(metric.getLabel());
71  			final JComboBox combo = new JComboBox();
72  			final String metricName = metric.getName();
73  			combo.addItem("Select " + metricName);
74  			for (CustomFieldValue value : metric.getValues()) {
75  				combo.addItem(value.getName());
76  			}
77  			combo.addActionListener(new ActionListener() {
78  				public void actionPerformed(final ActionEvent event) {
79  					setMetricField(combo, metricName);
80  				}
81  			});
82  			combos.put(metricName, combo);
83  			comboPanel.add(label);
84  			comboPanel.add(combo);
85  		}
86  
87  		postButton.setAction(getOKAction());
88  		postButton.setMnemonic('P');
89  		saveAsDraftButton.setAction(getDraftAction());
90  		saveAsDraftButton.setMnemonic('D');
91  		cancelButton.setAction(getCancelAction());
92  		commentText.setText(comment.getMessage());
93  
94  		defectCheckBox.addActionListener(new ActionListener() {
95  
96  			public void actionPerformed(ActionEvent event) {
97  				showMetricCombo(comment, defectCheckBox.isSelected());
98  				pack();
99  			}
100 		});
101 
102 		if (comment.isDefectRaised()) {
103 			defectCheckBox.setSelected(true);
104 			showMetricCombo(comment, true);
105 		} else {
106 			defectCheckBox.setSelected(false);
107 			showMetricCombo(comment, 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 	private void setMetricField(JComboBox combo, String field) {
140 		CustomField oldCf = comment.getCustomFields().get(field);
141 		if (oldCf != null) {
142 			comment.getCustomFields().remove(oldCf);
143 		}
144 		if (combo.getSelectedIndex() > 0) {
145 			CustomFieldBean cf = new CustomFieldBean();
146 			cf.setConfigVersion(review.getMetricsVersion());
147 			cf.setValue((String) combo.getSelectedItem());
148 			comment.getCustomFields().put(field, cf);
149 		}
150 	}
151 
152 	private void showMetricCombo(CommentBean comment, boolean visible) {
153 		for (String key : comment.getCustomFields().keySet()) {
154 			if (combos.get(key) != null) {
155 				combos.get(key).setSelectedItem(comment.getCustomFields().get(key).getValue());
156 				combos.get(key).setVisible(visible);
157 			}
158 		}
159 		comboPanel.setVisible(visible);
160 	}
161 
162 	public JComponent getPreferredFocusedComponent() {
163 		return commentText;
164 	}
165 
166 	public JComponent getRootComponent() {
167 		return rootComponent;
168 	}
169 
170 	@Nullable
171 	protected JComponent createCenterPanel() {
172 		return getRootComponent();
173 	}
174 
175 
176 	protected void doOKAction() {
177 		comment.setDraft(saveAsDraft);
178 		comment.setDefectRaised(defectCheckBox.isSelected());
179 //		if (comment.isDefectRaised()) {
180 //			setMetricField(rankComboBox, RANK_FIELD);
181 //			setMetricField(classificationComboBox, CLASSIFICATION_FIELD);
182 //		}
183 		comment.setMessage(commentText.getText());
184 		super.doOKAction();
185 	}
186 
187 	@Override
188 	protected Action[] createActions() {
189 		return new Action[0];
190 	}
191 
192 	public Action getDraftAction() {
193 		return draftAction;
194 	}
195 
196 	private Action draftAction = new AbstractAction() {
197 		{
198 			putValue(Action.NAME, "Save as Draft");
199 		}
200 
201 		public void actionPerformed(ActionEvent e) {
202 			saveAsDraft = true;
203 			doOKAction();
204 		}
205 	};
206 
207 	private void createUIComponents() {
208 	}
209 
210 	public CommentBean getComment() {
211 		return comment;
212 	}
213 
214 	/**
215 	 * Method generated by IntelliJ IDEA GUI Designer
216 	 * >>> IMPORTANT!! <<<
217 	 * DO NOT edit this method OR call it in your code!
218 	 *
219 	 * @noinspection ALL
220 	 */
221 	private void $$$setupUI$$$() {
222 		rootComponent = new JPanel();
223 		rootComponent.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
224 		rootComponent.setMinimumSize(new Dimension(650, 300));
225 		commentPane = new JScrollPane();
226 		rootComponent.add(commentPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
227 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW,
228 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
229 		commentText = new JTextArea();
230 		commentText.setLineWrap(false);
231 		commentText.setText("");
232 		commentText.setWrapStyleWord(false);
233 		commentPane.setViewportView(commentText);
234 		toolPanel = new JPanel();
235 		toolPanel.setLayout(new GridLayoutManager(1, 6, new Insets(0, 0, 0, 0), -1, -1));
236 		rootComponent.add(toolPanel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
237 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
238 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
239 		defectCheckBox = new JCheckBox();
240 		defectCheckBox.setText("Defect");
241 		toolPanel.add(defectCheckBox, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
242 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED,
243 				null, null, null, 0, false));
244 		postButton = new JButton();
245 		postButton.setText("Post");
246 		toolPanel.add(postButton, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_CENTER,
247 				GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
248 				GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
249 		saveAsDraftButton = new JButton();
250 		saveAsDraftButton.setText("Save as draft");
251 		toolPanel.add(saveAsDraftButton, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_CENTER,
252 				GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
253 				GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
254 		cancelButton = new JButton();
255 		cancelButton.setText("Cancel");
256 		toolPanel.add(cancelButton, new GridConstraints(0, 5, 1, 1, GridConstraints.ANCHOR_CENTER,
257 				GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
258 				GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
259 		final Spacer spacer1 = new Spacer();
260 		toolPanel.add(spacer1, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL,
261 				GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
262 		comboPanel = new JPanel();
263 		comboPanel.setLayout(new GridLayoutManager(1, 1, new Insets(0, 0, 0, 0), -1, -1));
264 		toolPanel.add(comboPanel, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH,
265 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
266 				GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
267 	}
268 
269 	/**
270 	 * @noinspection ALL
271 	 */
272 	public JComponent $$$getRootComponent$$$() {
273 		return rootComponent;
274 	}
275 }