KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > calendar > ui > comp > TimePicker


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.calendar.ui.comp;
19
20 import java.awt.Event JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import javax.swing.JComboBox JavaDoc;
27
28 public class TimePicker extends JComboBox JavaDoc {
29
30     int hour;
31
32     int minutes;
33
34     final static String JavaDoc defaultValue = "00:00";
35
36     public TimePicker() {
37
38         for (int i = 0; i <= 23; i++) {
39             addItem(i + ":00");
40             addItem(i + ":15");
41             addItem(i + ":30");
42             addItem(i + ":45");
43         }
44
45         hour = 0;
46         minutes = 0;
47
48         setSelectedItem("00:00");
49         
50         setEditable(true);
51         
52         addActionListener(new ActionListener JavaDoc() {
53
54             public void actionPerformed(ActionEvent JavaDoc arg0) {
55                 if (arg0.getActionCommand().equals("comboBoxChanged")) {
56                     TimePicker.this.setSelectedItem(validateInput((String JavaDoc) getSelectedItem()));
57                 } else if (arg0.getActionCommand().equals("comboBoxEdited")) {
58                     TimePicker.this.setSelectedItem(validateInput((String JavaDoc) getSelectedItem()));
59                 }
60             }
61             
62         });
63
64     }
65
66     public static String JavaDoc validateInput(String JavaDoc value) {
67                 
68         // regexp for correct timestamp
69
boolean valid = value.matches("[0-2][0-9]:[0-5][0-9]");
70         
71         if (valid)
72             return value;
73         
74         if (value.indexOf(":") > -1) {
75             // check value before :
76
String JavaDoc before = value.substring(0, value.indexOf(":"));
77             int iBefore = -1;
78             int iAfter = -1;
79             try {
80                 int testBefore = Integer.parseInt(before);
81                 if ((testBefore > -1) && (testBefore < 24)) {
82                     // correct value!
83
iBefore = testBefore;
84                 }
85             } catch (NumberFormatException JavaDoc e) {
86                 return defaultValue;
87             }
88
89             // check value after :
90
String JavaDoc after = value.substring(value.indexOf(":") + 1);
91             try {
92                 int testAfter = Integer.parseInt(after);
93                 if ((testAfter > -1) && (testAfter < 60)) {
94                     // correct value!
95
iAfter = testAfter;
96                 }
97             } catch (NumberFormatException JavaDoc e) {
98                 return defaultValue;
99             }
100             
101             if ((iBefore < 0) || (iAfter < 0))
102                 return defaultValue;
103             
104             String JavaDoc correctedValue = "";
105             // both values are okay, so create a correct time
106
if (iBefore < 10)
107                 correctedValue += "0";
108             correctedValue += iBefore + ":";
109             if (iAfter < 10)
110                 correctedValue += "0";
111             correctedValue += iAfter;
112             return correctedValue;
113         }
114         
115         // there is no :
116
try {
117             int iValue = Integer.parseInt(value);
118             if (iValue > -1) {
119                 if (iValue < 10) {
120                     return "0" + iValue + ":00";
121                 } else if (iValue < 24) {
122                     return iValue + ":00";
123                 }
124             }
125             // no correct value!
126
return defaultValue;
127         } catch (NumberFormatException JavaDoc e) {
128             return defaultValue;
129         }
130     }
131
132     public int getHour() {
133         String JavaDoc value = (String JavaDoc) getSelectedItem();
134         int h = Integer.parseInt(value.substring(0, value.indexOf(":")));
135
136         return h;
137     }
138
139     public int getMinutes() {
140         String JavaDoc value = (String JavaDoc) getSelectedItem();
141
142         int m = Integer.parseInt(value.substring(value.indexOf(":")+1, value.length()));
143
144         return m;
145     }
146
147     public void setTime(int hour, int minutes) {
148         
149         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
150         buf.append(hour);
151         buf.append(":");
152         // in case we have to add another "0"
153
if ( minutes == 0)
154             buf.append("00");
155         else
156             buf.append(minutes);
157
158         setSelectedItem(buf.toString());
159     }
160
161 }
162
Popular Tags