From 90e6cdbfbef228d9585884e113849f2526be10b5 Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Tue, 30 Oct 2012 13:38:52 +0000
Subject: [PATCH] SP-352 / BIS-236 : Provide Feedback to end user when doing an
 (Custom) Upload

SVN: 27412
---
 .../client/web/client/application/Dict.java   |  8 ++++
 .../ui/AbstractRegistrationForm.java          | 42 ++++++++++++++----
 .../application/ui/CustomImportForm.java      | 44 ++++++++++++++++---
 .../application/ui/widget/IInfoHandler.java   |  7 ++-
 .../client/application/ui/widget/InfoBox.java | 43 +++++++++++++++++-
 .../widget/PopupDialogBasedInfoHandler.java   | 12 +++--
 .../cisd/openbis/public/common-dictionary.js  |  9 +++-
 7 files changed, 146 insertions(+), 19 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/Dict.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/Dict.java
index 052e10df5d2..0e14ddb9a88 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/Dict.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/Dict.java
@@ -162,6 +162,8 @@ public abstract class Dict
 
     public static final String BUTTON_SAVE = "button_save";
 
+    public static final String BUTTON_UPLOAD = "button_upload";
+
     public static final String BUTTON_SAVE_AND_UPLOAD = "button_save_and_upload";
 
     public static final String BUTTON_CHOOSE = "button_choose";
@@ -1297,5 +1299,11 @@ public abstract class Dict
 
     public static final String ACTIVE_USERS_DIALOG = "active_users_dialog";
 
+    // Progress
+
+    public static final String PROGRESS_UPLOADING = "progress_uploading";
+
+    public static final String PROGRESS_PROCESSING = "progress_processing";
+
     // ----- end generic ------------------
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/AbstractRegistrationForm.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/AbstractRegistrationForm.java
index 6515b55e877..94517ac5848 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/AbstractRegistrationForm.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/AbstractRegistrationForm.java
@@ -88,6 +88,8 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
 
     protected InfoBox infoBox;
 
+    protected InfoBoxResetListener infoBoxResetListener;
+
     protected FormPanelWithSavePoint formPanel;
 
     protected final int labelWidth;
@@ -108,6 +110,8 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
 
     private Button revertButton;
 
