From f6bd083ffbdc77820009c03019ca2abcf502e3d4 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Thu, 26 Mar 2009 10:26:00 +0000
Subject: [PATCH] Fix for issue 3555 (Control panel : Manage entries menu does not refresh the view off the DIT)

---
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/AbstractBrowseEntriesPanel.java  |  124 ++++++++++++++++++-
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedListener.java |   42 +++++++
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/ImportLDIFPanel.java             |   20 +++
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java     |   40 ++++++
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/RestorePanel.java                |   19 +++
 opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedEvent.java    |   62 ++++++++++
 6 files changed, 294 insertions(+), 13 deletions(-)

diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java
index f0645a7..d76e9b5 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/datamodel/ControlPanelInfo.java
@@ -44,6 +44,8 @@
 import org.opends.admin.ads.util.ConnectionUtils;
 import org.opends.guitools.controlpanel.browser.IconPool;
 import org.opends.guitools.controlpanel.browser.LDAPConnectionPool;
+import org.opends.guitools.controlpanel.event.BackendPopulatedEvent;
+import org.opends.guitools.controlpanel.event.BackendPopulatedListener;
 import org.opends.guitools.controlpanel.event.BackupCreatedEvent;
 import org.opends.guitools.controlpanel.event.BackupCreatedListener;
 import org.opends.guitools.controlpanel.event.ConfigChangeListener;
@@ -101,6 +103,9 @@
   private LinkedHashSet<BackupCreatedListener> backupListeners =
     new LinkedHashSet<BackupCreatedListener>();
 
+  private LinkedHashSet<BackendPopulatedListener> backendPopulatedListeners =
+    new LinkedHashSet<BackendPopulatedListener>();
+
   private LinkedHashSet<IndexModifiedListener> indexListeners =
     new LinkedHashSet<IndexModifiedListener>();
 
@@ -364,6 +369,20 @@
   }
 
   /**
+   * Informs that a set of backends have been populated.  The method will notify
+   * to all the backend populated listeners.
+   * @param backends the populated backends.
+   */
+  public void backendPopulated(Set<BackendDescriptor> backends)
+  {
+    BackendPopulatedEvent ev = new BackendPopulatedEvent(backends);
+    for (BackendPopulatedListener listener : backendPopulatedListeners)
+    {
+      listener.backendPopulated(ev);
+    }
+  }
+
+  /**
    * Informs that an index has been modified.  The method will notify to all
    * the index listeners that an index has been modified.
    * @param modifiedIndex the modified index.
@@ -682,6 +701,27 @@
   }
 
   /**
+   * Adds a backend populated listener.
+   * @param listener the listener.
+   */
+  public void addBackendPopulatedListener(BackendPopulatedListener listener)
+  {
+    backendPopulatedListeners.add(listener);
+  }
+
+  /**
+   * Removes a backend populated listener.
+   * @param listener the listener.
+   * @return <CODE>true</CODE> if the listener is found and <CODE>false</CODE>
+   * otherwise.
+   */
+  public boolean removeBackendPopulatedListener(
+      BackendPopulatedListener listener)
+  {
+    return backendPopulatedListeners.remove(listener);
+  }
+
+  /**
    * Adds an index modification listener.
    * @param listener the listener.
    */
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedEvent.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedEvent.java
new file mode 100644
index 0000000..0cfa8cc
--- /dev/null
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedEvent.java
@@ -0,0 +1,62 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2009 Sun Microsystems, Inc.
+ */
+
+package org.opends.guitools.controlpanel.event;
+
+import java.util.Collections;
+import java.util.Set;
+
+import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
+
+
+/**
+ * The event used to notify that a backend has been populated (using import
+ * or restore for example).
+ *
+ */
+public class BackendPopulatedEvent
+{
+  private Set<BackendDescriptor> backends;
+
+  /**
+   * The constructor of the event.
+   * @param backends the set of populated backends.
+   */
+  public BackendPopulatedEvent(Set<BackendDescriptor> backends)
+  {
+    this.backends = Collections.unmodifiableSet(backends);
+  }
+
+  /**
+   * Returns the set of populated backends.
+   * @return the set of populated backends.
+   */
+  public Set<BackendDescriptor> getBackends()
+  {
+    return backends;
+  }
+}
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedListener.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedListener.java
new file mode 100644
index 0000000..2dc7023
--- /dev/null
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/event/BackendPopulatedListener.java
@@ -0,0 +1,42 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License, Version 1.0 only
+ * (the "License").  You may not use this file except in compliance
+ * with the License.
+ *
+ * You can obtain a copy of the license at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE
+ * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at
+ * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
+ * add the following below this CDDL HEADER, with the fields enclosed
+ * by brackets "[]" replaced with your own identifying information:
+ *      Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ *
+ *
+ *      Copyright 2009 Sun Microsystems, Inc.
+ */
+
+package org.opends.guitools.controlpanel.event;
+
+/**
+ * The listeners that receive notifications when a set of backends are
+ * populated.
+ *
+ */
+public interface BackendPopulatedListener
+{
+  /**
+   * Method called when a set of backends are populated.
+   * @param ev the event notifying that a set of backends have been populated.
+   */
+  public void backendPopulated(BackendPopulatedEvent ev);
+}
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/AbstractBrowseEntriesPanel.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/AbstractBrowseEntriesPanel.java
index baf3b9d..53888ff 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/AbstractBrowseEntriesPanel.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/AbstractBrowseEntriesPanel.java
@@ -72,6 +72,7 @@
 import javax.swing.event.TreeModelListener;
 import javax.swing.tree.DefaultMutableTreeNode;
 import javax.swing.tree.DefaultTreeModel;
