/*******************************************************************************
* 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.dialogs;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.utils.Activator;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.ValidatedDialog;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.editors.helpers.WGADeployment;
public class WGADeploymentDialog extends ValidatedDialog {
private Text _txtSymbolicName;
private Text _txtFileLocation;
private Button _btnBrowse;
private String _symbolicName;
private String _location;
private WGADeployment _deployment;
private Button _optionDownload;
private Button _optionLocalFile;
private Label _lblFileLocation;
private boolean _downloadRequested;
private Button _optionCustomURL;
private Composite _composite;
private Label _lblCustumURL;
private Text _txtCustomURL;
public WGADeploymentDialog(Shell parentShell) {
super(parentShell);
init(null);
}
public WGADeploymentDialog(Shell parentShell, WGADeployment deployment) {
super(parentShell);
init(deployment);
}
private void init(WGADeployment deployment){
setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.APPLICATION_MODAL);
setStatusLineAboveButtons(true);
setHelpAvailable(false);
_deployment = deployment;
}
@Override
protected Control createDialogArea(Composite parent) {
_composite = new Composite(parent, SWT.None);
GridData compositeStyle = new GridData(SWT.FILL, SWT.FILL, true, true);
_composite.setLayoutData(compositeStyle);
GridLayout layout = new GridLayout(2, false);
_composite.setLayout(layout);
Label lblSymbolicName = new Label(_composite, SWT.None);
lblSymbolicName.setText("Symbolic name:");
_txtSymbolicName = new Text(_composite, SWT.BORDER);
GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL);
txtStyle.minimumWidth = convertWidthInCharsToPixels(60);
_txtSymbolicName.setLayoutData(txtStyle);
if (_deployment != null) {
_txtSymbolicName.setText(_deployment.getName());
_txtSymbolicName.setEditable(false);
} else {
_txtSymbolicName.addModifyListener(this);
}
// Label filler = new Label(composite, SWT.None);
// filler.setText("");
Group group = new Group(_composite, SWT.BORDER);
group.setText("Specify an WGA distribution");
GridData groupData = new GridData(GridData.FILL_BOTH);
groupData.horizontalSpan = 2;
group.setLayoutData(groupData);
group.setLayout(new GridLayout(3, false));
GridData optionStyle = new GridData();
optionStyle.horizontalSpan = 3;
_optionDownload = new Button(group, SWT.RADIO);
_optionDownload.setText("Download latest WGA release from \n'" + WGADesignerPlugin.getDefault().getWGADownloadURL().toString() + "'.");
_optionDownload.setLayoutData(GridDataFactory.copyData(optionStyle));
_optionDownload.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogchanged();
}
});
_optionLocalFile = new Button(group, SWT.RADIO);
_optionLocalFile.setText("Use local WGA war file ...");
_optionLocalFile.setLayoutData(GridDataFactory.copyData(optionStyle));
_optionLocalFile.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogchanged();
}
});
_lblFileLocation = new Label(group, SWT.None);
_lblFileLocation.setText("File:");
_txtFileLocation = new Text(group, SWT.BORDER);
_txtFileLocation.setLayoutData(GridDataFactory.copyData(txtStyle));
_txtFileLocation.addModifyListener(this);
if (_deployment != null) {
_txtFileLocation.setFocus();
}
_btnBrowse = new Button(group, SWT.PUSH);
_btnBrowse.setText("...");
_btnBrowse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
handleBrowse();
}
});
_optionCustomURL = new Button(group, SWT.RADIO);
_optionCustomURL.setText("Download from ...");
_optionCustomURL.setLayoutData(GridDataFactory.copyData(optionStyle));
_optionCustomURL.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
dialogchanged();
}
});
_lblCustumURL = new Label(group, SWT.None);
_lblCustumURL.setText("URL:");
_txtCustomURL = new Text(group, SWT.BORDER);
GridData data = GridDataFactory.copyData(txtStyle);
data.horizontalSpan = 2;
_txtCustomURL.setLayoutData(data);
_txtCustomURL.addModifyListener(this);
if (_deployment != null && _deployment.getUpdateURL() != null && !_deployment.getUpdateURL().trim().equalsIgnoreCase("")) {
_optionCustomURL.setSelection(true);
} else {
_optionDownload.setSelection(true);
}
dialogchanged();
return _composite;
}
protected void dialogchanged() {
_optionCustomURL.setEnabled(false);
_lblCustumURL.setEnabled(false);
_txtCustomURL.setEnabled(false);
_lblFileLocation.setEnabled(false);
_txtFileLocation.setEnabled(false);
_btnBrowse.setEnabled(false);
if (_optionLocalFile.getSelection()) {
_lblFileLocation.setEnabled(true);
_txtFileLocation.setEnabled(true);
_btnBrowse.setEnabled(true);
} else if (_optionCustomURL.getSelection() && _deployment == null) {
_lblCustumURL.setEnabled(true);
_txtCustomURL.setEnabled(true);
}
if (_deployment != null) {
// edit mode
if (_deployment.getUpdateURL() != null && !_deployment.getUpdateURL().trim().equalsIgnoreCase("")) {
_optionCustomURL.setEnabled(true);
_lblCustumURL.setEnabled(true);
_txtCustomURL.setText(_deployment.getUpdateURL());
}
} else {
_optionCustomURL.setEnabled(true);
}
performValidation();
}
private void handleBrowse() {
FileDialog dialog = new FileDialog(getShell(), SWT.SINGLE);
dialog.setText("Select wga.war file");
dialog.setFilterExtensions(new String[] {"*.war"});
String selectedFile = dialog.open();
if (selectedFile != null) {
String[] fileNames = dialog.getFileNames();
File dir = new File(dialog.getFilterPath());
if (fileNames.length > 0) {
// TODO check if this is really a wga.war dist
_txtFileLocation.setText(new File(dir, fileNames[0]).getAbsolutePath());
}
}
}
@Override
public List<String> validate() {
List<String> messages = new ArrayList<String>();
// validate symname
String currentSymName = _txtSymbolicName.getText();
if (currentSymName.trim().equals("")) {
messages.add("Please define a symbolic name.");
}
if (!WorkbenchUtils.isValidResourceName(currentSymName)) {
messages.add("Symbolic name contains illegal characters. Only alphanumeric ascii characters and '_', '-' are allowed.");
}
if (_optionLocalFile.getSelection()) {
// validate location
String currentLocation = _txtFileLocation.getText().trim();
if (currentLocation.equals("")) {
messages.add("Please specify an location to an wga.war.");
} else if (currentLocation.endsWith(".war")){
// check if location exists
File wgaWar = new File(currentLocation);
if (!wgaWar.exists()) {
messages.add("The specified war file does not exist.");
}
if (!WGADeployment.isWGAVersionSupported(wgaWar)) {
String supportedVersionFrom = Activator.SUPPORTED_WGA_VERSION_MIN.getMajor() + "." + Activator.SUPPORTED_WGA_VERSION_MIN.getMinor() + ".x";
String supportedVersionTo = Activator.SUPPORTED_WGA_VERSION_MAX.getMajor() + "." + Activator.SUPPORTED_WGA_VERSION_MAX.getMinor() + ".x";
messages.add("Unsupported WGA version. Supported versions are '" + supportedVersionFrom + "' to '" + supportedVersionTo + "'.");
}
} else {
messages.add("Invalid wga.war file.");
}
} else if (_optionCustomURL.getSelection()) {
// validate url
String currentLocation = _txtCustomURL.getText().trim();
if (currentLocation.equals("")) {
messages.add("Please specify an URL to download an OpenWGA war archive.");
} else {
try {
new URL(currentLocation);
}
catch (MalformedURLException e) {
messages.add("Invalid url: '" + e.getMessage() + "'");
}
}
}
return messages;
}
@Override
public void computeResponse() {
_symbolicName = _txtSymbolicName.getText();
_location = null;
_downloadRequested = false;
if (_optionLocalFile.getSelection()) {
_location = _txtFileLocation.getText().trim();
}
if (_optionCustomURL.getSelection()) {
_downloadRequested = true;
_location = _txtCustomURL.getText();
} else {
_downloadRequested = _optionDownload.getSelection();
}
}
@Override
public void create() {
super.create();
getButton(OK).setText("Deploy");
}
public String getSymbolicName() {
return _symbolicName;
}
public String getLocation() {
return _location;
}
public boolean isDownloadRequested() {
return _downloadRequested;
}
}