List of usage examples for java.awt.event ItemEvent getItemSelectable
public ItemSelectable getItemSelectable()
From source file:richtercloud.reflection.form.builder.components.AmountMoneyPanel.java
/** * Creates a new AmountMoneyPanel with {@link #DEFAULT_CURRENCIES} and * {@code additionalCurrencies}./*from www. j a va 2s . c o m*/ * @param amountMoneyCurrencyStorage * @param amountMoneyUsageStatisticsStorage * @param amountMoneyExchangeRateRetriever * @param messageHandler * @throws richtercloud.reflection.form.builder.components.AmountMoneyCurrencyStorageException */ public AmountMoneyPanel(AmountMoneyCurrencyStorage amountMoneyCurrencyStorage, AmountMoneyUsageStatisticsStorage amountMoneyUsageStatisticsStorage, AmountMoneyExchangeRateRetriever amountMoneyExchangeRateRetriever, MessageHandler messageHandler) throws AmountMoneyCurrencyStorageException { this.messageHandler = messageHandler; Set<Currency> exchangeRateRetrieverSupportedCurrencies = amountMoneyExchangeRateRetriever .getSupportedCurrencies(); for (Currency currency : amountMoneyCurrencyStorage.getCurrencies()) { if (!exchangeRateRetrieverSupportedCurrencies.contains(currency)) { try { currency.getExchangeRate(); } catch (ConversionException ex) { throw new IllegalArgumentException(String.format( "Currency %s isn't supported by exchange rate retriever and doesn't have an exchange rate set", currency)); } } currencyComboBoxModel.addElement(currency); } initComponents(); ((SpinnerNumberModel) amountIntegerSpinner.getModel()) .setMaximum(((long) (Double.MAX_VALUE * MINIMAL_STEP))); //cast to long is necessary to make ChangeListener of //amountIntegerSpinner be notified this.amountIntegerSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { for (AmountMoneyPanelUpdateListener updateListener : AmountMoneyPanel.this.updateListeners) { updateListener.onUpdate(new AmountMoneyPanelUpdateEvent(getValue())); } } }); this.amountDecimalSpinner.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { for (AmountMoneyPanelUpdateListener updateListener : AmountMoneyPanel.this.updateListeners) { updateListener.onUpdate(new AmountMoneyPanelUpdateEvent(getValue())); } } }); this.currencyComboBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent itemEvent) { //convert after currency change (not necessary, but useful) Currency oldCurrency = (Currency) itemEvent.getItem(); Currency newCurrency = (Currency) itemEvent.getItemSelectable().getSelectedObjects()[0]; double newAmount; try { newAmount = oldCurrency.getConverterTo(newCurrency).convert(getAmount()); } catch (ConversionException ex) { try { //if the exchange rate isn't set AmountMoneyPanel.this.amountMoneyExchangeRateRetriever.retrieveExchangeRate(newCurrency); AmountMoneyPanel.this.amountMoneyExchangeRateRetriever.retrieveExchangeRate(oldCurrency); newAmount = oldCurrency.getConverterTo(newCurrency).convert(getAmount()); } catch (AmountMoneyCurrencyStorageException amountMoneyCurrencyStorageException) { throw new RuntimeException(amountMoneyCurrencyStorageException); } } AmountMoneyPanel.this.amountIntegerSpinner.setValue((int) newAmount); BigDecimal bd = new BigDecimal(newAmount * 100); bd = bd.setScale(0, //newScale RoundingMode.HALF_UP //the rounding mode "taught in school" ); AmountMoneyPanel.this.amountDecimalSpinner.setValue(bd.intValue() % 100); //notify registered update listeners for (AmountMoneyPanelUpdateListener updateListener : AmountMoneyPanel.this.updateListeners) { updateListener.onUpdate(new AmountMoneyPanelUpdateEvent(getValue())); } } }); this.amountMoneyCurrencyStorage = amountMoneyCurrencyStorage; this.amountMoneyUsageStatisticsStorage = amountMoneyUsageStatisticsStorage; this.amountMoneyExchangeRateRetriever = amountMoneyExchangeRateRetriever; }