1 32 33 package it.businesslogic.ireport.gui; 34 35 import javax.swing.FocusManager ; 36 import javax.swing.text.*; 37 import java.text.*; 38 import java.awt.event.*; 39 import it.businesslogic.ireport.gui.event.*; 40 41 45 public class JNumberField extends javax.swing.JTextField implements java.awt.event.FocusListener , ValueChangedListener, KeyListener { 46 47 48 private int decimals=2; 49 50 51 private boolean integer=false; 52 53 54 private double value = 0.0; 55 private double oldValue = 0.0; 56 57 private java.text.NumberFormat numberFormat=null; 58 59 60 private boolean grouping=true; 61 62 63 private javax.swing.event.EventListenerList listenerList = null; 64 65 private boolean init = false; 66 67 private NumberDocument nd = new NumberDocument(); 68 69 70 public JNumberField() { 71 72 super(); 73 this.addFocusListener(this); 74 numberFormat=NumberFormat.getInstance(); 75 numberFormat.setMaximumFractionDigits(2); 76 numberFormat.setMinimumFractionDigits(2); 77 numberFormat.setGroupingUsed(true); 78 this.setText(numberFormat.format(0)); 79 80 nd.addValueChangedListener( this ); 81 82 this.addKeyListener(this); 83 84 this.setDocument( nd ); 85 86 } 87 88 public void keyPressed(KeyEvent e) { 89 if (e.getKeyCode() == KeyEvent.VK_ENTER) 90 { 91 FocusManager.getCurrentManager().focusNextComponent(); 92 } 93 } 94 public void keyReleased(KeyEvent e) { 95 } 96 public void keyTyped(KeyEvent e) { 97 } 98 99 public void valueChanged(ValueChangedEvent evt) { 100 101 if (isInit()) return; 102 value = evt.getNewValue(); 103 } 105 106 public void textChanged(javax.swing.event.DocumentEvent evt) { 107 108 109 } 110 111 112 119 protected Document createDefaultModel() { 120 122 return new NumberDocument(); 124 } 125 126 130 public int getDecimals() { 131 return this.decimals; 132 } 133 134 140 public void setDecimals(int decimals) throws java.beans.PropertyVetoException { 141 this.decimals = decimals; 142 if (!isInteger()) { 143 numberFormat.setMaximumFractionDigits(decimals); 144 numberFormat.setMinimumFractionDigits(decimals); 145 } 146 this.setText( numberFormat.format(getValue())); 147 } 148 149 153 public boolean isInteger() { 154 return this.integer; 155 } 156 157 163 public void setInteger(boolean integer) throws java.beans.PropertyVetoException { 164 if (integer) { 165 numberFormat.setMaximumFractionDigits(0); 166 numberFormat.setMinimumFractionDigits(0); 167 } else { 168 169 if (getDecimals() == -1) { 170 numberFormat.setMinimumFractionDigits(1); 171 numberFormat.setMaximumFractionDigits(100); 172 } else { 173 numberFormat.setMinimumFractionDigits(getDecimals()); 174 numberFormat.setMaximumFractionDigits(getDecimals()); 175 } 176 } 177 this.integer = integer; 178 this.setText( numberFormat.format(getValue())); 179 } 180 181 185 public double getValue() { 186 return this.value; 187 } 188 189 195 public void setValue(double value) throws java.beans.PropertyVetoException { 196 this.setInit(true); 197 this.value = value; 198 this.oldValue = value; 199 nd.setValue(value); 200 this.setText( numberFormat.format(getValue()) ); 201 this.setInit(false); 202 } 203 204 public void setValue(int value) throws java.beans.PropertyVetoException { 205 setValue((double)value); 206 } 207 208 209 public void setText(String text) { 210 this.setInit(true); 211 super.setText(text); 212 this.setInit(false); 213 } 214 215 218 public void focusGained(FocusEvent e) { 219 220 if (getText().length() == 0) return; 221 this.oldValue = value; 222 numberFormat.setGroupingUsed(false); 223 setText( numberFormat.format(value)); 224 numberFormat.setGroupingUsed(this.isGrouping()); 225 } 226 227 230 public void focusLost(FocusEvent e) { 231 232 if (getText().length() == 0) return; 233 setText( numberFormat.format(value)); 234 if (oldValue != value) 235 this.fireActionListenerActionPerformed(new java.awt.event.ActionEvent ( this, 0, "") ); 236 } 237 238 242 public boolean isGrouping() { 243 return this.grouping; 244 } 245 246 252 public void setGrouping(boolean grouping) throws java.beans.PropertyVetoException { 253 this.grouping = grouping; 254 } 255 256 260 public synchronized void addActionListener(java.awt.event.ActionListener listener) { 261 if (listenerList == null ) { 262 listenerList = new javax.swing.event.EventListenerList (); 263 } 264 listenerList.add(java.awt.event.ActionListener .class, listener); 265 } 266 267 271 public synchronized void removeActionListener(java.awt.event.ActionListener listener) { 272 listenerList.remove(java.awt.event.ActionListener .class, listener); 273 } 274 275 280 private void fireActionListenerActionPerformed(java.awt.event.ActionEvent event) { 281 if (listenerList == null) return; 282 Object [] listeners = listenerList.getListenerList(); 283 for (int i = listeners.length-2; i>=0; i-=2) { 284 if (listeners[i]==java.awt.event.ActionListener .class) { 285 ((java.awt.event.ActionListener )listeners[i+1]).actionPerformed(event); 286 } 287 } 288 } 289 290 public boolean isInit() { 291 return init; 292 } 293 294 public void setInit(boolean init) { 295 this.init = init; 296 } 297 298 } 299 300 | Popular Tags |