KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import org.compiere.util.*;
21
22 /**
23  * String Input Verification.
24  * <pre>
25  * - Length is set to length of VFormat or FieldLength if no format defined
26  * Control Characters
27  * (Space) any character
28  * _ Space (fixed character)
29  *
30  * l any Letter a..Z NO space
31  * L any Letter a..Z NO space converted to upper case
32  * o any Letter a..Z or space
33  * O any Letter a..Z or space converted to upper case
34  *
35  * 0 Digits 0..9 NO space
36  * 9 Digits 0..9 or space
37  *
38  * a any Letters & Digits NO space
39  * A any Letters & Digits NO space converted to upper case
40  * c any Letters & Digits or space
41  * C any Letters & Digits or space converted to upper case
42  * </pre>
43  * @see VString
44  * @author Jorg Janke
45  * @version $Id: MDocString.java,v 1.4 2002/05/26 01:22:24 jjanke Exp $
46  */

47 public final class MDocString extends PlainDocument implements CaretListener
48 {
49     /**
50      * Constructor
51      * @param VFormat
52      * @param fieldLength
53      * @param tc
54      */

55     public MDocString(String JavaDoc VFormat, int fieldLength, JTextComponent tc)
56     {
57         super();
58         m_fieldLength = fieldLength;
59         setFormat (VFormat);
60         m_tc = tc;
61         if (tc != null)
62             m_tc.addCaretListener(this);
63     } // MDocNumber
64

65     private String JavaDoc m_VFormat; // Value Format String
66
private String JavaDoc m_mask; // Fixed Elements
67
private int m_fieldLength;
68     private int m_maxLength;
69     private JTextComponent m_tc;
70     private int m_lastDot = 0; // last dot position
71
//
72
private static final char SPACE = ' ';
73     private static final char SPACE_IND = '_';
74
75     /**
76      * Set Format
77      * @param VFormat
78      */

79     public final void setFormat (String JavaDoc VFormat)
80     {
81     // Log.trace(Log.l4_Data, "MDocString.setFormat - " + VFormat);
82
//
83
m_VFormat = VFormat;
84         if (m_VFormat == null)
85             m_VFormat = "";
86         m_mask = m_VFormat;
87         // Set Length
88
m_maxLength = m_fieldLength;
89         if (m_VFormat.length() == 0)
90             return;
91         if (m_maxLength > m_VFormat.length())
92             m_maxLength = m_VFormat.length();
93
94         // Create Mask
95
m_mask = m_mask.replace('c', SPACE); // c any Letter or Digit or space
96
m_mask = m_mask.replace('C', SPACE); // C any Letter or Digit or space converted to upper case
97
m_mask = m_mask.replace('a', SPACE); // a any Letters or Digits NO space
98
m_mask = m_mask.replace('A', SPACE); // A any Letters or Digits NO space converted to upper case
99
m_mask = m_mask.replace('l', SPACE); // l any Letter a..Z NO space
100
m_mask = m_mask.replace('L', SPACE); // L any Letter a..Z NO space converted to upper case
101
m_mask = m_mask.replace('o', SPACE); // o any Letter a..Z or space
102
m_mask = m_mask.replace('O', SPACE); // O any Letter a..Z or space converted to upper case
103
m_mask = m_mask.replace('0', SPACE); // 0 Digits 0..9 NO space
104
m_mask = m_mask.replace('9', SPACE); // 9 Digits 0..9 or space
105

106         // Check Caret
107
if (m_tc == null || m_tc.getCaret() instanceof VOvrCaret)
108             return;
109         else
110             m_tc.setCaret(new VOvrCaret());
111     } // setFormat
112

113     /**
114      * Insert String
115      * @param offset
116      * @param string
117      * @param attr
118      * @throws BadLocationException
119      */

120     public void insertString(int offset, String JavaDoc string, AttributeSet attr)
121         throws BadLocationException
122     {
123         // Max Length
124
if (offset >= m_maxLength)
125             return;
126         // Do we have a Format or inserted not manually (assuming correct Format)
127
if (m_VFormat.length() == 0 || string.length() != 1)
128         {
129             super.insertString(offset, string, attr);
130             return;
131         }
132
133         // Formating required
134
// Log.trace(this,Log.l5_DData, "MDocString.insertString - Offset=" + offset
135
// + ", String=" + string + ", MaxLength=" + m_maxLength + ", Format=" + m_VFormat + ", Mask=" + m_mask
136
// + ", Text=" + getText() + ", Length=" + getText().length());
137

138         String JavaDoc text = getText();
139
140         // Apply Mask, if not target length
141
if (m_VFormat.length() != text.length())
142         {
143             char[] result = m_mask.toCharArray();
144             for (int i = 0; i < result.length; i++)
145             {
146                 if (result[i] == SPACE && text.length() > i)
147                     result[i] = text.charAt(i);
148                 else if (result[i] == SPACE_IND)
149                     result[i] = SPACE;
150             }
151             //
152
super.remove(0, text.length());
153             super.insertString(0, new String JavaDoc(result), attr);
154             m_tc.setCaretPosition(offset);
155             text = getText();
156         }
157
158         // positioned before a mask character - jump over it
159
if (offset+1 < text.length() && m_mask.charAt(offset+1) != SPACE)
160             if (offset+2 < getText().length())
161                 m_tc.setCaretPosition(offset+2);
162
163         // positioned at the mask character
164
if (m_mask.charAt(offset) != SPACE)
165         {
166             if (offset+1 == m_mask.length())
167                 return;
168             offset++;
169             m_tc.setCaretPosition(offset+1);
170         }
171
172         // Conversion
173
char c = string.charAt(0);
174         char cmd = m_VFormat.charAt(offset);
175         Log.trace(Log.l6_Database, "char=" + c + ", cmd=" + cmd);
176         switch (cmd)
177         {
178             case 'c': // c any Letter or Digits or space
179
if (Character.isLetter(c) || Character.isDigit(c) || Character.isSpaceChar(c))
180                     ;
181                 else
182                     return;
183                 break;
184             //
185
case 'C': // C any Letter or Digits or space converted to upper case
186
if (Character.isLetter(c) || Character.isDigit(c) || Character.isSpaceChar(c))
187                     string = string.toUpperCase();
188                 else
189                     return;
190                 break;
191             //
192
case 'a': // a any Letter or Digits NO space
193
if (Character.isLetter(c) || Character.isDigit(c))
194                     ;
195                 else
196                     return;
197                 break;
198             //
199
case 'A': // A any Letter or Digits NO space converted to upper case
200
if (Character.isLetter(c) || Character.isDigit(c))
201                     string = string.toUpperCase();
202                 else
203                     return;
204                 break;
205             // ----
206
case 'l': // l any Letter a..Z NO space
207
if (!Character.isLetter(c))
208                     return;
209                 break;
210             //
211
case 'L': // L any Letter a..Z NO space converted to upper case
212
if (!Character.isLetter(c))
213                     return;
214                 string = string.toUpperCase();
215                 break;
216             //
217
case 'o': // o any Letter a..Z or space
218
if (Character.isLetter(c) || Character.isSpaceChar(c))
219                     ;
220                 else
221                     return;
222                 break;
223             //
224
case 'O': // O any Letter a..Z or space converted to upper case
225
if (Character.isLetter(c) || Character.isSpaceChar(c))
226                     string = string.toUpperCase();
227                 else
228                     return;
229                 break;
230             // ----
231
case '9': // 9 Digits 0..9 or space
232
if (Character.isDigit(c) || Character.isSpaceChar(c))
233                     ;
234                 else
235                     return;
236                 break;
237             //
238
case '0': // 0 Digits 0..9 NO space
239
if (!Character.isDigit(c))
240                     return;
241                 break;
242             //
243
case SPACE: // any character
244
break;
245             //
246
default: // other
247
return;
248         } // switch
249

250         super.remove(offset, 1); // replace current position
251
super.insertString(offset, string, attr);
252     } // insertString
253

254     /**
255      * Delete String
256      * @param offset
257      * @param length
258      * @throws BadLocationException
259      */

260     public void remove (int offset, int length)
261         throws BadLocationException
262     {
263         // No format or non manual entry
264
if (m_VFormat.length() == 0 || length != 1)
265         {
266             super.remove(offset, length);
267             return;
268         }
269
270     // Log.trace(Log.l5_DData, "MDocString.remove - Offset=" + offset + " Length=" + length);
271

272         // begin of string
273
if (offset == 0)
274         {
275             // empty the field
276
if (length == m_mask.length())
277                 super.remove(offset, length);
278             return;
279         }
280
281         // one position behind delimiter
282
if (offset-1 >= 0 && m_mask.charAt(offset-1) != SPACE)
283         {
284             if (offset-2 >= 0)
285                 m_tc.setCaretPosition(offset-2);
286             else
287                 return;
288         }
289         else
290             m_tc.setCaretPosition(offset-1);
291     } // deleteString
292

293     /**
294      * Get Full Text
295      * @return text
296      */

297     private String JavaDoc getText()
298     {
299         String JavaDoc str = "";
300         try
301         {
302             str = getContent().getString(0, getContent().length()-1); // cr at end
303
}
304         catch (Exception JavaDoc e)
305         {
306             str = "";
307         }
308         return str;
309     } // getString
310

311     /*************************************************************************/
312
313     /**
314      * Caret Listener
315      * @param e
316      */

317     public void caretUpdate(CaretEvent e)
318     {
319         // No Format
320
if (m_VFormat.length() == 0
321         // Format error - all fixed characters
322
|| m_VFormat.equals(m_mask))
323             return;
324         // Selection
325
if (e.getDot() != e.getMark())
326         {
327             m_lastDot = e.getDot();
328             return;
329         }
330         //
331
// Log.trace(Log.l5_DData, "MDocString.caretUpdate -" + m_VFormat + "-" + m_mask + "- dot=" + e.getDot() + ", mark=" + e.getMark());
332

333         // Is the current position a fixed character?
334
if (e.getDot()+1 > m_mask.length() || m_mask.charAt(e.getDot()) == SPACE)
335         {
336             m_lastDot = e.getDot();
337             return;
338         }
339
340         // Direction?
341
int newDot = -1;
342         if (m_lastDot > e.getDot()) // <-
343
newDot = e.getDot() - 1;
344         else // -> (or same)
345
newDot = e.getDot() + 1;
346         //
347
if (e.getDot() == 0) // first
348
newDot = 1;
349         else if (e.getDot() == m_mask.length()-1) // last
350
newDot = e.getDot() - 1;
351         //
352
// Log.trace(Log.l6_Database, "OnFixedChar=" + m_mask.charAt(e.getDot()) + ", newDot=" + newDot + ", last=" + m_lastDot);
353

354         m_lastDot = e.getDot();
355         if (newDot >= 0 && newDot < getText().length())
356             m_tc.setCaretPosition(newDot);
357     } // caretUpdate
358

359 } // MDocString
360
Popular Tags