[111432] Provide better initial selection in wizards
diff --git a/plugins/org.eclipse.wst.server.ui/plugin.properties b/plugins/org.eclipse.wst.server.ui/plugin.properties
index d700902..ec096e5 100644
--- a/plugins/org.eclipse.wst.server.ui/plugin.properties
+++ b/plugins/org.eclipse.wst.server.ui/plugin.properties
@@ -16,6 +16,8 @@
 extensionPointEditorActions=Editor Actions
 extensionPointServerImages=Server Images
 extensionPointWizardFragments=Wizard Fragments
+extensionPointInitialSelectionProvider=Initial Selection Provider
+extensionPointAudio=Audio
 
 # --------------- Wizards and Views ---------------
 
diff --git a/plugins/org.eclipse.wst.server.ui/plugin.xml b/plugins/org.eclipse.wst.server.ui/plugin.xml
index 67a579f..54901a2 100644
--- a/plugins/org.eclipse.wst.server.ui/plugin.xml
+++ b/plugins/org.eclipse.wst.server.ui/plugin.xml
@@ -7,7 +7,8 @@
   <extension-point id="editorActions" name="%extensionPointEditorActions" schema="schema/editorActions.exsd"/>
   <extension-point id="serverImages" name="%extensionPointServerImages" schema="schema/serverImages.exsd"/>
   <extension-point id="wizardFragments" name="%extensionPointWizardFragments" schema="schema/wizardFragments.exsd"/>
-  <extension-point id="audio" name="%extensionPointAudio"/>
+  <extension-point id="initialSelectionProvider" name="%extensionPointInitialSelectionProvider" schema="schema/initialSelectionProvider.exsd"/>
+  <extension-point id="audio" name="%extensionPointAudio" schema="schema/audio.exsd"/>
 
 <extension point="org.eclipse.ui.ide.projectNatureImages">
   <image
diff --git a/plugins/org.eclipse.wst.server.ui/schema/audio.exsd b/plugins/org.eclipse.wst.server.ui/schema/audio.exsd
new file mode 100644
index 0000000..c523ad2
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.ui/schema/audio.exsd
@@ -0,0 +1,138 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- Schema file written by PDE -->
+<schema targetNamespace="org.eclipse.wst.server.ui">
+<annotation>
+      <appInfo>
+         <meta.schema plugin="org.eclipse.wst.server.ui" id="audio" name="Audio"/>
+      </appInfo>
+      <documentation>
+         This extension point provides a way to sort runtime types, runtimes, server types, and servers. The first extension found is always used and it is expected that only one comparator exists in an installation.
+
+&lt;b&gt;Note:&lt;/b&gt; This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.
+      </documentation>
+   </annotation>
+
+   <element name="extension">
+      <complexType>
+         <choice>
+            <element ref="category" minOccurs="0" maxOccurs="unbounded"/>
+            <element ref="sound" minOccurs="0" maxOccurs="unbounded"/>
+         </choice>
+         <attribute name="point" type="string" use="required">
+            <annotation>
+               <documentation>
+                  a fully qualified identifier of the target extension point
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="id" type="string">
+            <annotation>
+               <documentation>
+                  an optional identifier of the extension instance
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="name" type="string">
+            <annotation>
+               <documentation>
+                  an optional name of the extension instance
+               </documentation>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <element name="category">
+      <annotation>
+         <appInfo>
+            <meta.element labelAttribute="name"/>
+         </appInfo>
+      </annotation>
+      <complexType>
+         <sequence>
+         </sequence>
+         <attribute name="id" type="string" use="required">
+            <annotation>
+               <documentation>
+                  specifies a unique identifier for this extension point
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="name" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <element name="sound">
+      <complexType>
+         <attribute name="id" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="category" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="name" type="string" use="required">
+            <annotation>
+               <documentation>
+                  
+               </documentation>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="examples"/>
+      </appInfo>
+      <documentation>
+         The following is an example of a sorter extension point:
+
+&lt;p&gt;
+&lt;pre&gt;
+ &lt;extension point=&quot;org.eclipse.wst.server.ui.sorter&quot;&gt;
+    &lt;image
+       id=&quot;com.example&quot;
+       class=&quot;com.example.runtime.MyComparator&quot;/&gt;
+ &lt;/extension&gt;
+&lt;/pre&gt;
+&lt;/p&gt;
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="apiInfo"/>
+      </appInfo>
+      <documentation>
+         
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="copyright"/>
+      </appInfo>
+      <documentation>
+         Copyright (c) 2000, 2005 IBM Corporation and others.&lt;br&gt;
+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 
+&lt;a href=&quot;http://www.eclipse.org/legal/epl-v10.html&quot;&gt;http://www.eclipse.org/legal/epl-v10.html&lt;/a&gt;
+      </documentation>
+   </annotation>
+
+</schema>
diff --git a/plugins/org.eclipse.wst.server.ui/schema/editorActions.exsd b/plugins/org.eclipse.wst.server.ui/schema/editorActions.exsd
index ce5cc1c..ad235ad 100644
--- a/plugins/org.eclipse.wst.server.ui/schema/editorActions.exsd
+++ b/plugins/org.eclipse.wst.server.ui/schema/editorActions.exsd
@@ -7,6 +7,8 @@
       </appInfo>

       <documentation>

          This extension point provides a way to add actions to the toolbar of an existing server editor.

