blob: fa2203f64687b47c0cce263e3bc0d941514bb4ad [file] [log] [blame]
/*
* Copyright (c) Robert Bosch GmbH. All rights reserved.
*/
package org.eclipse.blockchain.ui.views;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.blockchain.core.BlockchainCore;
import org.eclipse.blockchain.core.CoreCommandExecutor;
import org.eclipse.blockchain.ui.Activator;
import org.eclipse.blockchain.ui.util.UIUtilities;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
* @author ADG5COB
*/
public class GethServerDialog extends Dialog {
private Text dataDirText;
private Text initFileText;
private final String userDir = System.getProperty("user.dir");
private String networkId = "";
private Label networkIdLabel;
private final Map<String, String> gethOptions = new HashMap<>();
private String dataDirectory = "";
private String genesisFile = "";
/**
* @return -
*/
public String getDataDirectory() {
return this.dataDirectory;
}
/**
* @return -
*/
public String getGenesisFile() {
return this.genesisFile;
}
/**
* @return -
*/
public Map<String, String> getGethOptions() {
return this.gethOptions;
}
/**
* @param parentShell -
*/
public GethServerDialog(final Shell parentShell) {
super(parentShell);
populateGethOptions();
}
private void populateGethOptions() {
this.gethOptions.put("rpc", "");
this.gethOptions.put("rpccorsdomain", "*");
this.gethOptions.put("rpcapi", "db,eth,net,web3,personal");
this.gethOptions.put("gcmode", "archive");
}
/**
* {@inheritDoc}
*/
@Override
protected void configureShell(final Shell shell) {
super.configureShell(shell);
shell.setText("Geth Server Setup");
shell.setSize(300, 400);
}
/**
* {@inheritDoc}
*/
@Override
protected Control createDialogArea(final Composite parent) {
Composite containerComp = new Composite(parent, SWT.None);
containerComp.setLayout(new GridLayout(3, false));
containerComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
createContent(containerComp);
UIUtilities.moveShellToCenterOfScreen(Display.getDefault(), parent.getShell());
return parent;
}
private void createContent(final Composite containerComp) {
// First Layer
Label dataDirLabel = new Label(containerComp, SWT.None);
dataDirLabel.setText("Data Directory");
dataDirLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
this.dataDirText = new Text(containerComp, SWT.BORDER);
this.dataDirText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
this.dataDirText.setText("C:\\Deepthi\\BlockChain\\Account\\Node4");
this.dataDirText.setToolTipText(this.dataDirText.getText());
Button dataDirBrowse = new Button(containerComp, SWT.PUSH);
dataDirBrowse.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
dataDirBrowse.setText("Browse");
dataDirBrowse.addSelectionListener(new SelectionAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void widgetSelected(final SelectionEvent e) {
getAndSetSelectedDirectoryNode(containerComp, GethServerDialog.this.dataDirText);
}
});
// Second Layer
Label initFileLabel = new Label(containerComp, SWT.None);
initFileLabel.setText("Init File");
initFileLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
this.initFileText = new Text(containerComp, SWT.BORDER);
this.initFileText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
this.initFileText.setText("");
this.initFileText.setToolTipText(this.initFileText.getText());
Button initFileBrowse = new Button(containerComp, SWT.PUSH);
initFileBrowse.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
initFileBrowse.setText("Browse");
initFileBrowse.addSelectionListener(new SelectionAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void widgetSelected(final SelectionEvent e) {
getandSetSelectedGenesisFile(containerComp, GethServerDialog.this.initFileText);
}
});
// Scrolled Composite - contains network values
ScrolledComposite networkComposite = new ScrolledComposite(containerComp, SWT.V_SCROLL | SWT.BORDER);
networkComposite.setLayout(new GridLayout(1, false));
networkComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 3, 0));
Composite networkChild = createdScrolledContent(networkComposite);
networkComposite.setContent(networkChild);
networkComposite.setMinSize(networkComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
networkComposite.setExpandHorizontal(true);
networkComposite.setExpandVertical(true);
networkComposite.layout();
}
private Composite createdScrolledContent(final ScrolledComposite scrolledComposite) {
/**
* This contains the geth arguments which should be made configurable, as of now its static but networkId will be
* read from the genesis file
*/
Composite childComp = new Composite(scrolledComposite, SWT.None);
childComp.setLayout(new GridLayout(1, false));
childComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Group networkGroup = new Group(childComp, SWT.BORDER);
networkGroup.setLayout(new GridLayout(1, false));
networkGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
networkGroup.setText("Network");
this.networkIdLabel = new Label(networkGroup, SWT.None | SWT.READ_ONLY);
this.networkIdLabel.setText("Network ID : " +
(this.networkId.isEmpty() ? CoreCommandExecutor.getInstance().getDefaultNetworkIdForGeth() : this.networkId));
Group rpcGroup = new Group(childComp, SWT.BORDER);
rpcGroup.setLayout(new GridLayout(1, false));
rpcGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
rpcGroup.setText("RPC");
for (Map.Entry<String, String> entry : this.gethOptions.entrySet()) {
Label lab = new Label(rpcGroup, SWT.None);
String val = entry.getValue();
lab.setText(entry.getKey() + (val.isEmpty() ? "" : " : " + val));
}
return childComp;
}
private void getandSetSelectedGenesisFile(final Composite parent, final Text textWidget) {
FileDialog dataDir = new FileDialog(parent.getShell());
dataDir.setFilterPath(this.userDir);
String selectedDir = dataDir.open();
textWidget.setText(selectedDir);
try (BufferedReader br = new BufferedReader(new FileReader(new File(selectedDir)))) {
String contents = "";
while ((contents = br.readLine()) != null) {
if (contents.contains("chainId")) {
this.networkId = contents.substring(contents.indexOf(':') + 1).replace(",", "").trim();
this.networkIdLabel.setText("Network ID : " + this.networkId);
this.networkIdLabel.getParent().layout();
}
}
}
catch (IOException e) {
BlockchainCore.getInstance().logException(Activator.PLUGIN_ID, e.getMessage(), e);
}
}
private void getAndSetSelectedDirectoryNode(final Composite parent, final Text textWidget) {
DirectoryDialog dirDialog = new DirectoryDialog(parent.getShell());
dirDialog.setFilterPath(this.userDir);
String selectedDir = dirDialog.open();
textWidget.setText(selectedDir);
}
/**
* {@inheritDoc}
*/
@Override
protected void okPressed() {
this.dataDirectory = this.dataDirText.getText();
this.genesisFile = this.initFileText.getText();
super.okPressed();
}
}