From e404ebd74303d844ea4a95228200edb6a18179ef Mon Sep 17 00:00:00 2001 From: Hao Liu Date: Fri, 13 Dec 2024 22:42:47 +0800 Subject: [PATCH] Infer the default currency based on the user's locale when creating a new portfolio file --- .gitignore | 1 + .../ui/wizards/client/BaseCurrencySelectionPage.java | 12 ++++++------ .../name/abuchen/portfolio/money/CurrencyUnit.java | 10 ++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 7cbaf96540..cc0143cfdc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ workspace .metadata/* .recommenders/* .DS_Store +.sonarlint ### IntelliJ IDEA ### .idea diff --git a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/client/BaseCurrencySelectionPage.java b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/client/BaseCurrencySelectionPage.java index 8c1cfee185..982943202a 100644 --- a/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/client/BaseCurrencySelectionPage.java +++ b/name.abuchen.portfolio.ui/src/name/abuchen/portfolio/ui/wizards/client/BaseCurrencySelectionPage.java @@ -3,11 +3,6 @@ import java.util.Collections; import java.util.List; -import name.abuchen.portfolio.money.CurrencyUnit; -import name.abuchen.portfolio.ui.Messages; -import name.abuchen.portfolio.ui.util.FormDataFactory; -import name.abuchen.portfolio.ui.wizards.AbstractWizardPage; - import org.eclipse.jface.viewers.ArrayContentProvider; import org.eclipse.jface.viewers.ComboViewer; import org.eclipse.jface.viewers.IStructuredSelection; @@ -17,6 +12,10 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; +import name.abuchen.portfolio.money.CurrencyUnit; +import name.abuchen.portfolio.ui.Messages; +import name.abuchen.portfolio.ui.util.FormDataFactory; +import name.abuchen.portfolio.ui.wizards.AbstractWizardPage; class BaseCurrencySelectionPage extends AbstractWizardPage { private ComboViewer combo; @@ -41,12 +40,13 @@ public void createControl(Composite parent) Label label = new Label(container, SWT.NONE); label.setText(Messages.ColumnCurrency); + List currencies = CurrencyUnit.getAvailableCurrencyUnits(); Collections.sort(currencies); combo = new ComboViewer(container); combo.setContentProvider(ArrayContentProvider.getInstance()); combo.setInput(currencies); - combo.setSelection(new StructuredSelection(CurrencyUnit.getInstance(CurrencyUnit.EUR))); + combo.setSelection(new StructuredSelection(CurrencyUnit.getDefaultInstance())); Label description = new Label(container, SWT.WRAP); description.setText(this.explanationIndividualCurrency); diff --git a/name.abuchen.portfolio/src/name/abuchen/portfolio/money/CurrencyUnit.java b/name.abuchen.portfolio/src/name/abuchen/portfolio/money/CurrencyUnit.java index ebb65165c0..8748bacde1 100644 --- a/name.abuchen.portfolio/src/name/abuchen/portfolio/money/CurrencyUnit.java +++ b/name.abuchen.portfolio/src/name/abuchen/portfolio/money/CurrencyUnit.java @@ -6,6 +6,7 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.MissingResourceException; import java.util.NavigableMap; @@ -73,6 +74,15 @@ public static CurrencyUnit getInstance(String currencyCode) return CACHE.get(currencyCode); } + public static CurrencyUnit getDefaultInstance() + { + String defaultCurrencyISO4217 = java.util.Currency.getInstance(Locale.getDefault()).getCurrencyCode(); + CurrencyUnit defaultCurrencyUnit = CurrencyUnit.getInstance(defaultCurrencyISO4217); + if (defaultCurrencyUnit != null) + { return defaultCurrencyUnit; } + return CurrencyUnit.getInstance(EUR); + } + public static CurrencyUnit getInstanceBySymbol(String currencySymbol) { if (currencySymbol == null)