KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > JDateField


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/JDateField.java,v 1.8 2004/02/13 02:21:38 sebb Exp $
2
/*
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.util;
20
21 import java.awt.event.FocusEvent JavaDoc;
22 import java.awt.event.FocusListener JavaDoc;
23 import java.awt.event.KeyAdapter JavaDoc;
24 import java.awt.event.KeyEvent JavaDoc;
25 import java.text.DateFormat JavaDoc;
26 import java.text.ParseException JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Calendar JavaDoc;
29 import java.util.Date JavaDoc;
30
31 import javax.swing.JTextField JavaDoc;
32
33 /**
34  * This is Date mask control. Using this control we can pop up our date in the
35  * text field. And this control is Devloped basically for JDK1.3 and lower
36  * version support. This control is similer to JSpinner control this is
37  * available in JDK1.4 and above only.
38  * <p>
39  * This will set the date "yyyy/MM/dd HH:mm:ss" in this format only.
40  * </p>
41  *
42  * @author T.Elanjchezhiyan
43  * @version $Revision: 1.8 $ Last update: $Date: 2004/02/13 02:21:38 $
44  */

45 public class JDateField extends JTextField JavaDoc
46 {
47     private final static DateFormat JavaDoc dateFormat =
48         new SimpleDateFormat JavaDoc("yyyy/MM/dd HH:mm:ss");
49
50     /*
51      * The following array must agree with dateFormat
52      *
53      * It is used to translate the positions in the buffer
54      * to the values used by the Calendar class for the field id.
55      *
56      * Current format:
57      * MM/DD/YYYY HH:MM:SS
58      * 01234567890123456789
59      * ^buffer positions
60      */

61     private static int fieldPositions [] = {
62         Calendar.YEAR, // Y
63
Calendar.YEAR, // Y
64
Calendar.YEAR, // Y
65
Calendar.YEAR, // Y
66
Calendar.YEAR, // sp
67
Calendar.MONTH, // M
68
Calendar.MONTH, // M
69
Calendar.MONTH, // /
70
Calendar.DAY_OF_MONTH, // D
71
Calendar.DAY_OF_MONTH, // D
72
Calendar.DAY_OF_MONTH, // /
73
Calendar.HOUR_OF_DAY, // H
74
Calendar.HOUR_OF_DAY, // H
75
Calendar.HOUR_OF_DAY, // :
76
Calendar.MINUTE, // M
77
Calendar.MINUTE, // M
78
Calendar.MINUTE, // :
79
Calendar.SECOND, // S
80
Calendar.SECOND, // S
81
Calendar.SECOND // end
82
};
83
84     /**
85      * Create a DateField with the specified date.
86      */

87     public JDateField(Date JavaDoc date)
88     {
89         super(20);
90         this.addKeyListener(new KeyFocus());
91         this.addFocusListener(new FocusClass());
92         String JavaDoc myString = dateFormat.format(date);
93         setText(myString);
94     }
95
96     // Dummy constructor to allo JUnit tests to work
97
public JDateField()
98     {
99         this(new Date JavaDoc());
100     }
101     
102     /**
103      * Set the date to the Date mask control.
104      */

105     public void setDate(Date JavaDoc date)
106     {
107         setText(dateFormat.format(date));
108     }
109
110     /**
111      * Get the date from the Date mask control.
112      */

113     public Date JavaDoc getDate()
114     {
115         try
116         {
117             return dateFormat.parse(getText());
118         }
119         catch (ParseException JavaDoc e)
120         {
121             return new Date JavaDoc();
122         }
123         catch (Exception JavaDoc e)
124         {
125             // DateFormat.parse has some bugs (up to JDK 1.4.2) by which it
126
// throws unchecked exceptions. E.g. see:
127
// http://developer.java.sun.com/developer/bugParade/bugs/4699765.html
128
//
129
// To avoid problems with such situations, we'll catch all
130
// exceptions here and act just as for ParseException above:
131
return new Date JavaDoc();
132         }
133     }
134
135     /*
136      * Convert position in buffer to Calendar type
137      * Assumes that pos >=0 (which is true for getCaretPosition())
138      */

139     private static int posToField(int pos){
140         if (pos >= fieldPositions.length) { // if beyond the end
141
pos = fieldPositions.length - 1; // then set to the end
142
}
143         return fieldPositions[pos];
144     }
145
146
147     /**
148      * Converts a date/time to a calendar using the defined format
149      */

150     private static Calendar JavaDoc parseDate(String JavaDoc datetime)
151     {
152         Calendar JavaDoc c = Calendar.getInstance();
153         try
154         {
155             Date JavaDoc dat = dateFormat.parse(datetime);
156             c.setTime(dat);
157         }
158         catch (ParseException JavaDoc e)
159         {
160             //Do nothing; the current time will be returned
161
}
162         return c;
163     }
164     
165     /*
166      * Update the current field. The addend is only expected to be +1/-1,
167      * but other values will work.
168      * N.B. the roll() method only supports changes by a single unit - up or down
169      */

170     private void update(int addend, boolean shifted){
171         Calendar JavaDoc c = parseDate(getText());
172         int pos = getCaretPosition();
173         int field = posToField(pos);
174         if (shifted){
175             c.roll(field,true);
176         } else {
177             c.add(field,addend);
178         }
179         String JavaDoc newDate =dateFormat.format(c.getTime());
180         setText(newDate);
181         if (pos > newDate.length()) pos = newDate.length();
182         setCaretPosition(pos);// Restore position
183

184     }
185     /**
186      * @author T.Elanjchezhiyan
187      * @version $Revision: 1.8 $
188      */

189     class KeyFocus extends KeyAdapter JavaDoc
190     {
191         KeyFocus()
192         {
193         }
194         
195         public void keyPressed(KeyEvent JavaDoc e)
196         {
197             if (e.getKeyCode() == KeyEvent.VK_UP)
198             {
199                 update(1,e.isShiftDown());
200             }
201             else if (e.getKeyCode() == KeyEvent.VK_DOWN)
202             {
203                 update(-1,e.isShiftDown());
204             }
205         }
206     }
207
208     /**
209      * @author T.Elanjchezhiyan
210      * @version $Revision: 1.8 $
211      */

212     class FocusClass implements FocusListener JavaDoc
213     {
214         FocusClass()
215         {
216         }
217         public void focusGained(FocusEvent JavaDoc e)
218         {
219         }
220         public void focusLost(FocusEvent JavaDoc e)
221         {
222             try
223             {
224                 dateFormat.parse(getText());
225             }
226             catch (ParseException JavaDoc e1)
227             {
228                 requestFocus();
229             }
230         }
231     }
232 }
233
Popular Tags