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.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   * @author Jacek Jaroczynski
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 				// disable OK button
112 				freezeListener.fieldSyntaxError(fieldName);
113 			} else {
114 				// enable OK buttom
115 				freezeListener.fieldSyntaxOk(fieldName);
116 			}
117 			return !isIncorrect;
118 		}
119 	}
120 }