1 21 22 package org.opensubsystems.patterns.thickclient.application.swt; 23 24 import java.text.DecimalFormat ; 25 import java.text.DecimalFormatSymbols ; 26 import java.text.NumberFormat ; 27 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.events.VerifyEvent; 30 import org.eclipse.swt.events.VerifyListener; 31 import org.eclipse.swt.layout.FillLayout; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Layout; 34 import org.eclipse.swt.widgets.Text; 35 36 44 public class NumericText extends Composite 45 { 46 48 51 protected char m_cDecimalPoint; 52 53 56 protected String m_strDecimalPoint; 57 58 61 public static final int MAX_DECIMAL_DIGITS = 2; 62 63 66 protected Text m_textField; 67 68 72 protected boolean m_bDecimalPoint; 73 74 76 83 public NumericText( 84 Composite parent, 85 int style, 86 boolean bDecimalPoint 87 ) 88 { 89 super(parent, style); 90 91 NumberFormat format = NumberFormat.getCurrencyInstance(); 92 DecimalFormatSymbols decsym; 93 94 if (format instanceof DecimalFormat ) 95 { 96 decsym = ((DecimalFormat )format).getDecimalFormatSymbols(); 97 m_cDecimalPoint = decsym.getDecimalSeparator(); 98 } 99 else 100 { 101 m_cDecimalPoint = '.'; 102 } 103 m_strDecimalPoint = Character.toString(m_cDecimalPoint); 104 105 m_bDecimalPoint = bDecimalPoint; 106 setLayout(createLayout()); 107 108 int iTextStyle; 109 110 iTextStyle = SWT.RIGHT | SWT.SINGLE; 111 if ((style & SWT.READ_ONLY) != 0) 112 { 113 iTextStyle |= SWT.READ_ONLY; 114 } 115 m_textField = new Text(this, iTextStyle); 116 m_textField.addVerifyListener(new VerifyListener() 117 { 118 public void verifyText(VerifyEvent e) 119 { 120 String text = e.text; 122 char[] chars = new char[text.length()]; 123 text.getChars(0, chars.length, chars, 0); 124 for (int index = 0; index < chars.length; index++) 125 { 126 if (!((('0' <= chars[index]) && (chars[index] <= '9')) 127 || ((chars[index] == m_cDecimalPoint) && (m_bDecimalPoint)))) 128 { 129 e.doit = false; 130 return; 131 } 132 } 133 134 if (m_bDecimalPoint) 136 { 137 if ((chars.length == 1) && (chars[0] == m_cDecimalPoint)) 138 { 139 if (m_textField.getText().indexOf(m_cDecimalPoint) >= 0) 141 { 142 e.doit = false; 144 return; 145 } 146 } 147 else if ((chars.length == 1) 148 && (chars[0] >= '0' && chars[0] <= '9')) 149 { 150 int decIndex = m_textField.getText().indexOf(m_cDecimalPoint); 151 if ((decIndex >= 0) 152 && (m_textField.getText().length() 153 - decIndex > MAX_DECIMAL_DIGITS)) 154 { 155 e.doit = false; 156 return; 157 } 158 } 159 } 160 if (text.length() > 0) 161 { 162 if (m_bDecimalPoint) 163 { 164 try 165 { 166 text = m_textField.getText(); 167 if ((text != null) && (text.length() > 0) 168 && (!text.equals(m_strDecimalPoint))) 169 { 170 Double.parseDouble(text); 171 } 172 } 173 catch (NumberFormatException exec) 174 { 175 e.doit = false; 176 } 177 } 178 else 179 { 180 try 181 { 182 text = m_textField.getText(); 184 if ((text != null) && (text.length() > 0)) 185 { 186 Integer.parseInt(text); 187 } 188 } 189 catch (NumberFormatException nfe) 190 { 191 e.doit = false; 193 } 194 } 195 } 196 } 197 }); 198 } 199 200 205 public Text getTextField( 206 ) 207 { 208 return m_textField; 209 } 210 211 216 public Layout createLayout( 217 ) 218 { 219 return new FillLayout(); 220 } 221 222 230 public boolean setTextLimit( 231 int iLimit 232 ) 233 { 234 boolean bReturn = false; 235 236 if (m_textField != null) 237 { 238 m_textField.setTextLimit(iLimit); 239 bReturn = true; 240 } 241 return bReturn; 242 } 243 244 247 public String getDecimalPointText() 248 { 249 return m_strDecimalPoint; 250 } 251 } 252 | Popular Tags |