/*******************************************************************************
* 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.io.IOException;
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.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.natures.IncompatibleWGAConfigVersion;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
public class ChooseWebApplicationPage extends ValidatedWizardPage {
private static class WebApplicationsLabelProvider extends LabelProvider implements ITableLabelProvider {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
return element.toString();
}
}
private static class WebApplicationsContentProvider implements IStructuredContentProvider {
private WGARuntime _runtime;
public Object[] getElements(Object inputElement) {
try {
return _runtime.getConnectedDesignContainers().keySet().toArray();
}
catch (IncompatibleWGAConfigVersion e) {
}
catch (IOException e) {
}
return null;
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
_runtime = (WGARuntime)newInput;
}
}
private Label _lblHeader;
private Composite _container;
private TableViewer _tblWebApplications;
protected ChooseWebApplicationPage() {
super("ChooseWebApplicationPage");
setTitle("Select a web application for export");
}
@Override
public void computeResponse() {
ExportWGAApplication wizard = ((ExportWGAApplication)getWizard());
ISelection selection = _tblWebApplications.getSelection();
if (selection instanceof IStructuredSelection) {
wizard.setWebApplication((String)((IStructuredSelection)selection).getFirstElement());
}
}
@Override
public List<IStatus> validate() {
List<IStatus> errors = new ArrayList<IStatus>();
if (_tblWebApplications.getTable().getSelectionCount() <= 0) {
errors.add(new Status(IStatus.ERROR, WGADesignerPlugin.PLUGIN_ID, "Please select a web application for the export."));
}
return errors;
}
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);
_tblWebApplications = new TableViewer(_container);
_tblWebApplications.setLabelProvider(new WebApplicationsLabelProvider());
_tblWebApplications.setContentProvider(new WebApplicationsContentProvider());
GridData gd = new GridData(GridData.FILL_BOTH);
_tblWebApplications.getTable().setLayoutData(gd);
_tblWebApplications.addSelectionChangedListener(this);
setControl(_container);
}
@Override
public void show() {
ExportWGAApplication wizard = (ExportWGAApplication) getWizard();
if (!TomcatUtils.getInstance().isRunning(wizard.getRuntime())) {
boolean start = MessageDialog.openConfirm(getShell(), "Start WGA Runtime", "To export a web application the selected runtime '" + wizard.getRuntime().getName() + "' must be started. Start runtime now?");
if (start) {
StartWGARuntime.call(wizard.getRuntime(), true);
}
}
_lblHeader.setText("Available web applications in runtime '" + wizard.getRuntime().getName() + "':");
_container.layout(new Control[] {_lblHeader});
_tblWebApplications.setInput(wizard.getRuntime());
performValidation();
}
}