1 14 package org.compiere.grid.ed; 15 16 import java.awt.*; 17 import javax.swing.text.*; 18 import javax.swing.event.DocumentEvent ; 19 import java.text.*; 20 import java.util.*; 21 22 import org.compiere.util.*; 23 import org.compiere.apps.*; 24 25 34 public final class MDocNumber extends PlainDocument 35 { 36 43 public MDocNumber(int displayType, DecimalFormat format, 44 JTextComponent tc, String title) 45 { 46 super(); 47 if (format == null || tc == null || title == null) 48 throw new IllegalArgumentException ("MDocNumber - invalid argument"); 49 m_displayType = displayType; 51 m_format = format; 52 m_sym = m_format.getDecimalFormatSymbols(); 53 m_tc = tc; 54 m_title = title; 55 } 57 58 private int m_displayType = 0; 59 60 private DecimalFormat m_format = null; 61 62 private DecimalFormatSymbols m_sym = null; 63 64 private JTextComponent m_tc = null; 65 66 private String m_title = null; 67 68 69 70 77 public void insertString(int origOffset, String string, AttributeSet attr) 78 throws BadLocationException 79 { 80 if (origOffset < 0 || string == null) 82 throw new IllegalArgumentException ("MDocNumber.insertString - invalid argument"); 83 84 int offset = origOffset; 85 int length = string.length(); 86 if (length != 1) 88 { 89 super.insertString(offset, string, attr); 90 return; 91 } 92 93 96 String content = getText(); 97 if (content.indexOf(m_sym.getGroupingSeparator()) != -1) 99 { 100 StringBuffer result = new StringBuffer (); 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 m_tc.setCaretPosition(offset); 115 content = result.toString(); 117 } 119 122 char c = string.charAt(0); 123 if (Character.isDigit(c)) { 125 super.insertString(offset, string, attr); 127 return; 128 } 129 130 if (c == '+') 132 { 133 if (m_displayType == DisplayType.Integer) 136 return; 137 if (content.charAt(0) == '-') 138 super.remove(0, 1); 139 } 140 141 else if (c == '-' || c == m_sym.getMinusSign()) 143 { 144 if (m_displayType == DisplayType.Integer) 147 return; 148 if (content.length() > 0 && content.charAt(0) == '-') 150 super.remove(0, 1); 151 else 152 super.insertString(0, "-", attr); 153 } 154 155 else if (c == m_sym.getDecimalSeparator() || c == m_sym.getGroupingSeparator()) 158 { 159 if (m_displayType == DisplayType.Integer) 162 return; 163 int pos = content.indexOf(m_sym.getDecimalSeparator()); 164 165 String decimal = String.valueOf(m_sym.getDecimalSeparator()); 167 super.insertString(offset, decimal, attr); 168 169 if (pos != 0) 171 { 172 content = getText(); 173 StringBuffer result = new StringBuffer (); 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 } } 193 else 195 { 196 String 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 } 203 204 205 211 public void remove (int origOffset, int length) 212 throws BadLocationException 213 { 214 if (origOffset < 0 || length < 0) 216 throw new IllegalArgumentException ("MDocNumber.remove - invalid argument"); 217 218 int offset = origOffset; 219 if (length != 1) 220 { 221 super.remove(offset, length); 222 return; 223 } 224 227 String content = getText(); 228 if (content.indexOf(m_sym.getGroupingSeparator()) != -1) 230 { 231 StringBuffer result = new StringBuffer (); 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 } super.remove(offset, length); 247 } 249 253 private String getText() 254 { 255 Content c = getContent(); 256 String str = ""; 257 try 258 { 259 str = c.getString(0, c.length()-1); } 261 catch (Exception e) 262 {} 263 return str; 264 } 266 } | Popular Tags |