Bug 527081 - server name collission should prevent saving, undo should re-validate

Signed-off-by: Rob Stryker <stryker@redhat.com>
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/command/SetServerNameCommand.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/command/SetServerNameCommand.java
index 7ba416f..7e6c59b 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/command/SetServerNameCommand.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/command/SetServerNameCommand.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2003, 2005 IBM Corporation and others.
+ * Copyright (c) 2003, 2017 IBM Corporation and others.
  * 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
@@ -18,16 +18,22 @@
 public class SetServerNameCommand extends ServerCommand {
 	protected String name;
 	protected String oldName;
+	protected Validator validator;
 
+	public static interface Validator {
+		public void validate();
+	}
+	
 	/**
 	 * SetServerNameCommand constructor.
 	 * 
 	 * @param server a server
 	 * @param name a name for the server
 	 */
-	public SetServerNameCommand(IServerWorkingCopy server, String name) {
+	public SetServerNameCommand(IServerWorkingCopy server, String name, Validator validator) {
 		super(server, Messages.serverEditorOverviewServerNameCommand);
 		this.name = name;
+		this.validator = validator;
 	}
 
 	/**
@@ -36,6 +42,8 @@
 	public void execute() {
 		oldName = server.getName();
 		server.setName(name);
+		if( validator != null )
+			validator.validate();
 	}
 
 	/**
@@ -43,5 +51,7 @@
 	 */
 	public void undo() {
 		server.setName(oldName);
+		if( validator != null )
+			validator.validate();
 	}
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/editor/OverviewEditorPart.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/editor/OverviewEditorPart.java
index 4f32e68..93f9d8d 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/editor/OverviewEditorPart.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/editor/OverviewEditorPart.java
@@ -12,15 +12,11 @@
 
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
-import java.util.ArrayList;
-import java.util.Iterator;
+import java.util.*;
 import java.util.List;
-import java.util.Map;
 
 import org.eclipse.core.resources.*;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.*;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.ILaunchConfigurationType;
 import org.eclipse.debug.core.ILaunchManager;
@@ -340,9 +336,12 @@
 					if (updating)
 						return;
 					updating = true;
-					execute(new SetServerNameCommand(getServer(), serverName.getText()));
+					execute(new SetServerNameCommand(getServer(), serverName.getText(), new SetServerNameCommand.Validator() {
+						public void validate() {
+							OverviewEditorPart.this.validate();
+						}
+					}));
 					updating = false;
-					validate();
 				}
 			});
 			whs.setHelp(serverName, ContextIds.EDITOR_SERVER);
@@ -1026,15 +1025,18 @@
 		return pageModifiersList;
 	}
 
+	IStatus validationStatus = null;
 	protected void validate() {
 		IManagedForm mForm = getManagedForm();
 		if (mForm == null)
 			return;
-		
+		MultiStatus ms = new MultiStatus(ServerUIPlugin.PLUGIN_ID, 0, "Validating Overview Part", null);
 		mForm.getMessageManager().removeMessage("name", serverName);
 		if (server != null && serverName != null) {
-			if (ServerPlugin.isNameInUse(server, serverName.getText().trim()))
+			if (ServerPlugin.isNameInUse(server, serverName.getText().trim())) {
 				mForm.getMessageManager().addMessage("name", Messages.errorDuplicateName, null, IMessageProvider.ERROR, serverName);
+				ms.add(new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID,  Messages.errorDuplicateName));
+			}
 		}
 		
 		if (serverConfiguration != null) {
@@ -1046,17 +1048,29 @@
 					IProject project = null;
 					if (folder != null)
 						project = folder.getProject();
-					if (project != null && project.exists() && !project.isOpen())
+					if (project != null && project.exists() && !project.isOpen()) {
 						mForm.getMessageManager().addMessage("config", NLS.bind(Messages.errorConfigurationNotAccessible, project.getName()), null, IMessageProvider.ERROR, serverConfiguration);
-					else
+						ms.add(new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID,  NLS.bind(Messages.errorConfigurationNotAccessible, project.getName())));
+					} else {
 						mForm.getMessageManager().addMessage("config", Messages.errorMissingConfiguration, null, IMessageProvider.ERROR, serverConfiguration);
+						ms.add(new Status(IStatus.ERROR, ServerUIPlugin.PLUGIN_ID,  Messages.errorMissingConfiguration));
+					}
 	 			}
 			}
 		}
-		
+		validationStatus = (ms.isOK() ? Status.OK_STATUS : ms);
 		mForm.getMessageManager().update();
 	}
 
+	public IStatus[] getSaveStatus() {
+		IStatus[] all = super.getSaveStatus();
+		if( validationStatus == null )
+			return all;
+		List<IStatus> all2 = new ArrayList<IStatus>(Arrays.asList(all));
+		all2.addAll(Arrays.asList(validationStatus.getChildren()));
+		return all2.toArray(new IStatus[all2.size()]);
+	}
+	
 	protected void updateDecoration(ControlDecoration decoration, IStatus status) {
 		if (status != null) {
 			Image newImage = null;