/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.wizards;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubProgressMonitor;
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;
import org.eclipse.ui.IWorkingSet;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.SingleStructuredSelection;
import de.innovationgate.eclipse.wgadesigner.ResourceIDs;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.editors.helpers.WGADeployment;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
import de.innovationgate.eclipse.wgadesigner.utils.JDTUtils;
public class NewWGARuntime extends Wizard implements INewWizard {
private IStructuredSelection _selection;
private NewWGARuntimePage _page;
private WGARuntime _wgaRuntime;
@Override
public void addPages() {
_page = new NewWGARuntimePage(_selection);
addPage(_page);
}
public NewWGARuntime() {
setWindowTitle("New WGA Runtime Project");
}
@Override
public boolean performFinish() {
final String runtimeName = _page.getRuntimeName();
final WGADeployment deployment = _page.getWGADeployment();
final List<String> selectedWorkingSets = _page.getSelectedWorkingSets();
IRunnableWithProgress op = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException {
try {
doFinish(runtimeName, deployment, monitor, selectedWorkingSets);
} catch (CoreException e) {
WGADesignerPlugin.getDefault().logError(e.getMessage(), e);
throw new InvocationTargetException(e);
} finally {
monitor.done();
}
}
};
try {
getContainer().run(true, false, op);
try {
WorkbenchUtils.setNavigationViewSelection(new SingleStructuredSelection(_wgaRuntime.getConfigFile()));
} catch (Exception e) {
}
WorkbenchUtils.openEditor(WGADesignerPlugin.getDefault().getWorkbench(), _wgaRuntime.getConfigFile() , ResourceIDs.EDITOR_WGA_RUNTIME);
} catch (InterruptedException e) {
return false;
} catch (InvocationTargetException e) {
Throwable realException = e.getTargetException();
WorkbenchUtils.showErrorDialog(getShell(), "Unable to open runtime editor", realException);
return false;
} catch (PartInitException e) {
WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), getShell(), "Unable to open runtime editor", "Open runtime editor failed.", e);
return false;
}
return true;
}
protected void doFinish(String runtimeName, WGADeployment deployment, IProgressMonitor monitor, List<String> workingSetsToAddTo) throws CoreException {
monitor.beginTask("Creating WGA runtime '" + runtimeName + "'", 5);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(runtimeName);
if (project.exists()) {
throwCoreException("A project with name '" + runtimeName + "' already exists in the workspace.");
} else {
// create project
project.create(new SubProgressMonitor(monitor, 1));
project.open(new SubProgressMonitor(monitor, 1));
JDTUtils.addNature(project, WGADesignerPlugin.NATURE_WGA_RUNTIME);
_wgaRuntime = (WGARuntime) project.getNature(WGADesignerPlugin.NATURE_WGA_RUNTIME);
_wgaRuntime.getConfiguration().setWgaDistribution(deployment.getName());
_wgaRuntime.flushConfig();
monitor.worked(1);
if(workingSetsToAddTo!=null && workingSetsToAddTo.size()>0){
List<IWorkingSet> workingSets = new ArrayList<IWorkingSet>();//workingSetsToAddTo.size()
for(String currentWorkingSet : workingSetsToAddTo){
if(!currentWorkingSet.equals("")){
workingSets.add(PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(currentWorkingSet));
}
}
PlatformUI.getWorkbench().getWorkingSetManager().addToWorkingSets(_wgaRuntime.getProject(), workingSets.toArray(new IWorkingSet[0]));
}
// configure tomcat
try {
Map<String,String> variables = new HashMap<String,String>();
TomcatUtils.getInstance().initConfDir(_wgaRuntime.getCatalinaConf().getLocation().toFile(), variables);
monitor.worked(1);
} catch (IOException e) {
throwCoreException(e.getMessage(), e);
}
project.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 1));
}
}
private void throwCoreException(String message) throws CoreException {
throwCoreException(message, null);
}
private void throwCoreException(String message, Exception e) throws CoreException {
IStatus status =
new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, IStatus.OK, message, e);
throw new CoreException(status);
}
public void init(IWorkbench workbench, IStructuredSelection selection) {
_selection = selection;
}
public WGARuntime getCreatedRuntime() {
return _wgaRuntime;
}
}