+

+&lt;b&gt;Note:&lt;/b&gt; This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.

       </documentation>

    </annotation>

 

@@ -100,12 +102,12 @@
          <meta.section type="examples"/>

       </appInfo>

       <documentation>

-         The following is an example of an editor action extension point:
-
-&lt;p&gt;
-&lt;pre&gt;
- 
-&lt;/pre&gt;
+         The following is an example of an editor action extension point:

+

+&lt;p&gt;

+&lt;pre&gt;

+ 

+&lt;/pre&gt;

 &lt;/p&gt;

       </documentation>

    </annotation>

@@ -124,10 +126,10 @@
          <meta.section type="copyright"/>

       </appInfo>

       <documentation>

-         Copyright (c) 2000, 2005 IBM Corporation and others.&lt;br&gt;
-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 
+         Copyright (c) 2000, 2005 IBM Corporation and others.&lt;br&gt;

+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 

 &lt;a href=&quot;http://www.eclipse.org/legal/epl-v10.html&quot;&gt;http://www.eclipse.org/legal/epl-v10.html&lt;/a&gt;

       </documentation>

    </annotation>

diff --git a/plugins/org.eclipse.wst.server.ui/schema/initialSelectionProvider.exsd b/plugins/org.eclipse.wst.server.ui/schema/initialSelectionProvider.exsd
new file mode 100644
index 0000000..c2b37e3
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.ui/schema/initialSelectionProvider.exsd
@@ -0,0 +1,114 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<!-- Schema file written by PDE -->
+<schema targetNamespace="org.eclipse.wst.server.ui">
+<annotation>
+      <appInfo>
+         <meta.schema plugin="org.eclipse.wst.server.ui" id="initialSelectionProvider" name="InitialSelectionProvider"/>
+      </appInfo>
+      <documentation>
+         This extension point provides a way to initially select runtime types, runtimes, server types, and servers in the server wizards. The first extension found is always used and it is expected that only one initialSelectionProvider exists in an installation.
+
+&lt;b&gt;Note:&lt;/b&gt; This class/interface is part of an interim API that is still under development and expected to change significantly before reaching stability. It is being made available at this early stage to solicit feedback from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken (repeatedly) as the API evolves.
+      </documentation>
+   </annotation>
+
+   <element name="extension">
+      <complexType>
+         <sequence>
+            <element ref="selectionProvider"/>
+         </sequence>
+         <attribute name="point" type="string" use="required">
+            <annotation>
+               <documentation>
+                  a fully qualified identifier of the target extension point
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="id" type="string">
+            <annotation>
+               <documentation>
+                  an optional identifier of the extension instance
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="name" type="string">
+            <annotation>
+               <documentation>
+                  an optional name of the extension instance
+               </documentation>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <element name="selectionProvider">
+      <annotation>
+         <appInfo>
+            <meta.element labelAttribute="name"/>
+         </appInfo>
+      </annotation>
+      <complexType>
+         <sequence>
+         </sequence>
+         <attribute name="id" type="string" use="required">
+            <annotation>
+               <documentation>
+                  specifies a unique identifier for this extension point
+               </documentation>
+            </annotation>
+         </attribute>
+         <attribute name="class" type="string" use="required">
+            <annotation>
+               <documentation>
+                  specifies the fully qualified name of the Java class that extends &lt;samp&gt;org.eclipse.wst.server.ui.internal.viewers.InitialSelectionProvider&lt;/samp&gt;. The class must have a public 0-arg constructor.
+               </documentation>
+               <appInfo>
+                  <meta.attribute kind="java"/>
+               </appInfo>
+            </annotation>
+         </attribute>
+      </complexType>
+   </element>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="examples"/>
+      </appInfo>
+      <documentation>
+         The following is an example of the extension point:
+
+&lt;p&gt;
+&lt;pre&gt;
+ &lt;extension point=&quot;org.eclipse.wst.server.ui.initialSelectionProvider&quot;&gt;
+    &lt;selectionProvider
+       id=&quot;com.example&quot;
+       class=&quot;com.example.runtime.MySelectionProvider&quot;/&gt;
+ &lt;/extension&gt;
+&lt;/pre&gt;
+&lt;/p&gt;
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="apiInfo"/>
+      </appInfo>
+      <documentation>
+         
+      </documentation>
+   </annotation>
+
+   <annotation>
+      <appInfo>
+         <meta.section type="copyright"/>
+      </appInfo>
+      <documentation>
+         Copyright (c) 2005 IBM Corporation and others.&lt;br&gt;
+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 
+&lt;a href=&quot;http://www.eclipse.org/legal/epl-v10.html&quot;&gt;http://www.eclipse.org/legal/epl-v10.html&lt;/a&gt;
+      </documentation>
+   </annotation>
+
+</schema>
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/ServerUIPlugin.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/ServerUIPlugin.java
index 4be7ffb..b766a47 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/ServerUIPlugin.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/ServerUIPlugin.java
@@ -29,6 +29,7 @@
 import org.eclipse.wst.server.ui.internal.actions.RunOnServerActionDelegate;
 import org.eclipse.wst.server.ui.internal.editor.IServerEditorInput;
 import org.eclipse.wst.server.ui.internal.editor.ServerEditorInput;
