1 32 33 package it.businesslogic.ireport.gui; 34 35 import it.businesslogic.ireport.gui.event.ValueChangedEvent; 36 import it.businesslogic.ireport.util.Misc; 37 import javax.swing.text.*; 38 import java.text.*; 39 43 public class NumberDocument extends javax.swing.text.PlainDocument { 44 private char groupingSeparator = ','; 45 private char decimalSeparator = '.'; 46 47 private double value = 0; 48 49 50 public NumberDocument() { 51 52 DecimalFormatSymbols dfs = new DecimalFormatSymbols(); 53 groupingSeparator = dfs.getGroupingSeparator(); 54 decimalSeparator = dfs.getDecimalSeparator(); 55 } 56 57 public boolean checkNumber(String proposedResult) 58 { 59 if (groupingSeparator == '.' && proposedResult.indexOf(decimalSeparator) < 0) 61 { 62 } 64 else 65 { 66 proposedResult = Misc.string_replace( "", ""+groupingSeparator , proposedResult); 67 } 68 proposedResult = proposedResult.replace(decimalSeparator, '.'); 69 70 try { 71 if (proposedResult.trim().length() == 0) return true; 73 74 double oldValue = getValue(); 75 76 double newValue = 0; 77 if (proposedResult.length()> 0) newValue = Double.parseDouble(proposedResult); 78 79 fireValueChangedListenerValueChanged(new ValueChangedEvent(null, oldValue,newValue)); 80 81 return true; 82 } catch (NumberFormatException e) { 83 } 86 return false; 87 } 88 89 107 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 108 109 if (str == null || str.length() == 0) { 110 super.insertString(offs, str, a); 111 return; 112 } 113 String currentText = getText(0, getLength()); 114 String beforeOffset = currentText.substring(0, offs); 115 String afterOffset = currentText.substring(offs, currentText.length()); 116 String proposedResult = beforeOffset + str + afterOffset; 117 if (checkNumber(proposedResult)) 118 { 119 super.insertString(offs, str, a); 120 } 121 } 122 123 public void remove(int offs, int len) throws BadLocationException { 124 125 String currentText = getText(0, getLength()); 126 String beforeOffset = currentText.substring(0, offs); 127 String afterOffset = currentText.substring(len + offs, 128 currentText.length()); 129 String proposedResult = beforeOffset + afterOffset; 130 if (checkNumber(proposedResult)) 131 { 132 super.remove(offs, len); 133 } 134 } 135 136 public void replace(int offset, int length, String str, AttributeSet attrs) throws BadLocationException { 137 138 if (str == null || str.length() == 0) { 139 super.replace(offset, length, str, attrs); 140 return; 141 } 142 143 String currentText = getText(0, getLength()); 144 String beforeOffset = currentText.substring(0, offset); 145 String afterOffset = currentText.substring(length + offset, 146 currentText.length()); 147 String proposedResult = beforeOffset + str + afterOffset; 148 149 150 if (checkNumber(proposedResult)) 151 { 152 super.replace(offset, length, str, attrs); 153 } 154 } 155 156 public double getValue() { 157 return value; 158 } 159 160 public void setValue(double value) { 161 this.value = value; 162 } 163 164 167 private javax.swing.event.EventListenerList listenerList = null; 168 169 173 public synchronized void addValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) { 174 175 if (listenerList == null ) { 176 listenerList = new javax.swing.event.EventListenerList (); 177 } 178 listenerList.add (it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener); 179 } 180 181 185 public synchronized void removeValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) { 186 187 listenerList.remove (it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener); 188 } 189 190 195 private void fireValueChangedListenerValueChanged(it.businesslogic.ireport.gui.event.ValueChangedEvent event) { 196 197 if (listenerList == null) return; 198 Object [] listeners = listenerList.getListenerList (); 199 for (int i = listeners.length - 2; i >= 0; i -= 2) { 200 if (listeners[i]==it.businesslogic.ireport.gui.event.ValueChangedListener.class) { 201 ((it.businesslogic.ireport.gui.event.ValueChangedListener)listeners[i+1]).valueChanged (event); 202 } 203 } 204 } 205 206 } 207 | Popular Tags |