Skip to content

Commit 915a415

Browse files
committed
Enable disable currency exchange. (Logic)
1 parent e36afa0 commit 915a415

File tree

6 files changed

+98
-27
lines changed

6 files changed

+98
-27
lines changed

src/org/yccheok/jstock/gui/OptionsJDialog.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JStock - Free Stock Market Software
3-
* Copyright (C) 2010 Yan Cheng CHEOK <[email protected]>
3+
* Copyright (C) 2015 Yan Cheng Cheok <[email protected]>
44
*
55
* This program is free software; you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -17,6 +17,7 @@
1717
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
*/
1919

20+
2021
package org.yccheok.jstock.gui;
2122

2223
import org.yccheok.jstock.engine.Country;
@@ -95,8 +96,9 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
9596
final boolean oldIsFeeCalculationEnabled = jStockOptions.isFeeCalculationEnabled();
9697
final boolean oldIsDynamicChartVisible = jStockOptions.isDynamicChartVisible();
9798
final boolean oldIsFourDecimalPlacesEnabled = jStockOptions.isFourDecimalPlacesEnabled();
99+
final boolean oldIsCurrencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
98100
final PriceSource oldPriceSource = jStockOptions.getPriceSource(country);
99-
final Country oldLocalCurrencyCountry = jStockOptions.getLocalCurrencyCountry(country);
101+
final Country oldLocalCurrencyCountry = jStockOptions.getLocalCurrencyCountry(country);
100102