+import org.eclipse.wst.server.ui.internal.viewers.InitialSelectionProvider;
 import org.eclipse.wst.server.ui.internal.wizard.ClosableWizardDialog;
 import org.eclipse.wst.server.ui.internal.wizard.TaskWizard;
 import org.eclipse.wst.server.ui.internal.wizard.WizardTaskUtil;
@@ -49,22 +50,25 @@
  */
 public class ServerUIPlugin extends AbstractUIPlugin {
 	protected static final String VIEW_ID = "org.eclipse.wst.server.ui.ServersView";
-	
+
 	// server UI plugin id
 	public static final String PLUGIN_ID = "org.eclipse.wst.server.ui";
 
 	//public static final byte START = 0;
 	public static final byte STOP = 1;
 	//public static final byte RESTART = 2;
-	
+
 	// singleton instance of this class
 	private static ServerUIPlugin singleton;
 
 	protected Map imageDescriptors = new HashMap();
-	
+
 	// cached copy of all runtime wizards
 	private static Map wizardFragments;
-	
+
+	// cached initial selection provider
+	private static InitialSelectionProvider selectionProvider; 
+
 	static class WizardFragmentData {
 		String id;
 		IConfigurationElement ce;
@@ -574,6 +578,47 @@
 		Trace.trace(Trace.CONFIG, "-<- Done loading .wizardFragments extension point -<-");
 	}
 
+	/**
+	 * Returns the initial selection provider.
+	 *
+	 * @return an initial selection provider, or <code>null</code> if none could be found
+	 */
+	public static InitialSelectionProvider getInitialSelectionProvider() {
+		if (selectionProvider == null)
+			loadInitialSelectionProvider();
+		
+		return selectionProvider;
+	}
+
+	/**
+	 * Load the initial selection provider.
+	 */
+	private static synchronized void loadInitialSelectionProvider() {
+		if (selectionProvider != null)
+			return;
+		
+		Trace.trace(Trace.CONFIG, "->- Loading .initialSelectionProvider extension point ->-");
+		IExtensionRegistry registry = Platform.getExtensionRegistry();
+		IConfigurationElement[] cf = registry.getConfigurationElementsFor(ServerUIPlugin.PLUGIN_ID, "initialSelectionProvider");
+		
+		if (cf.length == 1) {
+			try {
+				selectionProvider = (InitialSelectionProvider) cf[0].createExecutableExtension("class");
+				Trace.trace(Trace.CONFIG, "  Loaded initialSelectionProvider: " + cf[0].getAttribute("id"));
+			} catch (Throwable t) {
+				Trace.trace(Trace.SEVERE, "  Could not load initialSelectionProvider: " + cf[0].getAttribute("id"), t);
+			}
+		} else if (cf.length > 1)
+			Trace.trace(Trace.WARNING, "More that one initial selection provider found - ignoring");
+		else
+			Trace.trace(Trace.CONFIG, "No initial selection provider found");
+		
+		if (selectionProvider == null)
+			selectionProvider = new InitialSelectionProvider();
+		
+		Trace.trace(Trace.CONFIG, "-<- Done loading .initialSelectionProvider extension point -<-");
+	}
+
 	protected static WizardFragment getWizardFragment(WizardFragmentData fragment) {
 		if (fragment == null)
 			return null;
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/AbstractTreeContentProvider.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/AbstractTreeContentProvider.java
index dc06c47..9cb8785 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/AbstractTreeContentProvider.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/AbstractTreeContentProvider.java
@@ -17,6 +17,7 @@
 
 import org.eclipse.jface.viewers.ITreeContentProvider;
 import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.wst.server.ui.internal.ServerUIPlugin;
 /**
  * Runtime type content provider.
  */
@@ -24,12 +25,11 @@
 	public static final byte STYLE_FLAT = 0;
 
 	public static final String ROOT = "root";
-	
+
 	protected byte style;
-	
+
 	protected Object initialSelection;
-	protected int initialSelectionOrder;
-	
+
 	public class TreeElement {
 		String text;
 		List contents;
@@ -50,12 +50,12 @@
 		
 		fillTree();
 	}
-	
+
 	public AbstractTreeContentProvider(byte style, boolean init) {
 		super();
 		this.style = style;
 	}
-	
+
 	protected abstract void fillTree();
 
 	protected void clean() {
@@ -64,7 +64,6 @@
 		textMap = new HashMap(2);
 		
 		initialSelection = null;
-		initialSelectionOrder = -1000;
 	}
 
 	protected TreeElement getOrCreate(List list, String text) {
@@ -187,8 +186,38 @@
 	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
 		// do nothing
 	}
-	
+
+	private Object[] getAllObjects() {
+		List list = new ArrayList();
+		Object[] obj = getElements(null);
+		if (obj != null) {
+			int size = obj.length;
+			for (int i = 0; i < size; i++) {
+				if (!(obj[i] instanceof AbstractTreeContentProvider.TreeElement))
+					list.add(obj[i]);
+				getAllChildren(list, obj[i]);
+			}
+		}
+		return list.toArray();
+	}
+
+	private void getAllChildren(List list, Object element) {
+		Object[] obj = getChildren(element);
+		if (obj != null) {
+			int size = obj.length;
+			for (int i = 0; i < size; i++) {
+				if (!(obj[i] instanceof AbstractTreeContentProvider.TreeElement))
+					list.add(obj[i]);
+				getAllChildren(list, obj[i]);
+			}
+		}
+	}
+
 	public Object getInitialSelection() {
+		if (initialSelection == null) {
+			InitialSelectionProvider isp = ServerUIPlugin.getInitialSelectionProvider();
+			initialSelection = isp.getInitialSelection(getAllObjects());
+		}
 		return initialSelection;
 	}
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/DefaultViewerSorter.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/DefaultViewerSorter.java
new file mode 100644
index 0000000..a4b9587
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/DefaultViewerSorter.java
@@ -0,0 +1,90 @@
+/*******************************************************************************
+ * Copyright (c) 2005 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.wst.server.ui.internal.viewers;
+
+import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.wst.server.core.IRuntimeType;
+import org.eclipse.wst.server.core.IServerType;
+/**
+ * Class used to sort categories, runtime types, and server types in the
+ * New wizards.
+ */
+public class DefaultViewerSorter extends ViewerSorter {
+	/**
+	 * @see ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
+	 */
+	public int compare(Viewer viewer, Object o1, Object o2) {
+		if (o1 instanceof AbstractTreeContentProvider.TreeElement)
+			o1 = ((AbstractTreeContentProvider.TreeElement) o1).text;
+		
+		if (o2 instanceof AbstractTreeContentProvider.TreeElement)
+			o2 = ((AbstractTreeContentProvider.TreeElement) o2).text;
+		
+		// filter out strings
+		if (o1 instanceof String && o2 instanceof String)
+			return compareCategories((String) o1, (String) o2);
+		if (o1 instanceof String)
+			return -1;
+		if (o2 instanceof String)
+			return 1;
+		
+		if (o1 instanceof IRuntimeType && o2 instanceof IRuntimeType)
+			return compareRuntimeTypes((IRuntimeType) o1, (IRuntimeType) o2);
+		
+		if (o1 instanceof IServerType && o2 instanceof IServerType)
+			return compareServerTypes((IServerType) o1, (IServerType) o2);
+		
+		return 0;
+	}
+
+	/**
+	 * Sort two category names.
+	 * 
+	 * @param s1 the first category
+	 * @param s2 the second category
+	 * @return a negative number if the first element is less  than the 
+	 *    second element; the value <code>0</code> if the first element is
+	 *    equal to the second element; and a positive number if the first
+	 *    element is greater than the second element
+	 */
+	protected int compareCategories(String s1, String s2) {
+		return s1.compareTo(s2);
+	}
+
+	/**
+	 * Sort two runtime types.
+	 * 
+	 * @param r1 the first runtime type
+	 * @param r2 the second runtime type
+	 * @return a negative number if the first element is less  than the 
+	 *    second element; the value <code>0</code> if the first element is
+	 *    equal to the second element; and a positive number if the first
+	 *    element is greater than the second element
+	 */
+	protected int compareRuntimeTypes(IRuntimeType r1, IRuntimeType r2) {
+		return r1.getName().compareToIgnoreCase(r2.getName());
+	}
+
+	/**
+	 * Sort two server types.
+	 * 
+	 * @param s1 the first server type
+	 * @param s2 the second server type
+	 * @return a negative number if the first element is less  than the 
+	 *    second element; the value <code>0</code> if the first element is
+	 *    equal to the second element; and a positive number if the first
+	 *    element is greater than the second element
+	 */
+	protected int compareServerTypes(IServerType s1, IServerType s2) {
+		return s1.getName().compareToIgnoreCase(s2.getName());
+	}
+}
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/InitialSelectionProvider.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/InitialSelectionProvider.java
new file mode 100644
index 0000000..84852ab
--- /dev/null
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/InitialSelectionProvider.java
@@ -0,0 +1,91 @@
+/*******************************************************************************
+ * Copyright (c) 2005 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
+ * http://www.eclipse.org/legal/epl-v10.html
+ * 
+ * Contributors:
+ *     IBM Corporation - Initial API and implementation
+ *******************************************************************************/
+package org.eclipse.wst.server.ui.internal.viewers;
+
+import org.eclipse.jface.viewers.ViewerSorter;
+import org.eclipse.wst.server.core.IRuntime;
+import org.eclipse.wst.server.core.IRuntimeType;
+import org.eclipse.wst.server.core.IServerType;
+import org.eclipse.wst.server.ui.internal.ServerUIPlugin;
+/**
+ * Class used to sort categories, runtime types, and server types in the
+ * New wizards.
+ */
+public class InitialSelectionProvider extends ViewerSorter {
+
+	public Object getInitialSelection(Object[] obj) {
+		if (obj == null || obj.length == 0)
+			return null;
+		
+		if (obj[0] instanceof IRuntimeType) {
+			int size = obj.length;
+			IRuntimeType[] rt = new IRuntimeType[size];
+			for (int i = 0; i < size; i++)
+				rt[i] = (IRuntimeType) obj[i];
+			return getInitialSelection(rt);
+		}
+		
+		if (obj[0] instanceof IServerType) {
+			int size = obj.length;
+			IServerType[] st = new IServerType[size];
+			for (int i = 0; i < size; i++)
+				st[i] = (IServerType) obj[i];
+			return getInitialSelection(st);
+		}
+		
+		return null;
+	}
+
+	/**
+	 * 
+	 * @param serverTypes
+	 * @return the initial selection
+	 */
+	public IServerType getInitialSelection(IServerType[] serverTypes) {
+		if (serverTypes == null)
+			return null;
+		
+		int size = serverTypes.length;
+		for (int i = 0; i < size; i++) {
+			if (hasRuntime(serverTypes[i]))
+				return serverTypes[i];
+		}
+		return serverTypes[0];
+	}
+
+	/**
+	 * 
+	 * @param runtimeTypes
+	 * @return the initial selection
+	 */
+	public IRuntimeType getInitialSelection(IRuntimeType[] runtimeTypes) {
+		if (runtimeTypes == null)
+			return null;
+		
+		int size = runtimeTypes.length;
+		for (int i = 0; i < size; i++) {
+			if (hasRuntime(runtimeTypes[i]))
+				return runtimeTypes[i];
+		}
+		return runtimeTypes[0];
+	}
+
+	protected boolean hasRuntime(IServerType serverType) {
+		return hasRuntime(serverType.getRuntimeType());
+	}
+
+	protected boolean hasRuntime(IRuntimeType runtimeType) {
+		if (runtimeType == null)
+			return false;
+		IRuntime[] runtimes = ServerUIPlugin.getRuntimes(runtimeType);
+		return runtimes != null && runtimes.length > 0;
+	}
+}
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeComposite.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeComposite.java
index 95407c5..b760e08 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeComposite.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeComposite.java
@@ -60,21 +60,9 @@
 			}
 		});
 		
