/** Save clear of moneyEntry. */ public void clearMoney() { if (moneyField.getDocument() == moneyDocument) { moneyDocument.clear(); } else { moneyField.setText(""); } }
/** Change current currency. */ void changeCurrency(String newCurrency, boolean isChangingAllMoneyFields) { if (isChangingAllMoneyFields) { changeAllMoneyFields(newCurrency); } else { moneyField.setValue(moneyField.getValue().getConverted(newCurrency), isInitialized()); currencyLabel.setText(newCurrency); } }
/** Marks, wether this date entry is used as a table cell. */ public void setUsedAsTableCell(boolean isCell) { if (isCell) { removeAll(); setBackground(moneyField.getBackground()); moneyField.setBorder(null); add("East", moneyField); add(Box.createHorizontalStrut(GUI.HorizontalLabelDistance)); add(currencyLabel); } }
public void setEnabled(boolean isEnabled) { setReadonly(!isEnabled); moneyField.setEditable(isEnabled); if (!isEnabled) { moneyField.setDocument(textDocument); moneyField.setText(""); } else { moneyField.setDocument(moneyDocument); } }
/** Sets opaqueness of this entry. */ public void setOpaque(boolean opaque) { if (guiInitialized) { moneyField.setOpaque(opaque); } super.setOpaque(opaque); }
/** @return Money value */ public Money getValue() { if (!isInitialized()) { return null; } else { return moneyField.getValue(); } }
/** Overload hasFocus. */ public boolean hasFocus() { if (moneyField.hasFocus() || (calculator != null && calculator.hasFocus())) { return true; } return false; }
/** Remove FocusListener. */ public void removeFocusListener(FocusListener listener) { if (moneyField != null) { moneyField.removeFocusListener(listener); } super.removeFocusListener(listener); }
/** Add FocusListener. */ public void addFocusListener(FocusListener listener) { if (moneyField != null) { moneyField.addFocusListener(listener); } super.addFocusListener(listener); }
/** Finally clean up. */ protected void finalize() throws Throwable { allMoneyEntries.remove(this); KeyStroke SHIFT_F10 = KeyStroke.getKeyStroke(KeyEvent.VK_F10, InputEvent.SHIFT_MASK); moneyField.unregisterKeyboardAction(SHIFT_F10); super.finalize(); }
/** Check mandatory and isEditable to set color settings. */ protected void performFlags() { Assert.isNotNull(moneyField); Assert.isNotNull(flagLabel); if (isReadonly()) { moneyField.setBackground(SystemColor.control); flagLabel.setIcon(GUI.getOptionalIcon()); } else { // @todo find appropriate system color moneyField.setBackground(Color.white); if (mandatory && getObjectValue() == null) { flagLabel.setIcon(GUI.getMandatoryIcon()); } else { flagLabel.setIcon(GUI.getOptionalIcon()); } } }
public void setReadonly(boolean isReadonly) { this.isReadonly = isReadonly; moneyField.setEditable(!isReadonly); // En/disable currency popup also if (moneyPopup != null) { moneyPopup.setShowing(!isReadonly); } performFlags(); }
/** 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 remove. */ public void remove(int offs, int length) throws BadLocationException { int sourceLength = getLength(); // Allow user to restore uninitialized state again by removing all if (offs == 0 && sourceLength == length) { super.remove(0, sourceLength); return; } // Do custom remove String sourceText = getText(0, sourceLength); StringBuffer strBuffer = new StringBuffer(sourceText.substring(0, offs)); int counter; for (counter = offs; counter < offs + length; counter++) { // Only remove digits and intDelims char currChar = sourceText.charAt(counter); if (Character.isDigit(currChar) || currChar == intDelim) { continue; } strBuffer.append(currChar); } // Append last part of sourceText if (counter < sourceLength) { strBuffer.append(sourceText.substring(counter)); } // Set text in field super.remove(0, sourceLength); insertString(0, strBuffer.toString(), (AttributeSet) getDefaultRootElement()); // Set caret pos int newDiff = sourceLength - getLength() - 1; if (newDiff < 0) { newDiff = 0; } if (offs - newDiff < 0) { newDiff = 0; } textField.setCaretPosition(offs - newDiff); }
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()); } }
/** Gets the opaqueness of this entry. */ public boolean isOpaque() { return moneyField.isOpaque(); }
/** Border delegation. */ public void setBorder(Border border) { if (guiInitialized) { moneyField.setBorder(border); } }
public boolean isInitialized() { return (!"".equals(moneyField.getText())); }
public boolean isEnabled() { return moneyField.isEditable() && !isReadonly(); }
/** 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); } }
/** Sets BackgroundColor of this entry */ public void setBackground(Color color) { if (moneyField != null) { moneyField.setBackground(color); currencyLabel.setBackground(color); } }
/** Initialization called by constructors. */ protected void initialize(int size) { // Check desired size if (size <= 0) { size = 7; } // Construct fields moneyField = new LimitedNumberField(size); moneyField.setHorizontalAlignment(SwingConstants.RIGHT); currencyLabel = new JLabel(); // Popup menu JPopupMenu popupMenu = new JPopupMenu(); copyItem = new JMenuItem("Copy"); copyItem.addActionListener(this); popupMenu.add(copyItem); pasteItem = new JMenuItem("Paste"); pasteItem.addActionListener(this); popupMenu.add(pasteItem); popupMenu.add(new JPopupMenu.Separator()); // Default currencies JMenuItem menuItem; defaultMenuItems = new JMenuItem[defaultCurrencies.length]; for (int i = 0; i < defaultCurrencies.length; i++) { menuItem = new CurrencyMenuItem(defaultCurrencies[i]); menuItem.addActionListener(this); popupMenu.add(menuItem); defaultMenuItems[i] = menuItem; } popupMenu.add(new JPopupMenu.Separator()); // european currencies String europeanCurrencies[] = Money.getEuropeanCurrencyList(); europeanCurrenciesMenu = new JMenu("Europe"); for (int i = 0; i < europeanCurrencies.length; i++) { menuItem = new CurrencyMenuItem(europeanCurrencies[i]); menuItem.addActionListener(this); europeanCurrenciesMenu.add(menuItem); if (i == 0) { europeanCurrenciesMenu.add(new JPopupMenu.Separator()); } } popupMenu.add(europeanCurrenciesMenu); // Calculator popupMenu.add(new JPopupMenu.Separator()); calculatorItem = new JMenuItem("Calculator"); calculatorItem.addActionListener(this); popupMenu.add(calculatorItem); // Add the popup moneyPopup = PopupAdapter.create(this.moneyField, popupMenu); KeyStroke SHIFT_F10 = KeyStroke.getKeyStroke(KeyEvent.VK_F10, InputEvent.SHIFT_MASK); moneyField.registerKeyboardAction(this, "popup", SHIFT_F10, JComponent.WHEN_FOCUSED); // Now init documents textDocument = new JTextField().getDocument(); moneyDocument = (LimitedMoneyDocument) moneyField.getDocument(); moneyDocument.addDocumentListener(this); // Layout elements setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); flagLabel = new JLabel(GUI.getMandatoryIcon()); add("West", flagLabel); add("Center", Box.createHorizontalStrut(3)); add(moneyField); add(Box.createHorizontalStrut(GUI.HorizontalLabelDistance)); add(currencyLabel); // Check mandatory and readonly state performFlags(); // Set cursor setCursor(GUI.ENTRY_CURSOR); // Finally set the initial value Money money = new Money(); moneyField.setValue(money, false); currencyLabel.setText(Money.getCurrencyFor(money.getCurrency())); guiInitialized = true; }
/** Gets BackgroundColor of this entry */ public Color getBackground(Color color) { return moneyField.getBackground(); }