/*******************************************************************************
* 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.export.webapp;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import de.innovationgate.eclipse.utils.ui.ValidatedWizardPage;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.actions.StartWGARuntime;
import de.innovationgate.eclipse.wgadesigner.models.WGADistribution;
import de.innovationgate.eclipse.wgadesigner.models.WGARuntimeConfigurationModel;
import de.innovationgate.eclipse.wgadesigner.natures.IncompatibleWGAConfigVersion;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
public class ChooseRuntimePage extends ValidatedWizardPage {
private static class RuntimeLabelProvider extends LabelProvider implements ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
WGARuntime runtime = (WGARuntime) element;
return runtime.getName();
}
}
private static class RuntimeContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
return WGADesignerPlugin.getAllRuntimes().values().toArray(new WGARuntime[0]);
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
private Label _lblHeader;
private Composite _container;
private TableViewer _tblRuntimes;
protected ChooseRuntimePage() {
super("ChooseRuntimePage");
setTitle("Select a web application for export");
}
@Override
public void computeResponse() {
ExportWGAApplication wizard = ((ExportWGAApplication)getWizard());
ISelection selection = _tblRuntimes.getSelection();
if (selection instanceof IStructuredSelection) {
wizard.setRuntime((WGARuntime)((IStructuredSelection)selection).getFirstElement());
}
}
@Override
public List<IStatus> validate() {
List<IStatus> errors = new ArrayList<IStatus>();
if (_tblRuntimes.getTable().getSelectionCount() <= 0) {
errors.add(new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, "Please select a runtime."));
} else {
ISelection selection = _tblRuntimes.getSelection();
if (selection instanceof IStructuredSelection) {
WGARuntime runtime = (WGARuntime)((IStructuredSelection)selection).getFirstElement();
validateWGARuntimeVersion(errors, runtime);
}
}
return errors;
}
static void validateWGARuntimeVersion(List<IStatus> errors, WGARuntime runtime) {
if (runtime != null) {
WGARuntimeConfigurationModel model = new WGARuntimeConfigurationModel();
try {
model.init(runtime);
if (model.getWgaDeployment() != null) {
if (!model.getWgaDeployment().getWGAVersion().isAtLeast(5, 3)) {
errors.add(new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, "The runtime must use at least OpenWGA 5.3 to export web applications. Please choose another runtime or update your OpenWGA version."));
}
}
}
catch (IncompatibleWGAConfigVersion e) {
errors.add(new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, "Incompatible WGA config version."));
}
}
}
public void createControl(Composite parent) {
_container = new Composite(parent, SWT.NULL);
GridLayout layout = new GridLayout();
layout.numColumns = 1;
_container.setLayout(layout);
_lblHeader = new Label(_container, SWT.NONE);
_tblRuntimes = new TableViewer(_container);
_tblRuntimes.setLabelProvider(new RuntimeLabelProvider());
_tblRuntimes.setContentProvider(new RuntimeContentProvider());
GridData gd = new GridData(GridData.FILL_BOTH);
_tblRuntimes.getTable().setLayoutData(gd);
_tblRuntimes.addSelectionChangedListener(this);
setControl(_container);
}
@Override
public void show() {
_lblHeader.setText("Available runtimes in workspace:");
_container.layout(new Control[] {_lblHeader});
_tblRuntimes.setInput("unsused");
performValidation();
}
}