+import javax.swing.tree.TreeNode;
 import javax.swing.tree.TreePath;
 
 import org.opends.admin.ads.util.ApplicationTrustManager;
@@ -84,6 +85,8 @@
 import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
 import org.opends.guitools.controlpanel.datamodel.IndexDescriptor;
 import org.opends.guitools.controlpanel.datamodel.ServerDescriptor;
+import org.opends.guitools.controlpanel.event.BackendPopulatedEvent;
+import org.opends.guitools.controlpanel.event.BackendPopulatedListener;
 import org.opends.guitools.controlpanel.event.BrowserEvent;
 import org.opends.guitools.controlpanel.event.BrowserEventListener;
 import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
@@ -109,6 +112,7 @@
  *
  */
 public abstract class AbstractBrowseEntriesPanel extends StatusGenericPanel
+implements BackendPopulatedListener
 {
   private JComboBox baseDNs;
   /**
@@ -239,6 +243,7 @@
     }
     super.setInfo(info);
     treePane.setInfo(info);
+    info.addBackendPopulatedListener(this);
   }
 
   /**
@@ -707,28 +712,26 @@
       {
         treePane.getTree().setRootVisible(displayAll);
         treePane.getTree().setShowsRootHandles(!displayAll);
-        boolean isBaseDN = false;
+        boolean added = false;
         for (BackendDescriptor backend :
           getInfo().getServerDescriptor().getBackends())
         {
           for (BaseDNDescriptor baseDN : backend.getBaseDns())
           {
+            boolean isBaseDN = false;
             if ((theDN != null) && baseDN.getDn().equals(theDN))
             {
               isBaseDN = true;
             }
             String dn = Utilities.unescapeUtf8(baseDN.getDn().toString());
-            if (displayAll)
+            if (displayAll || isBaseDN)
             {
               controller.addSuffix(dn, null);
-            }
-            else if (s.equals(dn))
-            {
-              controller.addSuffix(dn, null);
+              added = true;
             }
           }
         }
-        if (!isBaseDN && !displayAll)
+        if (!added && !displayAll)
         {
           BasicNode rootNode =
             (BasicNode)controller.getTree().getModel().getRoot();
@@ -840,6 +843,103 @@
    */
   protected abstract Component createMainPanel();
 
+
+  /**
+   * {@inheritDoc}
+   */
+  public void backendPopulated(BackendPopulatedEvent ev)
+  {
+    if (controller.getConfigurationConnection() != null)
+    {
+      boolean displayAll = false;
+      boolean errorOccurred = false;
+      DN theDN = null;
+      String s = getBaseDN();
+      if (s != null)
+      {
+        displayAll = s.equals(ALL_BASE_DNS);
+        if (!displayAll)
+        {
+          try
+          {
+            theDN = DN.decode(s);
+          }
+          catch (Throwable t)
+          {
+            errorOccurred = true;
+          }
+        }
+      }
+      else
+      {
+        errorOccurred = true;
+      }
+      if (!errorOccurred)
+      {
+        treePane.getTree().setRootVisible(displayAll);
+        treePane.getTree().setShowsRootHandles(!displayAll);
+        BasicNode rootNode =
+          (BasicNode)controller.getTree().getModel().getRoot();
+        boolean isSubordinate = false;
+        for (BackendDescriptor backend : ev.getBackends())
+        {
+          for (BaseDNDescriptor baseDN : backend.getBaseDns())
+          {
+            boolean isBaseDN = false;
+            if ((theDN != null) && baseDN.getDn().equals(theDN))
+            {
+              isBaseDN = true;
+            }
+            else if ((theDN != null) && baseDN.getDn().isAncestorOf(theDN))
+            {
+              isSubordinate = true;
+            }
+            String dn = Utilities.unescapeUtf8(baseDN.getDn().toString());
+            if (displayAll || isBaseDN)
+            {
+              try
+              {
+                if (!controller.hasSuffix(dn))
+                {
+                  controller.addSuffix(dn, null);
+                }
+                else
+                {
+                  int index = controller.findChildNode(rootNode, dn);
+                  if (index >= 0)
+                  {
+                    TreeNode node = rootNode.getChildAt(index);
+                    if (node != null)
+                    {
+                      TreePath path = new TreePath(
+                          controller.getTreeModel().getPathToRoot(node));
+                      controller.startRefresh(
+                          controller.getNodeInfoFromPath(path));
+                    }
+                  }
+                }
+              }
+              catch (IllegalArgumentException iae)
+              {
+                // The suffix node exists but is not a suffix node.
+                // Simply log a message.
+                LOG.log(Level.WARNING, "Suffix: "+dn+
+                    " added as a non suffix node. Exception: "+iae, iae);
+              }
+            }
+          }
+        }
+        if (isSubordinate)
+        {
+          if (controller.findChildNode(rootNode, s) == -1)
+          {
+            controller.addNodeUnderRoot(s);
+          }
+        }
+      }
+    }
+  }
+
   /**
    * {@inheritDoc}
    */
@@ -1322,12 +1422,13 @@
           treePane.getTree().setShowsRootHandles(!displayAll);
           if (s != null)
           {
-            boolean isBaseDN = false;
+            boolean added = false;
             for (BackendDescriptor backend :
               getInfo().getServerDescriptor().getBackends())
             {
               for (BaseDNDescriptor baseDN : backend.getBaseDns())
               {
+                boolean isBaseDN = false;
                 String dn = Utilities.unescapeUtf8(baseDN.getDn().toString());
                 if ((theDN != null) && baseDN.getDn().equals(theDN))
                 {
@@ -1337,11 +1438,12 @@
                 {
                   try
                   {
-                    if (!controller.hasSuffix(dn))
+                    if (displayAll || isBaseDN)
                     {
-                      if (displayAll || isBaseDN)
+                      if (!controller.hasSuffix(dn))
                       {
                         controller.addSuffix(dn, null);
+                        added = true;
                       }
                     }
                   }
@@ -1354,7 +1456,7 @@
                   }
                 }
               }
-              if (!isBaseDN && !displayAll)
+              if (!added && !displayAll)
               {
                 BasicNode rootNode =
                   (BasicNode)controller.getTree().getModel().getRoot();
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/ImportLDIFPanel.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/ImportLDIFPanel.java
index 19bf807..5fa2d5b 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/ImportLDIFPanel.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/ImportLDIFPanel.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008 Sun Microsystems, Inc.
+ *      Copyright 2008-2009 Sun Microsystems, Inc.
  */
 
 package org.opends.guitools.controlpanel.ui;
@@ -53,6 +53,7 @@
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentListener;
 
+import org.opends.guitools.controlpanel.datamodel.BackendDescriptor;
 import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
 import org.opends.guitools.controlpanel.event.BrowseActionListener;
 import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
@@ -748,6 +749,23 @@
         lastException = t;
         state = State.FINISHED_WITH_ERROR;
       }
+      HashSet<BackendDescriptor> backends = new HashSet<BackendDescriptor>();
+      for (BackendDescriptor backend :
+        getInfo().getServerDescriptor().getBackends())
+      {
+        for (String backendID : getBackends())
+        {
+          if (backendID.equalsIgnoreCase(backend.getBackendID()))
+          {
+            backends.add(backend);
+            break;
+          }
+        }
+      }
+      if (!backends.isEmpty())
+      {
+        getInfo().backendPopulated(backends);
+      }
     }
 
     /**
diff --git a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/RestorePanel.java b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/RestorePanel.java
index b4732cf..7b712ae 100644
--- a/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/RestorePanel.java
+++ b/opendj-sdk/opends/src/guitools/org/opends/guitools/controlpanel/ui/RestorePanel.java
@@ -22,7 +22,7 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2008 Sun Microsystems, Inc.
+ *      Copyright 2008-2009 Sun Microsystems, Inc.
  */
 
 package org.opends.guitools.controlpanel.ui;
@@ -416,6 +416,23 @@
         lastException = t;
         state = State.FINISHED_WITH_ERROR;
       }
+      HashSet<BackendDescriptor> backends = new HashSet<BackendDescriptor>();
+      for (BackendDescriptor backend :
+        getInfo().getServerDescriptor().getBackends())
+      {
+        for (String backendID : getBackends())
+        {
+          if (backendID.equalsIgnoreCase(backend.getBackendID()))
+          {
+            backends.add(backend);
+            break;
+          }
+        }
+      }
+      if (!backends.isEmpty())
+      {
+        getInfo().backendPopulated(backends);
+      }
     }
 
     /**

--
Gitblit v1.10.0