use of jgnash.engine.CurrencyNode in project jgnash by ccavanaugh.
the class IncomeExpensePieChartDialogController method updateChart.
private void updateChart() {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
final Account a = accountComboBox.getValue();
if (a != null) {
final CurrencyNode defaultCurrency = a.getCurrencyNode();
final NumberFormat numberFormat = NumericFormats.getFullCommodityFormat(defaultCurrency);
final ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
double total = a.getTreeBalance(startDatePicker.getValue(), endDatePicker.getValue(), defaultCurrency).doubleValue();
for (final Account child : a.getChildren()) {
double balance = child.getTreeBalance(startDatePicker.getValue(), endDatePicker.getValue(), defaultCurrency).doubleValue();
if (balance > 0 || balance < 0) {
final String label = child.getName() + " - " + numberFormat.format(balance);
final PieChart.Data data = new PieChart.Data(label, balance / total * 100);
// nodes are created lazily. Set the user data (Account) after the node is created
data.nodeProperty().addListener((observable, oldValue, newValue) -> newValue.setUserData(child));
pieChartData.add(data);
}
}
pieChart.setData(pieChartData);
final NumberFormat percentFormat = NumberFormat.getPercentInstance();
percentFormat.setMaximumFractionDigits(1);
percentFormat.setMinimumFractionDigits(1);
// Install tooltips on the data after it has been added to the chart
pieChart.getData().forEach(data -> Tooltip.install(data.getNode(), new Tooltip((((Account) data.getNode().getUserData()).getName() + " - " + percentFormat.format(data.getPieValue() / 100d)))));
// Indicate the node can be clicked on to zoom into the next account level
for (final PieChart.Data data : pieChart.getData()) {
data.getNode().setOnMouseEntered(event -> {
final Account account = (Account) data.getNode().getUserData();
if (account.isParent()) {
data.getNode().setCursor(CustomCursor.getZoomInCursor());
} else {
data.getNode().setCursor(Cursor.DEFAULT);
}
nodeFocused = true;
});
data.getNode().setOnMouseExited(event -> nodeFocused = false);
// zoom in on click if this is a parent account
data.getNode().setOnMouseClicked(event -> {
if (data.getNode().getUserData() != null) {
if (((Account) data.getNode().getUserData()).isParent()) {
accountComboBox.setValue((Account) data.getNode().getUserData());
}
}
});
}
final String title;
// pick an appropriate title
switch(a.getAccountType()) {
case EXPENSE:
title = resources.getString("Title.PercentExpense");
break;
case INCOME:
title = resources.getString("Title.PercentIncome");
break;
default:
title = resources.getString("Title.PercentDist");
break;
}
pieChart.setTitle(title);
pieChart.centerTitleProperty().set(accountComboBox.getValue().getName());
pieChart.centerSubTitleProperty().set(numberFormat.format(total));
// abs() on all values won't work if children aren't of uniform sign,
// then again, this chart is not right to display those trees
// boolean negate = total != null && total.signum() < 0;
} else {
pieChart.setData(FXCollections.emptyObservableList());
pieChart.setTitle("No Data");
}
}
use of jgnash.engine.CurrencyNode in project jgnash by ccavanaugh.
the class AddRemoveCurrencyController method handleNewCurrencyAction.
@FXML
private void handleNewCurrencyAction() {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
final CurrencyNode node = DefaultCurrencies.buildCustomNode(newCurrencyTextField.getText());
// the add could fail if the commodity symbol is a duplicate
if (engine.addCurrency(node)) {
selectedList.getItems().add(new LockedCommodityNode<>(node, false));
FXCollections.sort(selectedList.getItems());
newCurrencyTextField.clear();
}
}
use of jgnash.engine.CurrencyNode in project jgnash by ccavanaugh.
the class ModifyCurrencyController method messagePosted.
@Override
public void messagePosted(final Message message) {
final CommodityNode node = message.getObject(MessageProperty.COMMODITY);
if (node instanceof CurrencyNode) {
switch(message.getEvent()) {
case CURRENCY_REMOVE:
listView.getItems().remove(node);
if (node.equals(selectedCurrency.get())) {
handleClearAction();
}
break;
case CURRENCY_REMOVE_FAILED:
StaticUIMethods.displayError(resources.getString("Message.Warn.CurrencyInUse"));
break;
case CURRENCY_ADD:
case CONFIG_MODIFY:
handleClearAction();
loadModel();
break;
case CURRENCY_ADD_FAILED:
StaticUIMethods.displayError(resources.getString("Message.Error.AddCurrency"));
break;
case CURRENCY_MODIFY_FAILED:
StaticUIMethods.displayError(resources.getString("Message.Error.ModifyCurrency"));
break;
default:
}
}
}
use of jgnash.engine.CurrencyNode in project jgnash by ccavanaugh.
the class ModifyCurrencyController method handleApplyAction.
@FXML
private void handleApplyAction() {
final Engine engine = EngineFactory.getEngine(EngineFactory.DEFAULT);
Objects.requireNonNull(engine);
final CurrencyNode newNode = new CurrencyNode();
newNode.setSymbol(symbolTextField.getText());
newNode.setDescription(descriptionTextField.getText());
newNode.setPrefix(prefixTextField.getText());
newNode.setScale(scaleTextField.getInteger().byteValue());
newNode.setSuffix(suffixTextField.getText());
if (!engine.updateCommodity(selectedCurrency.get(), newNode)) {
StaticUIMethods.displayError(ResourceUtils.getString("Message.Error.CurrencyUpdate", newNode.getSymbol()));
}
loadModel();
}
use of jgnash.engine.CurrencyNode in project jgnash by ccavanaugh.
the class ControlsTest method createEngine.
private Engine createEngine() {
try {
testFile = Files.createTempFile("test", DataStoreType.BINARY_XSTREAM.getDataStore().getFileExt()).toFile().getAbsolutePath();
tempFile = testFile;
} catch (IOException e1) {
LogUtil.logSevere(ControlsTest.class, e1);
}
EngineFactory.deleteDatabase(testFile);
final Engine engine = EngineFactory.bootLocalEngine(testFile, EngineFactory.DEFAULT, EngineFactory.EMPTY_PASSWORD, DataStoreType.BINARY_XSTREAM);
Objects.requireNonNull(engine);
CurrencyNode node = engine.getDefaultCurrency();
if (!node.getSymbol().equals("USD")) {
engine.setDefaultCurrency(DefaultCurrencies.buildNode(Locale.US));
}
node = engine.getCurrency("CAD");
if (node == null) {
node = DefaultCurrencies.buildNode(Locale.CANADA);
assertNotNull(node);
assertTrue(engine.addCurrency(node));
}
Account account = new Account(AccountType.BANK, engine.getDefaultCurrency());
account.setName("Bank Accounts");
engine.addAccount(engine.getRootAccount(), account);
SecurityNode securityNode = new SecurityNode();
securityNode.setSymbol("GGG");
securityNode.setDescription("Google");
securityNode.setReportedCurrencyNode(engine.getDefaultCurrency());
engine.addSecurity(securityNode);
securityNode = new SecurityNode();
securityNode.setSymbol("MSFT");
securityNode.setDescription("Microsoft");
securityNode.setReportedCurrencyNode(engine.getDefaultCurrency());
engine.addSecurity(securityNode);
return engine;
}
Aggregations