KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.sql.Timestamp JavaDoc;
22
23 import org.compiere.util.*;
24
25 /**
26  * Date Model.
27  * Validates input based on date pattern
28  * @see VDate
29  *
30  * @author Jorg Janke
31  * @version $Id: MDocDate.java,v 1.8 2003/04/29 09:20:11 jpedersen Exp $
32  */

33 public final class MDocDate extends PlainDocument implements CaretListener
34 {
35     /**
36      * Constructor
37      * @param displayType display type
38      * @param format format
39      * @param tc text component
40      * @param title title
41      */

42     public MDocDate (int displayType, SimpleDateFormat format,
43         JTextComponent tc, String JavaDoc title)
44     {
45         super();
46         m_displayType = displayType;
47         m_tc = tc;
48         m_tc.addCaretListener(this);
49         //
50
m_format = format;
51         if (m_format == null)
52             m_format = new SimpleDateFormat();
53         m_format.setLenient(false);
54
55         // Mark delimiters as '^' in Pattern
56
char[] pattern = m_format.toPattern().toCharArray();
57         for (int i = 0; i < pattern.length; i++)
58         {
59             // do we have a delimiter?
60
if ("Mdy".indexOf(pattern[i]) == -1)
61                 pattern[i] = DELIMITER;
62         }
63         m_mask = new String JavaDoc(pattern);
64         //
65
m_title = title;
66         if (m_title == null)
67             m_title = "";
68     } // MDocDate
69

70     private JTextComponent m_tc;
71     private SimpleDateFormat m_format;
72     private String JavaDoc m_mask;
73     private static final char DELIMITER = '^';
74     // for Calendar
75
private String JavaDoc m_title;
76     private int m_displayType;
77     private int m_lastDot = 0; // last dot position
78

79     /**
80      * Insert String
81      * @param offset offset
82      * @param string string
83      * @param attr attributes
84      * @throws BadLocationException
85      */

86     public void insertString (int offset, String JavaDoc string, AttributeSet attr)
87         throws BadLocationException
88     {
89     // Log.trace(Log.l5_DData, "MDocDate.insertString - Offset=" + offset
90
// + " String=" + string + " Attr=" + attr
91
// + " Text=" + getText() + " Length=" + getText().length());
92

93         // manual entry
94
// DBTextDataBinder.updateText sends stuff at once - length=8
95
if (string != null && string.length() == 1)
96         {
97             // ignore if too long
98
if (offset >= m_mask.length())
99                 return;
100
101             // is it an empty field?
102
int length = getText().length();
103             if (offset == 0 && length == 0)
104             {
105                 Date today = new Date(System.currentTimeMillis());
106                 String JavaDoc dateStr = m_format.format(today);
107                 super.insertString(0, string + dateStr.substring(1), attr);
108                 m_tc.setCaretPosition(1);
109                 return;
110             }
111
112             // is it a digit ?
113
try
114             {
115                 Integer.parseInt(string);
116             }
117             catch (Exception JavaDoc pe)
118             {
119                 startDateDialog();
120                 return;
121             }
122
123             // try to get date in field, if invalid, get today's
124
/*try
125             {
126                 char[] cc = getText().toCharArray();
127                 cc[offset] = string.charAt(0);
128                 m_format.parse(new String(cc));
129             }
130             catch (ParseException pe)
131             {
132                 startDateDialog();
133                 return;
134             }*/

135
136             // positioned before the delimiter - jump over delimiter
137
if (offset != m_mask.length()-1 && m_mask.charAt(offset+1) == DELIMITER)
138                 m_tc.setCaretPosition(offset+2);
139
140             // positioned at the delimiter
141
if (m_mask.charAt(offset) == DELIMITER)
142             {
143                 offset++;
144                 m_tc.setCaretPosition(offset+1);
145             }
146             super.remove(offset, 1); // replace current position
147
}
148
149         // Set new character
150
super.insertString(offset, string, attr);
151     } // insertString
152

153     /**
154      * Delete String
155      * @param offset offset
156      * @param length length
157      * @throws BadLocationException
158      */

159     public void remove (int offset, int length)
160         throws BadLocationException
161     {
162     // Log.trace(Log.l5_DData, "MDocDate.remove - Offset=" + offset
163
// + " Length=" + length);
164

165         // begin of string
166
if (offset == 0 || length == 0)
167         {
168             // empty the field
169
if (length == m_mask.length() || length == 0)
170                 super.remove(offset, length);
171             return;
172         }
173
174         // one position behind delimiter
175
if (offset-1 >= 0 && offset-1 < m_mask.length()
176             && m_mask.charAt(offset-1) == DELIMITER)
177         {
178             if (offset-2 >= 0)
179                 m_tc.setCaretPosition(offset-2);
180             else
181                 return;
182         }
183         else
184             m_tc.setCaretPosition(offset-1);
185     } // deleteString
186

187     /**
188      * Caret Listener
189      * @param e event
190      */

191     public void caretUpdate(CaretEvent e)
192     {
193         // Selection
194
if (e.getDot() != e.getMark())
195         {
196             m_lastDot = e.getDot();
197             return;
198         }
199         //
200
// Log.trace(Log.l5_DData, "MDocDate.caretUpdate - Dot=" + e.getDot()
201
// + ", Mark=" + e.getMark());
202

203         // Is the current position a fixed character?
204
if (e.getDot()+1 > m_mask.length() || m_mask.charAt(e.getDot()) != DELIMITER)
205         {
206             m_lastDot = e.getDot();
207             return;
208         }
209
210         // Direction?
211
int newDot = -1;
212         if (m_lastDot > e.getDot()) // <-
213
newDot = e.getDot() - 1;
214         else // -> (or same)
215
newDot = e.getDot() + 1;
216         if (e.getDot() == 0) // first
217
newDot = 1;
218         else if (e.getDot() == m_mask.length()-1) // last
219
newDot = e.getDot() - 1;
220         //
221
// Log.trace(Log.l5_DData, "OnFixedChar=" + m_mask.charAt(e.getDot())
222
// + ", newDot=" + newDot + ", last=" + m_lastDot);
223
//
224
m_lastDot = e.getDot();
225         if (newDot >= 0 && newDot < getText().length())
226             m_tc.setCaretPosition(newDot);
227     } // caretUpdate
228

229     /**
230      * Get Full Text
231      * @return text
232      */

233     private String JavaDoc getText()
234     {
235         String JavaDoc str = "";
236         try
237         {
238             str = getContent().getString(0, getContent().length()-1); // cr at end
239
}
240         catch (Exception JavaDoc e)
241         {
242             str = "";
243         }
244         return str;
245     } // getString
246

247     /**
248      * Call Calendar Dialog
249      */

250     private void startDateDialog()
251     {
252         Log.trace(Log.l3_Util, "MDocDate.startDateDialog");
253
254         // Date Dialog
255
String JavaDoc result = getText();
256         Timestamp JavaDoc ts = null;
257         try
258         {
259             ts = new Timestamp JavaDoc(m_format.parse(result).getTime());
260         }
261         catch (Exception JavaDoc pe)
262         {
263             ts = new Timestamp JavaDoc(System.currentTimeMillis());
264         }
265         ts = VDate.startCalendar(m_tc, ts, m_format, m_displayType, m_title);
266         result = m_format.format(ts);
267
268         // move to field
269
try
270         {
271             super.remove(0, getText().length());
272             super.insertString(0, result, null);
273         }
274         catch (BadLocationException ble)
275         {
276             Log.error("MDocDate.startDateDialog", ble);
277         }
278     } // startDateDialog
279

280 } // MDocDate
281
Popular Tags