/** @return Money value */ public Money getValue() { if (!isInitialized()) { return null; } else { return moneyField.getValue(); } }
/** Change current currency. */ void changeCurrency(String newCurrency, boolean isChangingAllMoneyFields) { if (isChangingAllMoneyFields) { changeAllMoneyFields(newCurrency); } else { moneyField.setValue(moneyField.getValue().getConverted(newCurrency), isInitialized()); currencyLabel.setText(newCurrency); } }
/** Sets the Money value. */ public void setValue(Money money) { if (money == null) { setValue(new Money()); clearMoney(); performFlags(); return; } // Check displayed currency int newCurrency = money.getCurrency(); if (newCurrency != moneyField.getValue().getCurrency()) { currencyLabel.setText(Money.getCurrencyFor(newCurrency)); } // Set value moneyField.setValue(money); performFlags(); }
/** Handle string insertion. */ public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { // Store source data int sourceLength = getLength(); String sourceText = getText(0, sourceLength); StringBuffer strBuffer = new StringBuffer(sourceText); // Check if old value is zero if (offs == 0 && sourceLength > 0 && str.length() > 0) { long oldValue; try { oldValue = myFormat.parse(strBuffer.toString()).longValue(); if (oldValue == 0) { strBuffer.deleteCharAt(0); } } catch (Exception e) { } } // Now add new string strBuffer.insert(offs, str); BigDecimal value; try { value = new BigDecimal(myFormat.parse(strBuffer.toString()).doubleValue()); } catch (Exception e) { if (sourceLength > 0) { if (sourceText.startsWith(",")) { sourceText = "0" + sourceText; } value = new BigDecimal(getRealString(sourceText)); } else { value = new BigDecimal(0.0); } } // Set the new value if (textField == null) { return; } textField.setValue(new Money(value, textField.getValue().getCurrency()), false); super.remove(0, sourceLength); super.insertString(0, myFormat.format(value.doubleValue()), a); // Set caret to correct caret position if (!"".equals(sourceText)) // <=> initilized { int lengthDiff = getLength() - sourceLength; int caretPos = lengthDiff + offs; int caretDiff = sourceLength - offs; // Adjust for columns after centSperator (currently Diff < 3) if ((caretDiff > 0 && caretDiff < 3) || (value.abs().longValue() < 10 && caretPos == 0)) { caretPos += 1; } if (caretPos < 0) { caretPos = 0; } textField.setCaretPosition(caretPos); } else { textField.setCaretPosition(1); } }
public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (calculator != null && source == calculator.getOKButton()) { calculatorDialog.setVisible(false); setValue(new Money(calculator.convertOutput(), moneyField.getValue().getCurrency())); } else if (source == copyItem) { Clipboard clipboard = getToolkit().getSystemClipboard(); Money value = getValue(); if (value == null) { getToolkit().beep(); return; } StringSelection contents = new StringSelection(value.toString()); clipboard.setContents(contents, defaultClipboardOwner); } else if (source == pasteItem) { Clipboard clipboard = getToolkit().getSystemClipboard(); Transferable content = clipboard.getContents(this); if (content == null) { setValue(null); getToolkit().beep(); return; } try { String moneyData = (String) (content.getTransferData(DataFlavor.stringFlavor)); int index = moneyData.indexOf(' '); if (index <= 0) { getToolkit().beep(); return; } String value = moneyData.substring(0, index); String currencyAbbreviation = moneyData.substring(index + 1); value = value.replace(',', '.'); Money money = new Money(value, Money.getCurrencyValueFor(currencyAbbreviation)); setValue(money); } catch (Exception ex) { getToolkit().beep(); } } else if (source == calculatorItem) { Dimension size = this.moneyField.getSize(); runCalculatorDialog( new MouseEvent( this, 0, // id System.currentTimeMillis(), // when 0, // modifiers size.width, // x size.height, // y 1, // clickCount true // popuptrigger )); } else if (source instanceof JMenuItem) { JMenuItem menuItem = (JMenuItem) source; // Perform 'changed currency' - action String actionText = menuItem.getActionCommand(); // Cut off additional information from actionCommand int currencyDelimPos = actionText.indexOf(" "); if (currencyDelimPos > 0) { actionText = actionText.substring(0, currencyDelimPos); } // lastCurrencyItem will show any currency except the default ones boolean isNotDefault = true; for (int i = 0; i < defaultCurrencies.length; i++) { if (defaultCurrencies[i].equals(actionText)) { isNotDefault = false; break; } } // @todo ... check if lastCurrencyItem is needed or not [aj] seems to be old stuff if (isNotDefault && lastCurrencyItem != null) { lastCurrencyItem.setText(actionText); } // Change the currency changeCurrency(actionText, false); } else if ("popup".equals(e.getActionCommand())) // key: SHIFT+F10 <=> popup { Point point = getLocation(); moneyPopup.doPopup(0, getHeight()); } }