1 package org.lsst.ccs.plugin.jas3.elog;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.StringTokenizer;
7 import javax.swing.JFileChooser;
8 import javax.swing.SwingUtilities;
9 import javax.swing.DefaultListModel;
10 import javax.swing.*;
11 import org.lsst.ccs.elog.ELogConnectionUtils;
12 import org.lsst.ccs.elog.ElogEntry;
13 import org.lsst.ccs.elog.ElogTransactionResult;
14
15
16
17
18
19 public class LsstElogSubmissionDialog extends javax.swing.JPanel {
20
21 private ElogEntry entry;
22 private ArrayList<LsstElogAttachment> attachments = new ArrayList<>();
23 private String version = "";
24 private String category = "";
25 private String select = "select";
26 private String tagMessage;
27
28
29
30
31 public LsstElogSubmissionDialog() {
32 entry = new ElogEntry();
33
34 initComponents();
35
36
37 versionComboBox.setModel(new javax.swing.DefaultComboBoxModel(extendArray(ELogConnectionUtils.getVersions())));
38
39
40 categoryComboBox.setModel(new javax.swing.DefaultComboBoxModel());
41 tagList.setModel(new DefaultListModel());
42
43 }
44
45
46
47
48
49
50 @SuppressWarnings("unchecked")
51
52 private void initComponents() {
53
54 categoryLabel = new javax.swing.JLabel();
55 entryTextLabel = new javax.swing.JLabel();
56 jScrollPane1 = new javax.swing.JScrollPane();
57 entryTextArea = new javax.swing.JTextArea();
58 attachmentsLabel = new javax.swing.JLabel();
59 addAttachmentButton = new javax.swing.JButton();
60 tagsLabel = new javax.swing.JLabel();
61 titleLabel = new javax.swing.JLabel();
62 submitEntryButton = new javax.swing.JButton();
63 attachmentsPanel = new javax.swing.JPanel();
64 filler1 = new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));
65 categoryComboBox = new javax.swing.JComboBox();
66 versionLabel = new javax.swing.JLabel();
67 versionComboBox = new javax.swing.JComboBox();
68 jScrollPane2 = new javax.swing.JScrollPane();
69 tagList = new javax.swing.JList();
70 messageLabel = new javax.swing.JLabel();
71 clearTagsButton = new javax.swing.JButton();
72 clearFormButton = new javax.swing.JButton();
73
74 setPreferredSize(new java.awt.Dimension(484, 280));
75
76 categoryLabel.setText("Category:");
77
78 entryTextLabel.setText("Entry Text:");
79
80 entryTextArea.setColumns(20);
81 entryTextArea.setRows(5);
82 jScrollPane1.setViewportView(entryTextArea);
83
84 attachmentsLabel.setText("Attachments:");
85
86 addAttachmentButton.setText("Add Attachment ...");
87 addAttachmentButton.addActionListener(new java.awt.event.ActionListener() {
88 public void actionPerformed(java.awt.event.ActionEvent evt) {
89 addAttachmentButtonActionPerformed(evt);
90 }
91 });
92
93 tagsLabel.setText("List of Tags:");
94
95 titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
96 titleLabel.setText("New Elog Entry Form");
97
98 submitEntryButton.setText("Submit Elog Entry");
99 submitEntryButton.addActionListener(new java.awt.event.ActionListener() {
100 public void actionPerformed(java.awt.event.ActionEvent evt) {
101 submitEntryButtonActionPerformed(evt);
102 }
103 });
104
105 attachmentsPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
106 attachmentsPanel.setAlignmentY(1.0F);
107 attachmentsPanel.setMinimumSize(new java.awt.Dimension(40, 40));
108 attachmentsPanel.setLayout(new javax.swing.BoxLayout(attachmentsPanel, javax.swing.BoxLayout.Y_AXIS));
109
110 filler1.setAlignmentY(1.0F);
111 filler1.setAutoscrolls(true);
112 attachmentsPanel.add(filler1);
113
114 categoryComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
115 categoryComboBox.addActionListener(new java.awt.event.ActionListener() {
116 public void actionPerformed(java.awt.event.ActionEvent evt) {
117 categoryComboBoxActionPerformed(evt);
118 }
119 });
120
121 versionLabel.setText("eLog Version");
122
123 versionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
124 versionComboBox.addActionListener(new java.awt.event.ActionListener() {
125 public void actionPerformed(java.awt.event.ActionEvent evt) {
126 versionComboBoxActionPerformed(evt);
127 }
128 });
129
130 tagList.setModel(new javax.swing.AbstractListModel() {
131 String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
132 public int getSize() { return strings.length; }
133 public Object getElementAt(int i) { return strings[i]; }
134 });
135 jScrollPane2.setViewportView(tagList);
136
137 messageLabel.setForeground(new java.awt.Color(215, 62, 34));
138 messageLabel.setText(">");
139
140 clearTagsButton.setText("Clear selected tags");
141 clearTagsButton.addActionListener(new java.awt.event.ActionListener() {
142 public void actionPerformed(java.awt.event.ActionEvent evt) {
143 clearTagsButtonActionPerformed(evt);
144 }
145 });
146
147 clearFormButton.setText("Clear Form");
148 clearFormButton.addActionListener(new java.awt.event.ActionListener() {
149 public void actionPerformed(java.awt.event.ActionEvent evt) {
150 clearFormButtonActionPerformed(evt);
151 }
152 });
153
154 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
155 this.setLayout(layout);
156 layout.setHorizontalGroup(
157 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
158 .addGroup(layout.createSequentialGroup()
159 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
160 .addGroup(layout.createSequentialGroup()
161 .addGap(24, 24, 24)
162 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
163 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
164 .addComponent(tagsLabel)
165 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
166 .addGap(45, 45, 45)
167 .addComponent(versionLabel)))
168 .addGroup(layout.createSequentialGroup()
169 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
170 .addComponent(jScrollPane2)
171 .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
172 .addGap(1, 1, 1)
173 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
174 .addComponent(addAttachmentButton)
175 .addComponent(submitEntryButton)))
176 .addComponent(clearTagsButton, javax.swing.GroupLayout.Alignment.LEADING))
177 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
178 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
179 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
180 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
181 .addGroup(layout.createSequentialGroup()
182 .addComponent(versionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
183 .addGap(62, 62, 62)
184 .addComponent(categoryLabel)
185 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
186 .addComponent(categoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
187 .addGroup(layout.createSequentialGroup()
188 .addGap(19, 19, 19)
189 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
190 .addComponent(entryTextLabel)
191 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 753, Short.MAX_VALUE)))))
192 .addGroup(layout.createSequentialGroup()
193 .addGap(25, 25, 25)
194 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
195 .addComponent(messageLabel)
196 .addComponent(attachmentsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
197 .addComponent(attachmentsLabel)))))))
198 .addGroup(layout.createSequentialGroup()
199 .addGap(324, 324, 324)
200 .addComponent(titleLabel)
201 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
202 .addComponent(clearFormButton)
203 .addGap(0, 0, Short.MAX_VALUE)))
204 .addContainerGap())
205 );
206 layout.setVerticalGroup(
207 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
208 .addGroup(layout.createSequentialGroup()
209 .addGap(20, 20, 20)
210 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
211 .addComponent(titleLabel)
212 .addComponent(clearFormButton))
213 .addGap(18, 18, 18)
214 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
215 .addComponent(categoryLabel)
216 .addComponent(categoryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
217 .addComponent(versionLabel)
218 .addComponent(versionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
219 .addGap(13, 13, 13)
220 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
221 .addComponent(entryTextLabel, javax.swing.GroupLayout.Alignment.TRAILING)
222 .addComponent(tagsLabel))
223 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
224 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
225 .addGroup(layout.createSequentialGroup()
226 .addComponent(jScrollPane1)
227 .addGap(36, 36, 36))
228 .addGroup(layout.createSequentialGroup()
229 .addComponent(jScrollPane2)
230 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
231 .addComponent(clearTagsButton)
232 .addGap(33, 33, 33)))
233 .addComponent(attachmentsLabel)
234 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
235 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
236 .addComponent(addAttachmentButton)
237 .addComponent(attachmentsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
238 .addGap(10, 10, 10)
239 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
240 .addComponent(submitEntryButton)
241 .addComponent(messageLabel)))
242 );
243
244 getAccessibleContext().setAccessibleName("");
245 }
246
247 private void submitEntryButtonActionPerformed(java.awt.event.ActionEvent evt) {
248 if ( version.equals("") || version.startsWith(select)) {
249 messageLabel.setText("> Please select version and category");
250 return;
251 } else if ( category.equals("") || category.startsWith(select)) {
252 messageLabel.setText("> Please select category");
253 return;
254 }
255 messageLabel.setText(">");
256 entry.setCategory(category);
257
258 try {
259 entry.setFormName("default");
260 entry.setFormField("text", entryTextArea.getText());
261
262 if (tagList.isSelectionEmpty() ) {
263 tagMessage = "WARNING: No tags selected. ";
264 messageLabel.setText("> " + tagMessage);
265 } else {
266 tagMessage = "";
267 for (Object selectedTag : tagList.getSelectedValuesList()) {
268 entry.addTag( (String) selectedTag);
269 }
270 }
271
272 ElogTransactionResult result = ELogConnectionUtils.postEntryToElog(entry, version);
273 entry.reset();
274 SwingUtilities.invokeLater(new ClearDialog());
275 messageLabel.setText("> " + tagMessage + "Entry submitted.");
276 } catch (Exception e) {
277 e.printStackTrace();
278 }
279
280 }
281
282 private void addAttachmentButtonActionPerformed(java.awt.event.ActionEvent evt) {
283 messageLabel.setText(">");
284 JFileChooser dlg = new JFileChooser();
285 dlg.setDialogTitle("Select an Image");
286 dlg.setApproveButtonText("Select");
287 if (dlg.showOpenDialog(LsstElogSubmissionDialog.this) == JFileChooser.APPROVE_OPTION) {
288 final File file = dlg.getSelectedFile();
289 try {
290 String fileName = file.getName();
291 LsstElogAttachment attach = new LsstElogAttachment(fileName, ElogEntry.isFileImage(fileName), LsstElogSubmissionDialog.this);
292 attachments.add(attach);
293 entry.addFile(fileName, file);
294 SwingUtilities.invokeLater(new AddAttachment(attach));
295 } catch (Exception e) {
296 e.printStackTrace();
297 }
298 }
299 }
300
301 private void categoryComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
302 messageLabel.setText(">");
303
304 category = (String) categoryComboBox.getSelectedItem();
305 entry.setCategory(category);
306
307
308 try {
309 List<String> tags = ELogConnectionUtils.getTags(version);
310 DefaultListModel model = new DefaultListModel();
311 tagList.setModel(model);
312 for (String tag : tags) {
313 model.addElement(tag);
314 }
315 } catch (Exception e) {
316 throw new RuntimeException(e);
317 }
318 }
319
320 private void versionComboBoxActionPerformed(java.awt.event.ActionEvent evt) {
321 messageLabel.setText(">");
322 version = (String) versionComboBox.getSelectedItem();
323 category = "";
324 tagList.setModel(new DefaultListModel());
325 if (version.startsWith(select)) {
326 categoryComboBox.setModel(new javax.swing.DefaultComboBoxModel());
327 return;
328
329 }
330
331
332 try {
333
334 List<String> categories = ELogConnectionUtils.getCategories(version);
335 String[] cats = new String[categories.size()];
336 cats = categories.toArray(cats);
337 categoryComboBox.setModel(new javax.swing.DefaultComboBoxModel(extendArray(cats)));
338
339 } catch (Exception e) {
340 throw new RuntimeException(e);
341 }
342 }
343
344 private void clearTagsButtonActionPerformed(java.awt.event.ActionEvent evt) {
345 messageLabel.setText(">");
346 tagList.clearSelection();
347 }
348
349 private void clearFormButtonActionPerformed(java.awt.event.ActionEvent evt) {
350 messageLabel.setText(">");
351 tagList.clearSelection();
352 SwingUtilities.invokeLater(new ClearDialog());
353 }
354
355 void removeAttachment(LsstElogAttachment attach) {
356 messageLabel.setText(">");
357 attachments.remove(attach);
358 entry.removeAttachment(attach.getFileName(), attach.isImage());
359 SwingUtilities.invokeLater(new RemoveAttachment(attach));
360 }
361
362 private String[] extendArray(String[] array) {
363 String[] newArray = new String[1 + array.length];
364 newArray[0] = select;
365 System.arraycopy(array, 0, newArray, 1, array.length);
366 return newArray;
367 }
368
369 private class ClearDialog implements Runnable {
370
371 ClearDialog() {
372 }
373
374 @Override
375 public void run() {
376 for (LsstElogAttachment attach : attachments) {
377 attachmentsPanel.remove(attach);
378 }
379 attachmentsPanel.revalidate();
380 attachments.clear();
381 entryTextArea.setText("");
382 }
383 }
384
385 private class AddAttachment implements Runnable {
386
387 private LsstElogAttachment attach;
388
389 AddAttachment(LsstElogAttachment attach) {
390 this.attach = attach;
391 }
392
393 @Override
394 public void run() {
395 attachmentsPanel.add(attach, 0);
396 }
397 }
398
399 private class RemoveAttachment implements Runnable {
400
401 private LsstElogAttachment attach;
402
403 RemoveAttachment(LsstElogAttachment attach) {
404 this.attach = attach;
405 }
406
407 @Override
408 public void run() {
409 attachmentsPanel.remove(attach);
410 attachmentsPanel.revalidate();
411 attachmentsPanel.repaint();
412 }
413 }
414
415 private javax.swing.JButton addAttachmentButton;
416 private javax.swing.JLabel attachmentsLabel;
417 private javax.swing.JPanel attachmentsPanel;
418 private javax.swing.JComboBox categoryComboBox;
419 private javax.swing.JLabel categoryLabel;
420 private javax.swing.JButton clearFormButton;
421 private javax.swing.JButton clearTagsButton;
422 private javax.swing.JTextArea entryTextArea;
423 private javax.swing.JLabel entryTextLabel;
424 private javax.swing.Box.Filler filler1;
425 private javax.swing.JScrollPane jScrollPane1;
426 private javax.swing.JScrollPane jScrollPane2;
427 private javax.swing.JLabel messageLabel;
428 private javax.swing.JButton submitEntryButton;
429 private javax.swing.JList tagList;
430 private javax.swing.JLabel tagsLabel;
431 private javax.swing.JLabel titleLabel;
432 private javax.swing.JComboBox versionComboBox;
433 private javax.swing.JLabel versionLabel;
434
435 }