blob: c6afc2189f3e2e6f8a45df8f068753422780792c [file] [log] [blame]
/*
* Copyright (c) Robert Bosch GmbH. All rights reserved.
*/
package org.eclipse.blockchain.ui.wizard;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.blockchain.core.SolidityPreferenceConstants;
import org.eclipse.blockchain.core.log.EthereumLogService;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
/**
* @author ADG5COB
*/
public class EthereumProjectWizardPage extends WizardPage {
private Label projectNameLabel = null;
private Text projectNameText = null;
private String projectNameString;
private final String projectNameError = "Project Name cannot be empty!!!";
private final String existingProjectpathError = "Project paath cannot be empty!!!";
private String projectTemplateToBeCreated = null;
private boolean testNetTobeCreated = false;
private Text solcPathText;
private String solcPathString = "";
private Text existingProjectPathText;
private Text newProjectPathText;
private Button newPrjButton;
private Button existingPrjButton;
Color greyColor = new Color(Display.getDefault(), new RGB(128, 128, 128));
/**
* @param pageName page name
*/
protected EthereumProjectWizardPage(final String pageName) {
super(pageName);
setPageComplete(false);
setTitle("New Ethereum Project");
}
/**
* {@inheritDoc}
*/
@Override
public void createControl(final Composite parent) {
Composite wizardContainer = new Composite(parent, SWT.None);
GridLayout containerGrid = new GridLayout(2, false);
GridData genericGridData = new GridData(GridData.FILL_BOTH);
genericGridData.minimumHeight = 50;
wizardContainer.setLayout(containerGrid);
wizardContainer.setLayoutData(genericGridData);
// Select if it has to be imported as new or existing project
Group newOrExistingGrp = new Group(wizardContainer, NONE);
newOrExistingGrp.setLayout(new GridLayout(3, true));
newOrExistingGrp.setLayoutData(new GridData(GridData.FILL_BOTH));
GridData gridData = new GridData(SWT.FILL, SWT.LEFT, true, false, 1, 1);
Label importAs = new Label(newOrExistingGrp, SWT.NONE);
importAs.setText("Import project as: ");
importAs.setLayoutData(gridData);
this.newPrjButton = new Button(newOrExistingGrp, SWT.RADIO);
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
this.newPrjButton.setLayoutData(gridData);
this.newPrjButton.setText("New project");
this.existingPrjButton = new Button(newOrExistingGrp, SWT.RADIO);
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
this.existingPrjButton.setText("Existing project");
this.existingPrjButton.setLayoutData(gridData);
// create a group to for new project creation related widgets
Group newPrjGrp = new Group(wizardContainer, NONE);
newPrjGrp.setLayout(new GridLayout(3, false));
newPrjGrp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
newPrjGrp.setText("New project");
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
this.projectNameLabel = new Label(newPrjGrp, SWT.None);
this.projectNameLabel.setText("Project Name : ");
this.projectNameLabel.setLayoutData(gridData);
this.projectNameText = new Text(newPrjGrp, SWT.BORDER);
gridData = new GridData(SWT.FILL, SWT.LEFT, true, true, 2, 1);
this.projectNameText.setLayoutData(gridData);
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
Label projectPath = new Label(newPrjGrp, SWT.None);
projectPath.setText("Project path : ");
projectPath.setLayoutData(gridData);
this.newProjectPathText = new Text(newPrjGrp, SWT.BORDER);
gridData = new GridData(SWT.FILL, SWT.LEFT, true, false, 1, 1);
this.newProjectPathText.setLayoutData(gridData);
String workspaceLocation = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
this.newProjectPathText.setText(workspaceLocation);
Button newPrjPathBrowseButton = new Button(newPrjGrp, SWT.PUSH);
newPrjPathBrowseButton.setText("Browse");
gridData = new GridData(SWT.FILL, SWT.LEFT, false, false, 1, 1);
newPrjPathBrowseButton.setLayoutData(gridData);
newPrjPathBrowseButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
DirectoryDialog prjPathDialog = new DirectoryDialog(parent.getShell());
prjPathDialog.setFilterPath(workspaceLocation);
String selectedDir = prjPathDialog.open();
if (selectedDir != null) {
EthereumProjectWizardPage.this.newProjectPathText.setText(selectedDir);
}
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
// do nothing
}
});
// project template section
Label projectTemplateGroup = new Label(newPrjGrp, SWT.SHADOW_ETCHED_IN);
projectTemplateGroup.setText("Project Template : ");
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
projectTemplateGroup.setLayoutData(gridData);
// combo for project selection
Combo projectTemplateSelection = new Combo(newPrjGrp, NONE);
// Only simple project is supported for now - , "Web project", "Angular JS Project", "React Js Project"
projectTemplateSelection.setItems("Simple project");
projectTemplateSelection.setLayoutData(gridData);
projectTemplateSelection.select(0);
setProjectTemplateToBeImported(projectTemplateSelection.getText());
projectTemplateSelection.addSelectionListener(new SelectionAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void widgetSelected(final SelectionEvent e) {
setProjectTemplateToBeImported(projectTemplateSelection.getText());
}
});
// existing project section
Group existingPrjGrp = new Group(wizardContainer, NONE);
existingPrjGrp.setLayout(new GridLayout(3, false));
existingPrjGrp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
existingPrjGrp.setText("Existing project");
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
Label newProjectPath = new Label(existingPrjGrp, SWT.None);
newProjectPath.setText("Project path : ");
newProjectPath.setLayoutData(gridData);
this.existingProjectPathText = new Text(existingPrjGrp, SWT.BORDER);
gridData = new GridData(SWT.FILL, SWT.LEFT, true, false, 1, 1);
this.existingProjectPathText.setLayoutData(gridData);
this.existingProjectPathText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(final ModifyEvent e) {
if (getTextBoxText(EthereumProjectWizardPage.this.existingProjectPathText).trim().isEmpty()) {
setPageComplete(false);
setErrorMessage(EthereumProjectWizardPage.this.existingProjectpathError);
}
else {
setPageComplete(true);
setErrorMessage(null);
}
}
});
Button existingPrjPathBrowseButton = new Button(existingPrjGrp, SWT.PUSH);
existingPrjPathBrowseButton.setText("Browse");
gridData = new GridData(SWT.FILL, SWT.LEFT, false, false, 1, 1);
existingPrjPathBrowseButton.setLayoutData(gridData);
existingPrjPathBrowseButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
DirectoryDialog prjPathDialog = new DirectoryDialog(parent.getShell());
prjPathDialog.setFilterPath(System.getProperty("user.dir"));
String selectedDir = prjPathDialog.open();
if ((selectedDir != null) && !selectedDir.equals("")) {
setPageComplete(true);
setErrorMessage(null);
EthereumProjectWizardPage.this.existingProjectPathText.setText(selectedDir);
}
else {
setErrorMessage(EthereumProjectWizardPage.this.existingProjectpathError);
setPageComplete(false);
}
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
// do nothing
}
});
// Enable the new/existing fiel import depending on the radio button selection
this.newPrjButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
// Enable the new project creation group
newPrjGrp.setEnabled(true);
newPrjGrp.setForeground(null);
setForeGroundToChildren(newPrjGrp.getChildren(), null);
// Disable existing project creation
existingPrjGrp.setEnabled(false);
existingPrjGrp.setForeground(EthereumProjectWizardPage.this.greyColor);
setForeGroundToChildren(existingPrjGrp.getChildren(), EthereumProjectWizardPage.this.greyColor);
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
widgetSelected(e);
}
});
this.existingPrjButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
newPrjGrp.setEnabled(false);
newPrjGrp.setForeground(EthereumProjectWizardPage.this.greyColor);
setForeGroundToChildren(newPrjGrp.getChildren(), EthereumProjectWizardPage.this.greyColor);
existingPrjGrp.setEnabled(true);
existingPrjGrp.setForeground(null);
setForeGroundToChildren(existingPrjGrp.getChildren(), null);
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
widgetSelected(e);
}
});
// compiler section
Group compilerGrp = new Group(wizardContainer, NONE);
compilerGrp.setLayout(new GridLayout(3, false));
compilerGrp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
compilerGrp.setText("Compiler settings");
Label solcLabel = new Label(compilerGrp, SWT.NONE);
solcLabel.setText("Solidity compiler path : ");
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
solcLabel.setLayoutData(gridData);
this.solcPathText = new Text(compilerGrp, SWT.BORDER);
this.solcPathText.setText(getSolcPath());
gridData = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
this.solcPathText.setLayoutData(gridData);
Button compilerPathBrowseButton = new Button(compilerGrp, SWT.PUSH);
compilerPathBrowseButton.setText("Browse");
gridData = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
compilerPathBrowseButton.setLayoutData(gridData);
compilerPathBrowseButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent e) {
DirectoryDialog solcCompilerPathDialog = new DirectoryDialog(parent.getShell());
solcCompilerPathDialog.setFilterPath(System.getProperty("user.dir"));
String selectedDir = solcCompilerPathDialog.open();
if (selectedDir != null) {
EthereumProjectWizardPage.this.solcPathText.setText(selectedDir);
}
}
@Override
public void widgetDefaultSelected(final SelectionEvent e) {
// do nothing
}
});
Link solidityGitLink = new Link(compilerGrp, SWT.UNDERLINE_LINK);
solidityGitLink.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, false, 3, 1));
solidityGitLink.setText("<a>Solidity Compiler Download</a>");
solidityGitLink.setToolTipText(
"Download solidity compiler from this link and provide the file system path in above preference dialog");
solidityGitLink.addSelectionListener(new SelectionAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void widgetSelected(final SelectionEvent e) {
try {
PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser()
.openURL(new URL(SolidityPreferenceConstants.SOLIDITY_GIT_URL));
}
catch (PartInitException | MalformedURLException e1) {
EthereumLogService.INSTANCE.log(e1.getMessage());
e1.printStackTrace();
}
}
});
setControl(wizardContainer);
setErrorMessage(this.projectNameError);
addButtonListeners();
}
private void setForeGroundToChildren(final Control[] children, final Color color) {
for (Control child : children) {
if ((child instanceof Text)) {
((Text) child).setEditable(color == null);
}
child.setForeground(color);
}
}
private String getSolcPath() {
return InstanceScope.INSTANCE.getNode(SolidityPreferenceConstants.PREF_NODE)
.get(SolidityPreferenceConstants.PREF_KEY, "");
}
private void addButtonListeners() {
this.projectNameText.addModifyListener((final ModifyEvent e) -> {
if (!getTextBoxText(this.projectNameText).trim().equals("")) {
setPageComplete(true);
setErrorMessage(null);
}
else {
setPageComplete(false);
setErrorMessage(this.projectNameError);
}
});
}
private String getTextBoxText(final Text textBox) {
Display.getDefault().syncExec(() -> this.projectNameString = textBox.getText());
return this.projectNameString;
}
/**
* @return -
*/
public String getSolcPathFromTextBox() {
Display.getDefault().syncExec(() -> this.solcPathString = this.solcPathText.getText());
return this.solcPathString;
}
/**
* @return
*/
public String getNewProjectPath() {
return this.newProjectPathText.getText();
}
/**
* @return
*/
public String getExistingProjectPath() {
return this.existingProjectPathText.getText();
}
/**
* @return
*/
public boolean isNewProjectToBeCreated() {
return this.newPrjButton.getSelection();
}
/**
* @return - The project name entered in project creation wizard
*/
public String getProjectNameString() {
return this.projectNameString;
}
/**
* @return the selectedProject
*/
public String getProjectTemplaeToBeCreated() {
return this.projectTemplateToBeCreated;
}
/**
* @param selectedProject the selectedProject to set
*/
public void setProjectTemplateToBeImported(final String selectedProject) {
this.projectTemplateToBeCreated = selectedProject;
}
/**
* @return the testNetTobeCreated
*/
public boolean isTestNetTobeCreated() {
return this.testNetTobeCreated;
}
/**
* @param testNetTobeCreated the testNetTobeCreated to set
*/
public void setTestNetTobeCreated(final boolean testNetTobeCreated) {
this.testNetTobeCreated = testNetTobeCreated;
}
}