diff --git a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCodeBehind.java b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCodeBehind.java index 29cbab5e04..3c9169a7aa 100644 --- a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCodeBehind.java +++ b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferCodeBehind.java @@ -1,3 +1,20 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + package io.bitsquare.gui.trade.createoffer; import io.bitsquare.gui.CachedViewController; diff --git a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferModel.java b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferModel.java index 86f38afda5..4e377ee3f5 100644 --- a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferModel.java +++ b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferModel.java @@ -1,8 +1,22 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + package io.bitsquare.gui.trade.createoffer; -import com.google.bitcoin.core.Coin; -import com.google.bitcoin.utils.Fiat; -import com.google.inject.Inject; import io.bitsquare.bank.BankAccount; import io.bitsquare.btc.AddressEntry; import io.bitsquare.btc.FeePolicy; @@ -12,11 +26,26 @@ import io.bitsquare.settings.Settings; import io.bitsquare.trade.Direction; import io.bitsquare.trade.TradeManager; import io.bitsquare.user.User; + +import com.google.bitcoin.core.Coin; +import com.google.bitcoin.utils.Fiat; + +import com.google.inject.Inject; + import java.util.Locale; import java.util.UUID; -import javafx.beans.property.*; + +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.LongProperty; +import javafx.beans.property.ObjectProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.beans.property.SimpleLongProperty; +import javafx.beans.property.SimpleObjectProperty; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenter.java b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenter.java index a858ecae17..2fcc543fab 100644 --- a/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenter.java +++ b/src/main/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenter.java @@ -1,3 +1,20 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + package io.bitsquare.gui.trade.createoffer; import io.bitsquare.gui.util.BSFormatter; diff --git a/src/main/java/io/bitsquare/gui/util/BSFormatter.java b/src/main/java/io/bitsquare/gui/util/BSFormatter.java index fbe5484458..fc13b6e42f 100644 --- a/src/main/java/io/bitsquare/gui/util/BSFormatter.java +++ b/src/main/java/io/bitsquare/gui/util/BSFormatter.java @@ -23,10 +23,13 @@ import io.bitsquare.trade.Direction; import io.bitsquare.user.Arbitrator; import com.google.bitcoin.core.Coin; +import com.google.bitcoin.utils.CoinFormat; +import com.google.bitcoin.utils.Fiat; import java.text.DateFormat; import java.text.DecimalFormat; +import java.util.Currency; import java.util.Date; import java.util.List; import java.util.Locale; @@ -36,38 +39,92 @@ import org.slf4j.LoggerFactory; import static com.google.common.base.Preconditions.*; -//TODO cleanup... -public class BitSquareFormatter { - private static final Logger log = LoggerFactory.getLogger(BitSquareFormatter.class); +//TODO a lot of old trash... need to cleanup... +public class BSFormatter +{ + private static final Logger log = LoggerFactory.getLogger(BSFormatter.class); + + // format is like: 1,00 or 1,0010 never more then 4 decimals + private static CoinFormat coinFormat = CoinFormat.BTC.repeatOptionalDecimals(2, 1); + + // format is like: 1,00 never more then 2 decimals + private static CoinFormat fiatFormat = CoinFormat.FIAT.repeatOptionalDecimals(0, 0); + + private static String currencyCode = Currency.getInstance(Locale.getDefault()).getCurrencyCode(); + private static Locale locale = Locale.getDefault(); + + + /////////////////////////////////////////////////////////////////////////////////////////// + // Config + /////////////////////////////////////////////////////////////////////////////////////////// + + public static void useMilliBitFormat() + { + coinFormat = CoinFormat.MBTC.repeatOptionalDecimals(2, 1); + } + + public static void setFiatCurrencyCode(String currencyCode) + { + BSFormatter.currencyCode = currencyCode; + } + + public static void setLocale(Locale locale) + { + BSFormatter.locale = locale; + } + /////////////////////////////////////////////////////////////////////////////////////////// // BTC /////////////////////////////////////////////////////////////////////////////////////////// - public static String formatCoin(Coin coin) { - return coin != null ? coin.toPlainString() : ""; + public static String formatBtc(Coin coin) + { + try + { + return coinFormat.noCode().format(coin).toString(); + } catch (Throwable t) + { + log.warn("Exception at formatBtc: " + t.toString()); + return ""; + } } - public static String formatCoinWithCode(Coin coin) { - return coin != null ? coin.toFriendlyString() : ""; + public static String formatBtcWithCode(Coin coin) + { + try + { + return coinFormat.postfixCode().format(coin).toString(); + } catch (Throwable t) + { + log.warn("Exception at formatBtcWithCode: " + t.toString()); + return ""; + } + } + + public static Coin parseToCoin(String input) + { + try + { + input = input.replace(",", "."); + Double.parseDouble(input); // test if valid double + return Coin.parseCoin(input); + } catch (Throwable t) + { + log.warn("Exception at parseToCoin: " + t.toString()); + return Coin.ZERO; + } } /** - * @param input String input in decimal or integer format. Both decimal marks (",", ".") are supported. - * If input has an incorrect format it returns a zero value coin. - * @return + * Transform a coin with the properties defined in the format (used to reduce decimal places) + * + * @param coin The coin which should be transformed + * @return The transformed coin */ - public static Coin parseToCoin(String input) { - Coin result; - try { - input = input.replace(",", "."); - Double.parseDouble(input); - result = Coin.parseCoin(input); - } catch (Exception e) { - //log.warn("Exception at parseBtcToCoin: " + e.toString()); - result = Coin.ZERO; - } - return result; + public static Coin applyFormatRules(Coin coin) + { + return parseToCoin(formatBtc(coin)); } @@ -75,16 +132,41 @@ public class BitSquareFormatter { // FIAT /////////////////////////////////////////////////////////////////////////////////////////// - public static String formatPrice(double price) { - return formatDouble(price); + public static String formatFiat(Fiat fiat) + { + try + { + return fiatFormat.noCode().format(fiat).toString(); + } catch (Throwable t) + { + log.warn("Exception at formatFiat: " + t.toString()); + return ""; + } } - public static String formatVolume(double volume) { - return formatDouble(volume); + public static String formatFiatWithCode(Fiat fiat) + { + try + { + return fiatFormat.postfixCode().format(fiat).toString(); + } catch (Throwable t) + { + log.warn("Exception at formatFiatWithCode: " + t.toString()); + return ""; + } } - public static String formatVolumeWithMinVolume(double volume, double minVolume) { - return formatDouble(volume) + " (" + formatDouble(minVolume) + ")"; + public static Fiat parseToFiat(String input) + { + try + { + input = input.replace(",", "."); + Double.parseDouble(input); // test if valid double + return Fiat.parseFiat(currencyCode, input); + } catch (Exception e) + { + return Fiat.valueOf(currencyCode, 0); + } } @@ -93,79 +175,79 @@ public class BitSquareFormatter { /////////////////////////////////////////////////////////////////////////////////////////// /** - * @param input String to be converted to a double. Both decimal points "." and ", - * " are supported. Thousands separator is not supported. + * @param input String to be converted to a double. Both decimal points "." and "," are supported. Thousands separator is not supported. * @return Returns a double value. Any invalid value returns Double.NEGATIVE_INFINITY. */ - public static double parseToDouble(String input) { - try { + public static double parseToDouble(String input) + { + try + { checkNotNull(input); checkArgument(input.length() > 0); input = input.replace(",", ".").trim(); return Double.parseDouble(input); - } catch (Exception e) { + } catch (Exception e) + { return 0; } } - public static String formatCollateralAsBtc(String amount, double collateral) { - Coin amountAsCoin = BitSquareFormatter.parseToCoin(amount); - Coin collateralAsCoin = amountAsCoin.divide((long) (1d / collateral)); - return formatCoinWithCode(collateralAsCoin); - } - - public static String formatTotalsAsBtc(String amount, double collateral, Coin fees) { - Coin amountAsCoin = BitSquareFormatter.parseToCoin(amount); - Coin collateralAsCoin = amountAsCoin.divide((long) (1d / collateral)); - Coin totals = collateralAsCoin.add(fees); - return formatCoinWithCode(totals); - } - - public static String formatDirection(Direction direction, boolean allUpperCase) { + public static String formatDirection(Direction direction, boolean allUpperCase) + { String result = (direction == Direction.BUY) ? "Buy" : "Sell"; - if (allUpperCase) { + if (allUpperCase) + { result = result.toUpperCase(); } return result; } - public static String formatDouble(double value) { + public static String formatDouble(double value) + { return formatDouble(value, 4); } - public static String formatDouble(double value, int fractionDigits) { + public static String formatDouble(double value, int fractionDigits) + { DecimalFormat decimalFormat = getDecimalFormat(fractionDigits); return decimalFormat.format(value); } - public static DecimalFormat getDecimalFormat(int fractionDigits) { - DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.getDefault()); + public static DecimalFormat getDecimalFormat(int fractionDigits) + { + DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(locale); decimalFormat.setMinimumFractionDigits(fractionDigits); decimalFormat.setMaximumFractionDigits(fractionDigits); decimalFormat.setGroupingUsed(false); return decimalFormat; } - public static String countryLocalesToString(List countries) { + public static String countryLocalesToString(List countries) + { String result = ""; int i = 0; - for (Country country : countries) { + for (Country country : countries) + { result += country.getName(); i++; - if (i < countries.size()) { + if (i < countries.size()) + { result += ", "; } } return result; } - public static String languageLocalesToString(List languageLocales) { + public static String languageLocalesToString(List languageLocales) + { String result = ""; int i = 0; - for (Locale locale : languageLocales) { + for (Locale locale : languageLocales) + { result += locale.getDisplayLanguage(); i++; - if (i < languageLocales.size()) { + if (i < languageLocales.size()) + { result += ", "; } } @@ -173,13 +255,16 @@ public class BitSquareFormatter { } - public static String arbitrationMethodsToString(List items) { + public static String arbitrationMethodsToString(List items) + { String result = ""; int i = 0; - for (Arbitrator.METHOD item : items) { + for (Arbitrator.METHOD item : items) + { result += Localisation.get(item.toString()); i++; - if (i < items.size()) { + if (i < items.size()) + { result += ", "; } } @@ -187,26 +272,62 @@ public class BitSquareFormatter { } - public static String arbitrationIDVerificationsToString(List items) { + public static String arbitrationIDVerificationsToString(List items) + { String result = ""; int i = 0; - for (Arbitrator.ID_VERIFICATION item : items) { + for (Arbitrator.ID_VERIFICATION item : items) + { result += Localisation.get(item.toString()); i++; - if (i < items.size()) { + if (i < items.size()) + { result += ", "; } } return result; } - public static String formatDateTime(Date date) { - DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.getDefault()); - DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.getDefault()); + public static String formatDateTime(Date date) + { + DateFormat dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, locale); + DateFormat timeFormatter = DateFormat.getTimeInstance(DateFormat.DEFAULT, locale); return dateFormatter.format(date) + " " + timeFormatter.format(date); } - public static String formatCollateralPercent(double collateral) { - return getDecimalFormat(2).format(collateral * 100) + " %"; + public static String formatCollateralPercent(long collateral) + { + return getDecimalFormat(1).format(collateral / 10) + " %"; + } + + @Deprecated + public static String formatPrice(double volume) + { + return formatDouble(volume); + } + + @Deprecated + public static String formatVolume(double volume) + { + return formatDouble(volume); + } + + @Deprecated + public static String formatVolumeWithMinVolume(double volume, double minVolume) + { + return formatDouble(volume) + " (" + formatDouble(minVolume) + ")"; + } + + @Deprecated + public static String formatCoin(Coin coin) + { + return coin != null ? coin.toPlainString() : ""; + } + + + @Deprecated + public static String formatCoinWithCode(Coin coin) + { + return coin != null ? coin.toFriendlyString() : ""; } } diff --git a/src/test/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenterTest.java b/src/test/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenterTest.java index acc3d0697a..3cc189ecd6 100644 --- a/src/test/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenterTest.java +++ b/src/test/java/io/bitsquare/gui/trade/createoffer/CreateOfferPresenterTest.java @@ -1,3 +1,20 @@ +/* + * This file is part of Bitsquare. + * + * Bitsquare is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bitsquare is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bitsquare. If not, see . + */ + package io.bitsquare.gui.trade.createoffer; import io.bitsquare.bank.BankAccountType;