-		treeViewer.setSorter(new ViewerSorter() {
-			public int compare(Viewer viewer, Object e1, Object e2) {
-				if (e1 instanceof IRuntimeType && !(e2 instanceof IRuntimeType))
-					return 1;
-				if (!(e1 instanceof IRuntimeType) && e2 instanceof IRuntimeType)
-					return -1;
-				if (!(e1 instanceof IRuntimeType && e2 instanceof IRuntimeType))
-					return super.compare(viewer, e1, e2);
-				IRuntimeType r1 = (IRuntimeType) e1;
-				IRuntimeType r2 = (IRuntimeType) e2;
-				return r1.getName().compareToIgnoreCase(r2.getName());
-			}
-		});
+		treeViewer.setSorter(new DefaultViewerSorter());
 	}
-	
+
 	public void setVisible(boolean visible) {
 		super.setVisible(visible);
 		if (visible && initialSelection) {
@@ -83,7 +71,7 @@
 				treeViewer.setSelection(new StructuredSelection(contentProvider.getInitialSelection()), true);
 		}
 	}
-	
+
 	protected String getTitleLabel() {
 		return Messages.runtimeTypeCompTree;
 	}
@@ -104,6 +92,13 @@
 		treeViewer.setSelection(sel);
 	}
 
+	public void refresh() {
+		ISelection sel = treeViewer.getSelection();
+		RuntimeTypeTreeContentProvider cp = (RuntimeTypeTreeContentProvider) treeViewer.getContentProvider();
+		treeViewer.setContentProvider(new RuntimeTypeTreeContentProvider(cp.style, creation, type, version, runtimeTypeId));
+		treeViewer.setSelection(sel);
+	}
+
 	public IRuntimeType getSelectedRuntimeType() {
 		return selection;
 	}
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeTreeContentProvider.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeTreeContentProvider.java
index cc4d09b..2c7b16b 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeTreeContentProvider.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/RuntimeTypeTreeContentProvider.java
@@ -58,11 +58,6 @@
 				for (int i = 0; i < size; i++) {
 					IRuntimeType runtimeType = runtimeTypes[i];
 					if (!creation || runtimeType.canCreate()) {
-						int order = getRuntimeOrder(runtimeType);
-						if (order > initialSelectionOrder) {
-							initialSelection = runtimeType;
-							initialSelectionOrder = order;
-						}
 						TreeElement ele = null;
 						if (style == STYLE_VENDOR) {
 							ele = getOrCreate(list, runtimeType.getVendor());
@@ -98,21 +93,11 @@
 				int size = runtimeTypes.length;
 				for (int i = 0; i < size; i++) {
 					IRuntimeType runtimeType = runtimeTypes[i];
-					if (!creation || runtimeType.canCreate()) {
-						int order = getRuntimeOrder(runtimeType);
-						if (order > initialSelectionOrder) {
-							initialSelection = runtimeType;
-							initialSelectionOrder = order;
-						}
+					if (!creation || runtimeType.canCreate())
 						list.add(runtimeType);
-					}
 				}
 			}
 		}
 		elements = list.toArray();
 	}
