[216065] Better module validation
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java
index 3ca4b77..a289efc 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewManualServerComposite.java
@@ -15,6 +15,7 @@
 import java.util.List;
 
 import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IMessageProvider;
 import org.eclipse.jface.operation.IRunnableWithProgress;
@@ -71,6 +72,7 @@
 	protected String host;
 
 	protected IModuleType moduleType;
+	protected IModule module;
 	protected String serverTypeId;
 	protected boolean includeIncompatible;
 
@@ -84,16 +86,18 @@
 	 * @param parent a parent composite
 	 * @param wizard a wizard handle
 	 * @param moduleType a module type
+	 * @param module an optional module
 	 * @param serverTypeId a server type id, or null
 	 * @param includeIncompatible true to include incompatible servers that support similar module types
 	 * @param listener a server selection listener
 	 */
-	public NewManualServerComposite(Composite parent, IWizardHandle2 wizard, IModuleType moduleType, String serverTypeId, boolean includeIncompatible, ServerSelectionListener listener) {
+	public NewManualServerComposite(Composite parent, IWizardHandle2 wizard, IModuleType moduleType, IModule module, String serverTypeId, boolean includeIncompatible, ServerSelectionListener listener) {
 		super(parent, SWT.NONE);
 		this.wizard = wizard;
 		this.listener = listener;
 		
 		this.moduleType = moduleType;
+		this.module = module;
 		this.serverTypeId = serverTypeId;
 		this.includeIncompatible = includeIncompatible;
 		
@@ -102,6 +106,21 @@
 	}
 
 	/**
+	 * @deprecated Old internal constructor left to ensure no chance of incompatibility. You must remove any usage
+	 *    before moving to WTP 3.0.
+	 * 
+	 * @param parent
+	 * @param wizard
+	 * @param moduleType
+	 * @param serverTypeId
+	 * @param includeIncompatible
+	 * @param listener
+	 */
+	public NewManualServerComposite(Composite parent, IWizardHandle2 wizard, IModuleType moduleType, String serverTypeId, boolean includeIncompatible, ServerSelectionListener listener) {
+		this(parent, wizard, moduleType, null, serverTypeId, includeIncompatible, listener);
+	}
+
+	/**
 	 * Returns this page's initial visual components.
 	 */
 	protected void createControl() {
@@ -381,7 +400,20 @@
 		} else {
 			wizard.setMessage(null, IMessageProvider.NONE);
 			loadServerImpl(serverType);
+			if (server != null && module != null) {
+				IStatus status = NewServerComposite.isSupportedModule(server, module);
+				if (status != null) {
+					if (status.getSeverity() == IStatus.ERROR)
+						wizard.setMessage(status.getMessage(), IMessageProvider.ERROR);
+					else if (status.getSeverity() == IStatus.WARNING)
+						wizard.setMessage(status.getMessage(), IMessageProvider.WARNING);
+					else if (status.getSeverity() == IStatus.INFO)
+						wizard.setMessage(status.getMessage(), IMessageProvider.INFORMATION);
+					server = null;
+				}
+			}
 		}
+		
 		updateRuntimeCombo(serverType);
 		listener.serverSelected(server);
 		wizard.update();
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewServerComposite.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewServerComposite.java
index 542ecb7..6490742 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewServerComposite.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/wizard/page/NewServerComposite.java
@@ -14,6 +14,7 @@
 
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 import org.eclipse.jface.dialogs.Dialog;
 import org.eclipse.jface.dialogs.IMessageProvider;
 import org.eclipse.jface.operation.IRunnableWithProgress;
@@ -213,7 +214,7 @@
 		createAutoComposite(stack);
 		createManualComposite(stack);
 	
-		if (existingComp != null) {
+		if (existingComp != null && existing != null) {
 			if (isExistingServer()) {
 				mode = MODE_EXISTING;
 				stackLayout.topControl = existingComp;
@@ -334,63 +335,16 @@
 				
 				// check for compatibility
 				if (server != null && module != null) {
-					IServerType serverType = server.getServerType();
-					IModuleType mt = module.getModuleType();
-					if (!ServerUtil.isSupportedModule(serverType.getRuntimeType().getModuleTypes(), mt)) {
-						String type = mt.getName();
-						wizard.setMessage(NLS.bind(Messages.errorVersionLevel, new Object[] { type, mt.getVersion() }), IMessageProvider.ERROR);
+					IStatus status = isSupportedModule(server, module);
+					if (status != null) {
+						if (status.getSeverity() == IStatus.ERROR)
+							wizard.setMessage(status.getMessage(), IMessageProvider.ERROR);
+						else if (status.getSeverity() == IStatus.WARNING)
+							wizard.setMessage(status.getMessage(), IMessageProvider.WARNING);
+						else if (status.getSeverity() == IStatus.INFO)
+							wizard.setMessage(status.getMessage(), IMessageProvider.INFORMATION);
 						server = null;
 					}
-					
-					if (wizard.getMessage() == null) {
-						IModule[] rootModules = null;
-						try {
-							rootModules = server.getRootModules(module, null);
-						} catch (CoreException ce) {
-							IStatus status = ce.getStatus();
-							if (status != null) {
-								if (status.getSeverity() == IStatus.ERROR)
-									wizard.setMessage(status.getMessage(), IMessageProvider.ERROR);
-								else if (status.getSeverity() == IStatus.WARNING)
-									wizard.setMessage(status.getMessage(), IMessageProvider.WARNING);
-								else if (status.getSeverity() == IStatus.INFO)
-									wizard.setMessage(status.getMessage(), IMessageProvider.INFORMATION);
-								server = null;
-							}
-						} catch (Exception e) {
-							Trace.trace(Trace.WARNING, "Could not find root module", e);
-						}
-						if (rootModules != null) {
-							if (rootModules.length == 0) {
-								wizard.setMessage(Messages.errorRootModule, IMessageProvider.ERROR);
-								server = null;
-							} else {
-								int size = rootModules.length;
-								IStatus status = null;
-								boolean found = false;
-								for (int i = 0; i < size; i++) {
-									try {
-										status = server.canModifyModules(new IModule[] {rootModules[i]}, null, null);
-										if (status != null && status.isOK())
-											found = true;
-									} catch (Exception e) {
-										Trace.trace(Trace.WARNING, "Could not find root module", e);
-									}
-								}
-								if (!found && status != null) {
-									if (status != null) {
-										if (status.getSeverity() == IStatus.ERROR)
-											wizard.setMessage(status.getMessage(), IMessageProvider.ERROR);
-										else if (status.getSeverity() == IStatus.WARNING)
-											wizard.setMessage(status.getMessage(), IMessageProvider.WARNING);
-										else if (status.getSeverity() == IStatus.INFO)
-											wizard.setMessage(status.getMessage(), IMessageProvider.INFORMATION);
-										server = null;
-									}
-								}
-							}
-						}
-					}
 				}
 				
 				if (existingWC != null) {
@@ -409,7 +363,55 @@
 		data.heightHint = 150;
 		existingComp.setLayoutData(data);
 	}
-	
+
+	/**
+	 * Returns the status of whether the given module could be added to the server.
+	 * 
+	 * @param server a server
+	 * @param module a module
+	 * @return an IStatus representing the error or warning, or null if there are no problems
+	 */
+	protected static IStatus isSupportedModule(IServerAttributes server, IModule module) {
+		if (server != null && module != null) {
+			IServerType serverType = server.getServerType();
+			IModuleType mt = module.getModuleType();
+			if (!ServerUtil.isSupportedModule(serverType.getRuntimeType().getModuleTypes(), mt)) {
+				String type = mt.getName();
+				return new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID, NLS.bind(Messages.errorVersionLevel, new Object[] { type, mt.getVersion() }));
+			}
+			
+			IModule[] rootModules = null;
+			try {
+				rootModules = server.getRootModules(module, null);
+			} catch (CoreException ce) {
+				return ce.getStatus();
+			} catch (Exception e) {
+				Trace.trace(Trace.WARNING, "Could not find root module", e);
+			}
+			if (rootModules != null) {
+				if (rootModules.length == 0)
+					return new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID, Messages.errorRootModule);
+				
+				int size = rootModules.length;
+				IStatus status = null;
+				boolean found = false;
+				for (int i = 0; i < size; i++) {
+					try {
+						if (server != null)
+							status = server.canModifyModules(new IModule[] {rootModules[i]}, null, null);
+						if (status != null && status.isOK())
+							found = true;
+					} catch (Exception e) {
+						Trace.trace(Trace.WARNING, "Could not find root module", e);
+					}
+				}
+				if (!found && status != null)
+					return status;
+			}
+		}
+		return null;
+	}
+
 	protected boolean isExistingServer() {
 		if (module == null || launchMode == null)
 			return false;
@@ -457,7 +459,7 @@
 			public void setMessage(String newMessage, int newType) {
 				wizard.setMessage(newMessage, newType);
 			}
-		}, mt, serverTypeId, includeIncompatible, new NewManualServerComposite.ServerSelectionListener() {
+		}, mt, module, serverTypeId, includeIncompatible, new NewManualServerComposite.ServerSelectionListener() {
 			public void serverSelected(IServerAttributes server) {
 				updateTaskModel();
 			}