From 7bb144d1607a73e58d168f79e2de48bc69a9f839 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Thu, 05 Feb 2009 13:05:53 +0000
Subject: [PATCH] Update the code so that the scroll positions are maintained after the contents of the panels are updated.  This way, even if configuration or monitoring information are changed, there are no sudden changes of the scroll position which was quite annoying.

---
 opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java |   86 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java b/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java
index 7f80f0c..84854a2 100644
--- a/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java
+++ b/opends/src/guitools/org/opends/guitools/controlpanel/util/Utilities.java
@@ -31,9 +31,11 @@
 
 import java.awt.Color;
 import java.awt.Component;
+import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Image;
+import java.awt.Point;
 import java.awt.Toolkit;
 import java.awt.Window;
 import java.awt.event.MouseAdapter;
@@ -73,6 +75,7 @@
 import javax.swing.JTextArea;
 import javax.swing.JTextField;
 import javax.swing.SwingConstants;
+import javax.swing.SwingUtilities;
 import javax.swing.border.Border;
 import javax.swing.border.EmptyBorder;
 import javax.swing.border.EtchedBorder;
@@ -2101,4 +2104,87 @@
     label.setIcon(requiredIcon);
     label.setHorizontalTextPosition(SwingConstants.LEADING);
   }
+
+
+  /**
+   * Updates the scrolls with the provided points.
+   * This method uses SwingUtilities.invokeLater so it can be also called
+   * outside the event thread.
+   * @param pos the scroll and points.
+   */
+  public static void updateViewPositions(final ViewPositions pos)
+  {
+    SwingUtilities.invokeLater(new Runnable()
+    {
+      public void run()
+      {
+        for (int i=0; i<pos.size(); i++)
+        {
+          pos.getScrollPane(i).getViewport().setViewPosition(pos.getPoint(i));
+        }
+      }
+    });
+  }
+
+  /**
+   * Gets the view positions object for the provided component.  This includes
+   * all the scroll panes inside the provided component.
+   * @param comp the component.
+   * @return the view positions for the provided component.
+   */
+  public static ViewPositions getViewPositions(Component comp)
+  {
+    ViewPositions pos = new ViewPositions();
+    if (comp instanceof Container)
+    {
+      updateContainedViewPositions((Container)comp, pos);
+    }
+    return pos;
+  }
+
+  /**
+   * Returns the scrolpane where the provided component is contained.
+   * <CODE>null</CODE> if the component is not contained in any scrolpane.
+   * @param comp the component.
+   * @return the scrolpane where the provided component is contained.
+   */
+  public static JScrollPane getContainingScroll(Component comp)
+  {
+    JScrollPane scroll = null;
+    Container parent = comp.getParent();
+    while ((scroll == null) && (parent != null))
+    {
+      if (parent instanceof JScrollPane)
+      {
+        scroll = (JScrollPane)parent;
+      }
+      else
+      {
+        parent = parent.getParent();
+      }
+    }
+    return scroll;
+  }
+
+  private static void updateContainedViewPositions(Container comp,
+      ViewPositions pos)
+  {
+    if (comp instanceof JScrollPane)
+    {
+      JScrollPane scroll = (JScrollPane)comp;
+      Point p = scroll.getViewport().getViewPosition();
+      pos.add(scroll, p);
+    }
+    else
+    {
+      for (int i=0; i<comp.getComponentCount(); i++)
+      {
+        Component child = comp.getComponent(i);
+        if (child instanceof Container)
+        {
+          updateContainedViewPositions((Container)child, pos);
+        }
+      }
+    }
+  }
 }

--
Gitblit v1.10.0