1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.atlassian.theplugin.idea.jira.controls;
17
18 import com.atlassian.theplugin.idea.jira.ColoredTextFieldListener;
19 import com.atlassian.theplugin.idea.jira.DatePicker;
20 import com.atlassian.theplugin.jira.api.JIRAActionField;
21 import com.atlassian.theplugin.util.PluginUtil;
22 import com.intellij.openapi.ui.DialogWrapper;
23
24 import javax.swing.*;
25 import java.awt.*;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.text.ParseException;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import java.util.Locale;
32
33
34
35
36 public class FieldDueDate extends JPanel implements ActionFieldEditor {
37 private FieldTextField textField;
38 private static final int BOX_WIDTH = 5;
39 private static final String DUE_DATE_FORMAT = "dd/MMM/yy";
40
41 public FieldDueDate(final String date, final JIRAActionField field, final FreezeListener freezeListener) {
42
43 super();
44
45 setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
46
47 textField = new FieldTextField(date, field);
48 add(textField);
49 add(Box.createRigidArea(new Dimension(BOX_WIDTH, 0)));
50 final JButton button = new JButton("Select a Date");
51 add(button);
52
53 textField.getDocument().addDocumentListener(
54 new LocalDateTextFieldListener(textField, freezeListener, getFieldName(), true));
55
56 button.addActionListener(new ActionListener() {
57 public void actionPerformed(final ActionEvent event) {
58
59 SimpleDateFormat dateFormatter = new SimpleDateFormat(DUE_DATE_FORMAT, Locale.US);
60 Date dueDate;
61
62 try {
63 dueDate = dateFormatter.parse(textField.getText());
64 } catch (ParseException e) {
65 PluginUtil.getLogger().info("Wrong date format [" + textField.getText() + "]. Using TODAY", e);
66 dueDate = new Date();
67 }
68
69 DatePicker datePicker = new DatePicker("Select Due Date", dueDate);
70 datePicker.show();
71 if (datePicker.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
72 SimpleDateFormat dateFormat = new SimpleDateFormat(DUE_DATE_FORMAT, Locale.US);
73 textField.setText(dateFormat.format(datePicker.getSelectedDate()));
74 }
75 }
76 });
77 }
78
79 public JIRAActionField getEditedFieldValue() {
80 return textField.getEditedFieldValue();
81 }
82
83 public Component getComponent() {
84 return this;
85 }
86
87 public String getFieldName() {
88 return textField.getFieldName();
89 }
90
91 private class LocalDateTextFieldListener extends ColoredTextFieldListener {
92 private static final String REGEX = "^\\s*\\d{1,2}/[a-zA-Z]{3}/\\d{1,4}\\s*$";
93
94 private FreezeListener freezeListener;
95 private String fieldName;
96
97 public LocalDateTextFieldListener(
98 final FieldTextField textField, final FreezeListener freezeListener, final String fieldName,
99 final boolean emptyOk) {
100 super(textField, REGEX, emptyOk);
101
102 this.freezeListener = freezeListener;
103 this.fieldName = fieldName;
104 }
105
106 @Override
107 public boolean stateChanged() {
108 boolean isIncorrect = !super.stateChanged();
109
110 if (isIncorrect) {
111
112 freezeListener.fieldSyntaxError(fieldName);
113 } else {
114
115 freezeListener.fieldSyntaxOk(fieldName);
116 }
117 return !isIncorrect;
118 }
119 }
120 }