FELIX-151

Minor bug correction : the confirm dialog containing "Do you want ... old log of ..." is now modal.
Gateway selection is now more stable regarding the load and unload of NodePanel plugins.



git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@465163 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/mosgi.console.component/src/main/java/org/apache/felix/mosgi/console/component/RemoteLogger_jtree.java b/mosgi.console.component/src/main/java/org/apache/felix/mosgi/console/component/RemoteLogger_jtree.java
index 839489b..f18c23f 100644
--- a/mosgi.console.component/src/main/java/org/apache/felix/mosgi/console/component/RemoteLogger_jtree.java
+++ b/mosgi.console.component/src/main/java/org/apache/felix/mosgi/console/component/RemoteLogger_jtree.java
@@ -18,17 +18,35 @@
 
 import org.apache.felix.mosgi.console.ifc.CommonPlugin;
 import org.apache.felix.mosgi.console.ifc.Plugin;
-//import org.apache.felix.mosgi.console.component.JtreeCellRenderer;
 import org.apache.felix.mosgi.console.component.MyTree;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Bundle;
 
-import javax.swing.table.DefaultTableModel;
-import javax.swing.JTable;
+import java.beans.PropertyChangeEvent;
+
 import javax.swing.JButton;
+import javax.swing.JFrame;
 import javax.swing.JPanel;
+import javax.swing.JFileChooser;
+import javax.swing.JPopupMenu;
+import javax.swing.JMenuItem;
 import javax.swing.JScrollPane;
+import javax.swing.JTable;
+import javax.swing.table.DefaultTableCellRenderer;
+import javax.swing.table.DefaultTableModel;
+import javax.swing.JTree;
+import javax.swing.tree.DefaultTreeModel;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.TreePath;
+import javax.swing.JOptionPane;
+
 import java.awt.Component;
+import java.awt.BorderLayout;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.awt.event.ActionEvent;
 
 import javax.management.Notification;
 import javax.management.NotificationListener;
@@ -38,38 +56,17 @@
 
 import java.util.Hashtable;
 import java.util.StringTokenizer;
-
-import java.beans.PropertyChangeEvent;
-
-import java.awt.BorderLayout;
-import java.awt.event.ActionListener;
-import java.awt.event.ActionEvent;
 import java.util.Vector;
+import java.util.Date;
+import java.text.DateFormat;
 
 import java.io.PrintStream;
 import java.io.File;
 import java.io.FileNotFoundException;
-import javax.swing.JFileChooser;
 		
-import java.util.Date;
-import java.text.DateFormat;
 //import java.text.SimpleDateFormat;
-
-import javax.swing.table.DefaultTableCellRenderer;
-import javax.swing.JTree;
-import javax.swing.tree.DefaultTreeModel;
-import javax.swing.JFrame;
-import javax.swing.tree.DefaultMutableTreeNode;
-import javax.swing.tree.TreePath;
-import javax.swing.JOptionPane;
 //import org.osgi.service.prefs.Preferences;
 
