001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.calendar.ui.comp;
019:
020: import java.awt.Event;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.util.regex.Matcher;
024: import java.util.regex.Pattern;
025:
026: import javax.swing.JComboBox;
027:
028: public class TimePicker extends JComboBox {
029:
030: int hour;
031:
032: int minutes;
033:
034: final static String defaultValue = "00:00";
035:
036: public TimePicker() {
037:
038: for (int i = 0; i <= 23; i++) {
039: addItem(i + ":00");
040: addItem(i + ":15");
041: addItem(i + ":30");
042: addItem(i + ":45");
043: }
044:
045: hour = 0;
046: minutes = 0;
047:
048: setSelectedItem("00:00");
049:
050: setEditable(true);
051:
052: addActionListener(new ActionListener() {
053:
054: public void actionPerformed(ActionEvent arg0) {
055: if (arg0.getActionCommand().equals("comboBoxChanged")) {
056: TimePicker.this
057: .setSelectedItem(validateInput((String) getSelectedItem()));
058: } else if (arg0.getActionCommand().equals(
059: "comboBoxEdited")) {
060: TimePicker.this
061: .setSelectedItem(validateInput((String) getSelectedItem()));
062: }
063: }
064:
065: });
066:
067: }
068:
069: public static String validateInput(String value) {
070:
071: // regexp for correct timestamp
072: boolean valid = value.matches("[0-2][0-9]:[0-5][0-9]");
073:
074: if (valid)
075: return value;
076:
077: if (value.indexOf(":") > -1) {
078: // check value before :
079: String before = value.substring(0, value.indexOf(":"));
080: int iBefore = -1;
081: int iAfter = -1;
082: try {
083: int testBefore = Integer.parseInt(before);
084: if ((testBefore > -1) && (testBefore < 24)) {
085: // correct value!
086: iBefore = testBefore;
087: }
088: } catch (NumberFormatException e) {
089: return defaultValue;
090: }
091:
092: // check value after :
093: String after = value.substring(value.indexOf(":") + 1);
094: try {
095: int testAfter = Integer.parseInt(after);
096: if ((testAfter > -1) && (testAfter < 60)) {
097: // correct value!
098: iAfter = testAfter;
099: }
100: } catch (NumberFormatException e) {
101: return defaultValue;
102: }
103:
104: if ((iBefore < 0) || (iAfter < 0))
105: return defaultValue;
106:
107: String correctedValue = "";
108: // both values are okay, so create a correct time
109: if (iBefore < 10)
110: correctedValue += "0";
111: correctedValue += iBefore + ":";
112: if (iAfter < 10)
113: correctedValue += "0";
114: correctedValue += iAfter;
115: return correctedValue;
116: }
117:
118: // there is no :
119: try {
120: int iValue = Integer.parseInt(value);
121: if (iValue > -1) {
122: if (iValue < 10) {
123: return "0" + iValue + ":00";
124: } else if (iValue < 24) {
125: return iValue + ":00";
126: }
127: }
128: // no correct value!
129: return defaultValue;
130: } catch (NumberFormatException e) {
131: return defaultValue;
132: }
133: }
134:
135: public int getHour() {
136: String value = (String) getSelectedItem();
137: int h = Integer
138: .parseInt(value.substring(0, value.indexOf(":")));
139:
140: return h;
141: }
142:
143: public int getMinutes() {
144: String value = (String) getSelectedItem();
145:
146: int m = Integer.parseInt(value.substring(
147: value.indexOf(":") + 1, value.length()));
148:
149: return m;
150: }
151:
152: public void setTime(int hour, int minutes) {
153:
154: StringBuffer buf = new StringBuffer();
155: buf.append(hour);
156: buf.append(":");
157: // in case we have to add another "0"
158: if (minutes == 0)
159: buf.append("00");
160: else
161: buf.append(minutes);
162:
163: setSelectedItem(buf.toString());
164: }
165:
166: }
|