package net.helipilot50.aerospike.wizards;
import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import net.helipilot50.aerospike.Activator;
import net.helipilot50.aerospike.model.ClusterCoordinates;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
public class NewDBCoordinatesWizard extends Wizard implements INewWizard{
private ClusterCoordinates dbCoordinates;
private DBCoordinatesWizardPage page;
public NewDBCoordinatesWizard() {
super();
setNeedsProgressMonitor(true);
this.dbCoordinates = new ClusterCoordinates("", "", 3000);
}
public NewDBCoordinatesWizard(ClusterCoordinates obj) {
this();
this.dbCoordinates = obj;
}
@Override
public void init(IWorkbench workbench, IStructuredSelection selection) {
}
@Override
public boolean performFinish() {
final ClusterCoordinates db = dbCoordinates;
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException {
monitor.beginTask("Saving Database Coordinates for " + db.getName(), 6);
try {
doFinish(db, monitor);
} catch (CoreException e) {
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
getContainer().run(true, false, op);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
MessageDialog.openError(getShell(), "Error", realException.getMessage());
return false;
}
return true;
}
/**
* The worker method. .
*/
private void doFinish(ClusterCoordinates db, IProgressMonitor monitor)
throws CoreException {
IProject dbProject = Activator.getDefault().getDBProject();
IFolder dbFolder = dbProject.getFolder(db.getName());
monitor.worked(2);
if (!dbFolder.exists()){
dbFolder.create(true, true, monitor);
}
IFile coordinatesFile = dbFolder.getFile(ClusterCoordinates.FILE_NAME);
ByteArrayInputStream in = new ByteArrayInputStream(db.toString().getBytes());
monitor.worked(2);
if (!coordinatesFile.exists()){
coordinatesFile.create(in, true, monitor);
} else {
coordinatesFile.setContents(in, true, true, monitor);
}
monitor.worked(2);
}
/**
* Adding the page to the wizard.
*/
public void addPages() {
page = new DBCoordinatesWizardPage(this.dbCoordinates);
addPage(page);
}
}