101103
if (apply(jStockOptions) == false) {
102104
return;
@@ -115,7 +117,7 @@ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
115117
if (oldIsDynamicChartVisible != jStockOptions.isDynamicChartVisible()) {
116118
JStock.getInstance().initDynamicChartVisibility();
117119
}
118-
if (oldLocalCurrencyCountry != jStockOptions.getLocalCurrencyCountry(country) || oldIsFourDecimalPlacesEnabled != jStockOptions.isFourDecimalPlacesEnabled()) {
120+
if (oldIsCurrencyExchangeEnable != jStockOptions.isCurrencyExchangeEnable(country) || oldLocalCurrencyCountry != jStockOptions.getLocalCurrencyCountry(country) || oldIsFourDecimalPlacesEnabled != jStockOptions.isFourDecimalPlacesEnabled()) {
119121
JStock.getInstance().getPortfolioManagementJPanel().refreshGUIAfterOptionsJDialog();
120122
}
121123

src/org/yccheok/jstock/gui/PortfolioManagementJPanel.java

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,17 +1805,21 @@ private TransactionSummary addBuyTransaction(Transaction transaction) {
18051805
final JStock jstock = JStock.getInstance();
18061806
final JStockOptions jStockOptions = jstock.getJStockOptions();
18071807
final Country country = jStockOptions.getCountry();
1808-
final Currency stockCurrency = org.yccheok.jstock.portfolio.Utils.getStockCurrency(portfolioRealTimeInfo, transaction.getStock().code);
1809-
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
1810-
final Currency localCurrency = localCountry.localCurrency;
1808+
1809+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
1810+
if (currencyExchangeEnable) {
1811+
final Currency stockCurrency = org.yccheok.jstock.portfolio.Utils.getStockCurrency(portfolioRealTimeInfo, transaction.getStock().code);
1812+
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
1813+
final Currency localCurrency = localCountry.localCurrency;
18111814

1812-
if (stockCurrency.equals(localCurrency)) {
1813-
break;
1814-
}
1815+
if (stockCurrency.equals(localCurrency)) {
1816+
break;
1817+
}
18151818

1816-
_exchangeRateMonitor.addCurrencyPair(CurrencyPair.create(stockCurrency, localCurrency));
1817-
_exchangeRateMonitor.startNewThreadsIfNecessary();
1818-
_exchangeRateMonitor.refresh();
1819+
_exchangeRateMonitor.addCurrencyPair(CurrencyPair.create(stockCurrency, localCurrency));
1820+
_exchangeRateMonitor.startNewThreadsIfNecessary();
1821+
_exchangeRateMonitor.refresh();
1822+
}
18191823
}
18201824
break;
18211825
} while (true);
@@ -2408,6 +2412,16 @@ public boolean savePortfolio() {
24082412

24092413
private void refreshStatusBarExchangeRateVisibility() {
24102414
final JStock mainFrame = JStock.getInstance();
2415+
final JStockOptions jStockOptions = mainFrame.getJStockOptions();
2416+
final Country country = jStockOptions.getCountry();
2417+
2418+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
2419+
2420+
if (!currencyExchangeEnable) {
2421+
mainFrame.setStatusBarExchangeRateVisible(false);
2422+
mainFrame.setStatusBarExchangeRateToolTipText(null);
2423+
return;
2424+
}
24112425

24122426
Set<CurrencyPair> currencyPairs = this.getCurrencyPairs();
24132427

@@ -2417,9 +2431,17 @@ private void refreshStatusBarExchangeRateVisibility() {
24172431
return;
24182432
}
24192433

2434+
final int size = currencyPairs.size();
2435+
2436+
if (size > 1) {
2437+
mainFrame.setStatusBarExchangeRateVisible(false);
2438+
mainFrame.setStatusBarExchangeRateToolTipText(null);
2439+
return;
2440+
}
2441+
24202442
// We will display the currency exchange rate, only if there is 1
24212443
// currency pair.
2422-
if (currencyPairs.size() == 1) {
2444+
if (size == 1) {
24232445
// Update the tool tip text.
24242446
CurrencyPair currencyPair = currencyPairs.iterator().next();
24252447
Currency fromCurrency = currencyPair.from();
@@ -2447,9 +2469,6 @@ private void refreshStatusBarExchangeRateVisibility() {
24472469
}
24482470
return;
24492471
}
2450-
2451-
mainFrame.setStatusBarExchangeRateVisible(false);
2452-
mainFrame.setStatusBarExchangeRateToolTipText(null);
24532472
}
24542473

24552474
/**
@@ -2459,6 +2478,10 @@ public void initExchangeRateMonitor() {
24592478
final JStock jstock = JStock.getInstance();
24602479
final JStockOptions jStockOptions = jstock.getJStockOptions();
24612480

2481+
final Country country = jStockOptions.getCountry();
2482+
2483+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
2484+
24622485
refreshStatusBarExchangeRateVisibility();
24632486

24642487
final ExchangeRateMonitor oldExchangeRateMonitor = this.exchangeRateMonitor;
@@ -2474,6 +2497,17 @@ public void run() {
24742497
});
24752498
}
24762499

2500+
2501+
if (!currencyExchangeEnable) {
2502+
this.exchangeRateMonitor = null;
2503+
if (oldExchangeRateMonitor != null) {
2504+
this.updateWealthHeader();
2505+
}
2506+
return;
2507+
}
2508+
2509+
assert(currencyExchangeEnable);
2510+
24772511
this.exchangeRateMonitor = new ExchangeRateMonitor(
24782512
Constants.EXCHANGE_RATE_MONITOR_MAX_THREAD,
24792513
Constants.EXCHANGE_RATE_MONITOR_MAX_STOCK_SIZE_PER_SCAN,
@@ -2751,8 +2785,14 @@ private void updateWealthHeader() {
27512785
final SellPortfolioTreeTableModelEx sellPortfolioTreeTableModel = (SellPortfolioTreeTableModelEx)this.sellTreeTable.getTreeTableModel();
27522786

27532787
final Country country = jStockOptions.getCountry();
2754-
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
2755-
final Currency localCurrency = localCountry.localCurrency;
2788+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
2789+
final Currency localCurrency;
2790+
if (currencyExchangeEnable) {
2791+
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
2792+
localCurrency = localCountry.localCurrency;
2793+
} else {
2794+
localCurrency = null;
2795+
}
27562796

27572797
final double share;
27582798
final double cash;

src/org/yccheok/jstock/gui/charting/InvestmentFlowChartJDialog.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* JStock - Free Stock Market Software
3-
* Copyright (C) 2011 Yan Cheng CHEOK <[email protected]>
3+
* Copyright (C) 2015 Yan Cheng Cheok <[email protected]>
44
*
55
* This program is free software; you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -291,8 +291,14 @@ private void loadDimension() {
291291
private void updateROITimeSeries() {
292292
final JStockOptions jStockOptions = JStock.getInstance().getJStockOptions();
293293
final Country country = jStockOptions.getCountry();
294-
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
295-
final Currency localCurrency = localCountry.localCurrency;
294+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
295+
final Currency localCurrency;
296+
if (currencyExchangeEnable) {
297+
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
298+
localCurrency = localCountry.localCurrency;
299+
} else {
300+
localCurrency = null;
301+
}
296302

297303
final boolean noCodeAddedToMonitor = this.realTimeStockMonitor.isEmpty();
298304
final List<Code> codesNeedToAddToRealTimeStockMonitor = new ArrayList<Code>();
@@ -376,8 +382,14 @@ public PortfolioRealTimeInfo getPortfolioRealTimeInfo() {
376382
private XYDataset createInvestDataset() {
377383
final JStockOptions jStockOptions = JStock.getInstance().getJStockOptions();
378384
final Country country = jStockOptions.getCountry();
379-
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
380-
final Currency localCurrency = localCountry.localCurrency;
385+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
386+
final Currency localCurrency;
387+
if (currencyExchangeEnable) {
388+
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
389+
localCurrency = localCountry.localCurrency;
390+
} else {
391+
localCurrency = null;
392+
}
381393

382394
final TimeSeries series = new TimeSeries(GUIBundle.getString("InvestmentFlowChartJDialog_Invest"));
383395

src/org/yccheok/jstock/gui/treetable/BuyPortfolioTreeTableModelEx.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,14 @@ public Object getValueAt(Object node, int column) {
379379

380380
if (node instanceof Portfolio) {
381381
final Country country = jStockOptions.getCountry();
382-
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
383-
final Currency localCurrency = localCountry.localCurrency;
382+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
383+
final Currency localCurrency;
384+
if (currencyExchangeEnable) {
385+
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
386+
localCurrency = localCountry.localCurrency;
387+
} else {
388+
localCurrency = null;
389+
}
384390

385391
final Portfolio portfolio = (Portfolio)node;
386392

src/org/yccheok/jstock/gui/treetable/SellPortfolioTreeTableModelEx.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,14 @@ public Object getValueAt(Object node, int column) {
215215

216216
if (node instanceof Portfolio) {
217217
final Country country = jStockOptions.getCountry();
218-
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
219-
final Currency localCurrency = localCountry.localCurrency;
218+
final boolean currencyExchangeEnable = jStockOptions.isCurrencyExchangeEnable(country);
219+
final Currency localCurrency;
220+
if (currencyExchangeEnable) {
221+
final Country localCountry = jStockOptions.getLocalCurrencyCountry(country);
222+
localCurrency = localCountry.localCurrency;
223+
} else {
224+
localCurrency = null;
225+
}
220226

221227
final Portfolio portfolio = (Portfolio)node;
222228

src/org/yccheok/jstock/portfolio/Utils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public static Currency getStockCurrency(PortfolioRealTimeInfo portfolioRealTimeI
6767
}
6868

6969
public static double getExchangeRate(PortfolioRealTimeInfo portfolioRealTimeInfo, Currency localCurrency, Currency stockCurrency) {
70+
// Possible null.
71+
if (localCurrency == null) {
72+
return 1.0;
73+
}
74+
7075
final double exchangeRate;
7176
if (stockCurrency.equals(localCurrency)) {
7277
exchangeRate = 1.0;

0 commit comments

Comments
 (0)