object browser improved
authorVictor Kirhenshtein <victor@netxms.org>
Fri, 27 Jul 2007 07:07:38 +0000 (07:07 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Fri, 27 Jul 2007 07:07:38 +0000 (07:07 +0000)
src/client/console/nxmc/comm.cpp
src/client/console/nxmc/mainfrm.cpp
src/client/console/plugins/AlarmBrowser/browser.cpp
src/client/console/plugins/AlarmBrowser/view.cpp
src/client/console/plugins/ObjectBrowser/browser.cpp
src/client/console/plugins/ObjectBrowser/object_browser.h
src/client/console/plugins/ObjectBrowser/objview.cpp

index 28680bb..afa6838 100644 (file)
@@ -61,6 +61,9 @@ static void ClientEventHandler(NXC_SESSION session, DWORD nxcEvent, DWORD code,
 {
        switch(nxcEvent)
        {
+      case NXC_EVENT_OBJECT_CHANGED:
+                       PostNetXMSEvent(nxEVT_NXC_OBJECT_CHANGE, code, arg);
+                       break;
                case NXC_EVENT_NOTIFICATION:
                        switch(code)
                        {
index 34d4a2d..fb235d9 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: mainfrm.cpp,v 1.8 2007-07-24 22:34:21 victor Exp $ */
+/* $Id: mainfrm.cpp,v 1.9 2007-07-27 07:07:37 victor Exp $ */
 /* 
 ** NetXMS - Network Management System
 ** Portable management console
@@ -383,7 +383,6 @@ void nxMainFrame::OnViewRefresh(wxCommandEvent &event)
 
 void nxMainFrame::OnAlarmChange(wxCommandEvent &event)
 {
-       wxLogDebug(_T("MAINFRAME: onAlarmChange %d"), event.GetInt());
        // nxMainFrame is a final destination for client library events,
        // so it should destroy dynamic data associated with event
        safe_free(event.GetClientData());
index 85e8e9f..0477e91 100644 (file)
@@ -43,6 +43,7 @@ nxAlarmBrowser::nxAlarmBrowser()
 {
        SetName(_T("alarmbrowser"));
        SetLabel(_T("Alarm Browser"));
+       SetIcon(wxXmlResource::Get()->LoadIcon(_T("icoAlarmView")));
        RegisterUniqueView(_T("alarmbrowser"), this);
 
        m_view = new nxAlarmView(this, _T("/AlarmBrowser"));
index c60b541..7fe90d8 100644 (file)
@@ -142,10 +142,9 @@ bool nxAlarmView::MatchAlarm(NXC_ALARM *alarm)
 {
        if (m_object == NULL)
                return true;
-printf("MATCHING against %s\n", m_object->szName);             
        if (m_object->dwId == alarm->dwSourceObject)
                return true;
-       return NXCIsParent(NXMCGetSession(), m_object->dwId, alarm->dwSourceObject);
+       return NXCIsParent(NXMCGetSession(), m_object->dwId, alarm->dwSourceObject) ? true : false;
 }
 
 
index 3381aea..ee1cc49 100644 (file)
 #include "object_browser.h"
 
 
+//
+// Implementation of dynamic array of tree item references
+//
+
+#include <wx/arrimpl.cpp>
+WX_DEFINE_OBJARRAY(nxTreeItemList);
+
+
 //
 // Event table
 //
@@ -57,6 +65,8 @@ nxObjectBrowser::nxObjectBrowser()
 
        wxCommandEvent event(nxEVT_REFRESH_VIEW);
        AddPendingEvent(event);
+
+       NXMCEvtConnect(nxEVT_NXC_OBJECT_CHANGE, wxCommandEventHandler(nxObjectBrowser::OnObjectChange), this);
 }
 
 
@@ -66,6 +76,7 @@ nxObjectBrowser::nxObjectBrowser()
 
 nxObjectBrowser::~nxObjectBrowser()
 {
+       NXMCEvtDisconnect(nxEVT_NXC_OBJECT_CHANGE, wxCommandEventHandler(nxObjectBrowser::OnObjectChange), this);
        UnregisterUniqueView(_T("objectbrowser"));
 }
 
@@ -119,7 +130,7 @@ void nxObjectBrowser::OnViewRefresh(wxCommandEvent &event)
    // Populate objects' tree
        m_wndTreeCtrl->DeleteAllItems();
        root = m_wndTreeCtrl->AddRoot(_T("[root]"));
-//   m_dwTreeHashSize = 0;
+       ClearObjectItemsHash();
 
    for(i = 0; i < dwNumRootObj; i++)
       AddObjectToTree(ppRootObjects[i], root);
@@ -133,11 +144,24 @@ void nxObjectBrowser::OnViewRefresh(wxCommandEvent &event)
 
 void nxObjectBrowser::AddObjectToTree(NXC_OBJECT *object, wxTreeItemId &root)
 {
-       TCHAR itemText[256];
        wxTreeItemId item;
+       nxObjectItemsHash::iterator it;
+       nxTreeItemList *list;
+       
+       item = m_wndTreeCtrl->AppendItem(root, object->szName, object->iClass, -1, new nxObjectTreeItemData(object));
        
-   CreateTreeItemText(object, itemText);
-       item = m_wndTreeCtrl->AppendItem(root, itemText, object->iClass, -1, new nxObjectTreeItemData(object));
+       // Add item to hash
+       it = m_objectItemsHash.find(object->dwId);
+       if (it != m_objectItemsHash.end())
+       {
+               list = it->second;
+       }
+       else
+       {
+               list = new nxTreeItemList;
+               m_objectItemsHash[object->dwId] = list;
+       }
+       list->Add(item);
 
    // Don't add childs immediatelly to
    // prevent adding millions of items if node has thousands of interfaces in
@@ -146,34 +170,6 @@ void nxObjectBrowser::AddObjectToTree(NXC_OBJECT *object, wxTreeItemId &root)
 }
 
 
-//
-// Create class-depemdent text for tree item
-//
-
-void nxObjectBrowser::CreateTreeItemText(NXC_OBJECT *object, TCHAR *buffer)
-{
-   TCHAR szIpBuffer[32];
-
-   switch(object->iClass)
-   {
-      case OBJECT_SUBNET:
-         _stprintf(buffer, _T("%s [Status: %s]"), object->szName, NXMCGetStatusText(object->iStatus));
-         break;
-      case OBJECT_INTERFACE:
-         if (object->dwIpAddr != 0)
-            _stprintf(buffer, _T("%s [IP: %s/%d Status: %s]"), object->szName, 
-                    IpToStr(object->dwIpAddr, szIpBuffer), 
-                    BitsInMask(object->iface.dwIpNetMask), NXMCGetStatusText(object->iStatus));
-         else
-            _stprintf(buffer, _T("%s [Status: %s]"), object->szName, NXMCGetStatusText(object->iStatus));
-         break;
-      default:
-         _tcscpy(buffer, object->szName);
-         break;
-   }
-}
-
-
 //
 // Handler for object tree item expansion
 //
@@ -212,3 +208,93 @@ void nxObjectBrowser::OnTreeSelChanged(wxTreeEvent &event)
        item = event.GetItem();
        m_wndObjectView->SetObject(((nxObjectTreeItemData *)m_wndTreeCtrl->GetItemData(item))->GetObject());
 }
+
+
+//
+// Handler for object change events
+//
+
+void nxObjectBrowser::OnObjectChange(wxCommandEvent &event)
+{
+       NXC_OBJECT *object = (NXC_OBJECT *)event.GetClientData();
+       nxObjectItemsHash::iterator it;
+       nxTreeItemList *list;
+       size_t i, count;
+
+       wxLogDebug(_T("OBJ CHANGE: %s"), object->szName);
+
+       it = m_objectItemsHash.find(object->dwId);
+       list = (it != m_objectItemsHash.end()) ? it->second : NULL;
+
+       if (object->bIsDeleted)
+       {
+               if (list != NULL)
+               {
+                       count = list->GetCount();
+                       for(i = 0; i < count; i++)
+                               m_wndTreeCtrl->Delete(list->Item(i));
+                       delete list;
+                       m_objectItemsHash.erase(object->dwId);
+               }
+       }
+       else
+       {
+               // Check object's parents
+               if (list != NULL)
+               {
+                       wxTreeItemId item;
+                       NXC_OBJECT *parent;
+                       DWORD j;
+
+                       // Create a copy of object's parent list
+                       DWORD *parentList = (DWORD *)nx_memdup(object->pdwParentList, 
+                                                              sizeof(DWORD) * object->dwNumParents);
+
+                       count = list->GetCount();
+                       for(i = 0; i < count; i++)
+                       {
+            // Check if this item's parent still in object's parents list
+                               item = m_wndTreeCtrl->GetItemParent(list->Item(i));
+                               if (item.IsOk())
+                               {
+                                       parent = ((nxObjectTreeItemData *)m_wndTreeCtrl->GetItemData(item))->GetObject();
+               for(j = 0; j < object->dwNumParents; j++)
+                  if (object->pdwParentList[j] == parent->dwId)
+                  {
+                     parentList[j] = 0;   // Mark this parent as presented
+                     break;
+                  }
+               if (j == object->dwNumParents)  // Not a parent anymore
+               {
+                                               m_wndTreeCtrl->Delete(list->Item(i));
+                                               list->RemoveAt(i);
+                                               count--;
+                                               i--;
+                                       }
+                                       else  // Current tree item is still valid
+                                       {
+                                               m_wndTreeCtrl->SetItemText(list->Item(i), object->szName);
+                                       }
+                               }
+            else  // Current tree item has no parent
+                               {
+                                       m_wndTreeCtrl->SetItemText(list->Item(i), object->szName);
+                               }
+                       }
+               }
+       }
+}
+
+
+//
+// Clear has of object items
+//
+
+void nxObjectBrowser::ClearObjectItemsHash()
+{
+       nxObjectItemsHash::iterator it;
+
+       for(it = m_objectItemsHash.begin(); it != m_objectItemsHash.end(); it++)
+               delete it->second;
+       m_objectItemsHash.clear();
+}
index 2012fc1..964cc35 100644 (file)
 #include <nxmc_api.h>
 
 
+//
+// Arrays and hashes
+//
+
+WX_DECLARE_OBJARRAY(wxTreeItemId, nxTreeItemList);
+WX_DECLARE_HASH_MAP(DWORD, nxTreeItemList*, wxIntegerHash, wxIntegerEqual, nxObjectItemsHash);
+
+
 //
 // libnxcl object index structure
 //
@@ -144,9 +152,10 @@ private:
        wxTreeCtrl *m_wndTreeCtrl;
        nxObjectView *m_wndObjectView;
        bool m_isFirstResize;
+       nxObjectItemsHash m_objectItemsHash;
        
        void AddObjectToTree(NXC_OBJECT *object, wxTreeItemId &root);
-       void CreateTreeItemText(NXC_OBJECT *object, TCHAR *buffer);
+       void ClearObjectItemsHash();
 
 public:
        nxObjectBrowser();
@@ -158,6 +167,7 @@ protected:
        void OnViewRefresh(wxCommandEvent &event);
        void OnTreeItemExpanding(wxTreeEvent &event);
        void OnTreeSelChanged(wxTreeEvent &event);
+       void OnObjectChange(wxCommandEvent &event);
 
        DECLARE_EVENT_TABLE()
 };
index 81a7afa..b735276 100644 (file)
 #include "object_browser.h"
 
 
+//
+// Page IDs
+//
+
+#define OBJECT_PAGE_OVERVIEW           0
+#define OBJECT_PAGE_ALARMS                     1
+
+
 //
 // Event table
 //
@@ -95,16 +103,25 @@ void nxObjectView::OnPaint(wxPaintEvent &event)
 void nxObjectView::SetObject(NXC_OBJECT *object)
 {
        nxView *view;
+       wxWindow *wnd;
+       int page;
+       size_t index;
        
        m_object = object;
        RefreshRect(wxRect(0, 0, GetClientSize().x, m_headerOffset), false);
 
        Freeze();
 
+       index = m_notebook->GetSelection();
+       wnd = (index != wxNOT_FOUND) ? m_notebook->GetPage(index) : NULL;
+       page = (wnd != NULL) ? wnd->GetId() : OBJECT_PAGE_OVERVIEW;
+
        while(m_notebook->GetPageCount() > 0)
                m_notebook->DeletePage(0);
 
-       m_notebook->AddPage(new nxObjectOverview(m_notebook, object), _T("Overview"), false,
+       wnd = new nxObjectOverview(m_notebook, object);
+       wnd->SetId(OBJECT_PAGE_OVERVIEW);
+       m_notebook->AddPage(wnd, _T("Overview"), page == OBJECT_PAGE_OVERVIEW,
                            wxXmlResource::Get()->LoadIcon(_T("icoSmallInformation")));
 
        if ((object->iClass == OBJECT_NETWORK) || (object->iClass == OBJECT_SUBNET) ||
@@ -114,7 +131,8 @@ void nxObjectView::SetObject(NXC_OBJECT *object)
                view = NXMCCreateViewByClass(_T("AlarmView"), m_notebook, _T("ObjectBrowser"), object, NULL);
                if (view != NULL)
                {
-                       m_notebook->AddPage(view, _T("Alarms"), false, view->GetIcon());
+                       view->SetId(OBJECT_PAGE_ALARMS);
+                       m_notebook->AddPage(view, _T("Alarms"), page == OBJECT_PAGE_ALARMS, view->GetIcon());
                        view->RefreshView();
                }
        }