KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > grid > ed > MDocTime


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.grid.ed;
15
16 import javax.swing.text.*;
17 import javax.swing.event.*;
18 import java.text.*;
19 import java.util.*;
20 import java.awt.*;
21
22 import org.compiere.util.*;
23
24 /**
25  * Time Model.
26  * Validates input for hour or minute field
27  * @see VDate
28  *
29  * @author Jorg Janke
30  * @version $Id: MDocTime.java,v 1.2 2003/09/29 01:04:41 jjanke Exp $
31  */

32 public final class MDocTime extends PlainDocument
33 {
34     /**
35      * Constructor
36      * @param isHour Hour field
37      * @param is12Hour 12 hour format
38      */

39     public MDocTime(boolean isHour, boolean is12Hour)
40     {
41         super();
42         m_isHour = isHour;
43         m_is12Hour = is12Hour;
44     } // MDocTime
45

46     private boolean m_isHour;
47     private boolean m_is12Hour;
48
49     /**
50      * Insert String
51      * @param offset offset
52      * @param string string
53      * @param attr attributes
54      * @throws BadLocationException
55      */

56     public void insertString (int offset, String JavaDoc string, AttributeSet attr)
57         throws BadLocationException
58     {
59     // Log.trace(Log.l5_DData, "MDocTime.insertString - Offset=" + offset
60
// + ", String=" + string + ", Attr=" + attr + ", Text=" + getText() + ", Length=" + getText().length());
61

62         // manual entry
63
// DBTextDataBinder.updateText sends stuff at once
64
if (string != null && string.length() == 1)
65         {
66             // ignore if too long
67
if (offset > 2)
68                 return;
69
70             // is it a digit ?
71
if (!Character.isDigit(string.charAt(0)))
72             {
73                 Log.trace(Log.l4_Data, "MDocTime.insertString - no Digit", string);
74                 return;
75             }
76
77             // resulting string
78
char[] cc = getText().toCharArray();
79             cc[offset] = string.charAt(0);
80             String JavaDoc result = new String JavaDoc(cc);
81
82             int i = 0;
83             try
84             {
85                 i = Integer.parseInt(result.trim());
86             }
87             catch (Exception JavaDoc e)
88             {
89                 Log.error("MDocTime.insertString" + e);
90             }
91             if (i < 0)
92             {
93                 Log.trace(Log.l4_Data, "MDocTime.insertString - invalid value: " + i);
94                 return;
95             }
96             // Minutes
97
if (!m_isHour && i > 59)
98             {
99                 Log.trace(Log.l4_Data, "MDocTime.insertString - invalid minute value: " + i);
100                 return;
101             }
102             // Hour
103
if (m_isHour && m_is12Hour && i > 12)
104             {
105                 Log.trace(Log.l4_Data, "MDocTime.insertString - invalid 12 hour value: " + i);
106                 return;
107             }
108             if (m_isHour && !m_is12Hour && i > 24)
109             {
110                 Log.trace(Log.l4_Data, "MDocTime.insertString - invalid 24 hour value: " + i);
111                 return;
112             }
113             //
114
// super.remove(offset, 1); // replace current position
115
}
116         // Set new character
117
super.insertString(offset, string, attr);
118     } // insertString
119

120     /**
121      * Delete String
122      * @param offset offset
123      * @param length length
124      * @throws BadLocationException
125      */

126     public void remove (int offset, int length)
127         throws BadLocationException
128     {
129     // Log.trace(Log.l5_DData, "MDocTime.remove - Offset=" + offset + ", Length=" + length);
130

131         super.remove(offset, length);
132     } // deleteString
133

134     /**
135      * Get Full Text (always two character)
136      * @return text
137      */

138     private String JavaDoc getText()
139     {
140         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141         try
142         {
143             sb.append(getContent().getString(0, getContent().length()-1)); // cr at end
144
}
145         catch (Exception JavaDoc e)
146         {
147         }
148         while (sb.length() < 2)
149             sb.insert(0, ' ');
150         return sb.toString();
151     } // getString
152

153 } // MDocTime
154
Popular Tags