blob: 5695843b2cd929740070288a43c6f78c516e570f [file] [log] [blame]
package org.eclipse.blockchain.ui.views;
import java.util.Map;
import java.util.Set;
import org.eclipse.blockchain.core.BlockchainCore;
import org.eclipse.blockchain.core.SecoBlocksPreferenceConstants;
import org.eclipse.blockchain.core.Web3jHandler;
import org.eclipse.blockchain.core.events.BlockchainEnvironmentChangedTrigger;
import org.eclipse.blockchain.core.events.IBlockchainEnvironmentChangedEvent;
import org.eclipse.blockchain.core.events.IBlockchainEnvironmentChangedListener;
import org.eclipse.blockchain.ui.Activator;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.fx.ui.workbench3.FXViewPart;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.GridPane;
/**
* @author DMU1COB
*/
public class EtherAccountViewPart extends FXViewPart implements IBlockchainEnvironmentChangedListener {
static ComboBox<String> cmbAccount = new ComboBox<>();
static TextField txtBalance = new TextField();
static String selectedEnvironment = "";
private static String selectedItem;
/**
* {@inheritDoc}
*/
@Override
public void init(final IViewSite site) throws PartInitException {
super.init(site);
BlockchainEnvironmentChangedTrigger.getInstance().addBlockchainEnvironmentChangedListener(this);
}
@Override
protected Scene createFxScene() {
Scene scene = null;
try {
clearAccountsSection();
GridPane gridPane = new GridPane();
gridPane.setPadding(new Insets(20));
gridPane.setHgap(10);
gridPane.setVgap(10);
Label lblAccount = new Label();
lblAccount.setText("Accounts");
EtherAccountViewPart.cmbAccount.setPrefWidth(200);
EtherAccountViewPart.cmbAccount.setItems(getAccountList());
EtherAccountViewPart.cmbAccount.getSelectionModel().selectFirst();
Label lblEtherBalance = new Label();
lblEtherBalance.setText("Ether Balance");
Map<String, String> accountBalance =
Web3jHandler.getInstance().getAccount().getAccountBalance(getSelectedEnvironment());
// Add a ChangeListener to the ComboBox
EtherAccountViewPart.cmbAccount.getSelectionModel().selectedItemProperty()
.addListener(new ChangeListener<String>() {
@Override
public void changed(final ObservableValue<? extends String> ov, final String oldValue,
final String newValue) {
if (newValue != null) {
EtherAccountViewPart.txtBalance.setText(
Web3jHandler.getInstance().getAccount().getAccountBalance(getSelectedEnvironment()).get(newValue));
selectedItem = newValue;
/*
* if (EtherAccountViewPart.getSelectedEnvironment()
* .equals(SecoBlocksPreferenceConstants.EnvironmentType.EMBEDDED_EVM.toString())) {
* InstanceScope.INSTANCE.getNode(SecoBlocksPreferenceConstants.SECOBLOCKS_PREF_NODE)
* .put(SecoBlocksPreferenceConstants.EMBEDDED_EVM_SELECTED_ADDRESS, newValue); }
*/
}
}
});
Button btnAddAccount = new Button();
btnAddAccount.setText("+");
Label lblGasLimit = new Label();
lblGasLimit.setText("Gas Limit");
Label lblGasValue = new Label();
lblGasValue.setText("Gas Value");
TextField txtGasLimit = new TextField();
txtGasLimit.setEditable(false);
txtGasLimit.setTooltip(new Tooltip("Under dev"));
txtGasLimit.setDisable(true);
TextField txtGasValue = new TextField();
txtGasValue.setEditable(false);
txtGasValue.setTooltip(new Tooltip("Under dev"));
txtGasValue.setDisable(true);
if (Web3jHandler.getInstance().getAdminInstance() != null) {
/**
* This has to be properly constructed
*/
// txtGasValue.setText(Web3jHandler.getInstance().getGasPrice().toString());
}
setEthBalanace(accountBalance);
gridPane.add(lblAccount, 0, 0);
gridPane.add(EtherAccountViewPart.cmbAccount, 1, 0);
gridPane.add(btnAddAccount, 2, 0);
gridPane.add(lblGasLimit, 0, 1);
gridPane.add(lblGasValue, 0, 2);
gridPane.add(txtGasLimit, 1, 1);
gridPane.add(txtGasValue, 1, 2);
gridPane.add(lblEtherBalance, 0, 3);
gridPane.add(EtherAccountViewPart.txtBalance, 1, 3);
txtBalance.setEditable(false);
scene = new Scene(gridPane, 400, 400);
scene.getStylesheets().add(getClass().getResource("Views.css").toExternalForm());
}
catch (Exception e) {
BlockchainCore.getInstance().logException(Activator.PLUGIN_ID, e.getMessage(), e);
}
return scene;
}
/**
* @param accountBalance
*/
private static void setEthBalanace(final Map<String, String> accountBalance) {
if (!EtherAccountViewPart.cmbAccount.getItems().isEmpty()) {
EtherAccountViewPart.selectedItem = EtherAccountViewPart.cmbAccount.getSelectionModel().getSelectedItem();
txtBalance.setText(accountBalance.get(EtherAccountViewPart.selectedItem));
}
else {// This means no accounts
txtBalance.setText("");
}
}
/**
* @return
*/
private static ObservableList<String> getAccountList() {
ObservableList<String> accountList = FXCollections.observableArrayList();
Map<String, String> acctInfo = Web3jHandler.getInstance().getAccount().getAccounts(getSelectedEnvironment());
Set<String> accounts = acctInfo.keySet();
accountList.addAll(accounts);
if (!accountList.isEmpty()) {
if (getSelectedEnvironment().equals(SecoBlocksPreferenceConstants.EnvironmentType.GETH_CLIENT.toString())) {
accountList.remove(0);// skipping the coin-base account from displaying
}
}
return accountList;
}
@Override
protected void setFxFocus() {
}
/**
* @return the selectedItem
*/
public static String getSelectedItem() {
return selectedItem;
}
/**
*
*/
public static void updateView() {
EtherAccountViewPart.cmbAccount.setItems(getAccountList());
if (selectedItem == null) {
EtherAccountViewPart.cmbAccount.getSelectionModel().selectFirst();
}
else {
EtherAccountViewPart.cmbAccount.getSelectionModel().select(selectedItem);
}
setEthBalanace(Web3jHandler.getInstance().getAccount().getAccountBalance(getSelectedEnvironment()));
}
private static String getSelectedEnvironment() {
if (!selectedEnvironment.isEmpty()) {
return selectedEnvironment;
}
return selectedEnvironment = InstanceScope.INSTANCE.getNode(SecoBlocksPreferenceConstants.SECOBLOCKS_PREF_NODE).get(
SecoBlocksPreferenceConstants.ENVIRONMENT_PREF_KEY,
SecoBlocksPreferenceConstants.EnvironmentType.EMBEDDED_EVM.toString());
}
private void clearAccountsSection() {
if ((cmbAccount != null) && (txtBalance != null)) {
cmbAccount.setItems(FXCollections.observableArrayList());
txtBalance.setText("");
}
}
/**
* {@inheritDoc}
*/
@Override
public void blockchainEnvironmentChanged(final IBlockchainEnvironmentChangedEvent event) {
// Change accounts section when evnvironment is changed
selectedEnvironment = event.getActiveEvironment();
selectedItem = null;// On environment change reset previous environment selected account
updateView();
}
}