explorer: Replace formatting with a big TODO

This commit is contained in:
Andras Slemmer 2016-09-20 09:47:26 +01:00
parent f91b0978eb
commit 5d0ff4e60c
5 changed files with 28 additions and 56 deletions

View File

@ -1,35 +1,17 @@
package com.r3corda.explorer.formatters
import com.ibm.icu.text.DecimalFormat
import com.r3corda.core.contracts.Amount
import humanize.icu.spi.context.DefaultICUContext
import java.util.Currency
class AmountFormatter {
/**
* A note on formatting: Currently we don't have any fancy locale/use-case-specific formatting of amounts. This is a
* non-trivial problem that requires substantial work.
* Libraries to evaluate: IBM ICU currency library, github.com/mfornos/humanize, JSR 354 ref. implementation
*/
companion object {
val icuContext = DefaultICUContext()
fun currency(formatter: Formatter<Amount<Currency>>) = object : Formatter<Amount<Currency>> {
override fun format(value: Amount<Currency>) =
"${value.token.currencyCode} ${formatter.format(value)}"
}
val comma = object : Formatter<Amount<Currency>> {
override fun format(value: Amount<Currency>) =
NumberFormatter.doubleComma.format(value.quantity / 100.0)
}
val compact = object : Formatter<Amount<Currency>> {
val decimalFormat = (icuContext.compactDecimalFormat as DecimalFormat).apply {
minimumFractionDigits = 0
maximumFractionDigits = 4
setSignificantDigitsUsed(false)
}
override fun format(value: Amount<Currency>): String {
return decimalFormat.format(value.quantity / 100.0)
}
}
object AmountFormatter {
// TODO replace this once we settled on how we do formatting
val boring = object : Formatter<Amount<Currency>> {
override fun format(value: Amount<Currency>) = "${value.quantity} ${value.token}"
}
}

View File

@ -1,22 +1,10 @@
package com.r3corda.explorer.formatters
import java.text.DecimalFormat
class NumberFormatter {
companion object {
private val doubleCommaFormatter = DecimalFormat("#,###.00")
private val integralCommaFormatter = DecimalFormat("#,###")
private val _integralComma: Formatter<Any> = object : Formatter<Any> {
override fun format(value: Any) = integralCommaFormatter.format(value)
}
val doubleComma = object : Formatter<Double> {
override fun format(value: Double) = doubleCommaFormatter.format(value)
}
val numberComma: Formatter<Number> = _integralComma
val longComma: Formatter<Long> = _integralComma
object NumberFormatter {
// TODO replace this once we settled on how we do formatting
val boring = object : Formatter<Any> {
override fun format(value: Any) = value.toString()
}
val boringLong: Formatter<Long> = boring
}

View File

@ -10,7 +10,6 @@ import com.r3corda.core.contracts.StateAndRef
import com.r3corda.core.contracts.withoutIssuer
import com.r3corda.core.crypto.Party
import com.r3corda.explorer.formatters.AmountFormatter
import com.r3corda.explorer.formatters.NumberFormatter
import com.r3corda.explorer.model.ReportingCurrencyModel
import com.r3corda.explorer.model.SettingsModel
import com.r3corda.explorer.ui.*
@ -222,8 +221,8 @@ class CashViewer : View() {
init {
val amountNoIssuer = stateRow.stateAndRef.state.data.amount.withoutIssuer()
val amountFormatter = AmountFormatter.currency(AmountFormatter.comma)
val equivFormatter = AmountFormatter.comma
val amountFormatter = AmountFormatter.boring
val equivFormatter = AmountFormatter.boring
equivLabel.textProperty().bind(equivAmount.map { it.token.currencyCode.toString() })
stateIdValueLabel.text = stateRow.stateAndRef.ref.toString()
@ -302,7 +301,7 @@ class CashViewer : View() {
"Total $it position$plural"
})
val equivSumLabelFormatter = AmountFormatter.currency(AmountFormatter.compact)
val equivSumLabelFormatter = AmountFormatter.boring
equivSumLabel.textProperty().bind(selectedViewerNodeSumEquiv.map {
equivSumLabelFormatter.format(it)
})
@ -311,7 +310,7 @@ class CashViewer : View() {
cashStatesList.setCustomCellFactory { StateRowGraphic(it).root }
val cellFactory = AmountFormatter.comma.toTreeTableCellFactory<ViewerNode, Amount<Currency>>()
val cellFactory = AmountFormatter.boring.toTreeTableCellFactory<ViewerNode, Amount<Currency>>()
// TODO use smart resize
cashViewerTable.setColumnPrefWidthPolicy { tableWidthWithoutPaddingAndBorder, column ->

View File

@ -7,7 +7,6 @@ import com.r3corda.contracts.asset.Cash
import com.r3corda.core.contracts.StateAndRef
import com.r3corda.core.contracts.withoutIssuer
import com.r3corda.explorer.formatters.AmountFormatter
import com.r3corda.explorer.formatters.NumberFormatter
import com.r3corda.explorer.model.SelectedView
import com.r3corda.explorer.model.SettingsModel
import com.r3corda.explorer.model.TopLevelModel
@ -47,7 +46,7 @@ class Home : View() {
)
init {
val formatter = AmountFormatter.currency(AmountFormatter.compact)
val formatter = AmountFormatter.boring
ourCashLabel.textProperty().bind(sumAmount.map { formatter.format(it) })
ourCashPane.setOnMouseClicked { clickEvent ->
@ -57,7 +56,7 @@ class Home : View() {
}
ourTransactionsLabel.textProperty().bind(
Bindings.size(gatheredTransactionDataList).map { NumberFormatter.numberComma.format(it) }
Bindings.size(gatheredTransactionDataList).map { it.toString() }
)
ourTransactionsPane.setOnMouseClicked { clickEvent ->
if (clickEvent.button == MouseButton.PRIMARY) {

View File

@ -1,6 +1,9 @@
package com.r3corda.explorer.views
import com.r3corda.client.fxutils.*
import com.r3corda.client.fxutils.ChosenList
import com.r3corda.client.fxutils.bind
import com.r3corda.client.fxutils.lift
import com.r3corda.client.fxutils.map
import com.r3corda.client.model.*
import com.r3corda.contracts.asset.Cash
import com.r3corda.core.contracts.*
@ -260,7 +263,7 @@ class TransactionViewer: View() {
null.lift()
}
}
statesAmount.setCellFactory(NumberFormatter.longComma.toTableCellFactory())
statesAmount.cellFactory = NumberFormatter.boringLong.toTableCellFactory()
statesEquiv.setCellValueFactory {
val state = it.value.transactionState.data
if (state is Cash.State) {
@ -271,6 +274,7 @@ class TransactionViewer: View() {
null.lift()
}
}
statesEquiv.cellFactory = AmountFormatter.boring.toTableCellFactory()
}
init {
@ -318,7 +322,7 @@ class TransactionViewer: View() {
transactionViewTotalValueEquiv.setCellValueFactory<ViewerNode, AmountDiff<Currency>> { it.value.totalValueEquiv }
transactionViewTotalValueEquiv.cellFactory = object : Formatter<AmountDiff<Currency>> {
override fun format(value: AmountDiff<Currency>) =
"${value.positivity.sign}${AmountFormatter.comma.format(value.amount)}"
"${value.positivity.sign}${AmountFormatter.boring.format(value.amount)}"
}.toTableCellFactory()
// Contract states