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;
17  
18  import com.intellij.openapi.ui.DialogWrapper;
19  import net.sf.nachocalendar.CalendarFactory;
20  import net.sf.nachocalendar.components.CalendarPanel;
21  import net.sf.nachocalendar.model.DateSelectionModel;
22  
23  import javax.swing.*;
24  import java.awt.*;
25  import java.util.Calendar;
26  import java.util.Date;
27  
28  /**
29   * @author Jacek Jaroczynski
30   */
31  public class DatePicker extends DialogWrapper {
32  
33  	private JPanel panel = new JPanel();
34  
35  	private CalendarPanel calendar;
36  	private JPanel bottomPanel = new JPanel();
37  
38  	public DatePicker(final String title, final Date dateToSelect) {
39  		super(false);
40  		init();
41  
42  		setTitle(title);
43  
44  		createCalendar(dateToSelect);
45  
46  		createPanel();
47  	}
48  
49  	private void createCalendar(final Date dateToSelect) {
50  		calendar = CalendarFactory.createCalendarPanel(1);
51  		calendar.setSelectionMode(DateSelectionModel.SINGLE_SELECTION);
52  
53  		Calendar nowcal = Calendar.getInstance();
54  
55  		// calculate time to midnight
56  		nowcal.setTime(dateToSelect);
57  		nowcal.set(Calendar.HOUR_OF_DAY, 0);
58  		nowcal.set(Calendar.MINUTE, 0);
59  		nowcal.set(Calendar.SECOND, 0);
60  		nowcal.set(Calendar.MILLISECOND, 0);
61  
62  		Date nowZeroZero = nowcal.getTime();
63  
64  		calendar.setDate(nowZeroZero);
65  		calendar.setValue(nowZeroZero);
66  	}
67  
68  	private void createPanel() {
69  		panel.setLayout(new GridBagLayout());
70  		GridBagConstraints gbc = new GridBagConstraints();
71  
72  		gbc.anchor = GridBagConstraints.CENTER;
73  		gbc.fill = GridBagConstraints.HORIZONTAL;
74  		gbc.gridx = 0;
75  		gbc.gridy = 0;
76  		gbc.gridwidth = 2;
77  		gbc.weightx = 1;
78  		panel.add(new JLabel("Day", SwingConstants.CENTER), gbc);
79  		gbc.gridy = 1;
80  		panel.add(calendar, gbc);
81  
82  		gbc.gridy = 2;
83  		panel.add(bottomPanel, gbc);
84  	}
85  
86  	@Override
87  	protected JComponent createCenterPanel() {
88  		return panel;
89  	}
90  
91  	/**
92  	 * @return JPanel which can hold additional controls (for inheritance purposes)
93  	 */
94  	protected JPanel getPanelComponent() {
95  		return bottomPanel;
96  	}
97  
98  	/**
99  	 * @return Date selected in the calendar control
100 	 */
101 	public Date getSelectedDate() {
102 		return (Date) calendar.getValue();
103 	}
104 }