+    private boolean dirtyCheckEnabled = true;
+
     protected AbstractRegistrationForm(final IMessageProvider messageProvider, final String id)
     {
         this(messageProvider, id, DEFAULT_LABEL_WIDTH, DEFAULT_FIELD_WIDTH);
@@ -177,7 +181,7 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
 
     protected void updateDirtyCheckAfterChange(boolean isDirty)
     {
-        if (isDirty)
+        if (isDirtyCheckEnabled() && isDirty)
         {
             unsavedChangesInfo.setVisible(true);
         } else
@@ -231,7 +235,8 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
     private FormPanelWithSavePoint createFormPanel()
     {
         final FormPanelWithSavePoint panel = new FormPanelWithSavePoint();
-        panel.addClickListener(new InfoBoxResetListener(infoBox));
+        infoBoxResetListener = new InfoBoxResetListener(infoBox);
+        panel.addClickListener(infoBoxResetListener);
         panel.setHeaderVisible(false);
         panel.setBodyBorder(false);
         panel.setWidth(labelWidth + fieldWidth + PANEL_MARGIN);
@@ -279,7 +284,7 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
                 @Override
                 public final void componentSelected(final ButtonEvent ce)
                 {
-                    if (panel.isDirtyForSavePoint())
+                    if (isDirtyCheckEnabled() && panel.isDirtyForSavePoint())
                     {
                         ce.setCancelled(true);
                         new ConfirmationDialog(
@@ -313,7 +318,7 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
                 @Override
                 public final void componentSelected(final ButtonEvent ce)
                 {
-                    if (panel.isDirtyForSavePoint())
+                    if (isDirtyCheckEnabled() && panel.isDirtyForSavePoint())
                     {
                         ce.setCancelled(true);
                         new ConfirmationDialog(
@@ -368,7 +373,8 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
                 @Override
                 public void handleEvent(BaseEvent be)
                 {
-                    if (formPanel.isValid() && formPanel.isDirtyForSavePoint() == false)
+                    if (formPanel.isValid() && isDirtyCheckEnabled()
+                            && formPanel.isDirtyForSavePoint() == false)
                     {
                         be.setCancelled(true);
                         new ConfirmationDialog(messageProvider
@@ -429,7 +435,7 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
         protected abstract String createSuccessfullRegistrationInfo(T result);
 
         @Override
-        public final void finishOnFailure(final Throwable caught)
+        public void finishOnFailure(final Throwable caught)
         {
             setUploadEnabled(true);
         }
@@ -440,6 +446,8 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
     {
         private final InfoBox infoBox;
 
+        private boolean enabled = true;
+
         public InfoBoxResetListener(final InfoBox infoBox)
         {
             assert infoBox != null : "Unspecified info box.";
@@ -448,7 +456,15 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
 
         private void resetInfoBox()
         {
-            infoBox.reset();
+            if (enabled)
+            {
+                infoBox.reset();
+            }
+        }
+
+        public void setEnabled(boolean enabled)
+        {
+            this.enabled = enabled;
         }
 
         //
@@ -524,7 +540,17 @@ public abstract class AbstractRegistrationForm extends ContentPanel implements
     @Override
     public boolean shouldAskForCloseConfirmation()
     {
-        return formPanel.isDirtyForSavePoint();
+        return isDirtyCheckEnabled() && formPanel.isDirtyForSavePoint();
+    }
+
+    public void setDirtyCheckEnabled(boolean dirtyCheckEnabled)
+    {
+        this.dirtyCheckEnabled = dirtyCheckEnabled;
+    }
+
+    public boolean isDirtyCheckEnabled()
+    {
+        return dirtyCheckEnabled;
     }
 
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/CustomImportForm.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/CustomImportForm.java
index 12973009792..20bbe63512f 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/CustomImportForm.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/CustomImportForm.java
@@ -20,6 +20,7 @@ import static ch.systemsx.cisd.openbis.generic.client.web.client.application.fra
 
 import com.extjs.gxt.ui.client.event.BaseEvent;
 import com.extjs.gxt.ui.client.event.Events;
+import com.extjs.gxt.ui.client.event.FormEvent;
 import com.extjs.gxt.ui.client.event.Listener;
 import com.extjs.gxt.ui.client.widget.form.Field;
 import com.extjs.gxt.ui.client.widget.form.FileUploadField;
@@ -67,7 +68,11 @@ public class CustomImportForm extends AbstractRegistrationForm
             CustomImport customImport)
     {
         super(viewContext, id);
+
+        saveButton.setText(messageProvider.getMessage(Dict.BUTTON_UPLOAD));
         setResetButtonVisible(true);
+        setDirtyCheckEnabled(false);
+
         this.sessionKey = id + "-" + customImport.getCode();
         this.customImport = customImport;
         this.viewContext = viewContext;
@@ -90,6 +95,15 @@ public class CustomImportForm extends AbstractRegistrationForm
         }
         addUploadFeatures(sessionKey);
 
+        formPanel.addListener(Events.BeforeSubmit, new Listener<FormEvent>()
+            {
+                @Override
+                public void handleEvent(FormEvent be)
+                {
+                    infoBox.displayProgress(messageProvider.getMessage(Dict.PROGRESS_UPLOADING));
+                }
+            });
+
         formPanel.addListener(Events.Submit, new FormPanelListener(infoBox)
             {
                 @Override
@@ -101,24 +115,37 @@ public class CustomImportForm extends AbstractRegistrationForm
                 @Override
                 protected void onSuccessfullUpload()
                 {
+                    infoBox.displayProgress(messageProvider.getMessage(Dict.PROGRESS_PROCESSING));
+
                     CustomImportForm.this.viewContext.getCommonService().performCustomImport(
                             sessionKey,
                             CustomImportForm.this.customImport.getCode(),
                             new AbstractRegistrationCallback<String>(
                                     CustomImportForm.this.viewContext)
                                 {
+                                    @Override
+                                    protected void process(String result)
+                                    {
+                                        super.process(result);
+                                        resetPanel();
+                                    }
+
                                     @Override
                                     protected String createSuccessfullRegistrationInfo(String result)
                                     {
                                         return result
                                                 + " succesfully uploaded to the datastore server.";
                                     }
+
+                                    @Override
+                                    public void finishOnFailure(Throwable caught)
+                                    {
+                                        super.finishOnFailure(caught);
+                                        resetPanel();
+                                    }
+
                                 });
-                    for (FileUploadField field : fileFieldsManager.getFields())
-                    {
-                        field.clear();
-                    }
-                    setUploadEnabled();
+
                 }
             });
     }
@@ -265,6 +292,13 @@ public class CustomImportForm extends AbstractRegistrationForm
                 .getName());
     }
 
+    @Override
+    protected void setUploadEnabled(boolean enabled)
+    {
+        super.setUploadEnabled(enabled);
+        infoBoxResetListener.setEnabled(enabled);
+    }
+
     private void openUrl(String url)
     {
         Window.open(url, "_blank", "");
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/IInfoHandler.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/IInfoHandler.java
index 9175f744d43..e8366ecf918 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/IInfoHandler.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/IInfoHandler.java
@@ -18,7 +18,7 @@ package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.widget
 
 /**
  * Handles info and error texts.
- *
+ * 
  * @author Franz-Josef Elmer
  */
 public interface IInfoHandler
@@ -34,4 +34,9 @@ public interface IInfoHandler
      */
     public abstract void displayInfo(final String text);
 
+    /**
+     * Display given <var>text</var> as <i>progress</i> text.
+     */
+    public abstract void displayProgress(final String text);
+
 }
\ No newline at end of file
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/InfoBox.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/InfoBox.java
index 4a78bef7e3a..6183bc03eeb 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/InfoBox.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/InfoBox.java
@@ -18,6 +18,7 @@ package ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.widget
 
 import com.extjs.gxt.ui.client.widget.Html;
 import com.extjs.gxt.ui.client.widget.MessageBox;
+import com.google.gwt.user.client.Timer;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
 import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;
 
@@ -42,6 +43,8 @@ public final class InfoBox extends Html implements IInfoHandler
 
     private static final String WHITE = "#ffffff";
 
+    private Timer timer;
+
     /**
      * Default constructor with {@link HasHorizontalAlignment#ALIGN_CENTER}.
      */
@@ -77,11 +80,44 @@ public final class InfoBox extends Html implements IInfoHandler
         display(text, Type.INFO);
     }
 
+    /**
+     * Display given <var>text</var> as <i>progress</i> text.
+     */
+    @Override
+    public final void displayProgress(final String text)
+    {
+        display(text, Type.PROGRESS);
+
+        timer = new Timer()
+            {
+                String dots = "";
+
+                @Override
+                public void run()
+                {
+                    if (dots.length() < 5)
+                    {
+                        dots += ".";
+                    } else
+                    {
+                        dots = "";
+                    }
+                    setHtml(text + dots);
+                }
+            };
+        timer.scheduleRepeating(500);
+    }
+
     /**
      * Displays given <var>text</var> of given <var>type</var>.
      */
     public final void display(final String text, final Type type)
     {
+        if (timer != null)
+        {
+            timer.cancel();
+            timer = null;
+        }
         if (StringUtils.isBlank(text) == false)
         {
             setStyleAttribute("color", "#000000");
@@ -101,6 +137,11 @@ public final class InfoBox extends Html implements IInfoHandler
      */
     public final void reset()
     {
+        if (timer != null)
+        {
+            timer.cancel();
+            timer = null;
+        }
         setStyleAttribute("backgroundColor", WHITE);
         setStyleAttribute("borderColor", WHITE);
         // Make placeholder text invisible.
@@ -115,7 +156,7 @@ public final class InfoBox extends Html implements IInfoHandler
     private static enum Type
     {
 
-        ERROR("#f6cece", "#f5a9a9"), INFO("#cef6ce", "#a9f5a9");
+        ERROR("#f6cece", "#f5a9a9"), INFO("#cef6ce", "#a9f5a9"), PROGRESS("#cef6ce", "#a9f5a9");
 
         private final String backgroundColor;
 
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/PopupDialogBasedInfoHandler.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/PopupDialogBasedInfoHandler.java
index b371bb19f88..388989b8b53 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/PopupDialogBasedInfoHandler.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/widget/PopupDialogBasedInfoHandler.java
@@ -20,23 +20,29 @@ import com.extjs.gxt.ui.client.widget.MessageBox;
 
 /**
  * Info handler which shows info in a popup dialog.
- *
+ * 
  * @author Franz-Josef Elmer
  */
 public class PopupDialogBasedInfoHandler implements IInfoHandler
 {
     public static final IInfoHandler INSTANCE = new PopupDialogBasedInfoHandler();
-    
+
     @Override
     public void displayInfo(String text)
     {
         MessageBox.info("Info", text, null);
     }
-    
+
     @Override
     public void displayError(String text)
     {
         MessageBox.alert("Error", text, null);
     }
 
+    @Override
+    public void displayProgress(String text)
+    {
+        MessageBox.alert("Progress", text, null);
+    }
+
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/public/common-dictionary.js b/openbis/source/java/ch/systemsx/cisd/openbis/public/common-dictionary.js
index 2555b754104..dd5c05b7689 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/public/common-dictionary.js
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/public/common-dictionary.js
@@ -153,6 +153,7 @@ var common = {
   
   button_add: "Add {0}",
   button_save: "Save",
+  button_upload: "Upload",
   button_save_and_upload: "Save + Upload Data Set",
   button_choose: "Choose",
   button_cancel: "Cancel",
@@ -892,7 +893,13 @@ warning_no_script_message: "No script provided",
 //
 last_visits: "Last Visited Places",
 clear: "Clear",
- 
+
+//
+// Progress
+//
+progress_uploading: "Uploading",
+progress_processing: "Processing",
+
  // LAST LINE: KEEP IT AT THE END
   lastline: "" // we need a line without a comma
 };
-- 
GitLab