-	
-	private int getRuntimeOrder(IRuntimeType runtimeType) {
-		return 0;
-	}
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTreeContentProvider.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTreeContentProvider.java
index 108caa1..c13a38b 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTreeContentProvider.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTreeContentProvider.java
@@ -67,11 +67,6 @@
 					if (acceptServer(servers[i])) {
 						IServerType serverType = servers[i].getServerType();
 						IRuntimeType runtimeType = serverType.getRuntimeType();
-						int order = getServerOrder(serverType);
-						if (order > initialSelectionOrder) {
-							initialSelection = servers[i];
-							initialSelectionOrder = order;
-						}
 						TreeElement te = null;
 						if (style == STYLE_HOST) {
 							te = getOrCreate(list, servers[i].getHost());
@@ -96,15 +91,8 @@
 			if (servers != null) {
 				int size = servers.length;
 				for (int i = 0; i < size; i++) {
-					if (acceptServer(servers[i])) {
-						IServerType serverType = servers[i].getServerType();
+					if (acceptServer(servers[i]))
 						list.add(servers[i]);
-						int order = getServerOrder(serverType);
-						if (order > initialSelectionOrder) {
-							initialSelection = servers[i];
-							initialSelectionOrder = order;
-						}
-					}
 				}
 			}
 		}
@@ -127,8 +115,4 @@
 		}
 		return true;
 	}
