Fix the order of the users in the Google login panel - maintain the insertion order.

Change-Id: I287e489c455d6b6f278d60d779db0f1c245ee820
diff --git a/login/src/com/google/gct/login/CredentialedUserRoster.java b/login/src/com/google/gct/login/CredentialedUserRoster.java
index 152382e..7d50c70 100644
--- a/login/src/com/google/gct/login/CredentialedUserRoster.java
+++ b/login/src/com/google/gct/login/CredentialedUserRoster.java
@@ -20,7 +20,7 @@
 import org.jetbrains.annotations.Nullable;
 
 import java.util.Collection;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
 /**
@@ -29,7 +29,7 @@
  * {@link CredentialedUser} objects.
  */
 public class CredentialedUserRoster {
-  private final Map<String, CredentialedUser> allUsers = new HashMap<String, CredentialedUser>();
+  private final LinkedHashMap<String, CredentialedUser> allUsers = new LinkedHashMap<String, CredentialedUser>();
   private CredentialedUser activeUser;
   private Collection<GoogleLoginListener> listeners;
 
@@ -41,9 +41,9 @@
    * Returns a copy of the map of the current logged in users.
    * @return Copy of current logged in users.
    */
-  public Map<String, CredentialedUser> getAllUsers() {
+  public LinkedHashMap<String, CredentialedUser> getAllUsers() {
     synchronized (this) {
-      Map<String, CredentialedUser> clone = new HashMap<String, CredentialedUser>();
+      LinkedHashMap<String, CredentialedUser> clone = new LinkedHashMap<String, CredentialedUser>();
       clone.putAll(allUsers);
       return clone;
     }
diff --git a/login/src/com/google/gct/login/GoogleLogin.java b/login/src/com/google/gct/login/GoogleLogin.java
index b139343..d97b68e 100644
--- a/login/src/com/google/gct/login/GoogleLogin.java
+++ b/login/src/com/google/gct/login/GoogleLogin.java
@@ -45,6 +45,8 @@
 import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.SortedSet;
 
@@ -401,7 +403,7 @@
    * Returns a copy of the map of the current logged in users.
    * @return Copy of current logged in users.
    */
-  public Map<String, CredentialedUser> getAllUsers() {
+  public LinkedHashMap<String, CredentialedUser> getAllUsers() {
     return users.getAllUsers();
   }
 
@@ -607,7 +609,7 @@
 
     public void initializeUsers() {
       String activeUserString = GoogleLoginPrefs.getActiveUser();
-      SortedSet<String> allUsers = GoogleLoginPrefs.getStoredUsers();
+      List<String> allUsers = GoogleLoginPrefs.getStoredUsers();
       for(String aUser : allUsers) {
         // Add a new user, so that loadOAuth called from the GoogleLoginState constructor
         // will be able to create a customized key to get that user's OAuth data
diff --git a/login/src/com/google/gct/login/GoogleLoginPrefs.java b/login/src/com/google/gct/login/GoogleLoginPrefs.java
index 8795238..2279f99 100644
--- a/login/src/com/google/gct/login/GoogleLoginPrefs.java
+++ b/login/src/com/google/gct/login/GoogleLoginPrefs.java
@@ -23,6 +23,8 @@
 import com.intellij.openapi.diagnostic.Logger;
 import org.jetbrains.annotations.NotNull;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.SortedSet;
 import java.util.TreeSet;
 import java.util.prefs.BackingStoreException;
@@ -163,10 +165,10 @@
    * @return the stored list of users.
    */
   @NotNull
-  public static SortedSet<String> getStoredUsers() {
+  public static List<String> getStoredUsers() {
     Preferences prefs = getPrefs();
     String allUsersString = prefs.get(USERS, "");
-    SortedSet<String> allUsers = new TreeSet<String>();
+    List<String> allUsers = new ArrayList<String>();
     if(allUsersString.isEmpty()) {
       return allUsers;
     }
@@ -252,7 +254,7 @@
       return;
     }
 
-    SortedSet<String> allUsers = new TreeSet<String>();
+    List<String> allUsers = new ArrayList<String>();
     Splitter splitter = Splitter.on(DELIMITER).omitEmptyStrings();
     for (String scope : splitter.split(allUsersString)) {
       allUsers.add(scope);
@@ -269,7 +271,7 @@
 
   private static void removeUser(Preferences prefs, String user) {;
     String allUsersString = prefs.get(USERS, "");
-    SortedSet<String> allUsers = new TreeSet<String>();
+    List<String> allUsers = new ArrayList<String>();
     for (String scope : allUsersString.split(DELIMITER)) {
       allUsers.add(scope);
     }
diff --git a/login/src/com/google/gct/login/ui/GoogleLoginUsersPanel.java b/login/src/com/google/gct/login/ui/GoogleLoginUsersPanel.java
index debc24e..71c0627 100644
--- a/login/src/com/google/gct/login/ui/GoogleLoginUsersPanel.java
+++ b/login/src/com/google/gct/login/ui/GoogleLoginUsersPanel.java
@@ -38,7 +38,7 @@
 import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
-import java.util.Map;
+import java.util.LinkedHashMap;
 
 /**
  * The Google Login Panel that displays the currently logged in users and buttons to
@@ -206,7 +206,7 @@
   }
 
   private int initializeUsers() {
-    Map<String, CredentialedUser> allUsers = GoogleLogin.getInstance().getAllUsers();
+    LinkedHashMap<String, CredentialedUser> allUsers = GoogleLogin.getInstance().getAllUsers();
     listModel = new DefaultListModel();
 
     int activeUserIndex = allUsers.size();