1 32 33 package it.businesslogic.ireport.gui; 34 import it.businesslogic.ireport.gui.event.*; 35 import javax.swing.*; 36 import java.awt.event.*; 37 import java.util.*; 38 import java.text.*; 39 49 public class JNumberComboBox extends JComboBox implements KeyListener, ItemListener, FocusListener { 50 51 private NumberFormat numberFormat = null; 52 private Vector entries; 53 54 private double value; 55 56 private double minValue; 57 58 private double maxValue; 59 60 private boolean setting = false; 61 62 63 private javax.swing.event.EventListenerList listenerList = null; 64 65 private String postfix=""; 66 67 68 public JNumberComboBox() { 69 70 super(); 71 entries = new Vector(); 72 this.setFocusCycleRoot(false); 73 numberFormat = NumberFormat.getNumberInstance(); 74 numberFormat.setMinimumFractionDigits(0); 75 numberFormat.setMaximumFractionDigits(2); 76 this.setEditable(true); 77 78 for (int i=0; i<this.getComponentCount(); i++) { 81 this.getComponent(i).addFocusListener(this); 83 } 84 85 this.addFocusListener(this); 86 this.addKeyListener(this); 87 this.addActionListener(this); 89 } 90 91 public void addEntry(String name, double value) 92 { 93 Enumeration e = entries.elements(); 95 while (e.hasMoreElements()) 96 { 97 NumberEntry ne = (NumberEntry)e.nextElement(); 98 if (ne.name.equals(name)) 99 { 100 ne.value = value; 101 return; 102 } 103 } 104 NumberEntry entry = new NumberEntry(name,value); 105 this.addItem(entry); 106 this.entries.addElement(entry); 107 } 108 109 public void actionPerformed(ActionEvent e) 110 { 111 if (isSetting()) return; 112 super.actionPerformed(e); 114 115 if (this.getSelectedItem() != null) 116 { 117 Object obj = getSelectedItem(); 118 if (obj instanceof NumberEntry) 119 { 120 if (((NumberEntry)getSelectedItem()).value != value) 121 { 122 123 double oldValue = this.value; 124 this.value = ((NumberEntry)getSelectedItem()).value; 125 fireValueChangedListenerValueChanged(new ValueChangedEvent((JComponent)this, oldValue, this.value)); 126 127 } 128 129 } 130 else 131 { 132 String s = ""+getSelectedItem(); 133 try { 134 Number nb = numberFormat.parse(s); 135 double oldValue = this.value; 136 this.value = nb.doubleValue(); 137 fireValueChangedListenerValueChanged(new ValueChangedEvent((JComponent)this, oldValue, this.value)); 138 } catch (Exception ex) 139 { 140 141 } 142 } 143 this.setSetting(true); 144 this.setSelectedItem( numberFormat.format( value ) +getPostfix()); 145 this.setSetting(false); 146 } 147 } 148 149 154 public void keyPressed(KeyEvent e) { 155 if (e.getKeyCode() == e.VK_ENTER) 156 { 157 e.consume(); 158 FocusManager.getCurrentManager().focusNextComponent(); 159 160 } 161 } 162 163 168 public void keyReleased(KeyEvent e) { 169 } 170 171 176 public void keyTyped(KeyEvent e) { 177 } 178 179 182 public void focusGained(FocusEvent e) { 183 } 184 185 188 public void focusLost(FocusEvent e) { 189 acceptResultNow(); 190 } 191 192 public void acceptResultNow() 193 { 194 try { 195 196 197 Number num = numberFormat.parse( ""+ this.getSelectedItem()); 198 199 if (num.doubleValue() != value) 200 { 201 double oldValue = this.value; 202 this.value = num.doubleValue(); 203 fireValueChangedListenerValueChanged(new ValueChangedEvent(this, oldValue, this.value)); 204 } 205 } catch (Exception ex) 206 { 207 fireValueChangedListenerValueChanged(new ValueChangedEvent(this, this.value, this.value)); 209 } 210 boolean setting = isSetting(); 211 this.setSetting(true); 212 this.setSelectedItem( numberFormat.format( value )+getPostfix() ); 213 this.setSetting(setting); 214 } 215 216 220 public double getMinValue() { 221 return minValue; 222 } 223 224 228 public void setMinValue(double minValue) { 229 this.minValue = minValue; 230 } 231 232 236 public double getMaxValue() { 237 return maxValue; 238 } 239 240 244 public void setMaxValue(double maxValue) { 245 this.maxValue = maxValue; 246 } 247 248 252 public double getValue() { 253 return value; 254 } 255 256 260 public void setValue(double value) { 261 if (this.value != value) 262 { 263 double oldValue = this.value; 264 this.value = value; 265 fireValueChangedListenerValueChanged(new ValueChangedEvent(this, oldValue, this.value)); 266 } 267 boolean setting = isSetting(); 268 this.setSetting(true); 269 this.setSelectedItem( numberFormat.format( this.value )+getPostfix() ); 270 this.setSetting(setting); 271 } 272 273 277 public synchronized void addValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) { 278 if (listenerList == null ) { 279 listenerList = new javax.swing.event.EventListenerList (); 280 } 281 listenerList.add(it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener); 282 } 283 284 288 public synchronized void removeValueChangedListener(it.businesslogic.ireport.gui.event.ValueChangedListener listener) { 289 listenerList.remove(it.businesslogic.ireport.gui.event.ValueChangedListener.class, listener); 290 } 291 292 297 private void fireValueChangedListenerValueChanged(it.businesslogic.ireport.gui.event.ValueChangedEvent event) { 298 if (listenerList == null) return; 299 Object [] listeners = listenerList.getListenerList(); 300 for (int i = listeners.length-2; i>=0; i-=2) { 301 if (listeners[i]==it.businesslogic.ireport.gui.event.ValueChangedListener.class) { 302 ((it.businesslogic.ireport.gui.event.ValueChangedListener)listeners[i+1]).valueChanged(event); 303 } 304 } 305 } 306 307 311 public java.lang.String getPostfix() { 312 return postfix; 313 } 314 315 319 public void setPostfix(java.lang.String postfix) { 320 this.postfix = postfix; 321 } 322 323 public boolean isSetting() { 324 return setting; 325 } 326 327 public void setSetting(boolean setting) { 328 this.setting = setting; 329 } 330 331 public void itemStateChanged(ItemEvent e) { 332 333 if (isSetting()) return; 334 if (e.getStateChange() == e.SELECTED) 335 { 336 actionPerformed(null); 337 } 338 } 339 340 } 341 342 | Popular Tags |