-	
-	private int getServerOrder(IServerType serverType) {
-		return 0;
-	}
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeComposite.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeComposite.java
index e1258da..ab697aa 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeComposite.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeComposite.java
@@ -14,8 +14,6 @@
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.jface.viewers.Viewer;
-import org.eclipse.jface.viewers.ViewerSorter;
 import org.eclipse.swt.widgets.Composite;
 
 import org.eclipse.wst.server.core.IModuleType;
@@ -64,21 +62,9 @@
 			}
 		});
 		
-		treeViewer.setSorter(new ViewerSorter() {
-			public int compare(Viewer viewer, Object e1, Object e2) {
-				if (e1 instanceof IServerType && !(e2 instanceof IServerType))
-					return 1;
-				if (!(e1 instanceof IServerType) && e2 instanceof IServerType)
-					return -1;
-				if (!(e1 instanceof IServerType && e2 instanceof IServerType))
-					return super.compare(viewer, e1, e2);
-				IServerType r1 = (IServerType) e1;
-				IServerType r2 = (IServerType) e2;
-				return r1.getName().compareTo(r2.getName());
-			}
-		});
+		treeViewer.setSorter(new DefaultViewerSorter());
 	}
-	
+
 	public void setVisible(boolean visible) {
 		super.setVisible(visible);
 		if (visible && initialSelection) {
@@ -87,7 +73,7 @@
 				treeViewer.setSelection(new StructuredSelection(contentProvider.getInitialSelection()), true);
 		}
 	}
