/*******************************************************************************
* 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.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.ui.ValidatedDialog;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.models.WGARemoteServer;
import de.innovationgate.eclipse.wgadesigner.models.WGARemoteServersModel;
public class AddWGARemoteServerDialog extends ValidatedDialog {
private WGARemoteServer _server = null;
private WGARemoteServersModel _servermodel;
protected Text _srvName;
protected Text _srvURL;
protected Text _srvUser;
protected Text _srvPW;
protected URL _serverurl;
private Button _btnTestConnection;
public AddWGARemoteServerDialog(Shell parent, WGARemoteServersModel servermodel, WGARemoteServer server) {
this(parent, servermodel);
_server = server;
init();
}
public AddWGARemoteServerDialog(Shell parent, WGARemoteServersModel servermodel) {
super(parent);
_servermodel = servermodel;
setTitle("Add WGA Remoteserver");
init();
}
public void init() {
setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.MAX | SWT.APPLICATION_MODAL);
setStatusLineAboveButtons(true);
setHelpAvailable(false);
}
protected Control createDialogArea(Composite parent) {
Composite composite = new Composite(parent, SWT.None);
composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
GridLayout layout = new GridLayout(2, false);
composite.setLayout(layout);
GridData txtStyle = new GridData(GridData.FILL_HORIZONTAL);
txtStyle.minimumWidth = convertWidthInCharsToPixels(60);
Label lblTemplateName = new Label(composite, SWT.None);
lblTemplateName.setText("Server name:");
_srvName = new Text(composite, SWT.BORDER);
_srvName.setLayoutData(txtStyle);
_srvName.addModifyListener(this);
//Label filler = new Label(composite, SWT.None);
//filler.setText("");
Label lblUrl = new Label(composite, SWT.None);
lblUrl.setText("URL:");
_srvURL = new Text(composite, SWT.BORDER);
_srvURL.setLayoutData(txtStyle);
_srvURL.addModifyListener(this);
//filler = new Label(composite, SWT.None);
//filler.setText("");
Label lblUser = new Label(composite, SWT.None);
lblUser.setText("User:");
_srvUser = new Text(composite, SWT.BORDER);
_srvUser.setLayoutData(txtStyle);
_srvUser.addModifyListener(this);
//filler = new Label(composite, SWT.None);
//filler.setText("");
Label lblPW = new Label(composite, SWT.None);
lblPW.setText("Password:");
_srvPW = new Text(composite, SWT.PASSWORD | SWT.BORDER);
_srvPW.setLayoutData(txtStyle);
_srvPW.addModifyListener(this);
_btnTestConnection = new Button(composite, SWT.PUSH);
_btnTestConnection.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
}
public void widgetSelected(SelectionEvent e) {
handleTestConnection();
}
});
_btnTestConnection.setText("Test connection");
_btnTestConnection.setEnabled(false);
GridData g = new GridData();
g.horizontalAlignment = GridData.END;
g.horizontalSpan = 2;
_btnTestConnection.setLayoutData(g);
if (_server != null) {
_srvName.setText(_server.getName());
_srvURL.setText(_server.getUrl().toString());
_srvUser.setText(_server.getUser());
_srvPW.setText(_server.getPassword());
}
return composite;
}
@Override
public List<String> validate() {
List<String> messages = new ArrayList<String>();
String serverName =_srvName.getText();
if (isEmpty(serverName)) {
messages.add("Please type a servername");
}
if (!WorkbenchUtils.isValidResourceName(serverName)) {
messages.add("Server name contains invalid characters. Only alphanumeric ascii characters and '_', '-' are allowed.");
}
if (_server != null) {
if (!_server.getName().equals(_srvName.getText()) && _servermodel.hasRemoteServer(new WGARemoteServer(_srvName.getText(), null, null, null))) {
messages.add("Server name already in use");
}
} else {
if (_servermodel.hasRemoteServer(new WGARemoteServer(_srvName.getText(), null, null, null))) {
messages.add("Server name already in use");
}
}
if (isEmpty(_srvURL.getText())) {
messages.add("Please type a URL");
}
if (isEmpty(_srvUser.getText())) {
messages.add("Please type a username");
}
try {
_serverurl = new URL(_srvURL.getText());
} catch (MalformedURLException e) {
messages.add("Invalid URL");
}
if (messages.size() > 0) {
_btnTestConnection.setEnabled(false);
} else {
_btnTestConnection.setEnabled(true);
}
return messages;
}
public void computeResponse() {
if (_server == null) { // ADD
_server = new WGARemoteServer(_srvName.getText(), _serverurl, _srvUser.getText(), _srvPW.getText());
} else { // EDIT
_server.setName(_srvName.getText());
_server.setUrl(_serverurl);
_server.setUser(_srvUser.getText());
_server.setPassword(_srvPW.getText());
}
}
private boolean isEmpty(String value) {
if (value != null) {
return value.length() == 0;
} else {
return true;
}
}
public WGARemoteServer getServer() {
return _server;
}
protected void handleTestConnection() {
if (validate().size() == 0) {
try {
computeResponse();
_server.connectToServer();
updateStatus(new Status(Status.INFO, WGADesignerPlugin.PLUGIN_ID, "Server is reachable"));
} catch (Exception e) {
String message = null;
if (e.getMessage() != null) {
message = "Connection to server failed: '" + e.getMessage() + "'.";
} else {
message = "Connection to server failed. See log for details.";
}
updateStatus(new Status(Status.WARNING, WGADesignerPlugin.PLUGIN_ID, message));
WGADesignerPlugin.getDefault().logWarning("Unable to connect to server '" + _server.getUrl().toString() + "'.", e);
}
}
}
}