-import java.awt.event.MouseListener;
-import java.awt.event.MouseAdapter;
-import java.awt.event.MouseEvent;
-import javax.swing.JPopupMenu;
-import javax.swing.JMenuItem;
-
 public class RemoteLogger_jtree extends DefaultTreeModel implements CommonPlugin, NotificationListener{
 
   private MyTree logTree;
@@ -92,7 +89,7 @@
     JtreeCellRenderer treeCellRenderer=new JtreeCellRenderer(bdlCtx);
     this.logTree.setCellRenderer(treeCellRenderer);
     this.logTree.setLargeModel(true);
-    this.logTree.setToggleClickCount(2); 
+    //this.logTree.setToggleClickCount(2); 
     this.logTree.setRootVisible(false);
     // this create an invisible tree even if I use *expand* so...
     // I use expand after the first insert into the tree
@@ -101,9 +98,9 @@
       public void mousePressed(MouseEvent e) {
         int selRow = logTree.getRowForLocation(e.getX(), e.getY());
         selPath = logTree.getPathForLocation(e.getX(), e.getY());
-        if(selRow != -1 & e.getButton()>1) {
+        if ( selRow!=-1 & e.getButton()>1 ) {
 	  String nodeString="\""+((DefaultMutableTreeNode) selPath.getLastPathComponent()).getUserObject()+"\"";
-	  JMenuItem itemm=new JMenuItem("Delete logs "+nodeString+"");
+	  JMenuItem itemm=new JMenuItem("Delete logs "+nodeString);
 	  itemm.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
 	      removeNodeFromParent((DefaultMutableTreeNode) selPath.getLastPathComponent());
@@ -150,16 +147,11 @@
           //System.out.println("RemoteLogger_jtree add a notification listener on this Remote Logger : "+mbs);
 	  ((MBeanServerConnection)e.getNewValue()).addNotificationListener(new ObjectName("OSGI:name=Remote Logger"), this, null, e.getOldValue());
           nodes.put(mbs, "ok");
-          if(JOptionPane.showConfirmDialog(null,"Do you want \""+this.getName()+"\" ask old log to this gateway :\n"+((String) e.getOldValue())+" ?")==JOptionPane.YES_OPTION) {
-            try {
-	      //System.out.println("   => mosgi.console.component.RemoteLoger_jtree gonna ask the oldLog");
-	      mbs.invoke(new ObjectName("OSGI:name=Remote Logger"), "sendOldLog", new Object[]{}, new String[]{});
-	    } catch (Exception ee) {
-	      System.out.println("   => mosgi.console.component.RemoteLoger_jtree : getOldLog : "+ee);
-	    }
+          if (JOptionPane.showConfirmDialog(jp,"Do you want \""+this.getName()+"\" ask old log to this gateway :\n"+((String) e.getOldValue())+" ?")==JOptionPane.YES_OPTION) {
+	    mbs.invoke(new ObjectName("OSGI:name=Remote Logger"), "sendOldLog", new Object[]{}, new String[]{});
 	  }
         }
-      }catch(Exception ex){
+      } catch(Exception ex){
         ex.printStackTrace();
       }
     }
@@ -177,7 +169,7 @@
     }
     if (theNode==null){ // create the node
       theNode=new DefaultMutableTreeNode(nodeToCreateAndGet);
-      // Unable to set tree expand whithout a first node:
+      // Unable to set tree expand whithout a first node so :
       if (rootNode.getChildCount()==0){
         this.insertNodeInto(theNode, parent, 0);
         logTree.expandPath(new TreePath(rootNode.getPath()));
@@ -204,8 +196,7 @@
     String date="??/??/??";
     if (ts==0) {
       isOldLog=true;
-    }
-    if (!isOldLog){
+    } else {
       Date timeDate=new Date(ts);
       //DateFormat dateFormat = new SimpleDateFormat("hh'h'mm dd-MM-yy");
       DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM); // use local date format
@@ -219,17 +210,13 @@
     String state=""+eventName.get(new Integer((int) Integer.parseInt(st.nextToken())));
     String lvl=st.nextToken();
     String msg=st.nextToken();
-    // Get and maybe create parents nodes : ip, ref, idname
+    // Get and maybe create parents nodes : ip / ref / idname
     DefaultMutableTreeNode dmtn_ip=createIfNeed(ip, rootNode, isOldLog);
     DefaultMutableTreeNode dmtn_ref=createIfNeed(ref, dmtn_ip, isOldLog);
     DefaultMutableTreeNode dmtn_idname=createIfNeed(idname, dmtn_ref, isOldLog);
     // insert the leaf with message under id/ref/idname
     DefaultMutableTreeNode dmtn=new DefaultMutableTreeNode(date+" | "+time+" | "+state+" | "+lvl+" | "+msg,false);
-    if (isOldLog){
-      this.insertNodeInto(dmtn, dmtn_idname, dmtn_idname.getChildCount());
-    } else{
-      this.insertNodeInto(dmtn, dmtn_idname, 0);
-    }
+    this.insertNodeInto(dmtn, dmtn_idname, (isOldLog)?dmtn_idname.getChildCount():0);
     this.reload(dmtn_idname);
   }
   
diff --git a/mosgi.console.gui/src/main/java/org/apache/felix/mosgi/console/gui/NodePanel.java b/mosgi.console.gui/src/main/java/org/apache/felix/mosgi/console/gui/NodePanel.java
index e59304f..c583b1d 100644
--- a/mosgi.console.gui/src/main/java/org/apache/felix/mosgi/console/gui/NodePanel.java
+++ b/mosgi.console.gui/src/main/java/org/apache/felix/mosgi/console/gui/NodePanel.java
@@ -18,6 +18,12 @@
 */
 package org.apache.felix.mosgi.console.gui;
 
+import org.apache.felix.mosgi.console.ifc.Plugin;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.ServiceReference;
+
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.IOException;
@@ -25,26 +31,21 @@
 import java.rmi.server.RMIClassLoader;
 import java.rmi.server.RMIClassLoaderSpi;
 import java.security.SecureClassLoader;
-import java.util.ArrayList;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.Vector;
 import java.util.Set;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.ChangeEvent;
 import javax.swing.JTabbedPane;
+import java.lang.IllegalStateException;
 import javax.management.AttributeNotFoundException;
 import javax.management.InstanceNotFoundException;
-//import javax.management.MalformedObjectNameException;
 import javax.management.MBeanException;
 import javax.management.MBeanServerConnection;
 import javax.management.ObjectName;
 import javax.management.ReflectionException;
-import org.apache.felix.mosgi.console.ifc.Plugin;
-
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.ServiceReference;
-import javax.swing.event.ChangeListener;
-import javax.swing.event.ChangeEvent;
+//import javax.management.MalformedObjectNameException;
 
 public class NodePanel extends JTabbedPane implements PropertyChangeListener, ChangeListener {
   //private String repo;
@@ -73,6 +74,7 @@
   }
 
   public void propertyChange(PropertyChangeEvent event) {
+    //System.out.println("   PCE : "+event.getPropertyName());
     if (event.getPropertyName().equals(Plugin.PLUGIN_ADDED)) {
       Plugin cp=(Plugin) event.getNewValue();
       this.add(cp.getName(), cp.getGUI());
@@ -81,37 +83,33 @@
       this.pluginList.put(cp.pluginLocation(), cp); 
     }else if(event.getPropertyName().equals(Plugin.PLUGIN_REMOVED)) {
       Plugin cp = (Plugin) event.getNewValue();
+      String cpLoc=cp.pluginLocation();
       this.remove(cp.getGUI());
-      this.a.removePropertyChangeListener(cp);
-      //	this.pluginList.remove(cp.pluginLocation());
+      //this.a.removePropertyChangeListener(cp);
+      //this.pluginList.remove(cpLoc);
     }else if(event.getPropertyName().equals(Plugin.EMPTY_NODE)) {
-      System.out.println("******* Debug No node selected");
+      //System.out.println("******* Debug No node selected");
       this.clean();
     }else if (event.getPropertyName().equals(Plugin.NEW_NODE_SELECTED)) {
-      //System.out.println("Event NEW_NODE_SELECTED");
       /* Update the tabs (Plugin) in the JTabbedPane (NodePanel) */
       MBeanServerConnection mbsc = (MBeanServerConnection)event.getNewValue();
       try {
 	this.clean();
         Set ons = mbsc.queryNames( null, null );
-        String msg="";
         for( Iterator i=ons.iterator(); i.hasNext(); ) {
 	  ObjectName name = ( ObjectName )i.next();
-          msg="Queried name: "+name.toString();
           if ( "TabUI".equals(name.getDomain()) ) {
-            msg+=" New tab: "+name.toString();
 	    /* Get the plugin implementation via a bundle */
 	    String tabBundle = (String) mbsc.getAttribute(name, "BundleName");
-            msg+=" Bundle name for current Plugin: "+tabBundle;
 	    if (tabBundle!=null){
               Plugin p = (Plugin) this.pluginList.get(tabBundle);
               if (p == null){
-  	          Bundle b = m_context.installBundle(tabBundle);
-		  b.start();
-                  System.out.println(msg+" Bundle started");
-                  //Thread.sleep(5000);
+  	        Bundle b = m_context.installBundle(tabBundle);
+	        b.start();
+                System.out.println(" - Bundle started: \""+name.toString()+"\" - "+tabBundle);
+                //Thread.sleep(5000);
               }else{
-                System.out.println(msg+" Register service plugin: " + p);
+                System.out.println(" - Register service plugin: "+p);
                 p.registerServicePlugin();
               }
 
@@ -151,8 +149,8 @@
 //            Plugin tab = (Plugin)tabObj;
               /* register the tab on the JTabbedPane */
 //            this.add(tab.getName(), tab.getGUI());
-            }else{
-	      System.out.println(msg+" No "+tabBundle+" property defined. I cannot install "+tabBundle+" tab");
+            } else{
+	      System.out.println(" - No bundleName attribute defined for \""+name.toString()+"\". I cannot install tab");
             }
           }
         }
@@ -184,14 +182,19 @@
 
   private void clean(){
     this.removeAll();
+    Vector pluginList_tmp=new Vector();
     for ( Iterator i=pluginList.keySet().iterator(); i.hasNext();) {
       Object o=i.next();
-      try {
-        ((Plugin) pluginList.get(o)).unregisterServicePlugin();
-      } catch(Exception e){
-        System.out.println("INFO Something went wrong when unregistering the Plugin. Please control the beavior of the unregisterServicePlugin function of this tab"+o);
-      }
+      pluginList_tmp.add(o);
     }
+    for ( int i = 0 ; i < pluginList_tmp.size() ; i++) {
+      Plugin p=(Plugin) pluginList.get(pluginList_tmp.elementAt(i));
+      try {
+        p.unregisterServicePlugin();
+      } catch (Exception ex) {
+	//System.out.println("\""+p.getName()+"\" : "+ex);
+      }
+    } 
   }
 
 }