-	
+
 	public void setHost(boolean newHost) {
 		if (isLocalhost == newHost)
 			return;
@@ -99,7 +85,7 @@
 		//treeViewer.expandToLevel(2);
 		treeViewer.setSelection(sel, true);
 	}
-	
+
 	public void setIncludeIncompatibleVersions(boolean b) {
 		includeIncompatibleVersions = b;
 		ISelection sel = treeViewer.getSelection();
@@ -134,4 +120,11 @@
 	public IServerType getSelectedServerType() {
 		return selection;
 	}
+
+	public void refresh() {
+		ISelection sel = treeViewer.getSelection();
+		ServerTypeTreeContentProvider cp = (ServerTypeTreeContentProvider) treeViewer.getContentProvider();
+		treeViewer.setContentProvider(new ServerTypeTreeContentProvider(cp.style, moduleType));
+		treeViewer.setSelection(sel);
+	}
 }
\ No newline at end of file
diff --git a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeTreeContentProvider.java b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeTreeContentProvider.java
index 5b4dc29..7c91e41 100644
--- a/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeTreeContentProvider.java
+++ b/plugins/org.eclipse.wst.server.ui/serverui/org/eclipse/wst/server/ui/internal/viewers/ServerTypeTreeContentProvider.java
@@ -55,11 +55,6 @@
 			for (int i = 0; i < size; i++) {
 				IServerType serverType = serverTypes[i];
 				if (include(serverType)) {
-					int order = getServerOrder(serverType);
-					if (order > initialSelectionOrder) {
-						initialSelection = serverType;
-						initialSelectionOrder = order;
-					}
 					if (style == STYLE_FLAT) {
 						list.add(serverType);
 					} else if (style != STYLE_MODULE_TYPE) {
@@ -148,8 +143,4 @@
 		includeIncompatibleVersions = b;
 		fillTree();
 	}
-	
-	private int getServerOrder(IServerType serverType) {
-		return 0;
-	}
 }
\ No newline at end of file