Fix enter causing miscompletion of new project watches

Pressing Enter while completing a project name when adding a new
project to your watch list caused a server error with the partially
completed name displayed.  Work around it with the same solution
we used within d9a780a526f6649f863d1f118b4c8bc5a0d52cef, delaying
the submit until the completion is closed.

Bug: issue 507
Change-Id: I1efa776ae544e80254bbbc397af9106a458d8815
Signed-off-by: Shawn O. Pearce <sop@google.com>
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchPanel.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchPanel.java
index ad02588..9fb3748 100644
--- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchPanel.java
+++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/account/ProjectWatchPanel.java
@@ -31,6 +31,8 @@
 import com.google.gwt.event.dom.client.KeyCodes;
 import com.google.gwt.event.dom.client.KeyPressEvent;
 import com.google.gwt.event.dom.client.KeyPressHandler;
+import com.google.gwt.event.logical.shared.SelectionEvent;
+import com.google.gwt.event.logical.shared.SelectionHandler;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.CheckBox;
@@ -38,6 +40,7 @@
 import com.google.gwt.user.client.ui.FlowPanel;
 import com.google.gwt.user.client.ui.SuggestBox;
 import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
+import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
 import com.google.gwtexpui.globalkey.client.NpTextBox;
 import com.google.gwtjsonrpc.client.VoidResult;
 
@@ -50,6 +53,7 @@
   private Button addNew;
   private SuggestBox nameTxt;
   private Button delSel;
+  private boolean submitOnSelection;
 
   ProjectWatchPanel() {
     final FlowPanel body = new FlowPanel();
@@ -83,7 +87,22 @@
       box.addKeyPressHandler(new KeyPressHandler() {
         @Override
         public void onKeyPress(KeyPressEvent event) {
+          submitOnSelection = false;
+
           if (event.getCharCode() == KeyCodes.KEY_ENTER) {
+            if (nameTxt.isSuggestionListShowing()) {
+              submitOnSelection = true;
+            } else {
+              doAddNew();
+            }
+          }
+        }
+      });
+      nameTxt.addSelectionHandler(new SelectionHandler<Suggestion>() {
+        @Override
+        public void onSelection(SelectionEvent<Suggestion> event) {
+          if (submitOnSelection) {
+            submitOnSelection = false;
             doAddNew();
           }
         }