]> source.dussan.org Git - vaadin-framework.git/commitdiff
bring to front now somewhat supports several updating order of several windows at...
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 20 Dec 2010 16:07:33 +0000 (16:07 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Mon, 20 Dec 2010 16:07:33 +0000 (16:07 +0000)
svn changeset:16587/svn branch:6.5

src/com/vaadin/terminal/gwt/client/ui/VWindow.java
src/com/vaadin/ui/Window.java

index 18c41d2e4a51b67a3a6dd633ed75a9dfbecadb33..68501ae082169ef25ee48b604cf147de72b9b108 100644 (file)
@@ -5,11 +5,12 @@
 package com.vaadin.terminal.gwt.client.ui;
 
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.Set;
 
 import com.google.gwt.core.client.Scheduler;
-import com.google.gwt.core.client.Scheduler.ScheduledCommand;
 import com.google.gwt.event.dom.client.BlurEvent;
 import com.google.gwt.event.dom.client.BlurHandler;
 import com.google.gwt.event.dom.client.DomEvent.Type;
@@ -65,6 +66,8 @@ public class VWindow extends VOverlay implements Container,
 
     private static ArrayList<VWindow> windowOrder = new ArrayList<VWindow>();
 
+    private static boolean orderingDefered;
+
     public static final String CLASSNAME = "v-window";
 
     /**
@@ -172,6 +175,8 @@ public class VWindow extends VOverlay implements Container,
 
     private boolean visibilityChangesDisabled;
 
+    private int bringToFrontSequence = -1;
+
     public VWindow() {
         super(false, false, true); // no autohide, not modal, shadow
         // Different style of shadow for windows
@@ -189,7 +194,7 @@ public class VWindow extends VOverlay implements Container,
         contentPanel.addBlurHandler(this);
     }
 
-    private void bringToFront() {
+    public void bringToFront() {
         int curIndex = windowOrder.indexOf(this);
         if (curIndex + 1 < windowOrder.size()) {
             windowOrder.remove(this);
@@ -201,7 +206,7 @@ public class VWindow extends VOverlay implements Container,
     }
 
     /**
-     * Returns true if window is the topmost window
+     * Returns true if this window is the topmost VWindow
      * 
      * @return
      */
@@ -209,7 +214,7 @@ public class VWindow extends VOverlay implements Container,
         return windowOrder.get(windowOrder.size() - 1).equals(this);
     }
 
-    public void setWindowOrder(int order) {
+    private void setWindowOrder(int order) {
         setZIndex(order + Z_INDEX);
     }
 
@@ -506,19 +511,56 @@ public class VWindow extends VOverlay implements Container,
              * server side.
              */
             contentPanel.focus();
-            /*
-             * Modal windows bring them self the front with scheduleFinally(),
-             * deferred is used here so possible (additional) bringToFront
-             * brings the right modal window to top. If this window is not
-             * modal, scheduleFinally would be ok too.
-             */
-            Scheduler.get().scheduleDeferred(new ScheduledCommand() {
+            bringToFrontSequence = uidl.getIntAttribute("bringToFront");
+            deferOrdering();
+        }
+    }
+
+    /**
+     * Calling this method will defer ordering algorithm, to order windows based
+     * on servers bringToFront and modality instructions. Non changed windows
+     * will be left intact.
+     */
+    private static void deferOrdering() {
+        if (!orderingDefered) {
+            orderingDefered = true;
+            Scheduler.get().scheduleFinally(new Command() {
                 public void execute() {
-                    bringToFront();
+                    doServerSideOrdering();
                 }
             });
         }
+    }
 
+    private static void doServerSideOrdering() {
+        orderingDefered = false;
+        VWindow[] array = windowOrder.toArray(new VWindow[windowOrder.size()]);
+        Arrays.sort(array, new Comparator<VWindow>() {
+            public int compare(VWindow o1, VWindow o2) {
+                /*
+                 * Order by modality, then by bringtofront sequence.
+                 */
+
+                if (o1.vaadinModality && !o2.vaadinModality) {
+                    return 1;
+                } else if (!o1.vaadinModality && o2.vaadinModality) {
+                    return -1;
+                } else if (o1.bringToFrontSequence > o2.bringToFrontSequence) {
+                    return 1;
+                } else if (o1.bringToFrontSequence < o2.bringToFrontSequence) {
+                    return -1;
+                } else {
+                    return 0;
+                }
+            }
+        });
+        for (int i = 0; i < array.length; i++) {
+            VWindow w = array[i];
+            if (w.bringToFrontSequence != -1 || w.vaadinModality) {
+                w.bringToFront();
+                w.bringToFrontSequence = -1;
+            }
+        }
     }
 
     @Override
@@ -704,15 +746,8 @@ public class VWindow extends VOverlay implements Container,
                     + "-modalitycurtain");
             if (isAttached()) {
                 showModalityCurtain();
-                bringToFront();
-            } else {
-                Scheduler.get().scheduleFinally(new Command() {
-                    public void execute() {
-                        // vaadinModality window must on top of others
-                        bringToFront();
-                    }
-                });
             }
+            deferOrdering();
         } else {
             if (modalityCurtain != null) {
                 if (isAttached()) {
index 911d00217aa66fdabd888d821e373b7c59eb453d..b31255cb26a59456cbe1edff231715b3bd13c321 100644 (file)
@@ -566,9 +566,9 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
             target.addAttribute("fixedposition", true);
         }
 
-        if (bringToFront) {
-            target.addAttribute("bringToFront", true);
-            bringToFront = false;
+        if (bringToFront != null) {
+            target.addAttribute("bringToFront", bringToFront.intValue());
+            bringToFront = null;
         }
 
         if (centerRequested) {
@@ -1458,7 +1458,16 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
         return true;
     }
 
-    private boolean bringToFront = false;
+    private Integer bringToFront = null;
+
+    /*
+     * This sequesnce is used to keep the right order of windows if multiple
+     * windows are brought to front in a single changeset. Incremented and saved
+     * by childwindows. If sequence is not used, the order is quite random
+     * (depends on the order getting to dirty list. e.g. which window got
+     * variable changes).
+     */
+    private int bringToFrontSequence = 0;
 
     /**
      * If there are currently several sub windows visible, calling this method
@@ -1483,9 +1492,8 @@ public class Window extends Panel implements URIHandler, ParameterHandler,
                 throw new IllegalStateException(
                         "There are modal windows currently visible, non-modal window cannot be brought to front.");
             }
-            w.bringToFront = false;
         }
-        bringToFront = true;
+        bringToFront = getParent().bringToFrontSequence++;
         requestRepaint();
     }