/* | |
* Copyright (c) Robert Bosch GmbH. All rights reserved. | |
*/ | |
package org.eclipse.blockchain.server.ui.wizard.fragment; | |
import java.util.Set; | |
import org.eclipse.blockchain.core.BlockchainCore; | |
import org.eclipse.blockchain.core.CoreCommandExecutor; | |
import org.eclipse.blockchain.core.Web3jHandler; | |
import org.eclipse.blockchain.ui.views.AccountsView; | |
import org.eclipse.blockchain.ui.views.EtherAccountViewPart; | |
import org.eclipse.core.runtime.CoreException; | |
import org.eclipse.core.runtime.IProgressMonitor; | |
import org.eclipse.jface.dialogs.MessageDialog; | |
import org.eclipse.swt.widgets.Composite; | |
import org.eclipse.swt.widgets.Display; | |
import org.eclipse.ui.IWorkbench; | |
import org.eclipse.ui.IWorkbenchListener; | |
import org.eclipse.ui.PlatformUI; | |
import org.eclipse.wst.server.ui.wizard.IWizardHandle; | |
import org.eclipse.wst.server.ui.wizard.WizardFragment; | |
/** | |
* This is just a proto-type class and not constructed fully. | |
* | |
* @author ADG5COB | |
*/ | |
public class GethWizardFragment extends WizardFragment { | |
private GethWizardComposite gethComposite; | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public boolean hasComposite() { | |
return true; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public Composite createComposite(final Composite parent, final IWizardHandle handle) { | |
this.gethComposite = new GethWizardComposite(parent, handle); | |
return this.gethComposite; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public boolean isComplete() { | |
if (this.gethComposite != null) { | |
return this.gethComposite.isWizardComplete(); | |
} | |
return false; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
@Override | |
public void performFinish(final IProgressMonitor monitor) throws CoreException { | |
this.gethComposite.prePerformFinish(); | |
String anyErrors = startServer(); | |
if (anyErrors.isEmpty()) { | |
Display.getDefault().syncExec(() -> { | |
EtherAccountViewPart.updateView(); | |
}); | |
// Below activation should happen from UI-Thread | |
// PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() | |
// .activate(PlatformUI.getWorkbench().getViewRegistry().find("").createView().getSite().getPart()); | |
super.performFinish(monitor); | |
} | |
else { | |
getTaskModel().putObject("server", null); | |
super.performCancel(monitor); | |
} | |
} | |
/** | |
* This will restrict other default child fragments from getting displayed. {@inheritDoc} | |
*/ | |
@Override | |
public boolean isForceLastFragment() { | |
return true; | |
} | |
private String startServer() { | |
try { | |
String anyErrors = CoreCommandExecutor.getInstance().startGethServer(this.gethComposite.getDataDirectory(), | |
this.gethComposite.getGenesisFile(), this.gethComposite.getGethOptions()); | |
Web3jHandler web3j = Web3jHandler.getInstance(); | |
if (!anyErrors.isEmpty()) { | |
showError(anyErrors); | |
return anyErrors; | |
} | |
anyErrors = web3j.createInitialAccounts(); | |
if (!anyErrors.isEmpty()) { | |
showError(anyErrors); | |
return anyErrors; | |
} | |
Set<String> accounts = web3j.getAccount().getAccounts().keySet(); | |
AccountsView.setAccountsCombo(accounts.toArray(new String[accounts.size()])); | |
PlatformUI.getWorkbench().getDisplay(); | |
Display.getDefault().syncExec(() -> { | |
EtherAccountViewPart.updateView(); | |
}); | |
} | |
catch (Exception e) { | |
BlockchainCore.getInstance().logException("org.eclipse.blockchain.server.ui", e.getMessage(), e); | |
} | |
PlatformUI.getWorkbench().addWorkbenchListener(new IWorkbenchListener() { | |
@Override | |
public boolean preShutdown(final IWorkbench workbench, final boolean forced) { | |
CoreCommandExecutor.getInstance().terminateGethServer(); | |
return true; | |
} | |
@Override | |
public void postShutdown(final IWorkbench workbench) { | |
/** | |
* Not required | |
*/ | |
} | |
}); | |
return ""; | |
} | |
private void showError(final String anyErrors) { | |
Display.getDefault().syncExec(() -> { | |
MessageDialog errorDialog = new MessageDialog(Display.getDefault().getActiveShell(), "Geth Server Start Error", | |
null, anyErrors, MessageDialog.ERROR, new String[] { "OK" }, 0); | |
errorDialog.open(); | |
}); | |
} | |
} |