Search in sources :

Example 31 with CurrencyNode

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");
    }
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Account(jgnash.engine.Account) PieChart(javafx.scene.chart.PieChart) Tooltip(javafx.scene.control.Tooltip) Engine(jgnash.engine.Engine) NumberFormat(java.text.NumberFormat)

Example 32 with CurrencyNode

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();
    }
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Engine(jgnash.engine.Engine) InjectFXML(jgnash.uifx.util.InjectFXML) FXML(javafx.fxml.FXML)

Example 33 with CurrencyNode

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:
        }
    }
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) CommodityNode(jgnash.engine.CommodityNode)

Example 34 with CurrencyNode

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();
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Engine(jgnash.engine.Engine) InjectFXML(jgnash.uifx.util.InjectFXML) FXML(javafx.fxml.FXML)

Example 35 with CurrencyNode

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;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) Account(jgnash.engine.Account) SecurityNode(jgnash.engine.SecurityNode) IOException(java.io.IOException) Engine(jgnash.engine.Engine)

Aggregations

CurrencyNode (jgnash.engine.CurrencyNode)58 Account (jgnash.engine.Account)28 Engine (jgnash.engine.Engine)28 BigDecimal (java.math.BigDecimal)10 ArrayList (java.util.ArrayList)10 LocalDate (java.time.LocalDate)8 NumberFormat (java.text.NumberFormat)7 List (java.util.List)7 FXML (javafx.fxml.FXML)7 AccountGroup (jgnash.engine.AccountGroup)7 Transaction (jgnash.engine.Transaction)6 ResourceBundle (java.util.ResourceBundle)5 SecurityNode (jgnash.engine.SecurityNode)5 InjectFXML (jgnash.uifx.util.InjectFXML)5 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)4 JasperPrint (net.sf.jasperreports.engine.JasperPrint)4 HashMap (java.util.HashMap)3 Objects (java.util.Objects)3 ExecutionException (java.util.concurrent.ExecutionException)3