KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.*;
17 import javax.swing.text.*;
18 import javax.swing.event.DocumentEvent JavaDoc;
19 import java.text.*;
20 import java.util.*;
21
22 import org.compiere.util.*;
23 import org.compiere.apps.*;
24
25 /**
26  * Number Document Model.
27  * Locale independent editing of numbers by removing thousands
28  * and treating ., as decimal separator. Final formatting in VNumber.setValue
29  *
30  * @see VNumber
31  * @author Jorg Janke
32  * @version $Id: MDocNumber.java,v 1.3 2002/05/26 01:22:24 jjanke Exp $
33  */

34 public final class MDocNumber extends PlainDocument
35 {
36     /**
37      * Constructor
38      * @param displayType
39      * @param format
40      * @param tc
41      * @param title
42      */

43     public MDocNumber(int displayType, DecimalFormat format,
44         JTextComponent tc, String JavaDoc title)
45     {
46         super();
47         if (format == null || tc == null || title == null)
48             throw new IllegalArgumentException JavaDoc("MDocNumber - invalid argument");
49         //
50
m_displayType = displayType;
51         m_format = format;
52         m_sym = m_format.getDecimalFormatSymbols();
53         m_tc = tc;
54         m_title = title;
55     } // MDocNumber
56

57     /** DisplayType used */
58     private int m_displayType = 0;
59     /** Number Format */
60     private DecimalFormat m_format = null;
61     /** Number Format Symbols */
62     private DecimalFormatSymbols m_sym = null;
63     /** The 'owning' component */
64     private JTextComponent m_tc = null;
65     /** Title for calculator */
66     private String JavaDoc m_title = null;
67
68     /*************************************************************************/
69
70     /**
71      * Insert String
72      * @param origOffset
73      * @param string
74      * @param attr
75      * @throws BadLocationException
76      */

77     public void insertString(int origOffset, String JavaDoc string, AttributeSet attr)
78         throws BadLocationException
79     {
80     // ADebug.trace(ADebug.l5_DData, "MDocNumber.insert - O=" + origOffset + " S=" + string + " L=" + string.length());
81
if (origOffset < 0 || string == null)
82             throw new IllegalArgumentException JavaDoc("MDocNumber.insertString - invalid argument");
83
84         int offset = origOffset;
85         int length = string.length();
86         // From DataBinder (assuming correct format)
87
if (length != 1)
88         {
89             super.insertString(offset, string, attr);
90             return;
91         }
92
93         /**
94          * Manual Entry
95          */

96         String JavaDoc content = getText();
97         // remove all Thousands
98
if (content.indexOf(m_sym.getGroupingSeparator()) != -1)
99         {
100             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
101             for (int i = 0; i < content.length(); i++)
102             {
103                 if (content.charAt(i) == m_sym.getGroupingSeparator())
104                 {
105                     if (i < offset)
106                         offset--;
107                 }
108                 else
109                     result.append(content.charAt(i));
110             }
111             super.remove(0, content.length());
112             super.insertString(0, result.toString(), attr);
113             //
114
m_tc.setCaretPosition(offset);
115         // ADebug.trace(ADebug.l6_Database, "Clear Thousands (" + m_format.toPattern() + ")" + content + " -> " + result.toString());
116
content = result.toString();
117         } // remove Thousands
118

119         /**********************************************************************
120          * Check Character entered
121          */

122         char c = string.charAt(0);
123         if (Character.isDigit(c)) // c >= '0' && c <= '9')
124
{
125         // ADebug.trace(ADebug.l6_Database, "Digit=" + c);
126
super.insertString(offset, string, attr);
127             return;
128         }
129
130         // Plus - remove minus sign
131
if (c == '+')
132         {
133         // ADebug.trace(ADebug.l6_Database, "Plus=" + c);
134
// only positive numbers
135
if (m_displayType == DisplayType.Integer)
136                 return;
137             if (content.charAt(0) == '-')
138                 super.remove(0, 1);
139         }
140
141         // Toggle Minus - put minus on start of string
142
else if (c == '-' || c == m_sym.getMinusSign())
143         {
144         // ADebug.trace(ADebug.l6_Database, "Minus=" + c);
145
// no minus possible
146
if (m_displayType == DisplayType.Integer)
147                 return;
148             // remove or add
149
if (content.length() > 0 && content.charAt(0) == '-')
150                 super.remove(0, 1);
151             else
152                 super.insertString(0, "-", attr);
153         }
154
155         // Decimal - remove other decimals
156
// Thousand - treat as Decimal
157
else if (c == m_sym.getDecimalSeparator() || c == m_sym.getGroupingSeparator())
158         {
159         // ADebug.trace(ADebug.l6_Database, "decimal=" + c + " (ds=" + m_sym.getDecimalSeparator() + "; gs=" + m_sym.getGroupingSeparator() + ")");
160
// no decimals on integers
161
if (m_displayType == DisplayType.Integer)
162                 return;
163             int pos = content.indexOf(m_sym.getDecimalSeparator());
164
165             // put decimal in
166
String JavaDoc decimal = String.valueOf(m_sym.getDecimalSeparator());
167             super.insertString(offset, decimal, attr);
168
169             // remove other decimals
170
if (pos != 0)
171             {
172                 content = getText();
173                 StringBuffer JavaDoc result = new StringBuffer JavaDoc();
174                 int correction = 0;
175                 for (int i = 0; i < content.length(); i++)
176                 {
177                     if (content.charAt(i) == m_sym.getDecimalSeparator())
178                     {
179                         if (i == offset)
180                             result.append(content.charAt(i));
181                         else if (i < offset)
182                             correction++;
183                     }
184                     else
185                         result.append(content.charAt(i));
186                 }
187                 super.remove(0, content.length());
188                 super.insertString(0, result.toString(), attr);
189                 m_tc.setCaretPosition(offset-correction+1);
190             } // remove other decimals
191
} // decial or thousand
192

193         // something else
194
else
195         {
196             String JavaDoc result = VNumber.startCalculator(m_tc, getText(),
197                 m_format, m_displayType, m_title);
198             super.remove(0, content.length());
199             super.insertString(0, result, attr);
200         }
201     } // insertString
202

203     /*************************************************************************/
204
205     /**
206      * Delete String
207      * @param origOffset
208      * @param length
209      * @throws BadLocationException
210      */

211     public void remove (int origOffset, int length)
212         throws BadLocationException
213     {
214     // ADebug.trace(ADebug.l5_DData, "MDocNumber.remove - Offset=" + offset + " Length=" + length);
215
if (origOffset < 0 || length < 0)
216             throw new IllegalArgumentException JavaDoc("MDocNumber.remove - invalid argument");
217
218         int offset = origOffset;
219         if (length != 1)
220         {
221             super.remove(offset, length);
222             return;
223         }
224         /**
225          * Manual Entry
226          */

227         String JavaDoc content = getText();
228         // remove all Thousands
229
if (content.indexOf(m_sym.getGroupingSeparator()) != -1)
230         {
231             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
232             for (int i = 0; i < content.length(); i++)
233             {
234                 if (content.charAt(i) == m_sym.getGroupingSeparator() && i != origOffset)
235                 {
236                     if (i < offset)
237                         offset--;
238                 }
239                 else
240                     result.append(content.charAt(i));
241             }
242             super.remove(0, content.length());
243             super.insertString(0, result.toString(), null);
244             m_tc.setCaretPosition(offset);
245         } // remove Thousands
246
super.remove(offset, length);
247     } // remove
248

249     /**
250      * Get Full Text
251      * @return text
252      */

253     private String JavaDoc getText()
254     {
255         Content c = getContent();
256         String JavaDoc str = "";
257         try
258         {
259             str = c.getString(0, c.length()-1); // cr at end
260
}
261         catch (Exception JavaDoc e)
262         {}
263         return str;
264     } // getString
265

266 } // MDocNumber
267
Popular Tags