Search in sources :

Example 1 with AccountMenuItem

use of com.owlplug.auth.ui.AccountMenuItem in project OwlPlug by DropSnorz.

the class MainController method refreshAccounts.

/**
 * Refresh Account comboBox.
 */
public void refreshAccounts() {
    ArrayList<UserAccount> accounts = new ArrayList<UserAccount>();
    for (UserAccount account : authenticationService.getAccounts()) {
        accounts.add(account);
    }
    accountComboBox.hide();
    accountComboBox.getItems().clear();
    accountComboBox.getItems().setAll(accounts);
    accountComboBox.getItems().add(new AccountMenuItem(" + New Account"));
    long selectedAccountId = this.getPreferences().getLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, -1);
    if (selectedAccountId != -1) {
        Optional<UserAccount> selectedAccount = authenticationService.getUserAccountById(selectedAccountId);
        if (selectedAccount.isPresent()) {
            // Bug workaround. The only way to pre-select the account is to find its
            // index in the list
            // If not, the selected cell is not rendered correctly
            accountComboBox.getItems().stream().filter(account -> account.getId().equals(selectedAccount.get().getId())).findAny().ifPresent(accountComboBox.getSelectionModel()::select);
        } else {
            accountComboBox.setValue(null);
        }
    } else {
        accountComboBox.setValue(null);
    }
}
Also used : AccountMenuItem(com.owlplug.auth.ui.AccountMenuItem) ArrayList(java.util.ArrayList) UserAccount(com.owlplug.auth.model.UserAccount)

Example 2 with AccountMenuItem

use of com.owlplug.auth.ui.AccountMenuItem in project OwlPlug by DropSnorz.

the class MainController method initialize.

/**
 * FXML initialize method.
 */
@FXML
public void initialize() {
    viewRegistry.preload();
    this.tabPaneHeader.getSelectionModel().selectedIndexProperty().addListener((options, oldValue, newValue) -> {
        tabPaneContent.getSelectionModel().select(newValue.intValue());
        leftDrawer.close();
        // store tab.
        if (newValue.intValue() == 2) {
            storeController.requestLayout();
        }
    });
    accountComboBox.getSelectionModel().selectedItemProperty().addListener((options, oldValue, newValue) -> {
        if (newValue instanceof AccountMenuItem) {
            accountController.show();
            // Delay comboBox selector change
            Platform.runLater(() -> accountComboBox.setValue(oldValue));
        }
        if (newValue instanceof UserAccount) {
            UserAccount userAccount = (UserAccount) newValue;
            this.getPreferences().putLong(ApplicationDefaults.SELECTED_ACCOUNT_KEY, userAccount.getId());
        }
        accountComboBox.hide();
    });
    accountComboBox.setButtonCell(new AccountCellFactory(imageCache, Pos.CENTER_RIGHT).call(null));
    accountComboBox.setCellFactory(new AccountCellFactory(authenticationService, imageCache, true));
    JFXComboBoxListViewSkin<AccountItem> accountCBSkin = new JFXComboBoxListViewSkin<AccountItem>(accountComboBox);
    accountCBSkin.setHideOnClick(false);
    accountComboBox.setSkin(accountCBSkin);
    refreshAccounts();
    downloadUpdateButton.setOnAction(e -> {
        PlatformUtils.openDefaultBrowser(this.getApplicationDefaults().getUpdateDownloadUrl());
    });
    updatePane.setVisible(false);
    Task<Boolean> retrieveUpdateStatusTask = new Task<Boolean>() {

        @Override
        protected Boolean call() throws Exception {
            return updateService.isUpToDate();
        }
    };
    retrieveUpdateStatusTask.setOnSucceeded(e -> {
        if (!retrieveUpdateStatusTask.getValue()) {
            updatePane.setVisible(true);
        }
    });
    new Thread(retrieveUpdateStatusTask).start();
}
Also used : Task(javafx.concurrent.Task) AccountMenuItem(com.owlplug.auth.ui.AccountMenuItem) JFXComboBoxListViewSkin(com.jfoenix.skins.JFXComboBoxListViewSkin) AccountItem(com.owlplug.auth.ui.AccountItem) UserAccount(com.owlplug.auth.model.UserAccount) AccountCellFactory(com.owlplug.auth.ui.AccountCellFactory) FXML(javafx.fxml.FXML)

Aggregations

UserAccount (com.owlplug.auth.model.UserAccount)2 AccountMenuItem (com.owlplug.auth.ui.AccountMenuItem)2 JFXComboBoxListViewSkin (com.jfoenix.skins.JFXComboBoxListViewSkin)1 AccountCellFactory (com.owlplug.auth.ui.AccountCellFactory)1 AccountItem (com.owlplug.auth.ui.AccountItem)1 ArrayList (java.util.ArrayList)1 Task (javafx.concurrent.Task)1 FXML (javafx.fxml.FXML)1