Source code: javatools/swing/DatePanel.java
1 /*
2 * DatePanel.java
3 *
4 * Created on 28 aprile 2002, 18.53
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.swing;
26
27 import java.util.*;
28 import java.text.*;
29
30 /**
31 * It consists of a panel to get date-time information from the user.
32 * @author Antonio Petrelli
33 * @version 0.1.7
34 */
35 public class DatePanel extends javax.swing.JPanel {
36
37 /** Creates new form DatePanel */
38 public DatePanel() {
39 int i;
40 SimpleDateFormat formatter;
41 DecimalFormat decFormatter;
42 Date tempDate;
43
44 javatoolsBundle = java.util.ResourceBundle.getBundle("res/JavatoolsBundle");
45 initComponents();
46 cal = Calendar.getInstance();
47 cal.setLenient(true);
48 formatter = new SimpleDateFormat("MMMM");
49 decFormatter = new DecimalFormat("00");
50 for (i=1; i <= 31; i++)
51 cboDay.addItem(new Integer(i));
52 for (i=0; i < 12; i++) {
53 cal.set(1990, i, 1);
54 tempDate = cal.getTime();
55 cboMonth.addItem(formatter.format(tempDate));
56 }
57 for (i=0; i < 24; i++)
58 cboHours.addItem(decFormatter.format(new Integer(i)));
59 for (i=0; i < 60; i++)
60 cboMinutes.addItem(decFormatter.format(new Integer(i)));
61 for (i=0; i < 60; i++)
62 cboSeconds.addItem(decFormatter.format(new Integer(i)));
63 }
64
65 /** Initializes the panel values.
66 * @param yyyy The year.
67 * @param MM The month.
68 * @param dd The day.
69 * @param hh The hour (0-23).
70 * @param mm The minutes.
71 * @param ss The seconds.
72 */
73 public void init(int yyyy, int MM, int dd, int hh, int mm, int ss) {
74 cal.set(yyyy, MM, dd, hh, mm, ss);
75 dateValue = cal.getTime();
76 setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
77 cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
78 cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
79 }
80
81 /** Initializes the panel values.
82 * @param pDate The date to be set.
83 */
84 public void init(Date pDate) {
85 dateValue = pDate;
86 cal.setTime(dateValue);
87 setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
88 cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
89 cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
90 }
91
92 /** Initializes the panel with default values (1-1-1900 00:00)
93 */
94 public void init() {
95 dateValue = new Date();
96 cal.setTime(dateValue);
97 setDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
98 cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR),
99 cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
100 }
101
102 /** Returns the displayed date.
103 * @return The displayed date.
104 */
105 public Date getDate() {
106 cal.set(Integer.parseInt(txtYear.getText()),
107 cboMonth.getSelectedIndex(), cboDay.getSelectedIndex() + 1,
108 cboHours.getSelectedIndex(), cboMinutes.getSelectedIndex(),
109 cboSeconds.getSelectedIndex());
110 return cal.getTime();
111 }
112
113 /** This method is called from within the constructor to
114 * initialize the form.
115 * WARNING: Do NOT modify this code. The content of this method is
116 * always regenerated by the Form Editor.
117 */
118 private void initComponents() {//GEN-BEGIN:initComponents
119 cboDay = new javax.swing.JComboBox();
120 cboMonth = new javax.swing.JComboBox();
121 lblDate = new javax.swing.JLabel();
122 lblTime = new javax.swing.JLabel();
123 cboHours = new javax.swing.JComboBox();
124 cboMinutes = new javax.swing.JComboBox();
125 txtYear = new javax.swing.JTextField();
126 cboSeconds = new javax.swing.JComboBox();
127
128 setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
129
130 setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
131 cboDay.setToolTipText(javatoolsBundle.getString("Day_of_the_month"));
132 add(cboDay, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 30, 50, -1));
133
134 cboMonth.setToolTipText(javatoolsBundle.getString("Month_of_the_year"));
135 cboMonth.setPreferredSize(new java.awt.Dimension(120, 25));
136 add(cboMonth, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 30, -1, -1));
137
138 lblDate.setText(javatoolsBundle.getString("Date"));
139 add(lblDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));
140
141 lblTime.setText(javatoolsBundle.getString("Time_of_the_clock"));
142 add(lblTime, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, -1));
143
144 cboHours.setToolTipText(javatoolsBundle.getString("Hour_of_the_clock"));
145 add(cboHours, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, 50, -1));
146
147 cboMinutes.setToolTipText(javatoolsBundle.getString("Minutes_of_the_clock"));
148 add(cboMinutes, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 80, 50, -1));
149
150 txtYear.setToolTipText(javatoolsBundle.getString("Year"));
151 add(txtYear, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 30, 60, 20));
152
153 cboSeconds.setToolTipText(javatoolsBundle.getString("Seconds_of_the_clock"));
154 add(cboSeconds, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 80, 50, -1));
155
156 }//GEN-END:initComponents
157
158 // Variables declaration - do not modify//GEN-BEGIN:variables
159 private javax.swing.JComboBox cboHours;
160 private javax.swing.JComboBox cboMinutes;
161 private javax.swing.JComboBox cboDay;
162 private javax.swing.JComboBox cboSeconds;
163 private javax.swing.JTextField txtYear;
164 private javax.swing.JLabel lblDate;
165 private javax.swing.JComboBox cboMonth;
166 private javax.swing.JLabel lblTime;
167 // End of variables declaration//GEN-END:variables
168
169 private Calendar cal;
170 private Date dateValue;
171 private java.util.ResourceBundle javatoolsBundle;
172
173 private void setDate(int yyyy, int MM, int dd, int hh, int mm, int ss) {
174 txtYear.setText((new Integer(yyyy)).toString());
175 cboMonth.setSelectedIndex(MM);
176 cboDay.setSelectedIndex(dd - 1);
177 cboHours.setSelectedIndex(hh);
178 cboMinutes.setSelectedIndex(mm);
179 cboSeconds.setSelectedIndex(ss);
180 }
181 }