/** Return a menu for DesktopWindow and add a special listener. */ public static JMenu getCurrencyMenu(ActionListener listener) { JRadioButtonMenuItem menuItem; String defaultCurrency = Money.getCurrencyFor(Money.getDefaultCurrency()); JMenu menu = new JMenu("Währung"); ButtonGroup group = new ButtonGroup(); JMenu europeanMenu = new JMenu("Europa"); String europeanCurrencies[] = Money.getEuropeanCurrencyList(); for (int i = 0; i < europeanCurrencies.length; i++) { menuItem = new RadioCurrencyMenuItem(europeanCurrencies[i]); menuItem.addActionListener(listener); europeanMenu.add(menuItem); group.add(menuItem); if (europeanCurrencies[i].startsWith(defaultCurrency)) { menuItem.setSelected(true); } if (i == 0) { europeanMenu.addSeparator(); } } menu.add(europeanMenu); return menu; }
public void runCalculatorDialog(MouseEvent e) { if (calculatorDialog == null) { JFrame frame = null; Container container = getTopLevelAncestor(); if (container instanceof JFrame) { frame = (JFrame) container; } calculatorDialog = new JDialog(frame, "Rechner", true); calculator = new Calculator(); calculator.getOKButton().addActionListener(this); calculatorDialog.getContentPane().add(calculator); calculatorDialog.pack(); } // Set calculator's init value Money money = getValue(); if (money != null) { calculator.setValue(money.getValue()); } else { calculator.setValue(0.0); } // calculate POP (point of presentation) Point point = ((JComponent) e.getSource()).getLocationOnScreen(); int x = point.x + e.getX(); int y = point.y + e.getY(); // ensure that it does not exceed the screen limits Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); Dimension dim = calculatorDialog.getPreferredSize(); if (x + dim.width >= screenDim.width) { x = screenDim.width - dim.width - 1; } if (y + dim.height >= screenDim.height) { y = screenDim.height - dim.height - 1; } // make it visible at wanted location calculatorDialog.setLocation(x, y); calculatorDialog.show(); }
/** Sets money value. */ public void setValue(Money value, boolean updateGui) { this.value = value; if (updateGui) { setText(myFormat.format(value.doubleValue())); } }
/** 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(); }
/** 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; }
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()); } }