1 14 package org.compiere.grid.ed; 15 16 import java.awt.*; 17 import java.awt.event.*; 18 import javax.swing.*; 19 import javax.swing.text.*; 20 import java.text.*; 21 import java.math.*; 22 import java.beans.*; 23 24 import org.compiere.util.*; 25 import org.compiere.apps.*; 26 import org.compiere.plaf.*; 27 import org.compiere.swing.*; 28 import org.compiere.model.*; 29 30 36 public final class VNumber extends JComponent 37 implements VEditor, ActionListener, KeyListener, FocusListener 38 { 39 public final static int SIZE = 12; 41 44 public VNumber() 45 { 46 this("Number", false, false, true, DisplayType.Number, "Number"); 47 } 49 59 public VNumber(String columnName, boolean mandatory, boolean isReadOnly, boolean isUpdateable, 60 int displayType, String title) 61 { 62 super(); 63 super.setName(columnName); 64 m_columnName = columnName; 65 m_title = title; 66 setDisplayType(displayType); 67 LookAndFeel.installBorder(this, "TextField.border"); 69 this.setLayout(new BorderLayout()); 70 this.setPreferredSize(m_text.getPreferredSize()); int height = m_text.getPreferredSize().height; 73 74 m_text.setBorder(null); 76 m_text.setHorizontalAlignment(JTextField.TRAILING); 77 m_text.addKeyListener(this); 78 m_text.addFocusListener(this); 79 setMandatory(mandatory); 81 this.add(m_text, BorderLayout.CENTER); 82 83 m_button.setIcon(Env.getImageIcon("Calculator10.gif")); 85 m_button.setMargin(new Insets(0, 0, 0, 0)); 86 m_button.setFocusable(false); 87 m_button.setPreferredSize(new Dimension(height, height)); 88 m_button.addActionListener(this); 89 this.add (m_button, BorderLayout.EAST); 90 91 this.setPreferredSize(this.getPreferredSize()); 94 if (isReadOnly || !isUpdateable) 96 setReadWrite(false); 97 else 98 setReadWrite(true); 99 } 101 104 public void dispose() 105 { 106 m_text = null; 107 m_button = null; 108 m_mField = null; 109 } 111 115 protected void setDocument(Document doc) 116 { 117 m_text.setDocument(doc); 118 } 120 private String m_columnName; 121 protected int m_displayType; private DecimalFormat m_format; 123 private String m_title; 124 private boolean m_setting; 125 private String m_oldText; 126 private String m_initialText; 127 128 private boolean m_rangeSet = false; 129 private Double m_minValue; 130 private Double m_maxValue; 131 132 133 private CTextField m_text = new CTextField(SIZE); 135 private CButton m_button = new CButton(); 136 137 private MField m_mField = null; 138 139 145 public boolean setRange(Double minValue, Double maxValue) 146 { 147 m_rangeSet = true; 148 m_minValue = minValue; 149 m_maxValue = maxValue; 150 return m_rangeSet; 151 } 153 159 public boolean setRange(String minValue, String maxValue) 160 { 161 if (minValue == null || maxValue == null) 162 return false; 163 try 164 { 165 m_minValue = Double.valueOf(minValue); 166 m_maxValue = Double.valueOf(maxValue); 167 } 168 catch (NumberFormatException nfe) 169 { 170 return false; 171 } 172 m_rangeSet = true; 173 return m_rangeSet; 174 } 176 180 public void setDisplayType (int displayType) 181 { 182 m_displayType = displayType; 183 if (!DisplayType.isNumeric(displayType)) 184 m_displayType = DisplayType.Number; 185 m_format = DisplayType.getNumberFormat(displayType); 186 m_text.setDocument (new MDocNumber(displayType, m_format, m_text, m_title)); 187 } 189 193 public void setReadWrite (boolean value) 194 { 195 if (m_text.isReadWrite() != value) 196 m_text.setReadWrite(value); 197 if (m_button.isReadWrite() != value) 198 m_button.setReadWrite(value); 199 if (m_button.isVisible() != value) 201 m_button.setVisible(value); 202 } 204 208 public boolean isReadWrite() 209 { 210 return m_text.isReadWrite(); 211 } 213 217 public void setMandatory (boolean mandatory) 218 { 219 m_text.setMandatory(mandatory); 220 } 222 226 public boolean isMandatory() 227 { 228 return m_text.isMandatory(); 229 } 231 235 public void setBackground(Color color) 236 { 237 m_text.setBackground(color); 238 } 240 244 public void setBackground (boolean error) 245 { 246 m_text.setBackground(error); 247 } 249 253 public void setForeground(Color fg) 254 { 255 m_text.setForeground(fg); 256 } 258 262 public void setValue(Object value) 263 { 264 Log.trace(Log.l6_Database, "VNumber.setValue", value); 265 if (value == null) 266 m_oldText = ""; 267 else 268 m_oldText = m_format.format(value); 269 if (m_setting) 271 return; 272 m_text.setText (m_oldText); 273 m_initialText = m_oldText; 274 } 276 280 public void propertyChange (PropertyChangeEvent evt) 281 { 282 if (evt.getPropertyName().equals(org.compiere.model.MField.PROPERTY)) 283 setValue(evt.getNewValue()); 284 } 286 290 public Object getValue() 291 { 292 if (m_text == null) 293 return null; 294 String value = m_text.getText(); 295 if (value == null || value.length() == 0) 296 return null; 297 if (value.equals(".") || value.equals(",") || value.equals("-")) 298 value = "0"; 299 try 300 { 301 Number number = m_format.parse(value); 302 value = number.toString(); BigDecimal bd = new BigDecimal(value); 304 return bd.setScale(m_format.getMaximumFractionDigits(), BigDecimal.ROUND_HALF_UP); 305 } 306 catch (Exception e) 307 { 308 Log.error("VNumber.getValue", e); 309 } 310 return Env.ZERO; 311 } 313 317 public String getDisplay() 318 { 319 return m_text.getText(); 320 } 322 323 324 325 329 public void actionPerformed (ActionEvent e) 330 { 331 Log.trace(Log.l4_Data, "VNumber.actionPerformed", e.getActionCommand()); 332 if (e.getActionCommand().equals(ValuePreference.NAME)) 333 { 334 ValuePreference.start (m_mField, getValue()); 335 return; 336 } 337 338 if (e.getSource() == m_button) 339 { 340 m_button.setEnabled(false); 341 String str = startCalculator(this, m_text.getText(), m_format, m_displayType, m_title); 342 m_text.setText(str); 343 m_button.setEnabled(true); 344 try 345 { 346 fireVetoableChange (m_columnName, m_oldText, getValue()); 347 } 348 catch (PropertyVetoException pve) {} 349 m_text.requestFocus(); 350 } 351 } 353 357 public void keyTyped(KeyEvent e) {} 358 public void keyPressed(KeyEvent e) {} 359 360 366 public void keyReleased(KeyEvent e) 367 { 368 Log.trace(Log.l4_Data, "VNumber.keyReleased - " + e.getKeyCode()); 369 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) 371 m_text.setText(m_initialText); 372 m_setting = true; 373 try 374 { 375 fireVetoableChange (m_columnName, m_oldText, null); } 377 catch (PropertyVetoException pve) {} 378 m_setting = false; 379 } 381 385 public void focusGained (FocusEvent e) 386 { 387 if (m_text != null) 388 m_text.selectAll(); 389 } 391 395 public void focusLost (FocusEvent e) 396 { 397 try 398 { 399 fireVetoableChange (m_columnName, m_initialText, getValue()); 400 } 401 catch (PropertyVetoException pve) {} 402 } 404 413 public static String startCalculator(Container jc, String value, 414 DecimalFormat format, int displayType, String title) 415 { 416 Log.trace(Log.l3_Util, "VNumber startCalculator - " + value); 417 BigDecimal startValue = new BigDecimal(0.0); 418 try 419 { 420 if (value != null && value.length() > 0) 421 { 422 Number number = format.parse(value); 423 startValue = new BigDecimal (number.toString()); 424 } 425 } 426 catch (ParseException pe) 427 { 428 Log.error("VNumber invalidEntry - " + pe.getMessage()); 429 } 430 431 Frame frame = Env.getFrame(jc); 433 Calculator calc = new Calculator(frame, title, 435 displayType, format, startValue); 436 AEnv.showCenterWindow(frame, calc); 437 BigDecimal result = calc.getNumber(); 438 Log.trace(Log.l4_Data, "Result=" + result); 439 calc = null; 441 if (result != null) 442 return format.format(result); 443 else 444 return value; } 447 451 public void setField (MField mField) 452 { 453 } 458 459 460 464 public void removeActionListener(ActionListener l) 465 { 466 listenerList.remove(ActionListener.class, l); 467 } 469 473 public void addActionListener(ActionListener l) 474 { 475 listenerList.add(ActionListener.class, l); 476 } 478 504 } | Popular Tags |