/*******************************************************************************
* Copyright (c) 2012, 2014 Pivotal Software, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License,
* Version 2.0 (the "License�); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
********************************************************************************/
package org.cloudfoundry.ide.eclipse.server.ui.internal.wizards;
import java.lang.reflect.InvocationTargetException;
import org.cloudfoundry.ide.eclipse.server.core.internal.CloudFoundryPlugin;
import org.cloudfoundry.ide.eclipse.server.core.internal.CloudFoundryServer;
import org.cloudfoundry.ide.eclipse.server.core.internal.client.CloudFoundryServerBehaviour;
import org.cloudfoundry.ide.eclipse.server.ui.internal.Messages;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.statushandlers.StatusManager;
/**
* @author Steffen Pingel
*/
public class RegisterAccountWizard extends Wizard {
private final CloudFoundryServer cloudServer;
private RegisterAccountWizardPage page;
private String email;
private String password;
public RegisterAccountWizard(CloudFoundryServer cloudServer) {
this.cloudServer = cloudServer;
setWindowTitle(Messages.RegisterAccountWizard_TITLE_REGISTER_ACC);
setNeedsProgressMonitor(true);
}
@Override
public void addPages() {
page = new RegisterAccountWizardPage(cloudServer);
addPage(page);
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
@Override
public boolean performFinish() {
final String url = cloudServer.getUrl();
email = page.getEmail();
password = page.getPassword();
try {
getContainer().run(true, false, new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
CloudFoundryServerBehaviour.register(url, email, password,
cloudServer.getSelfSignedCertificate(), monitor);
}
catch (CoreException e) {
throw new InvocationTargetException(e);
}
catch (OperationCanceledException e) {
throw new InterruptedException();
}
finally {
monitor.done();
}
}
});
return true;
}
catch (InvocationTargetException e) {
if (e.getCause() instanceof CoreException) {
String message = NLS.bind(Messages.RegisterAccountWizard_ERROR_REGISTER_ACC, cloudServer.getServer().getName(),
e.getCause().getMessage());
page.setErrorMessage(message);
Status status = new Status(IStatus.ERROR, CloudFoundryPlugin.PLUGIN_ID, message, e);
StatusManager.getManager().handle(status, StatusManager.LOG);
}
else {
Status status = new Status(IStatus.ERROR, CloudFoundryPlugin.PLUGIN_ID, NLS.bind(
Messages.RegisterAccountWizard_ERROR_UNEXPECTED, e.getMessage()), e);
StatusManager.getManager().handle(status, StatusManager.SHOW | StatusManager.BLOCK | StatusManager.LOG);
}
}
catch (InterruptedException e) {
// ignore
}
return false;
}
}